Hamiltonian Monte Carlo

mcmcbayesianhamiltonian-dynamicsleapfrogsymplecticmetropolis-hastingsmomentumcomputational-statisticshmcenergy-conservation

Definition

Hamiltonian Monte Carlo (HMC) is a Markov Chain Monte Carlo (MCMC) method that uses gradient information to make large, directed moves in the posterior, suppressing the random-walk behaviour that makes simple Metropolis-Hastings (MH) slow in high dimensions. It augments the target distribution over parameters qq with fictitious momentum variables pp, simulates Hamiltonian dynamics jointly over (q,p)(q, p) via the leapfrog integrator, then accepts or rejects the trajectory endpoint using the Metropolis criterion.

Key Ideas

How It Works

Leapfrog Integrator

The Störmer-Verlet / leapfrog scheme interleaves half-steps for momentum with full steps for position, for i = 1, ..., L:

  1. p(t+ε/2)=p(t)(ε/2)U(q(t))p(t + \varepsilon/2) = p(t) - (\varepsilon/2)\,\nabla U(q(t))
  2. q(t+ε)=q(t)+εM1p(t+ε/2)q(t + \varepsilon) = q(t) + \varepsilon M^{-1} p(t + \varepsilon/2)
  3. p(t+ε)=p(t+ε/2)(ε/2)U(q(t+ε))p(t + \varepsilon) = p(t + \varepsilon/2) - (\varepsilon/2)\,\nabla U(q(t + \varepsilon))

This is the unique first-order symplectic integrator for separable Hamiltonians; it exactly preserves a "shadow" Hamiltonian H~H+O(ε2)\tilde{H} \approx H + O(\varepsilon^2), ensuring acceptance rates remain high even with moderately large stepsizes.

Tuning

Two hyperparameters: stepsize ε\varepsilon and number of leapfrog steps LL (trajectory length εL\varepsilon L). Optimal acceptance rate ≈ 65% (Roberts-Gelman-Gilks 1997, analogous to the 0.234 rule for random-walk Metropolis in dd dimensions). Trajectory length should be long enough for q* to be nearly independent of q, but not so long that the trajectory loops back. The No-U-Turn Sampler (NUTS, Hoffman-Gelman 2011) automates both: it grows the trajectory until the simulation turns around (U-turns), eliminating manual tuning. NUTS is the default in Stan.

Mass Matrix

MM acts as a preconditioning matrix. Setting M=Σ1M = \Sigma^{-1} (inverse posterior covariance) decorrelates parameters and equalises leapfrog stepsizes across dimensions. In practice MM is estimated from warmup iterations (diagonal approximation is standard).

Why It Matters

Scaling: the total arithmetic cost per independent sample scales as O(d2)O(d^2) for random-walk Metropolis versus O(d5/4)O(d^{5/4}) for HMC — a d3/4d^{3/4} improvement. (Equivalently: HMC needs O(d1/4)O(d^{1/4}) gradient evaluations per sample and RWM needs O(d)O(d) density evaluations, each evaluation itself costing O(d)O(d) arithmetic.) In high-dimensional posteriors (modern hierarchical models, neural networks, latent Gaussian fields) this is a decisive advantage.

Probabilistic programming: HMC/NUTS is the sampler in Stan, PyMC, and Turing.jl. The ability to target arbitrary differentiable posteriors without hand-crafted proposal distributions enabled a generation of applied Bayesian models that would have been computationally infeasible with Gibbs or random-walk MH.

Limitations:

Open Questions

Related