{"id":6615,"library":"emcee","title":"emcee","description":"emcee is an MIT licensed pure-Python implementation of Goodman & Weare's Affine Invariant Markov chain Monte Carlo (MCMC) Ensemble sampler. It is a widely used toolkit for Bayesian parameter estimation in scientific fields, particularly astronomy, and maintains an active release cadence with minor updates and bug fixes. [3, 6, 9]","status":"active","version":"3.1.6","language":"en","source_language":"en","source_url":"https://github.com/dfm/emcee","tags":["MCMC","sampling","Bayesian inference","statistics","astronomy","physics"],"install":[{"cmd":"pip install emcee","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Required for numerical operations and array handling.","package":"numpy","optional":false},{"reason":"Often used for optimization and statistical functions, and some internal fixes relate to SciPy compatibility (e.g., kstest).","package":"scipy","optional":true}],"imports":[{"symbol":"emcee","correct":"import emcee"},{"symbol":"EnsembleSampler","correct":"from emcee import EnsembleSampler"}],"quickstart":{"code":"import numpy as np\nimport emcee\n\n# Define the logarithm of the posterior probability density function\ndef log_prob(x, mu, cov):\n    diff = x - mu\n    return -0.5 * np.dot(diff, np.linalg.solve(cov, diff))\n\n# Set up the problem dimensions and parameters\nndim = 2  # Number of dimensions\nnwalkers = 32 # Number of MCMC walkers\n\n# True mean and covariance for the Gaussian\nnp.random.seed(42)\nmu_true = np.array([0.5, -0.2])\ncov_true = np.array([[1.0, 0.5], [0.5, 1.5]])\n\n# Initialize walkers in a small ball around the true mean\np0 = mu_true + 1e-3 * np.random.randn(nwalkers, ndim)\n\n# Instantiate the sampler\nsampler = emcee.EnsembleSampler(nwalkers, ndim, log_prob, args=(mu_true, cov_true))\n\n# Run the MCMC production chain\nstate = sampler.run_mcmc(p0, 100)\n# After burn-in, reset and run for more steps\nsampler.reset()\nstate = sampler.run_mcmc(state, 1000)\n\n# Get the chain of samples\nsamples = sampler.get_chain(flat=True)\n\nprint(f\"Mean acceptance fraction: {np.mean(sampler.acceptance_fraction):.3f}\")\nprint(f\"First 5 samples:\\n{samples[:5]}\")","lang":"python","description":"This quickstart demonstrates how to use `emcee` to sample a 2-dimensional Gaussian distribution. It defines a `log_prob` function for the posterior, initializes walkers, runs a burn-in phase, resets the sampler, and then runs the main MCMC chain to obtain samples. [2]"},"warnings":[{"fix":"Consult the `emcee` v3 documentation for the `EnsembleSampler` constructor and the `Moves` and `Parallelization` sections to adapt your code. For parallelization, use a `pool` object (e.g., from `multiprocessing`). [8]","message":"When upgrading from `emcee` v2.x to v3.x, several arguments to `EnsembleSampler` related to proposal control (`a`, `live_dangerously`) and parallelization (`threads`) were deprecated. These functionalities are now managed via the `moves` interface and the `pool` argument, respectively. [8]","severity":"breaking","affected_versions":"Upgrading from <3.0 to >=3.0"},{"fix":"Ensure your `log_prob_fn` includes both the log-prior and log-likelihood terms. For parameter values outside valid physical bounds, explicitly return `-np.inf` from `log_prob_fn`. [11]","message":"The `log_prob_fn` passed to `EnsembleSampler` must return the natural logarithm of the *posterior probability*, not just the likelihood. It should also return `-np.inf` if the parameters are unphysical or lead to a probability of zero. [2, 11]","severity":"gotcha","affected_versions":"All versions"},{"fix":"Initialize walkers by drawing from a small Gaussian ball around a reasonable guess (e.g., a maximum likelihood estimate) or from a broad, valid prior distribution. Ensure all initial positions have finite `log_prob` values. [11]","message":"Poor initialization of walkers can lead to slow convergence, biased results, or errors (e.g., 'Too few points to create valid contours' or math warnings). Walkers should be initialized in a region of non-zero probability. [15, 17, 18]","severity":"gotcha","affected_versions":"All versions"},{"fix":"If encountering unexpected behavior related to numerical operations or statistical tests, check `emcee`'s release notes for dependency-specific fixes and ensure your `numpy` and `scipy` versions are compatible with your `emcee` version. Upgrading all libraries to their latest stable versions is generally recommended.","message":"Specific versions of `emcee` have included compatibility fixes for `numpy` and `scipy`. For example, v3.1.6 fixed compatibility with older NumPy versions, and v3.1.4 addressed the updated `kstest` interface in SciPy 1.10. [12]","severity":"gotcha","affected_versions":"Potentially specific minor versions of `emcee` with older/newer `numpy`/`scipy` versions."}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}