Hamiltonian Monte Carlo and NUTS
A smarter way to sample from a complicated posterior distribution by simulating a physical object rolling across the probability landscape using its momentum, rather than shuffling randomly step by step — dramatically fewer wasted moves than basic MCMC.
Prerequisites: MCMC and Metropolis–Hastings, Gradient Descent
Basic Metropolis-Hastings MCMC explores a posterior distribution by proposing a small random step and accepting or rejecting it — like a blindfolded person feeling their way around a room by shuffling randomly, only moving where the floor doesn't drop away. In low dimensions this eventually covers the space fine, but over, say, 20 correlated parameters, random shuffling wastes enormous numbers of steps wandering in place or getting rejected, since most random directions in high dimensions lead somewhere worse. Hamiltonian Monte Carlo (HMC) replaces the blind shuffle with something far more efficient: it uses the shape of the posterior (its gradient) to simulate a smooth trajectory that glides across high-probability regions the way a ball rolls across a landscape, using momentum instead of guesswork.
An analogy: a marble rolling on a hilly surface versus a blindfolded person groping around
Picture the negative log of the posterior density as a landscape of hills and valleys — low points are high-probability regions. A blind, random-shuffling sampler pokes around step by step, often stepping "uphill" into low-probability territory and getting rejected. HMC instead gives the sampler a marble and some momentum, simulating it physically rolling across the landscape according to the slope at every point — it naturally swoops through valleys and only slows climbing hills, covering long, sensible distances per move instead of tiny random hops. The No-U-Turn Sampler (NUTS) is a refinement that automatically decides how far to let the marble roll before it curves back on itself — exploring new territory and no longer, without a human hand-tuning trajectory length.
The maths, one symbol at a time
HMC augments the parameter (position) with an auxiliary momentum variable , and simulates trajectories under Hamiltonian dynamics:
In words: (total "energy") sums a "potential energy" term, , low where the posterior is high (so the marble settles into high-probability valleys), plus a "kinetic energy" term from momentum that lets it carry speed through the landscape rather than crawling. The sampler alternates: draw a random momentum kick, simulate the marble's motion for several small steps by following the gradient of (why HMC needs a differentiable model), then accept or reject the resulting based on how well energy was conserved along the path. NUTS automates the trajectory length, stopping once the path would start doubling back on itself (a "U-turn"), so both wasteful loops and wasteful short hops are avoided automatically.
Worked example 1: why gradient information beats random shuffling
Consider a posterior that's a narrow, elongated ridge — highly correlated parameters, like two regression coefficients that trade off against each other. A random-walk Metropolis sampler proposing isotropic steps will mostly propose points off the ridge (rejected), since "most directions" in a narrow ridge point off it; an accepted move traveling any meaningful distance along the ridge can take hundreds of wasted proposals. HMC's gradient-following trajectory naturally curves to follow the ridge's shape, since the gradient of points along the direction of highest density change — a single HMC trajectory can traverse a distance along the ridge that would take a random-walk sampler orders of magnitude more steps to cover.
Worked example 2: NUTS avoiding a wasted long trajectory
Suppose an analyst manually tunes plain HMC's trajectory length to 50 steps, but the posterior is fairly compact in one region — after only 15 steps the trajectory has already looped back near its starting point, and continuing another 35 steps just retraces explored territory, wasting three-fifths of the computation. NUTS detects the U-turn automatically around step 15 and stops there, then repeats this adaptively for every draw — using long trajectories where the posterior geometry calls for them and short ones where it doesn't, with no manual tuning.
The explorer above shows gradient-following steps crawling toward a minimum on a loss surface — HMC uses the same underlying gradient information, but adds momentum so the trajectory glides through the landscape rather than creeping toward it one small step at a time.
What this means in practice
HMC and NUTS are the default samplers behind modern Bayesian software (Stan, PyMC, NumPyro) precisely because they scale to the dozens or hundreds of correlated parameters typical of real factor models, hierarchical models, and Bayesian neural networks — where plain Metropolis-Hastings would need impractically many iterations to converge (see MCMC convergence diagnostics). The tradeoff: HMC needs a differentiable log-posterior, ruling out discrete parameters unless handled separately.
Hamiltonian Monte Carlo uses the gradient of the log-posterior to simulate a physically-inspired trajectory with momentum, letting the sampler glide efficiently through high-probability regions instead of randomly shuffling — NUTS extends this by automatically choosing how long each trajectory should run, avoiding both wasteful short hops and wasteful looping-back trajectories without manual tuning.
HMC's efficiency depends on correctly-tuned step size and mass matrix; a poorly tuned step size can cause the simulated trajectory to become numerically unstable ("divergent transitions"), silently biasing the sampled posterior in ways that basic convergence diagnostics might not immediately flag as a problem. Modern implementations like NUTS auto-tune much of this during a warm-up phase, but divergence warnings from the sampler should never be ignored or discarded as noise — they specifically indicate regions of the posterior the sampler failed to explore correctly, which can silently distort downstream conclusions if left unresolved.
Related concepts
Practice in interviews
Further reading
- Neal, 'MCMC Using Hamiltonian Dynamics' (2011)
- Hoffman and Gelman, 'The No-U-Turn Sampler' (2014)