Momentum Loans for the Posterior
A random-walk sampler is polite to a fault.
It stands at the current point and asks for a nearby one. If the nearby point is plausible, it moves. If not, it stays. This is enough for a theorem, and often not enough for a computation.
Hamiltonian Monte Carlo begins with a stranger bargain:
borrow momentum
move like a physical particle
give the energy back
The borrowed momentum is fake. The energy is a construction. The motion is only numerical. But the resulting proposal can cross long posterior valleys without behaving like a diffusive drunkard.
This is why HMC feels different from many Markov chain methods. It does not ask for a random local perturbation. It asks for a trajectory.
Turn Density Into Terrain
Let the target density be:
\[\pi(q) \propto \exp(-U(q)).\]The variable (q) is the position. The function (U(q)) is potential energy: low where the posterior is dense, high where the posterior is unlikely.
Now introduce an auxiliary momentum (p), usually Gaussian, and define:
\[H(q,p)=U(q)+K(p).\]For the simple kinetic energy in the lab:
\[K(p)=\frac{1}{2m}p^\top p.\]The joint density is:
\[\pi(q,p) \propto \exp(-H(q,p)).\]If we sample a fresh momentum and then follow Hamiltonian dynamics, two things make the trick usable:
- exact Hamiltonian dynamics preserve (H);
- exact Hamiltonian dynamics preserve volume.
Energy preservation means a trajectory can move far while staying on a plausible level set. Volume preservation means the transformation does not need a nasty Jacobian correction. Neal emphasizes exactly these features: HMC uses Hamiltonian dynamics to make distant Metropolis proposals and avoids the slow diffusion of simple random-walk proposals.1
The posterior did not change. We changed the proposal mechanism.
Leapfrog Keeps the Books
Continuous Hamiltonian dynamics are beautiful:
\[\frac{dq}{dt}=\frac{\partial H}{\partial p},\qquad \frac{dp}{dt}=-\frac{\partial H}{\partial q}.\]The computer cannot follow them exactly. It must take steps.
The usual HMC stepper is leapfrog:
p <- p - (epsilon / 2) grad U(q)
q <- q + epsilon p / m
p <- p - (epsilon / 2) grad U(q)
Repeated L times, this produces a proposal. Then a Metropolis step accepts it
with probability:
min(1, exp(-(H_new - H_old)))
This is the small miracle: the integrator is allowed to make numerical error, and the accept/reject step audits that error.
But not every integrator is equally forgivable. Forward Euler also takes steps, but it tends to drift in energy. Leapfrog is time reversible and volume-preserving, the two discrete properties HMC needs in order for the Metropolis correction to be simple. Neal’s review introduces the leapfrog scheme as the standard discretization for computer implementation of HMC.1
In practice, HMC lives in the gap between exact physics and finite arithmetic.
An Energy Audit You Can Watch
The lab below simulates HMC proposals in two dimensions. It is intentionally small enough to audit. The target density is visible, the trajectory is visible, and the energy error is measured instead of guessed.
The default target is a highly correlated Gaussian. With step size 0.18,
24 leapfrog steps, and 72 independent proposal audits, the lab gets about
97.5% expected acceptance, an expected jump of about 2.01, no divergences,
maximum energy error about 0.37, and a reverse error around 4e-16. On the
same starting point, forward Euler sends the energy error to about 12,740.
That contrast is the essay in one picture.
Deterministic toy model. The Metropolis audit uses the leapfrog proposal and reports expected acceptance over independently sampled positions and momenta. Forward Euler is shown only as an energy-drift contrast.
I also swept 972 target/step/mass/proposal/seed combinations. Large-step settings produced 442 divergent cases, which is the point of the demo rather than a failure of the code. The finite default case is the contrast: the proposal travels, the energy books almost balance, and Metropolis has a small correction to make.
Try four moves.
First, leave the default alone and watch the energy panel. The leapfrog line does not stay exactly at zero, but it oscillates near zero. That is good enough for the Metropolis audit to accept most proposals.
Second, increase Step size. At first the expected jump can improve because the trajectory moves more aggressively. Then the energy error grows, the sweep panel collapses, and divergences appear. A divergent transition is the sampler telling you that the numerical physics has stopped being credible.
Third, switch to banana posterior. The target is curved, but not terribly pathological. HMC can follow the bend if the step size is not too ambitious. Raise Mass scale and notice that the same momentum produces slower position motion, so a formerly aggressive step can become tame.
Fourth, switch to funnel neck. This is the geometry that gives HMC its bad days. In the wide part of the funnel, big moves are harmless. In the narrow neck, the same step size can be too large by orders of magnitude. A single global step size is now trying to satisfy incompatible local scales.
Step Size and Path Length Share a Fuse
The two obvious HMC tuning parameters are:
epsilon = step size
L = number of leapfrog steps
Their product is the integration time:
trajectory length = epsilon * L
If the trajectory is too short, HMC degenerates toward local behavior. It may have a beautiful acceptance rate and still crawl. If the trajectory is too long, it can waste computation, loop back toward where it started, or accumulate unnecessary numerical error.
Hoffman and Gelman’s No-U-Turn Sampler was designed around this pain point: HMC is sensitive to both step size and path length, so NUTS adaptively grows a trajectory and stops when it begins to turn back on itself.2 This is not a minor convenience. It is a recognition that “how long should I simulate the physics?” is not an easy scalar question across a curved posterior.
In the lab, the step-size sweep is a crude version of what real software has to learn. It is not enough to ask whether a proposal is accepted. You also care how far accepted proposals move per gradient evaluation.
The Proposal Has to Walk Backward
The Metropolis correction is not a forgiveness machine for arbitrary numerical schemes.
For the usual HMC proposal, the deterministic map produced by the integrator must be reversible and volume-preserving. Leapfrog has these properties. Forward Euler does not. That is why the lab does not use Euler proposals for the Metropolis audit; it uses Euler only as a warning.
The reverse-error metric checks the involution idea. Run the leapfrog trajectory forward, flip the momentum, run the same number of steps again, and you should come back to the starting position and opposite momentum. In the default case, the error is around floating point roundoff.
This is not decorative physics. It is the algebra that lets a deterministic trajectory become a valid Markov transition after an accept/reject step.
A Perfect Acceptance Rate Can Crawl
An HMC run with 99% acceptance can be excellent.
It can also be timid.
Acceptance measures energy error, not exploration. If epsilon is tiny and L
is small, almost every proposal is accepted because almost nothing happened.
This is why the lab reports expected jump alongside acceptance. In real
diagnostics you would look at effective sample size per gradient evaluation,
divergences, tree depth, R-hat across chains, energy diagnostics, and posterior
predictive checks.
Betancourt’s conceptual introduction makes this broader point: HMC succeeds when its simulated trajectories align with the geometry of the target, and it fails when that geometry cannot be resolved by the chosen numerical implementation.3
High acceptance is not a certificate of geometry.
The Funnel Changes the Ruler
The funnel target in the lab is a two-dimensional cartoon of a hierarchical model:
y controls the scale
x | y gets narrower as y decreases
In the neck, the curvature is high. In the mouth, the curvature is low. A single Euclidean mass and a single global step size must serve both regions. That is unfair.
This is why reparameterization can matter more than heroic tuning. A centered hierarchical parameterization can create funnel geometry; a non-centered parameterization may remove much of it. HMC is a powerful geometric sampler, but it is still sampling the geometry you gave it.
If the geometry has cliffs, necks, ridges, or separated islands, the sampler will report that through divergences, small step sizes, poor energy behavior, or slow exploration.
The sampler is not being dramatic. It is reading the landscape.
Where the Loan Stops Working
HMC buys long-distance proposals in differentiable continuous spaces.
It does not buy:
discrete variables without extra machinery
easy jumps between isolated modes
immunity to bad parameterization
cheap gradients
automatic correctness under discontinuities
Duane, Kennedy, Pendleton, and Roweth introduced hybrid Monte Carlo in lattice field theory, combining molecular-dynamics trajectories with a Monte Carlo accept/reject step.4 That ancestry is still visible. HMC is at its best when gradients are meaningful and the posterior is a connected terrain that can be traversed by dynamics.
When the target has isolated rooms separated by low-density walls, ordinary HMC may still stay in one room. When the target has a funnel neck, ordinary HMC may need a small step size set by the worst local curvature. When the log density is not differentiable, the method loses the vector field it was built to follow.
Physics is not a free lunch. It is a better proposal language.
The Loan Has to Balance
I like to think of HMC as a loan.
The sampler borrows momentum from a Gaussian bank, spends it moving through the posterior, and then has to repay the Hamiltonian. If leapfrog keeps the books nearly balanced, Metropolis signs off. If the numerical trajectory invents or destroys too much energy, Metropolis rejects the loan.
That is why the energy plot matters more than the animation.
The path is pretty. The audit is the algorithm.
-
Radford M. Neal, “MCMC Using Hamiltonian Dynamics”, in Handbook of Markov Chain Monte Carlo, 2011. ↩ ↩2
-
Matthew D. Hoffman and Andrew Gelman, “The No-U-Turn Sampler: Adaptively Setting Path Lengths in Hamiltonian Monte Carlo”, Journal of Machine Learning Research 15, 2014. ↩
-
Michael Betancourt, “A Conceptual Introduction to Hamiltonian Monte Carlo”, 2017. ↩
-
Simon Duane, A. D. Kennedy, Brian J. Pendleton, and Duncan Roweth, “Hybrid Monte Carlo”, Physics Letters B 195(2), 1987. ↩