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 , do two things:
- Propose a candidate from some proposal distribution , for example a small random step.
- Accept or reject it. Accept the move with probability
and if you reject, stay put and record again. Here is the target density (known only up to a constant) and is your proposal. When the proposal is symmetric, , this collapses to the simple Metropolis rule : 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 : 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.
Metropolis–Hastings builds a Markov chain whose stationary distribution is your target. The acceptance ratio only uses , so the intractable normalizing constant cancels, you sample a posterior you could never integrate.
Worked example: one Metropolis step
Take a target proportional to (a standard normal, up to the constant you do not know) with a symmetric random-walk proposal, so acceptance is .
- You are at and propose (farther from the peak at ). The ratio is . So you accept the move with probability , draw a uniform random number, and move only if it comes up under .
- Instead propose (closer to the peak). The ratio is , capped at , so you accept for certain.
Run this thousands of times and the histogram of visited values fills in a bell curve, without ever computing the normalizing .
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 – 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 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