What is Hamiltonian Monte Carlo?

Hamiltonian Monte Carlo is a Markov Chain Monte Carlo method that augments continuous parameters with momentum and uses approximate Hamiltonian dynamics to propose distant states with a Metropolis correction.

Quick Facts

SpecificationOfficial Specification

How It Works

Turn posterior geometry into Hamiltonian dynamics

HMC defines potential energy V(q) = -log pi(q) and usually quadratic kinetic energy T(p) = 0.5 p^T M^-1 p. Hamilton's equations move position q and momentum p while conserving their total energy in continuous time. The auxiliary momentum makes long, directed proposals possible without changing the desired marginal target for q.

Neal's HMC review explains why volume preservation and reversibility allow these trajectories to become valid Metropolis proposals. HMC requires gradients and typically operates in an unconstrained parameterization.

Approximate trajectories with leapfrog steps

The leapfrog integrator alternates half momentum steps with full position steps. It is reversible and volume preserving, but discretization changes the Hamiltonian slightly, so the endpoint is accepted with min(1, exp(H_start - H_end)). Too large a step size creates rejection or divergences; too small a step size wastes gradient evaluations.

Trajectory length and mass matrix matter equally. Short paths revert toward random-walk behavior, excessively long paths retrace work, and a poorly scaled metric forces tiny steps. Warmup may adapt these controls, but retained sampling should use a transition whose validity is understood.

Treat divergences as evidence of missing exploration

The Stan HMC reference treats divergent transitions as warnings that numerical trajectories failed to follow difficult posterior geometry. Raising the target acceptance rate may reduce step size, but persistent divergences usually require reparameterization, stronger identification, or a revised model.

Run multiple chains and inspect divergences, maximum trajectory depth, energy behavior, rank-normalized split R-hat, bulk and tail ESS, and Monte Carlo error. HMC does not naturally update discrete parameters and can still miss separated modes.

Key Characteristics

  • Uses target-density gradients to guide proposals
  • Augments parameters with temporary momentum variables
  • Builds reversible volume-preserving leapfrog trajectories
  • Corrects integration error with a Metropolis decision
  • Adapts efficiently to correlated continuous posteriors
  • Can fail on divergences, discrete states, and isolated modes

Common Use Cases

  1. Bayesian inference with many continuous parameters
  2. Hierarchical models after suitable reparameterization
  3. Posterior sampling with strong linear correlations
  4. Probabilistic programming with automatic differentiation
  5. Validating faster approximate posterior methods

Example

loading...
Loading code...

Frequently Asked Questions

How does HMC differ from random-walk Metropolis?

Random-walk proposals move without using target geometry and often diffuse slowly. HMC uses gradients and momentum to make coordinated distant proposals, then applies a Metropolis correction for leapfrog integration error.

Why does Hamiltonian Monte Carlo need gradients?

The gradient of negative log density supplies the force that bends a trajectory toward the target's typical set. Missing, discontinuous, or numerically unstable gradients invalidate or severely degrade that trajectory construction.

What causes divergent transitions in HMC?

A numerical trajectory can fail when step size is too large for sharp curvature, often exposed by funnels, weak identification, or poor parameterization. Persistent divergences indicate potentially biased exploration and should not be ignored.

Can HMC sample discrete parameters?

Not directly, because leapfrog dynamics require gradients over a continuous space. Discrete variables must be marginalized, enumerated, or updated through another valid transition within a larger inference scheme.

How should HMC performance be compared?

Compare effective samples and Monte Carlo error per unit time, not acceptance rate alone. Also inspect divergences, trajectory limits, energy diagnostics, R-hat, bulk and tail ESS, predictive checks, and relevant posterior slices.

Related Terms

Related Articles