How MCMC samplers explore a posterior
MCMC turns "I can't integrate this posterior" into "I can wander through it and let the visits pile up." Watch three different samplers explore the same two-parameter target and see why the choice of sampler matters.
What is actually happening
You want the posterior $p(\theta \mid y)$ but you cannot compute its normalizing constant (the integral in the denominator of Bayes' rule). MCMC sidesteps that: it builds a random walk through parameter space whose long-run visiting frequency equals the posterior. You never compute the integral — you just take steps according to a simple rule, and the histogram of where the chain has been is the posterior.
Every sampler below only ever needs the posterior up to a constant — i.e., prior × likelihood — because the unknown constant cancels in every accept/reject ratio. That is the whole trick.
The target here is a two-parameter posterior shaped like a tilted ellipse (a correlated bivariate normal — think of two parameters, like a slope and an intercept, whose estimates are correlated). Correlation is what makes samplers struggle, so you can dial it up and watch each sampler cope.
Metropolis vs Gibbs vs HMC — pros and cons
| Sampler | How it proposes a move | Strengths | Weaknesses |
|---|---|---|---|
| Random-walk Metropolis | Jump a random step from the current point; accept with probability $\min(1, \text{posterior ratio})$. | Dead simple; needs only the un-normalized posterior; works on anything. | Slow, especially in high dimensions or with correlation. Step size must be tuned: too small → crawls; too large → mostly rejects. Acceptance ~23% is the classic sweet spot. |
| Gibbs | Update one parameter at a time by drawing it from its full conditional distribution, holding the others fixed. | No tuning, no rejections — every draw is accepted. Excellent when the conditionals are known (conjugate models); this is what classic BUGS/JAGS lean on. | Needs the conditionals in closed form. Moves are axis-aligned, so strong correlation between parameters makes it inch along a ridge very slowly. |
| Hamiltonian Monte Carlo (HMC) | Give the point "momentum" and roll it along the posterior's gradient (physics simulation), then accept/reject to correct for numerical error. | Takes long, informed jumps that follow the shape of the posterior, so it mixes far faster in high dimensions and under correlation. The engine behind Stan and Nimble's HMC. | Needs the gradient of the log-posterior. Two knobs (step size $\epsilon$, number of leapfrog steps $L$) to tune; a bad choice diverges. More work per iteration. |
And NUTS? The No-U-Turn Sampler is HMC that automatically chooses how many leapfrog steps to take (it stops when the trajectory starts doubling back), removing the hardest tuning knob. It is the default in Stan and a good default when you have gradients. The practical ladder: reach for Gibbs when conjugacy gives you the conditionals, HMC/NUTS when you have gradients and correlation or high dimension, and random-walk Metropolis as the always-available fallback (and the easiest to understand).
Things to try
- Crank up the correlation. Slide the correlation toward $\pm 0.95$. Watch random-walk Metropolis and Gibbs both bog down, crawling along the diagonal ridge, while HMC still slides along it in big strokes.
- Mis-tune Metropolis. Set its step size very small (chain barely moves, acceptance near 100% but useless) then very large (almost every proposal rejected). Find the middle where acceptance is ~20–40%.
- Watch Gibbs turn corners. Gibbs only ever moves horizontally then vertically — you can see the right-angle staircase. Under high correlation those steps get tiny.
- HMC trajectories. With HMC, each proposal is the end of a curved glide across the posterior (shown faintly). Increase the leapfrog steps $L$ and each move reaches further.
← See the 1D version (likelihood → posterior → MCMC) · Day 4 lab