Once upon a time, there was an algorithm. This algorithm became very popular but it turned out that there were better ones. Some folks came up with a better one and some other folks implemented it in Python. It's a little crazy how much it rocks.
emcee is a GPLv2 licensed pure-Python implementation of Goodman & Weare’s Affine Invariant Markov chain Monte Carlo (MCMC) Ensemble sampler and these pages will show you how to use it.
This documentation won’t teach you too much about MCMC but there are a lot of resources available for that (try this one). We also posted a paper to the arXiv explaining the emcee algorithm in much greater detail.
If you wanted to draw samples from a 10 dimensional Gaussian, you would do something like:
import numpy as np import emcee def lnprob(x, ivar): return -0.5 * np.sum(ivar * x ** 2) ndim, nwalkers = 10, 100 ivar = 1. / np.random.rand(ndim) p0 = [np.random.rand(ndim) for i in range(nwalkers)] sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=[ivar]) sampler.run_mcmc(p0, 1000)
“My research—modeling strong gravitational lenses with 10-20 free parameters—would be very difficult or impossible without emcee.”
—Shane Bussmann (CfA)
Since the initial release, emcee has been used in many published scientific studies. Here is an incomplete list:
Direct contributions to the code base:
Comments, corrections & suggestions: