Quant Memo
Advanced

MCMC and Metropolis–Hastings

A way to draw samples from a complicated probability distribution you can only evaluate up to a constant, by building a random walk whose long-run resting place is exactly that distribution. The engine behind practical Bayesian inference.

Prerequisites: Bayesian Inference, Markov Chains

Suppose you want samples from some distribution, a Bayesian posterior, say, but you can only compute its density up to an unknown scaling constant. That is the usual situation: Bayes' theorem gives the posterior as (likelihood × prior) divided by a normalizing integral that is hopelessly hard to compute in more than a couple of dimensions. Markov chain Monte Carlo (MCMC) sidesteps the integral entirely. Instead of computing the distribution, it wanders through the space in a cleverly designed random walk whose long-run visiting frequencies match the target distribution exactly. Collect the places it visits and you have your samples.

The Metropolis–Hastings rule

The workhorse recipe is Metropolis–Hastings. From your current position xx, do two things:

  1. Propose a candidate xx' from some proposal distribution q(xx)q(x' \mid x), for example a small random step.
  2. Accept or reject it. Accept the move with probability
a=min ⁣(1, p(x)q(xx)p(x)q(xx)),a = \min\!\left(1, \ \frac{p(x')\, q(x \mid x')}{p(x)\, q(x' \mid x)}\right) ,

and if you reject, stay put and record xx again. Here p()p(\cdot) is the target density (known only up to a constant) and q()q(\cdot) is your proposal. When the proposal is symmetric, q(xx)=q(xx)q(x'\mid x) = q(x\mid x'), this collapses to the simple Metropolis rule a=min(1, p(x)/p(x))a = \min(1, \ p(x')/p(x)): always move uphill to higher density, and move downhill only sometimes, in proportion to how much lower the new spot is.

The magic is in that ratio p(x)/p(x)p(x')/p(x): the unknown normalizing constant appears in both the top and bottom and cancels. You never need it. That single fact is why MCMC can sample a posterior no one can integrate.

current x uphill: always accept downhill: accept sometimes
One Metropolis step. A proposal to higher density (green) is always accepted; a proposal to lower density (clay) is accepted only with probability $p(x')/p(x)$. Over many steps the walker lingers where the density is high, reproducing the target.

Metropolis–Hastings builds a Markov chain whose stationary distribution is your target. The acceptance ratio only uses p(x)/p(x)p(x')/p(x), so the intractable normalizing constant cancels, you sample a posterior you could never integrate.

Worked example: one Metropolis step

Take a target proportional to ex2/2e^{-x^2/2} (a standard normal, up to the constant you do not know) with a symmetric random-walk proposal, so acceptance is min(1, p(x)/p(x))=min ⁣(1, e(x2x2)/2)\min(1, \ p(x')/p(x)) = \min\!\big(1,\ e^{-(x'^2 - x^2)/2}\big).

  • You are at x=0.5x = 0.5 and propose x=1.5x' = 1.5 (farther from the peak at 00). The ratio is e(1.520.52)/2=e(2.250.25)/2=e10.37e^{-(1.5^2 - 0.5^2)/2} = e^{-(2.25 - 0.25)/2} = e^{-1} \approx 0.37. So you accept the move with probability 0.370.37, draw a uniform random number, and move only if it comes up under 0.370.37.
  • Instead propose x=0.2x' = 0.2 (closer to the peak). The ratio is e(0.040.25)/2=e0.1051.11e^{-(0.04 - 0.25)/2} = e^{0.105} \approx 1.11, capped at 11, so you accept for certain.

Run this thousands of times and the histogram of visited xx values fills in a bell curve, without ever computing the normalizing 2π\sqrt{2\pi}.

What to watch out for

  • Burn-in. The chain starts wherever you put it, which may be far from the high-density region. Throw away the first stretch of samples so your estimates are not polluted by the walk-in period.
  • Correlated samples. Consecutive draws are not independent, each is a small step from the last, so a thousand MCMC samples carry less information than a thousand independent ones. Summarize this with the effective sample size, and thin or run longer if it is low.
  • Step size. Tiny proposals almost always accept but crawl; huge proposals almost always reject and get stuck. The sweet spot accepts roughly 20%20\%50%50\% of moves; tune the proposal scale to land there.
  • Multimodality. If the target has two far-apart peaks separated by a low-density valley, a random-walk chain can get trapped in one peak and never visit the other, silently missing half the distribution.

MCMC output is not a set of independent samples, and the chain needs time to forget its starting point. Always discard burn-in, check convergence (multiple chains, the R^\hat R statistic, effective sample size), and be suspicious if a multimodal posterior looks unimodal, the chain may be stuck.

You only ever need the target up to a constant. For a Bayesian posterior that means you can feed in likelihood × prior directly and ignore the evidence integral, which is precisely the quantity that makes exact Bayesian inference intractable.

MCMC is what makes Bayesian inference work when conjugate priors do not apply, it is a Markov chain engineered to have the target as its stationary law, and it is a close cousin of other simulation tools like Monte Carlo integration and importance sampling. Modern samplers (Gibbs, Hamiltonian Monte Carlo, NUTS) are all descendants of this one accept-reject idea.

Related concepts

Practice in interviews

Further reading

  • Metropolis et al. (1953); Hastings (1970)
  • Gelman et al., Bayesian Data Analysis (Ch. 11–12)
  • Robert & Casella, Monte Carlo Statistical Methods
ShareTwitterLinkedIn