Skip to content
Back to search results

Markov Chain Monte Carlo (MCMC) — Metropolis-Hastings and Gibbs Sampling

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
Font size

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 you want to know the shape of a mountain range hidden under fog. You cannot see it all at once, but you can take a step in a random direction and decide whether to move based on whether the ground is higher or lower. By walking long enough and recording where you have been, the places you visited most often trace out the peaks — that is the posterior distribution. MCMC does the same thing in a high-dimensional parameter space: it builds a chain of samples that gradually wanders into regions of high probability, so that the histogram of visited values approximates the target distribution without ever needing a closed-form expression for it.

History

The foundational Metropolis algorithm was published in 1953 by Metropolis, Rosenbluth, Rosenbluth, Teller, and Teller for molecular simulation. Hastings (1970) generalised it to asymmetric proposal distributions, producing the Metropolis-Hastings algorithm. Geman and Geman (1984) introduced Gibbs sampling in the context of image restoration. The algorithm became a mainstream statistical tool in the early 1990s after Gelfand and Smith (1990) demonstrated its broad applicability for Bayesian computation, and Gelman and Rubin (1992) provided the R̂ convergence diagnostic. The subsequent development of Hamiltonian Monte Carlo by Duane et al. (1987) and its automatic variant NUTS by Hoffman and Gelman (2014) extended MCMC to high-dimensional problems. Gelman et al.'s Bayesian Data Analysis (2013) and the Handbook of Markov Chain Monte Carlo (Brooks et al., 2011) are the standard comprehensive references.

When to use it

MCMC is the appropriate tool when a Bayesian model has been specified and the posterior distribution cannot be computed analytically — which is the common case for models with more than a handful of parameters or with non-conjugate priors. It is also the standard approach for latent-variable models, hierarchical models, and any setting where uncertainty must be propagated fully through a complex pipeline. No distributional assumptions about the data are needed beyond the likelihood specification. The main practical requirement is a sufficient number of iterations for the chain to converge: fewer than roughly 1 000 post-burn-in samples will typically not give a reliable characterisation of the posterior, and in high-dimensional settings many more are needed. When the parameter space is very high-dimensional, Hamiltonian Monte Carlo (HMC) or the No-U-Turn Sampler (NUTS) — automatic extensions of the MCMC idea — are substantially 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 thoughtfully, and poorly chosen priors or misspecified likelihoods can bias the posterior in ways that are hard to detect.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Enables exact (asymptotically) Bayesian inference for models where the posterior has no closed form, covering hierarchical models, missing-data problems, and complex likelihoods.
  • Produces a full joint posterior sample, so any derived quantity, prediction, or decision can be computed with properly propagated uncertainty.
  • Applies to continuous, binary, and count outcomes and imposes no distributional assumptions on the data beyond the modeller-specified likelihood.
  • Running multiple parallel chains makes convergence verification 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

1

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.

2

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.

3

At each iteration the algorithm proposes a move in parameter space. Under Metropolis-Hastings, a candidate is drawn from a proposal distribution and accepted with a probability that depends on the ratio of posterior densities at the candidate and current positions, ensuring the chain's stationary distribution is the target posterior. Under Gibbs sampling, each parameter is updated in turn by drawing directly from its full conditional distribution, which avoids the need for an explicit acceptance step. Because consecutive samples share information, the chain exhibits autocorrelation; thinning — retaining only every k-th draw — can reduce this at the cost of fewer stored samples.

4

After discarding the burn-in, convergence is assessed. The Gelman-Rubin R̂ statistic compares within-chain to between-chain variance across the multiple chains; values below 1.05 indicate that the chains have mixed and converged to the same distribution. Trace plots (the sequence of sampled values over iterations) and effective sample size are also inspected. If R̂ is large or trace plots show poor mixing, the model, priors, or sampler settings must be revised before inference proceeds.

5

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 — ratios, differences, nonlinear functions 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̂ < 1.05 is required, not just many iterations.
  • Ignoring autocorrelation and reporting effective sample size as though it were the raw count overstates precision; report effective sample size alongside raw counts.
  • Applying MCMC to a model with fewer than roughly 1 000 post-burn-in iterations will generally not yield a reliable posterior; if the computational budget is that tight, consider analytical Bayesian methods or a conjugate model.

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̂ values below 1.05 for all parameters are the conventional threshold. Trace plots should show chains that overlap and explore the same region; funnel-shaped or drifting traces indicate poor mixing. Effective sample size should also be checked — a very small effective sample size 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 nonetheless accepted with high probability. This makes them far more efficient than random-walk Metropolis-Hastings in high-dimensional parameter spaces, where random proposals are almost always rejected. As a practical rule, if your model has more than about ten parameters or if Metropolis-Hastings chains show poor mixing and very low effective sample sizes, switching to HMC or NUTS is advisable.

How many iterations do I need?

There is no universal answer, but a common starting point is 1 000 warm-up (burn-in) iterations plus 2 000 sampling iterations per chain, across four chains, giving 8 000 post-burn-in draws. The adequacy of this depends on the autocorrelation of the chain and the complexity of the posterior. The chain is sufficient when R̂ < 1.05 for all parameters and the effective sample size is large enough to characterise the posterior features of interest — typically at least a few hundred effective samples for means and a few thousand for tail quantiles.

Related methods

Sources

  1. 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 reference
    Copied.
  2. Brooks, S., Gelman, A., Jones, G.L. & Meng, X.-L. (Eds.) (2011). Handbook of Markov Chain Monte Carlo. Chapman & Hall/CRC.

    Open DOI reference
    Copied.