MCMC: Metropolis–Hastings & Gibbs
Random walks whose long-run distribution is the posterior.
Why should a random walk that sometimes refuses to move end up distributed exactly like your posterior? This lesson proves that it does, then turns to the harder questions: how fast the walk forgets where it started, how big its steps should be (the source of the famous 0.234), what Gibbs sampling really is, and why no diagnostic can promise that a chain has seen the whole distribution.
What you’ll learn
- Prove that the Metropolis–Hastings kernel is reversible with respect to , and state the conditions under which ergodic averages converge and satisfy a central limit theorem.
- Show that Gibbs updates are Metropolis–Hastings moves accepted with probability 1, and derive the Gibbs sampler's autocorrelation for a correlated Gaussian.
- Prove a geometric convergence bound for the independence sampler by a minorization argument.
- Derive the optimal scaling of random-walk Metropolis (acceptance about 0.234, proposal variance proportional to ) from its diffusion limit.
- Relate mixing time to the spectral gap and to bottlenecks, and explain what and the effective sample size can and cannot detect.
Markov chains on general state spaces
Invariance alone does not make a chain converge: it must also be able to reach every region (-irreducible: from any start, every set of positive -probability is reached with positive probability) and must not cycle deterministically through disjoint regions (aperiodic). Then the standard theory applies (see Roberts & Rosenthal, 2004).
The factor is the integrated autocorrelation time: correlated samples estimate as well as independent ones, which is the effective sample size. A CLT can fail outright for a valid sampler. An independence sampler whose proposal has lighter tails than the target rejects so persistently in the tails that, for an exponential target with an exponential proposal of at least twice the rate, the identity function has no central limit theorem at all: the chain is not geometrically ergodic, and its averages still converge, but not at the rate.
Metropolis–Hastings is correct, and the Metropolis choice is best
For the kernel has density . Multiply by and push inside the minimum:
The right side is symmetric in and , so the off-diagonal flows balance. The rejection part lives on the diagonal , where it is trivially symmetric:
Integrate over and use . Irreducibility and aperiodicity hold under mild conditions, for instance for a Gaussian random walk on a target with a continuous positive density, so the convergence theorem applies:
Detailed balance does not single out this acceptance rule: Barker's rule also satisfies it. Peskun (1973) showed, on finite state spaces, that among such rules the Metropolis–Hastings choice yields the smallest asymptotic variance for every function: it makes every off-diagonal transition probability as large as detailed balance allows, so the chain wastes the fewest steps standing still. Tierney (1998) extended this ordering of reversible kernels to general state spaces.
The proposal is where the art lies. Three classical choices:
- Random walk: ; symmetric, so . Robust, but local.
- Independence sampler: , a fixed approximation of (say, a heavy-tailed fit at the mode). Then with : a Markov-chain version of importance sampling.
- Gradient-based: Langevin and Hamiltonian proposals use (Hamiltonian Monte Carlo).
For the chain moves to with density . Both arguments are at least , because everywhere:
So for some Markov kernel . Run a copy from and a copy started in , coupled so that at each step, with probability , both jump to the same draw from . After that they move together, and the copy from stays distributed as . The coupling inequality bounds the distance by the probability of not having met:
An independence sampler uses a proposal with . After 10 iterations, what does the theorem guarantee about the total-variation distance to , from any start?
- At most
- At most
- At most about
- Nothing, unless the chain started in
Show the answer
C. At most about
. The bound holds uniformly over starting points, which makes this chain uniformly ergodic. A poor proposal (large ) makes the guaranteed rate close to 1.
Gibbs sampling is Metropolis–Hastings
A Gibbs update of coordinate draws and keeps . Introduced for image restoration with Gibbs random fields by Geman & Geman (1984), and brought into Bayesian statistics by Gelfand & Smith (1990), with data augmentation (Tanner & Wong, 1987) as a close relative, it is a special case of Metropolis–Hastings.
The proposal density is , and the reverse move has . Factor , and likewise for :
Since , every factor cancels. Each update is reversible; a random-scan sampler (pick at random) is reversible too, while a systematic scan ( in order) is a composition of reversible kernels: -invariant, but not reversible in general.
Take standard bivariate Gaussian with correlation . The full conditionals are and . Composing one systematic sweep, with independent standard normals :
So the -chain is AR(1) with coefficient (its innovation variance keeps the stationary variance at 1). Its autocorrelations are , and the effective sample size per sweep follows:
With correlation , what percentage of Gibbs sweeps are effectively independent samples of ?
Show the answer
1 %
, about 1%. With it is about 10.5%. Strong posterior correlation is why practitioners block correlated parameters (update them jointly), reparameterize to reduce correlation, or collapse (integrate parameters out analytically, as in the collapsed sampler for LDA).
Optimal scaling: where 0.234 comes from
With step , the log acceptance ratio is a sum over coordinates. Taylor-expand each term to second order in :
In stationarity the first sum is asymptotically by the central limit theorem. The second converges to , using the integration-by-parts identity :
For this "mean equals minus half the variance" Gaussian, , and as well. The acceptance rate is their sum:
Each accepted step moves a coordinate by about in squared distance, so steps achieve , the diffusion's speed. With , maximize : the first-order condition gives .
The target is independent standard normal coordinates (). What proposal standard deviation per coordinate does the theorem recommend?
Show the answer
0.238
. The proposal must shrink like , so each coordinate diffuses a distance of order per step, and exploring the target takes order iterations. For a Gaussian target with covariance , the matching rule is a proposal covariance .
| Sampler | Tuning parameter must scale as | Optimal acceptance | Cost to explore |
|---|---|---|---|
| Random-walk Metropolis | proposal variance | 0.234 | order iterations |
| Langevin (MALA; Roberts & Rosenthal, 1998) | proposal variance | 0.574 | order iterations |
| Hamiltonian Monte Carlo (Beskos et al., 2013) | leapfrog step , integration time fixed | 0.651 | order leapfrog steps per proposal |
Mixing, bottlenecks and what diagnostics miss
How long must a chain run? The mixing time answers it in the worst case over starts. For a reversible, irreducible, aperiodic chain on a finite space with absolute spectral gap (one minus the largest eigenvalue modulus below 1) and relaxation time , the spectral gap controls mixing up to logarithmic factors (Levin & Peres, 2017):
The gap in turn is controlled by geometry. The bottleneck ratio , with the equilibrium flow out of , satisfies Cheeger's inequality for the spectral gap of a reversible chain (Jerrum & Sinclair, 1989; Lawler & Sokal, 1988).
Two well-separated modes are the canonical bottleneck: with one mode, the flow across the low-density valley is tiny, so is tiny and is enormous, whatever the local step size. A random walk must accept a sequence of moves into the valley, an event whose probability falls roughly exponentially with the valley's depth in .
Think first Four chains of length each have within-chain variance 1 and means , , and (one chain found the other mode). Compute , where is the variance of the four chain means. What if all four chains had stayed in the first mode, with means , , and ?
The chain means have sample variance , so and : far above the recommended 1.01, so the disagreement is flagged. With all four chains in one mode, , comfortably "converged", although half of the posterior has never been seen. Diagnostics compare chains with each other; they cannot see mass that no chain has visited.
Remedies for multimodality change the target or the moves rather than the step size. Parallel tempering (replica exchange; Swendsen & Wang, 1986) runs chains on flattened versions , , and swaps states between temperatures, so the hot chains carry the cold one across valleys; it too can mix slowly, and Woodard, Schmidler & Huber (2009) give conditions for rapid and for torpid mixing. Starting many chains from dispersed points remains the most practical defense.
Key takeaways
- Metropolis–Hastings is reversible because is symmetric; with irreducibility and aperiodicity, and ergodic averages converge. Reversible, geometrically ergodic chains satisfy a CLT with variance .
- Among acceptance rules satisfying detailed balance, the Metropolis–Hastings choice minimizes asymptotic variance (Peskun). The independence sampler converges at rate when , and is not geometrically ergodic when is unbounded.
- Gibbs updates are Metropolis–Hastings moves accepted with probability 1. On a correlated Gaussian the sweep autocorrelation is , so ESS per sweep is : blocking and reparameterization matter.
- Random-walk Metropolis on -dimensional product targets needs proposal scale , acceptance about 0.234 and order iterations; MALA and HMC improve this to and .
- Mixing is governed by the spectral gap, and bottlenecks (Cheeger) make multimodal targets mix exponentially slowly. and ESS detect disagreement and autocorrelation, never unvisited mass.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which property of the Metropolis–Hastings kernel guarantees that is its stationary distribution?
- The acceptance rate is between 20% and 50%
- The proposal is symmetric
- Detailed balance: is symmetric in and
- The chain is started from a draw of
Show the answer
C. Detailed balance: is symmetric in and
Reversibility with respect to integrates to invariance. Convergence to from other starts additionally needs irreducibility and aperiodicity.
For an i.i.d. product target, the dimension goes from to . How should the random-walk proposal standard deviation per coordinate change, and what happens to the number of iterations needed?
- Keep it fixed; the iterations stay the same
- Halve it; the iterations grow about fourfold
- Divide it by 4; the iterations grow about sixteenfold
- Halve it; the iterations stay the same
Show the answer
B. Halve it; the iterations grow about fourfold
The optimal scale is , so quadrupling halves it. The limiting diffusion runs on a time scale of iterations, so the cost of exploring grows linearly in .
For a systematic-scan Gibbs sampler on a bivariate Gaussian with correlation , what is the lag-one autocorrelation of the -chain from sweep to sweep?
Show the answer
D.
One sweep gives : the update of multiplies by , and the update of given multiplies by again. The ESS per sweep is .
Four chains, all started in the same mode of a bimodal posterior, give and a large ESS. What can you conclude?
- The chains agree with one another about the region they explored, which says nothing about modes none of them visited
- The posterior is unimodal
- The chains have converged to the full posterior
- The ESS proves that the samples are representative
Show the answer
A. The chains agree with one another about the region they explored, which says nothing about modes none of them visited
compares between-chain and within-chain variability; if every chain is trapped in the same mode, both measure only that mode. Dispersed starts, tempering or knowledge of the model are needed to detect the other mode.
Further reading
- N. Metropolis, A. W. Rosenbluth, M. N. Rosenbluth, A. H. Teller & E. Teller, "Equation of state calculations by fast computing machines," Journal of Chemical Physics 21(6), 1087–1092 (1953).
- W. K. Hastings, "Monte Carlo sampling methods using Markov chains and their applications," Biometrika 57(1), 97–109 (1970).
- G. O. Roberts, A. Gelman & W. R. Gilks, "Weak convergence and optimal scaling of random walk Metropolis algorithms," Annals of Applied Probability 7(1), 110–120 (1997).
- G. O. Roberts & J. S. Rosenthal, "General state space Markov chains and MCMC algorithms," Probability Surveys 1, 20–71 (2004).
- D. A. Levin & Y. Peres, Markov Chains and Mixing Times, 2nd ed. (American Mathematical Society, 2017).