Markov Chain Monte Carlo (MCMC)

Statistics
Type
Posterior sampling algorithm

What does it do?

Markov Chain Monte Carlo (MCMC) is a family of algorithms for drawing samples from probability distributions that cannot be computed in closed form, most often the posterior distributions of Bayesian models.

Instead of deriving the posterior analytically, MCMC constructs a Markov chain whose stationary distribution is the target posterior and uses its draws to approximate that posterior. The samples then support estimates of posterior summaries and credible intervals for a wide range of models.

The intuition behind the method

Imagine you want to map a mountainous landscape in fog, but you can only sense the elevation where you are standing. MCMC works like a guided random walk across that landscape: at each step it proposes a move and accepts or rejects it based on how much higher (more probable) the new spot is. Over many steps the walk spends time in proportion to the height at each location, tracing out the shape of the posterior without ever needing a map of the whole surface.

History

MCMC grew out of Monte Carlo methods for simulating physical systems and was adopted in Bayesian statistics once computing power made sampling from complex posteriors practical. Gibbs sampling and Metropolis-type proposals became standard tools for posterior computation, and gradient-based methods such as Hamiltonian Monte Carlo were later developed to explore high-dimensional posteriors more efficiently. Practical guidance on running several chains, monitoring convergence and checking the sampler is given in standard references such as Gelman et al. (2013) and Brooks et al. (2011).

When to use it

Choose MCMC when the posterior distribution has no closed form. This is common for non-conjugate combinations of likelihood and prior, for complex hierarchical structures, and for models with heavy-tailed likelihoods. Non-linearity in the mean function alone does not require MCMC, since many non-linear models still have tractable posteriors. The method applies to continuous, binary and categorical outcomes. Do not use it when a conjugate or otherwise analytically tractable model answers the question, since closed-form posteriors avoid sampling error and are cheaper to compute. It is also a poor fit when only a quick point estimate is needed from a simple model, because the setup, tuning and convergence checks add substantial effort. When the data are sparse relative to the number of parameters, the prior will carry much of the inference, so it should be justified before the sampler is run.

Limitations and considerations

  • Requires specifying a model and priors; with small samples, a poorly chosen prior can dominate the posterior.
  • Convergence is not guaranteed and must be diagnosed explicitly through R-hat, ESS and trace-plot inspection.
  • Computationally intensive for large datasets or high-dimensional parameter spaces compared with many frequentist alternatives.
  • For parameters that the data identify weakly, the posterior stays close to the prior, so the draws mainly reflect the prior rather than the data.

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

Strengths

  • Can sample from posteriors that have no closed form, across many model types and parameter dimensions.
  • Provides the full posterior distribution, not just a point estimate, so uncertainty is explicitly quantified.
  • Does not require normality of the data or the outcome — applicable to hierarchical, non-linear, and non-standard models.
  • Posterior samples can be used directly for derived quantities (differences, ratios, predictions), with their Monte Carlo error assessed through the effective sample size.

Applications

  • Bayesian regression and hierarchical models where conjugate closed-form posteriors are unavailable.
  • Ecological and epidemiological studies with multi-level data structures.
  • Psychometric models such as item response theory and structural equation models, where full posterior uncertainty is of interest.
  • Analyses in which prior knowledge should be formally combined with observed data.

How it works

How it works

1

Bayes' rule is the foundation: the posterior distribution of the parameters is proportional to the likelihood of the data given those parameters, multiplied by the prior. MCMC's task is to draw samples from this posterior when it cannot be written in closed form.

Formula
p(\theta\mid y) \propto p(y\mid\theta)\,p(\theta)
2

At each iteration the algorithm evaluates how well a proposed parameter value explains the observed data; this is the likelihood. Combined with the prior, it determines whether the proposal is accepted. Metropolis-Hastings accepts proposals with a probability that preserves the target distribution as its stationary distribution. Hamiltonian Monte Carlo (HMC) and its variant NUTS use gradient information from the log-posterior to propose distant, high-probability moves more efficiently.

Formula
p(y\mid\theta)
3

After a warm-up (burn-in) phase, during which the sampler tunes its step sizes, the retained draws approximate the posterior. The posterior mean is estimated as the average of the retained draws, as in the formula above, where B is the length of the warm-up phase and T the total number of iterations. A 95% credible interval is usually read from the 2.5th and 97.5th percentiles of the retained draws. Before any summary is reported, convergence should be checked: R-hat should be below 1.1 and the effective sample size (ESS) should be adequate for the quantities being reported.

Formula
\hat{\theta} = \frac{1}{T-B}\sum_{t=B+1}^{T}\theta^{(t)},\quad \theta^{(t)}\sim p(\theta\mid y)

Common pitfalls

  • Running too few iterations or skipping the warm-up phase and then treating unconverged samples as valid posterior draws.
  • Ignoring convergence diagnostics — an R-hat above 1.1 or a very low ESS invalidates all posterior summaries.
  • Using an unintentionally strong or poorly justified prior that overrides the data, particularly with small n.
  • Forgetting that MCMC draws are autocorrelated: effective sample size, not raw sample count, governs the precision of posterior estimates, and strong autocorrelation can leave the ESS far below the number of draws.
  • Interpreting credible intervals as frequentist confidence intervals — they carry a direct probabilistic meaning that confidence intervals do not.

Frequently asked questions

How do I know when the MCMC chains have converged?

The primary diagnostic is R-hat (potential scale reduction factor). The usual maximum acceptable value is 1.1, and values near 1.01 are the recommended target. Values in this range indicate that multiple chains have mixed and are sampling from the same distribution. Check the effective sample size (ESS) for each parameter, since some parameters mix more slowly than others, and make sure it is large enough for the estimates you report. Inspect trace plots and autocorrelation plots to confirm that chains move freely without trends or sticking; low autocorrelation is what keeps the ESS close to the number of draws.

What is the difference between Metropolis-Hastings and Hamiltonian Monte Carlo (HMC/NUTS)?

Metropolis-Hastings proposes new parameter values from a simple proposal distribution and accepts or rejects them using a ratio of the posterior at the proposed and current values, corrected for the proposal distribution when it is asymmetric. It is general but can be slow to explore high-dimensional posteriors. HMC uses gradient information from the log-posterior to simulate dynamics that guide proposals toward high-probability regions, allowing large steps that are still accepted at high rates. NUTS is a self-tuning version of HMC that chooses the trajectory length automatically rather than requiring the number of leapfrog steps to be set by hand.

How many iterations do I need?

There is no fixed number that suits every model. Run several chains from dispersed starting values, discard the warm-up phase, and keep sampling until R-hat is below 1.1 and the ESS is adequate for the quantities you report. If these criteria are not met, run longer, check the model for poor identifiability, or reparameterise it. Complex or high-dimensional models usually need longer runs.

Can MCMC be used when my data are not normally distributed?

Yes. One of MCMC's principal advantages is that it does not require the data or the outcome to be normally distributed. You specify a likelihood that matches your data type, such as Poisson for counts, Bernoulli for binary outcomes or Student-t for heavy tails, and MCMC samples from the resulting posterior regardless of its shape.

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.). CRC Press.

    ISBN: 978-1439840955
  2. Brooks, S., Gelman, A., Jones, G. & Meng, X.-L. (Eds.). (2011). Handbook of Markov Chain Monte Carlo. CRC Press.

    ISBN: 978-1420079418
  3. Data Science for Financial Econometrics (2021), p. 302

  4. Statistics and Data Analysis for Financial Engineering (2015), Sect. 20.7.3