Sampler Laboratory
Four samplers, one landscape
Pick a distribution with a nasty personality — a curved ridge, two islands, a ring, a funnel — and release four different Markov chains on it at once. They all target the same π and all are provably correct in the limit. What separates them is how many centuries that limit takes. Click anywhere on the map to restart every chain from that point.
Target:
—
Random-walk MH
Gibbs
Independent MH
Hamiltonian MC
true contours
Toggle any sampler off with its button below to declutter the map.
Show:
Random walk
–
Gibbs
–
Independent
–
HMC
–
Iterations
0
HMC divergences
0
Each sampler box reads ESS above and its acceptance rate below. ESS is the honest currency: it is how many independent draws the chain is actually worth.
Running estimate of 𝔼[x] — who converges on the right answer, and how fast
truth (numerically integrated over the plotted box)
A flat line far from the dashed truth is the dangerous failure: a chain that is confident, stable, and wrong because it never left its starting basin.
True 𝔼[x]
–
error · RW
–
error · Gibbs
–
error · Indep
–
error · HMC
–
The four samplers
Random-walk MHMetropolis (1953) |
Propose y = x + σ·𝒩(0, I) and accept with α = min(1, π(y)/π(x)). The proposal is symmetric, so q cancels entirely. Dead simple and needs nothing but the ability to evaluate π up to a constant — but it diffuses, covering distance only like √T, and σ has to be small enough for the narrowest direction of the target.
α = min(1, π(y)/π(x)) |
GibbsGeman & Geman (1984) |
Update one coordinate at a time by drawing from its exact conditional, x ~ π(x | y). Here that conditional is built numerically on a fine grid, so every move is accepted — there is no acceptance rate to tune, which is why Gibbs powered a generation of Bayesian software. The catch: moves are strictly axis-aligned, so a diagonal ridge is climbed in tiny stairsteps. One caveat specific to this lab — because the grid spans the plotted box, this Gibbs sampler cannot leave the frame, while the other three can; on a heavy-tailed target like the funnel that quietly flatters it.
xt+1 ~ π(· | yt) ; yt+1 ~ π(· | xt+1) |
Independent MHHastings (1970) |
Ignore the current position: propose from a fixed broad distribution q covering the whole space, then correct with the full Hastings ratio. Because proposals are global it can teleport between separated modes — the one thing a random walk cannot do. But if q is a poor match for π, almost everything is rejected and the chain freezes for thousands of iterations.
α = min(1, [π(y)q(x)] / [π(x)q(y)]) |
Hamiltonian MCDuane et al. (1987) |
Give the state a random momentum, then simulate frictionless physics on the surface U = −log π using the leapfrog integrator. Trajectories follow the target's geometry, so distance grows like T rather than √T. The price: it needs ∇log π, and a step size larger than the narrowest scale makes the integrator explode — those failures are the divergences counted above.
H(x,p) = −log π(x) + ½|p|² ; α = min(1, eH(x,p) − H(x′,p′)) |
Things worth trying
| Squeezed ridge | Watch the random walk shuffle sideways while HMC runs the length of the ridge — roughly 30× the ESS for the same iteration count. Then drop the step-size scale to 0.3: the random walk's acceptance jumps to about 60% and its ESS falls by two thirds. High acceptance is not the goal; moving is. |
| Two islands | The most instructive screen in the lab. Independent MH is the only sampler that visits both modes, and it is the only one whose 𝔼[x] error stays near zero — the other three are wrong by more than 2.5. Now look at their ESS: HMC happily reports a four-figure effective sample size while sitting in one hump the entire run. ESS cannot see a mode you never visited. This is precisely the failure R̂ across dispersed starts exists to catch. |
| Neal's funnel | The famous stress test. The neck is narrower than any fixed step size, so HMC's integrator explodes there — the divergence counter climbs into the hundreds and the chain silently avoids the region it cannot integrate, leaving a badly biased 𝔼[x]. Real HMC implementations report divergences to you for exactly this reason: they are the sampler admitting it skipped something. |
| Ring | No single step size works: big enough to travel around the ring means stepping across the empty middle. Gibbs wins outright here, because each axis-aligned conditional is bimodal — it can jump straight across the hole in one move. |