Commit 307ab12a authored by Gregory Ashton's avatar Gregory Ashton

Adds the emcee sampler class

parent 4db79501
Pipeline #22888 passed with stages
in 14 minutes and 29 seconds
......@@ -752,6 +752,54 @@ class Pymultinest(Sampler):
return self.result
class Emcee(Sampler):
def _run_external_sampler(self):
nwalkers = self.kwargs.pop('nwalkers', 100)
nsteps = self.kwargs.pop('nsteps', 100)
nburn = self.kwargs.pop('nburn', 50)
emcee = self.external_sampler
tqdm = utils.get_progress_bar(self.kwargs.pop('tqdm', 'tqdm'))
sampler = emcee.EnsembleSampler(
nwalkers=nwalkers, dim=self.ndim, lnpostfn=self.lnpostfn,
pos0 = [self.get_random_draw_from_prior() for i in range(nwalkers)]
for result in tqdm(
sampler.sample(pos0, iterations=nsteps), total=nsteps):
self.result.sampler_output = np.nan
self.result.samples = sampler.chain[:, nburn:, :].reshape(
(-1, self.ndim))
self.result.walkers = sampler.chain[:, :, :]
self.result.log_evidence = np.nan
self.result.log_evidence_err = np.nan
try:"Max autocorr time = {}".format(
except emcee.autocorr.AutocorrError as e:"Unable to calculate autocorr time: {}".format(e))
return self.result
def lnpostfn(self, theta):
return self.log_likelihood(theta) + self.log_prior(theta)
def plot_walkers(self, save=True, **kwargs):
nwalkers, nsteps, ndim = self.result.walkers.shape
idxs = np.arange(nsteps)
fig, axes = plt.subplots(nrows=ndim, figsize=(6, 3*self.ndim))
for i, ax in enumerate(axes):
ax.plot(idxs, self.result.walkers[:, :, i].T, lw=0.1, color='k')
filename = '{}/{}_walkers.png'.format(self.outdir, self.label)
logging.debug('Saving walkers plot to {}'.format('filename'))
class Ptemcee(Sampler):
def _run_external_sampler(self):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment