- Introduced by
- Metropolis et al. (1953); generalised by Hastings (1970)
- Year
- 1953
- Type
- Markov chain Monte Carlo sampler
What does it do?
The Metropolis-Hastings (MH) algorithm is a general-purpose Markov chain Monte Carlo (MCMC) method for drawing samples from a probability distribution whose density is known up to a normalising constant.
It was introduced by Metropolis, Rosenbluth, Rosenbluth, Teller, and Teller (1953) in computational physics, and Hastings (1970) generalised its acceptance rule to asymmetric proposal distributions. Its propose-then-accept-or-stay step underlies many later samplers; Gibbs sampling, for example, can be viewed as a case in which the acceptance probability is always one.
The intuition behind the method
Imagine a landscape where height represents how plausible each parameter value is. The chain starts at some point, proposes a random nearby step, and decides whether to take it. A higher, more plausible proposal is always accepted. A lower proposal is accepted only some of the time, with probability equal to the ratio of the two plausibilities when the proposal is symmetric, so the chain can leave a local peak instead of becoming stuck on it. Over many iterations, the fraction of time spent in each region approaches its probability under the target, which yields a representative sample without normalising the density analytically.
History
The algorithm originated in a 1953 paper by Nicholas Metropolis, Arianna Rosenbluth, Marshall Rosenbluth, Augusta Teller, and Edward Teller, written at Los Alamos National Laboratory to compute equation-of-state properties of substances made of interacting molecules on the MANIAC computer. That original version used a symmetric proposal distribution. In 1970, W. K. Hastings generalised the sampling method to arbitrary, including asymmetric, proposal distributions in Biometrika, producing the form used today. Gibbs sampling, a special case in which every proposed move is accepted, was developed later and became widely used in Bayesian statistics. The No-U-Turn Sampler (NUTS), an adaptive extension of Hamiltonian Monte Carlo, is among the modern automatic samplers that build on this framework.
When to use it
Use Metropolis-Hastings when the posterior cannot be sampled directly and no conjugate or closed-form solution exists, but the unnormalised posterior (prior times likelihood) can be evaluated at any point. It applies to a wide range of Bayesian models, including regression, hierarchical, latent variable and mixture models, provided the density can be computed pointwise. The proposal must be able to reach every region of the target's support so that the chain is irreducible, and the chain must be aperiodic and run long enough to converge. Plain random-walk MH tends to be inefficient in high-dimensional parameter spaces, where Hamiltonian Monte Carlo is usually the better choice, particularly when the posterior is smooth and continuous and gradients of the log-posterior are available.
Limitations and considerations
- Efficiency falls with dimension. A frequently cited target acceptance rate for a random-walk Gaussian proposal is about 23%. This figure is an asymptotic result for high-dimensional targets with roughly independent components, so it is a rough guide rather than a target for an arbitrary posterior. Holding the acceptance rate near a chosen value becomes harder as the number of parameters grows.
- Successive draws are correlated, so the effective sample size is typically smaller than the chain length and Monte Carlo standard errors are larger than they would be for independent draws.
- Convergence is asymptotic and must be checked empirically (R-hat, effective sample size, trace plots); no finite-run guarantee of closeness to the target exists.
- Mixing can be very slow when the posterior is multimodal, strongly curved (banana-shaped), or has strong correlations between parameters, because small random-walk moves rarely cross between modes or follow narrow ridges.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Applies to any target whose density can be evaluated up to a constant; it needs no conjugacy or closed-form posterior.
- The marginal likelihood never has to be computed, because the normalising constant cancels in the acceptance ratio.
- Under irreducibility and aperiodicity, detailed balance makes the target the stationary distribution, so long-run draws are asymptotically correct for it.
- Simple to implement for low- to moderate-dimensional problems, and the proposal can be chosen freely, with asymmetric proposals handled by the Hastings correction.
- The accept-or-stay structure is the template for extensions: Gibbs sampling (acceptance always one), Hamiltonian Monte Carlo and its adaptive variant NUTS, and reversible-jump MCMC all build on it.
Applications
- Fitting Bayesian hierarchical models in epidemiology, ecology, and social science where closed-form posteriors are unavailable.
- Parameter estimation for complex physical or engineering models in which the likelihood is known but the posterior is analytically intractable.
- Bayesian variable selection and model comparison via reversible-jump extensions of the MH algorithm.
- Posterior inference in latent variable models such as mixture models and hidden Markov models.
- Calibrating simulation models by sampling from the posterior over mechanistic parameters given observed data.
How it works
How it works
The target distribution is the unnormalised posterior: the product of the likelihood and the prior. Because only the ratio of target densities at two points is ever computed, the normalising constant (the marginal likelihood) cancels out and need never be evaluated. This is a practical advantage for high-dimensional Bayesian models.
Formula
\pi(\theta) \propto p(\theta)\,p(\mathbf{y}\mid\theta)At each iteration t, a candidate parameter value θ* is drawn from a proposal distribution q(θ*|θ^(t)), which the analyst specifies. Common choices are a symmetric random walk (θ* = θ^(t) + ε, with ε ~ N(0, σ²I)) and independent proposals. The proposal does not need to match the shape of the target, but it must be able to reach every region of the target's support, or the chain cannot explore those regions.
Formula
\theta^* \sim q(\theta^*\mid\theta^{(t)})The acceptance ratio α compares the target density at the proposed and current points, corrected for any asymmetry in the proposal. The correction factor q(θ^(t)|θ*) / q(θ*|θ^(t)) equals 1 for symmetric proposals, which reduces the ratio to π(θ*)/π(θ^(t)), the original Metropolis rule. Hastings' (1970) generalisation keeps this correction, so directed or asymmetric proposals do not bias the stationary distribution.
Formula
\alpha(\theta^{(t)}, \theta^*) = \min\!\left(1,\; \frac{\pi(\theta^*)\,q(\theta^{(t)}\mid\theta^*)}{\pi(\theta^{(t)})\,q(\theta^*\mid\theta^{(t)})}\right)A uniform random variable u ~ Uniform(0,1) is drawn. If u ≤ α the chain moves to θ*; otherwise it stays at θ^(t), and that value is recorded again as the next draw. Repeated refusals are not discarded; each repeated value is a legitimate element of the chain. The chain is run for a burn-in period, which is discarded, and then for T further iterations, whose values form the posterior sample.
Formula
\theta^{(t+1)} = \begin{cases} \theta^* & \text{with probability } \alpha \\ \theta^{(t)} & \text{with probability } 1 - \alpha \end{cases}Detailed balance implies that π is a stationary distribution of the chain: the probability flux from θ to θ* under the kernel equals the flux from θ* to θ. Satisfying detailed balance is sufficient but not necessary for stationarity. Convergence to π from any starting point additionally requires irreducibility and aperiodicity, which is why the proposal must be able to reach the whole support. Once the chain has converged and these conditions hold, averages of the draws (ergodic averages) approach posterior expectations as the number of iterations T grows. Any finite-run estimate carries Monte Carlo error, which should be reported as a standard error rather than as an exact value.
Formula
\pi(\theta)\,q(\theta^*\mid\theta)\,\alpha(\theta, \theta^*) = \pi(\theta^*)\,q(\theta\mid\theta^*)\,\alpha(\theta^*, \theta)
Common pitfalls
- Using early draws before convergence. Discard the burn-in period before computing posterior summaries.
- Leaving the proposal scale untuned. A very small scale gives high acceptance but slow exploration; a very large scale gives low acceptance and long runs of repeated values.
- Running a single chain and treating apparent stability as convergence. Run several chains from dispersed starting values and check R-hat.
- Ignoring autocorrelation. A long chain with strong autocorrelation can contain far fewer effectively independent draws than its length suggests, so check effective sample size before reporting.
- Reading non-convergence (drifting trace plots, R-hat above 1.01) as a feature of the posterior rather than a failure of the sampler.
Frequently asked questions
How do I choose the proposal distribution?
For a random-walk proposal, a common working guideline is to tune the proposal standard deviation so that the acceptance rate falls roughly between 20% and 50%. The value of about 23.4% for a d-dimensional Gaussian target is an asymptotic optimum for high-dimensional targets with roughly independent components under random-walk proposals. It is not a guarantee for finite-dimensional or strongly correlated posteriors, so treat the 20–50% range as a rough check rather than a target. In practice, run short pilot chains, adjust the scale, and check trace plots for healthy mixing. Adaptive MH methods automate this tuning by adjusting the proposal covariance during burn-in, and the adaptation should be finished before the retained draws are collected.
What is the difference between the Metropolis and the Metropolis-Hastings algorithm?
The original 1953 Metropolis algorithm requires a symmetric proposal distribution, so the proposal ratio q(θ^(t)|θ*)/q(θ*|θ^(t)) equals 1 and drops from the acceptance ratio. Hastings' 1970 generalisation retains this correction factor, enabling any proposal distribution, including directed, independent, or data-driven proposals, while preserving the correct stationary distribution.
How do I know when the chain has converged?
Run several chains (the usual guidance here is at least four) from overdispersed starting points. Compute the Gelman-Rubin potential scale reduction factor R-hat for each parameter; values below 1.01 are a conventional threshold for acceptable convergence. Inspect trace plots for stationarity and compute the effective sample size (ESS). A figure of around 400 effective draws per parameter is a common rule of thumb rather than a guarantee. Software such as Stan reports bulk and tail effective sample sizes.
When should I prefer Hamiltonian Monte Carlo over Metropolis-Hastings?
Hamiltonian Monte Carlo (HMC) uses gradients of the log-posterior to make large, informed moves that are accepted with high probability. For smooth, continuous posteriors in higher dimensions, HMC and its adaptive variant NUTS typically produce effective samples per unit of computation far more efficiently than a random-walk MH sampler. Plain MH remains useful for low-dimensional problems, for discrete parameters where gradients are unavailable, and as the reference algorithm from which these methods are understood.
Related methods
Sources
Metropolis, N., Rosenbluth, A. W., Rosenbluth, M. N., Teller, A. H., & Teller, E. (1953). Equation of state calculations by fast computing machines. The Journal of Chemical Physics, 21(6), 1087–1092.
Open DOI referenceHastings, W. K. (1970). Monte Carlo sampling methods using Markov chains and their applications. Biometrika, 57(1), 97–109.
Open DOI referenceRobert, C. P., & Casella, G. (2004). Monte Carlo Statistical Methods (2nd ed.). Springer.
ISBN: 978-0-387-21239-5Gelman, 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-1-439-84095-5