- Introduced by
- Metropolis et al. (1953); Gibbs sampler formalised by Geman & Geman (1984)
- Year
- 1953 (Metropolis-Hastings); 1984 (Gibbs)
- Type
- Simulation-based Bayesian inference / numerical integration
What does it do?
Markov Chain Monte Carlo (MCMC) is a family of simulation algorithms that constructs a Markov chain whose stationary distribution is the target posterior, enabling Bayesian inference and high-dimensional integral computation that would otherwise be analytically intractable.
Pioneered by Metropolis and colleagues in 1953 and extended by Hastings in 1970, MCMC underpins modern Bayesian statistics. The two most widely used variants are Metropolis-Hastings, which proposes moves from a general proposal distribution, and Gibbs sampling, which draws each parameter in turn from its full conditional distribution.
The intuition behind the method
Imagine mapping a mountain range hidden in fog, where you can only sense the ground beneath your feet. You take a random step and decide whether to move based on the local height: uphill moves are always favoured, but downhill moves are sometimes accepted, which lets the walker escape local peaks. Recorded over a long walk, the places visited most often trace out the high ground, which corresponds to the posterior distribution. MCMC does the same in a high-dimensional parameter space: the chain of samples settles into regions of high posterior probability, so the histogram of visited values approximates the target without a closed-form expression for it.
History
The Metropolis algorithm was published in 1953 by Metropolis, Rosenbluth, Rosenbluth, Teller, and Teller for molecular simulation, and Hastings (1970) generalised it to asymmetric proposal distributions. Geman and Geman (1984) introduced Gibbs sampling in the context of image restoration, and Gelfand and Smith (1990) demonstrated its broad applicability to Bayesian computation. Gelman and Rubin (1992) provided the R̂ convergence diagnostic, while Duane et al. (1987) and Hoffman and Gelman (2014) are associated with Hamiltonian Monte Carlo and its automatic variant NUTS. Gelman et al. (2013) and Brooks et al. (2011) are the comprehensive references cited for this method.
When to use it
MCMC is the appropriate tool when a Bayesian model has been specified and its posterior cannot be computed analytically, which is common for models with many parameters or non-conjugate priors. It is also a standard approach for latent-variable and hierarchical models, and for settings where uncertainty must be propagated through a complex pipeline. The data model enters through the likelihood, which is itself a distributional assumption the analyst must justify. Choose MCMC only if the computational budget allows enough iterations for the chains to converge and for the effective sample size to support the quantities of interest; a very short run is unlikely to characterise the posterior well. When the parameter space is high-dimensional, Hamiltonian Monte Carlo (HMC) or the No-U-Turn Sampler (NUTS), automatic extensions of the MCMC idea, are usually more efficient than basic Metropolis-Hastings.
Limitations and considerations
- Convergence is not guaranteed in finite time; a chain that appears to have converged may still be stuck in a local region of the posterior.
- Autocorrelation between successive samples means the effective sample size is smaller than the raw number of iterations, requiring longer runs than an equivalent independent sample would need.
- In high-dimensional parameter spaces, random-walk Metropolis-Hastings mixes poorly; Hamiltonian Monte Carlo or NUTS is needed for efficient exploration.
- Model specification requires the analyst to choose priors and a likelihood carefully; a poorly chosen prior or misspecified likelihood can bias the posterior, and such problems are not always evident from the chain's convergence diagnostics.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Gives valid posterior inference in the limit of long runs once the chain has converged; finite-run accuracy depends on convergence and effective sample size.
- Produces a joint posterior sample, so derived quantities such as ratios, differences, or nonlinear functions of parameters can be summarised with uncertainty propagated from the same draws.
- Applies to continuous, binary, and count outcomes, because the outcome type is handled by the likelihood the modeller specifies.
- Running several chains from different starting points makes convergence checking systematic through the Gelman-Rubin R̂ diagnostic.
Applications
- Fitting hierarchical Bayesian models in psychology, education, and medicine where data are grouped and partial pooling is desired.
- Estimating parameters of epidemiological compartmental models (SIR, SEIR) from observed case counts, including uncertainty quantification.
- Bayesian structural equation modelling and latent-variable models in social science research.
- Posterior inference for item response theory (IRT) models in educational measurement.
- Calibrating complex simulation models against observed data when the likelihood is available but non-conjugate.
- Financial risk modelling and option pricing under Bayesian parameter uncertainty.
How it works
How it works
The analyst defines the Bayesian model: a likelihood function connecting parameters to observed data, and prior distributions encoding beliefs about each parameter before seeing the data. Together these define the unnormalised posterior that MCMC will explore. The choice of priors and likelihood family determines the structure of the chain and must be grounded in subject-matter knowledge.
Starting values are chosen for all parameters, and the number of chains, the length of the warm-up (burn-in) period, and the total number of post-burn-in iterations are set. Running multiple independent chains, typically four, from different starting points is standard practice, as it allows convergence to be assessed by comparing chains rather than relying on a single trajectory.
At each iteration the algorithm proposes a move in parameter space. Under Metropolis-Hastings, a candidate θ* is drawn from a proposal distribution q(θ*|θ) and accepted with probability min{1, [p(y|θ*) p(θ*) q(θ|θ*)] / [p(y|θ) p(θ) q(θ*|θ)]}, where p(y|θ)p(θ) is the unnormalised posterior at each point. When the proposal is symmetric, the proposal terms cancel and the ratio reduces to the ratio of posterior densities at the candidate and current positions. This acceptance rule makes the target posterior the stationary distribution of the chain. Under Gibbs sampling, each parameter is updated in turn by drawing from its full conditional distribution, so the draw is always accepted and no separate acceptance step is needed; Gibbs can therefore be viewed as a special case of Metropolis-Hastings in which the acceptance probability is always 1. Successive draws are positively correlated, so the chain exhibits autocorrelation. The first draws, taken before the chain reaches stationarity, are usually discarded as burn-in, and because the draws are correlated, MCMC generally needs more draws than independence samplers such as importance or rejection sampling. Thinning, which keeps only every k-th draw, reduces storage and the correlation between stored draws, but it discards information; it is optional and not required for valid inference from the full chain.
After discarding the burn-in, convergence is assessed. The Gelman-Rubin R̂ statistic compares within-chain and between-chain variance across the parallel chains; values close to 1 indicate that the chains have mixed and are sampling the same distribution. A threshold of R̂ < 1.05 is a common working convention for this method, but thresholds vary across authors and software, so it should be applied to every parameter alongside the other checks. Trace plots, which show sampled values over iterations, and effective sample size are inspected alongside R̂. For HMC and NUTS samplers, the number of divergent transitions and the energy diagnostic (E-BFMI) should also be checked; even a small number of divergent transitions warrants investigation, for example by reparameterising the model or raising the target acceptance rate of the sampler. If R̂ remains above the working threshold or the traces show poor mixing, increase the number of iterations, reparameterise the model, revise the priors, or change the sampler before inference proceeds.
The collection of post-burn-in samples forms an empirical approximation to the posterior. Point estimates (posterior mean or median), credible intervals, and posterior predictive distributions are computed directly from the sample. Because the samples are draws from the joint posterior, any derived quantity, such as a ratio, a difference, or a nonlinear function of parameters, can be estimated by applying the same function to each sample and summarising the resulting distribution.
Common pitfalls
- Skipping or shortening the burn-in period means the early non-stationary portion of the chain contaminates posterior estimates; always discard a generous warm-up.
- Running only a single chain makes it impossible to detect whether the sampler has converged or is trapped in one region; always run at least four independent chains.
- Treating a large raw iteration count as proof of convergence without computing R̂ or inspecting trace plots can give false confidence; R̂ should be close to 1 for every parameter, with the 1.05 working threshold applied alongside trace inspection and effective sample size.
- Ignoring autocorrelation and reporting effective sample size as though it were the raw count overstates precision; report effective sample size alongside raw counts.
- Reporting posterior summaries from a very short post-burn-in run can give unreliable estimates, particularly for tail quantities; check the effective sample size for each quantity of interest before reporting it.
- Ignoring divergent transitions in HMC or NUTS runs can mean that regions of high posterior curvature were poorly explored; a non-zero count should be investigated before results are reported.
Frequently asked questions
How do I know whether my MCMC chain has converged?
The primary diagnostic is the Gelman-Rubin R̂ statistic, computed by running at least four independent chains from different starting points and comparing within-chain to between-chain variance. R̂ should be close to 1 for every parameter; the threshold of 1.05 recorded for this method is a working convention rather than a universal rule. Trace plots should show chains that overlap and explore the same region; drifting or stuck traces indicate poor mixing. Effective sample size should also be checked, since a small value relative to the raw count signals high autocorrelation and the need for more iterations or a better sampler.
What is the difference between Metropolis-Hastings and Gibbs sampling?
Metropolis-Hastings proposes a move for all parameters simultaneously (or in blocks) from a user-specified proposal distribution and accepts or rejects the proposal with a probability that preserves the target posterior as the stationary distribution. Gibbs sampling instead updates each parameter one at a time by drawing directly from its full conditional distribution, the distribution of that parameter given all others and the data. Gibbs sampling requires that these full conditionals be available in closed form, which is the case for many conjugate models; when they are available, Gibbs is highly efficient because every proposal is accepted. Metropolis-Hastings is more broadly applicable but requires tuning the proposal to achieve acceptable acceptance rates.
When should I use Hamiltonian Monte Carlo instead of Metropolis-Hastings?
Hamiltonian Monte Carlo (HMC) and its automatic variant NUTS use gradient information from the log-posterior to propose large, informed moves that are accepted with high probability. This makes them more efficient than random-walk Metropolis-Hastings when the parameter space is high-dimensional and random proposals are rarely accepted. If chains show poor mixing or low effective sample sizes, particularly as the number of parameters grows, switching to HMC or NUTS is worth considering, though the size of the gain depends on the model.
How many iterations do I need?
There is no universal answer. A common illustrative configuration is 1,000 warm-up and 2,000 sampling iterations per chain across four chains, giving 8,000 post-burn-in draws; these figures are starting points, not requirements. Adequacy depends on the autocorrelation of the chain and the complexity of the posterior: the run is sufficient when R̂ is close to 1 for all parameters and the effective sample size is large enough for the quantities of interest. Tail quantiles typically need more effective draws than means.
Related methods
Sources
Gelman, A., Carlin, J.B., Stern, H.S., Dunson, D.B., Vehtari, A. & Rubin, D.B. (2013). Bayesian Data Analysis (3rd ed.). Chapman & Hall/CRC.
Open DOI referenceBrooks, S., Gelman, A., Jones, G.L. & Meng, X.-L. (Eds.) (2011). Handbook of Markov Chain Monte Carlo. Chapman & Hall/CRC.
Open DOI referenceCOMPUTATIONAL METHOD IN STATISTICS AND ECONOMETRICS, Section 6.4 (page not recorded in library)