Funnels, coordinates and the Hastings ratio

Jump to the first figure ↓

A Markov chain Monte Carlo sampler walks through a distribution one step at a time, and in the long run the places it visits are a sample from it. Whether it gets there depends on the distribution's shape in the coordinates the sampler moves in, and on an acceptance rule that is exactly right. Press Play on a panel to watch a chain move; the numbers beside it say how close it has come to the exact answer.

Samplers with fixed settings. These panels use random-walk Metropolis, Gibbs, and Hamiltonian Monte Carlo with the step size and number of steps you set. Chains start at the point shown, with no warm-up or automatic tuning, so every step can be watched. See How it works for the targets and diagnostics.

1 One funnel, three coordinates

Neal's funnel is the shape a hierarchical model takes when the spread between its groups is uncertain: y is normal with SD σ, and given y, x is normal with SD ey. Low y is a narrow neck and high y a wide mouth, so no one step size suits both. Sampled in these coordinates (centred), a chain hardly enters the neck: steps sized for the mouth overshoot it, and there HMC's trajectories diverge. Written as two independent standard normals, with x = xraw eσyraw (non-centred), the same distribution is a round blob that a sampler crosses easily. A third parameterization uses two quantiles in (0, 1) put through Φ⁻¹, sampled on the logit scale. The funnel is untruncated in every parameterization.

    Where the sampler moves

    The funnel, x against y

    Energy at completed steps

    Inside one HMC transition

    Leapfrog alternates two half updates of momentum with a full position update. The sampler checks the signed increase H − H₀ only after the second half update. Above 1000, it stops and rejects this transition.

    1. Momentum ½p ← p + ½ε ∇log π(q)
    2. Position + gradientq ← q + εp; recalculate ∇log π(q)
    3. Momentum ½ + checkp ← p + ½ε ∇log π(q); check H

    y at each iteration

    The draws of y so far

    The funnel's curves hold 50% and 95% of x at each y; in the sampler's own coordinates, the contours hold 50%, 90% and 99% of the mass. Dashed lines on the trace are y's exact 5%, 50% and 95% quantiles; the curve over the histogram is its exact density.

    Sample in
    Sampler

    Neal's funnel is σ = 1.5 (his v = 2y has SD 3).

    2 The Hastings ratio

    A Metropolis–Hastings step proposes a move from x to x′ and accepts it with probability min(1, π(x′) q(x | x′) / π(x) q(x′ | x)). When proposals are not symmetric, the ratio q(x | x′)/q(x′ | x) keeps the chain on its target. Multiplying a positive parameter by eλZ, a common move for scales and rates, proposes large values more widely than small ones, and its ratio is x′/x. Leave the ratio out and the chain settles on π(x)/x; turn it upside down and it settles on π(x)/x². Here both coordinates of a log-normal pair are multiplied at each step.

      The draws of x1 so far

      Solid contours and curve: the log-normal the chain is meant to sample. Dashed: the distribution a chain with this ratio actually samples, when that is different.

      The Hastings ratio

      3 A normal and an evil smiley

      The correlated normal comes with a Gibbs sampler, which redraws one coordinate at a time from its exact conditional distribution, so every move is accepted but each moves along one axis only. The smiley is a mixture: a banana-shaped mouth with three-quarters of the mass and two small eyes above it, which a sampler has to find and travel between.

        The draws of x so far

        The draws of y so far

        Contours hold 50%, 90% and 99% of the mass; the curves over the histograms are the exact marginal densities.

        Target
        Sampler

        Gibbs needs the exact conditionals, which only the normal has here.

        How it works

        The samplers

        Random-walk Metropolis proposes a move by a normal step with the SD you set, in both coordinates at once, and accepts it with probability min(1, π(x′)/π(x)). HMC draws a standard normal momentum, follows the target's gradient by the leapfrog method with step size ε for the number of steps you set, and accepts the end point with probability min(1, e−ΔH), where ΔH is the signed change in total energy; when that change passes 1000 (Stan's threshold), the transition is divergent: it stops there and is rejected. Gibbs redraws x and y in turn from their conditionals. Every chain starts at the point shown, with no warm-up, and draws from the seed, so a seed gives the same chain for the same settings. The API provides the same computations.

        Reading the energy check

        With unit mass, potential energy is U(q) = −log π(q), kinetic energy is K(p) = ½pᵀp, and total energy is H = U + K. Exact Hamiltonian motion would keep H constant. The plot records H after each completed leapfrog step; its dashed line is that transition's starting energy H₀ + 1000. The rule here checks the signed increase H − H₀, not its absolute value. Values beyond the plot's axis are marked off scale, and a non-finite calculation is marked explicitly.

        The three highlighted parts show the actual momentum, position and gradient updates. Energy can move between the half updates; those partial states are not divergence checks. A large positive energy error at the completed step stops this implementation's trajectory and rejects its endpoint. Inspect the first divergence to see a recorded example, then use Previous part, Next part or the integration slider. The Stan reference manual explains why numerical integration can fail in a narrow neck. These examples use fixed-length HMC with no warm-up or adaptation.

        The numbers

        The effective sample size is bulk ESS: the chain is split in two halves and rank-normalised, with Geyer's initial monotone sequence. The KS distance is the largest gap between the draws' distribution function and the exact one. The exact marginals are closed forms or one-dimensional integrals (the funnel's x and the smiley's y).