Skip to content

Instantly share code, notes, and snippets.

@astro313
Created September 20, 2016 02:23
Show Gist options
  • Save astro313/d46043c12ba348e5b07152d91073c103 to your computer and use it in GitHub Desktop.
Save astro313/d46043c12ba348e5b07152d91073c103 to your computer and use it in GitHub Desktop.
use emcee to generate ~N ind. samples
import emcee
import numpy as np
# TODO:
# write something using sampler.sample() instead of predefined niter to ensure we have enough uncorrelated sample after run..
nindependent = 0
nind = 500
niter = 1000
attempts = 10
counter = 0
if counter <= attempt:
while nindependent < nind:
print("Number of independent samples from last iteration: {:}".format(nindependent))
try:
pos0, lnprob0, rstate0 = sampler.run_mcmc(pzero, burnin)
except ValueError:
pos0, lnprob0, rstate0, _ = sampler.run_mcmc(pos0, niter, rstate=rstate0, lnprob0=lnprob0)
try:
nindependent = sampler.iterations/numpy.mean(sampler.acor)
except RuntimeError:
print("acor didn't converge")
nindependent = 0
counter +=1
else:
print("Need more iterations to reach {:d} independent samples.".format(nindependent))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment