Source-linked AI summary
emcee: The MCMC Hammer
Daniel Foreman-Mackey, David W. Hogg, Dustin Lang, Jonathan Goodman
TL;DR
MCMC is valuable for expensive scientific inference, but efficient generation of independent posterior samples remains important. This paper presents emcee, a Python implementation of an affine-invariant ensemble sampler whose autocorrelation performance matches the serial stretch move while enabling generic parallelization.
Problem
Efficient MCMC methods are needed to generate independent posterior samples with few evaluations when likelihoods or priors require expensive computation.
Method
The paper presents an independent, open-source Python implementation of an affine-invariant ensemble sampler designed to exploit generic parallelization.
Results
The method’s autocorrelation-time performance is comparable to the serial stretch move while supporting generic parallelization.
Takeaways & Limitations
Once users understand MCMC fundamentals, emcee is a well-engineered option for performance on large problems.
Takeaways & Limitations
For multi-modal target densities, walkers can become stuck in different modes, rapidly worsening acceptance fraction and autocorrelation time.
Abstract
from arXiv · showhide
We introduce a stable, well tested Python implementation of the affine-invariant ensemble sampler for Markov chain Monte Carlo (MCMC) proposed by Goodman & Weare (2010). The code is open source and has already been used in several published projects in the astrophysics literature. The algorithm behind emcee has several advantages over traditional MCMC sampling methods and it has excellent performance as measured by the autocorrelation time (or function calls per independent sample). One major advantage of the algorithm is that it requires hand-tuning of only 1 or 2 parameters compared to $\sim N^2$ for a traditional algorithm in an N-dimensional parameter space. In this document, we describe the algorithm and the details of our implementation and API. Exploiting the parallelism of the ensemble method, emcee permits any user to take advantage of multiple CPU cores without extra effort. The code is available online at http://dan.iel.fm/emcee under the MIT License.
1. Introduction
MCMC provides efficient sampling approximations to posterior PDFs in high-dimensional scientific models, including cases with many nuisance parameters and expensive likelihood computations. The section motivates affine-invariant sampling as a way to reduce tuning difficulties caused by parameter covariances and traditional methods’ roughly N^2 tuning burden.
- Motivation: MCMC efficiently approximates posterior PDFs in high-dimensional parameter spaces, supporting probabilistic data analysis in cosmology and astrophysics.These applications often involve expensive models, many free parameters, and low-signal-to-noise observations.
- Marginalization: MCMC samples jointly over model and nuisance parameters, automatically yielding samples from the marginalized posterior for parameters of interest.This avoids explicitly evaluating a potentially daunting integral over a large nuisance-parameter set.
- Efficiency: Efficient MCMC is especially valuable when likelihoods or priors require expensive simulations or computations, because independent posterior samples should require few function evaluations.The methods presented are designed for this efficiency regime.
- Limitations of traditional MCMC: Traditional Metropolis-Hastings sampling requires approximately N^2 tuning parameters because each covariance-matrix element is unspecified, making tuning difficult in large, expensive problems.Anisotropic target densities can be transformed into easier isotropic sampling problems, but the tuning burden remains problematic.
- Affine-invariant sampling: Affine invariance makes an algorithm perform equally well under linear transformations, reducing sensitivity to parameter covariances; the Goodman–Weare algorithm requires only two performance-tuning hyperparameters.The paper presents an independent implementation of this algorithm that had already proved effective in several astrophysics projects.
2. The Algorithm
The section introduces MCMC as a Markov-chain procedure for generating representative posterior samples, contrasts Metropolis–Hastings with the affine-invariant stretch move, and explains a detailed-balance-preserving parallelization. The stretch move evolves an ensemble of walkers and achieves autocorrelation-time performance comparable to its serial version while enabling generic parallelization.
- MCMC foundations: MCMC generates a random walk whose Markov-chain samples become representative of the target distribution over time.Each chain point depends only on the previous position.
- Metropolis-Hastings: Metropolis–Hastings proposes a new position from a transition distribution and accepts or rejects it probabilistically.When rejected, the chain repeats its previous position.
- Stretch move: The stretch move is an affine-invariant ensemble algorithm that significantly outperforms standard Metropolis–Hastings.It simultaneously evolves K walkers, with each proposal based on the current positions of the K −1 complementary walkers.
- The parallel stretch move: To preserve detailed balance during parallel updates, the ensemble is split into two subsets that are updated using the complementary subset.Updating all walkers simultaneously from the same ensemble state would subtly violate detailed balance.
- The parallel stretch move: The parallel stretch move has autocorrelation time comparable to the serial algorithm while enabling generic parallelization.This combination makes the method extremely powerful.
3. Tests
The tests advocate autocorrelation time as an affine-invariant measure of sampler performance and convergence, while noting that acceptance fraction must avoid both near-zero and near-one extremes. The stretch-move algorithm achieves shorter autocorrelation times than Metropolis–Hastings on several non-trivial densities, requiring fewer posterior-PDF computations for the same number of independent samples.
- Acceptance fraction: Acceptance fraction af should avoid both extrema: near-zero acceptance yields few independent, unrepresentative samples, whereas near-one acceptance produces a target-agnostic random walk.The passage notes that there is no agreement on one optimal acceptance rate, but both extremes are unacceptable.
- Autocorrelation time: Autocorrelation time measures the posterior-PDF evaluations required to produce independent target-density samples.The stretch-move algorithm has a significantly shorter autocorrelation time on several non-trivial densities.
- Autocorrelation time: Fewer PDF computations are required than with an M–H sampler to produce the same number of independent samples.
- Autocorrelation time: The autocorrelation time is advocated because it measures representative-sampling cost and is affine invariant.Its affine invariance makes it suitable for measuring performance and diagnosing convergence on transformed densities.
- Implementation: emcee can optionally estimate autocorrelation time with acor2, a direct Python port of the original GW10 algorithm implemented in C++.
4. Discussion & Tips
The discussion recommends developing MCMC intuition with a hand-built Metropolis–Hastings sampler before adopting emcee for performance, while emphasizing careful initialization, diagnostics, walker counts, and chain lengths. It also notes limitations for multimodal targets and constrained parameter spaces.
- Workflow: New MCMC users should first write and tune their own Metropolis–Hastings code, then switch to emcee or similarly engineered software for large problems.The authors argue that typical emcee performance is better than home-built Metropolis–Hastings code, but hands-on experience develops irreplaceable intuition.
- Initialization: Walkers can be initialized from the prior or a reasonable parameter-space range, but a tight N-dimensional ball near a likely maximum-probability point is often more effective.The prior-based approach is described as more objective, whereas the concentrated initialization is preferred when the point is plausibly close to the maximum-probability region.
- Diagnostics: Autocorrelation time is the preferred performance indicator; acceptance fractions should generally fall between 0.2 and 0.5, while higher ESJD tends to indicate shorter autocorrelation time.ESJD can be evaluated more robustly on short chains, but it is not affine-invariant and has no trivial interpretation in terms of independent samples.
- Walker count: emcee generally benefits from hundreds of walkers because doubling walkers doubles compute per step while also returning twice as many independent samples per autocorrelation time.The authors specifically recommend increasing walker count in almost all cases of low acceptance fraction, subject to performance limits.
- Chain length: Running too few steps can hide long autocorrelation times, especially when multimodal posteriors require slow migration between modes.Within-mode autocorrelation can be short while mode-to-mode migration is long, and low acceptance ratios generally signal very long autocorrelation times.
- Limitations: emcee can underperform on multimodal targets and parameters with non-trivial constraints, where walkers may become stuck or vector-like linear proposals may be invalid.Reparameterization can sometimes address constrained parameters, but specialized samplers may be needed in some cases.
A. Installation
emcee is easiest to install with pip4 on a UNIX-based system, with numpy required separately. Alternatively, users can install from downloaded source using setup.py.
- Installation: pip4 provides the easiest installation route for emcee on a UNIX-based system.Installing for all users may require superuser permissions.
- Installation: numpy is required to use emcee and can usually be installed with pip.emcee has been tested with Python 2.7 and numpy 1.6, though earlier versions may also work.
- Installation: Users can alternatively download the source code and run python setup.py install in the unzipped directory.numpy must also be installed when using this method.
B. Issues & Contributions
emcee’s development is coordinated on GitHub, where users can report problems and contribute improvements. The project welcomes contributions, including patches.
- Development of emcee is coordinated on GitHub.
- Contributions to the project are welcome.
- Users encountering code problems are asked to report them and consider contributing a patch.
C. Online Documentation
The emcee website provides practical usage guidance through API documentation and examples of possible workflows.
- C. Online Documentation: The website offers API documentation and many examples of possible workflows for using emcee in practice.The paper directs readers to http://dan.iel.fm/emcee for this documentation.