AI Glossary / Probabilistic machine learning / Intermediate
Hamiltonian Monte Carlo
Definition
Hamiltonian Monte Carlo, or HMC, is a Markov chain Monte Carlo method for sampling from continuous probability distributions using gradients of their log density. It introduces artificial momentum variables and simulates motion through parameter space to propose new states. An acceptance step corrects numerical integration error in the standard algorithm.
What it means
Bayesian inference often needs a collection of plausible parameter values rather than a single best estimate. Simple random-walk sampling can explore correlated, high-dimensional distributions slowly. HMC uses local gradient information to make coordinated proposals that can travel farther while remaining compatible with the target distribution.
The physics terminology is a computational construction. Parameters act as positions, artificial momentum helps move them, and negative log probability plays the role of potential energy. The method samples uncertainty; it is not an optimization method whose goal is to descend permanently toward one highest-density point.
How it works
Define potential energy U(q) as minus the log target density, up to an irrelevant constant. Introduce momentum p, commonly Gaussian with a positive-definite mass matrix M, giving kinetic energy p-transpose M-inverse p divided by two. Their sum is the Hamiltonian H.
For a basic HMC transition, draw momentum and integrate Hamilton's equations using leapfrog steps. Each step alternates a half momentum update, a full position update and another half momentum update. Gradients of the potential control the momentum changes. Step size and trajectory length determine the numerical path.
The proposal is accepted with probability min(1, exp(H_start-H_proposal)); otherwise the chain retains its previous position. Momentum is normally refreshed for the next transition. The mass matrix and step size influence efficiency, and adaptive variants such as the no-U-turn sampler choose trajectory lengths systematically. Valid sampling also requires appropriate handling of parameter constraints and numerical behavior.
Three practical examples
1. Hierarchical estimation: explore posterior uncertainty in group effects and shared parameters. Diagnose difficult posterior geometry rather than assuming that a large number of draws guarantees reliable inference.
2. Bayesian regression: estimate joint uncertainty in correlated coefficients and use draws to calculate predictive quantities. Distinguish uncertainty about parameters from noise in future outcomes.
3. Scientific model calibration: infer continuous parameters of a differentiable simulator or likelihood. Check both gradient correctness and the cost of evaluating the model before choosing a sampling setup.
Think of it this way
Think of moving through an uneven landscape with momentum instead of choosing a fresh small random direction at every step. The motion can carry a proposal across a long valley. The analogy is limited: the landscape is a mathematical log-density surface, momentum is artificial, and the acceptance correction plus repeated momentum draws are essential to the sampling procedure.
Worked example
Consider a standard-normal target for a scalar q, so U(q)=q squared/2 and its gradient is q. Use unit mass, starting position q=1, stipulated sampled momentum p=0.5 and one leapfrog step of size 0.2. This is a teaching transition, not a practical sampling configuration.
The first half momentum update gives p_half=0.5-0.1 × 1=0.4. The full position update gives q_new=1+0.2 × 0.4=1.08. The second half momentum update gives p_new=0.4-0.1 × 1.08=0.292.
Initial energy is (1 squared + 0.5 squared)/2=0.625. Proposed energy is (1.08 squared + 0.292 squared)/2=0.625832. The acceptance probability is exp(-0.000832), approximately 0.999168. Thus a uniform draw of 0.7 would accept the proposal. The proposed position has moved away from zero, where the target density is highest, yet remains a valid sampling proposal. A momentum sign flip used in some reversible formulations leaves this kinetic energy unchanged.
Illustrative case study
Fictional case: Willow Forecasting fits a hierarchical demand model and obtains many HMC draws. The first report emphasizes the number of samples, but a reviewer sees divergent transitions concentrated in a narrow part of parameter space. The team investigates its parameterization and weakly informed scale parameters before trusting interval estimates.
After revising the model representation, it reruns multiple chains and examines mixing and effective sample sizes. This fictional case illustrates diagnosis and model review, not a claim that one adjustment always removes divergences or improves forecasting accuracy.
Common misconceptions and mistakes
HMC draws are not generally independent. Their usefulness depends on exploration, which is why effective sample size can differ substantially from the number of recorded iterations. High acceptance alone is not sufficient evidence of efficiency: extremely small steps can produce expensive, unproductive exploration.
The usual gradient-based formulation does not directly move through discrete parameter values. Such variables may need marginalization or another inference strategy. Multiple separated modes and strongly varying curvature can remain difficult. An acceptance correction addresses the discretized proposal's sampling construction; it does not repair an incorrect likelihood, erroneous gradient or inadequate exploration within a finite run.
Business use
Use HMC when continuous model parameters and differentiable log densities make it appropriate. Plan for the cost of repeated gradient evaluations and record warmup, chain settings and any parameter transformations. A convenient software default still needs assessment against the actual posterior geometry.
Review convergence diagnostics, effective sample sizes and divergences alongside predictive checks. For business decisions, compute the quantities that matter from the draws and report their uncertainty, rather than presenting raw coefficient chains as the outcome. If chains disagree or warnings persist, treat the estimates as unresolved instead of averaging away the evidence of a sampling problem.
People also ask
Does HMC need the normalized posterior density?
No. An unknown multiplicative normalizing constant cancels from the necessary calculations, provided the log density and gradients can be evaluated appropriately.
Is HMC gradient descent?
No. It uses gradients within a sampling method and should explore the distribution rather than converge to a single optimum.
What is NUTS?
The no-U-turn sampler is an HMC-based method that adapts trajectory construction to reduce manual trajectory-length tuning.
Check your understanding
Why can the example accept a move from q=1 to q=1.08 despite lower target density there?
Answer: HMC evaluates the change in joint position-momentum energy. Momentum changed too, and the total energy error is very small.
Related terms
- Markov chain Monte Carlo
- Leapfrog integrator
- No-U-turn sampler
- Effective sample size
- potential energy
- mass matrix
- Metropolis acceptance
- divergent transition
Learn next
Reproduce a leapfrog transition for a one-dimensional normal target, then compare random-walk and HMC sampling on a correlated two-dimensional normal. Study diagnostics and constrained-parameter transformations before moving to a complex posterior.
More in Probabilistic machine learning
- Generalized linear mixed model - A generalized linear mixed model, or GLMM, combines a generalized linear response model with random effects that represent variation among groups or other...
- Gibbs kernel - The Gibbs kernel is a Gaussian-process covariance function that allows the characteristic length scale to vary with the input location.
- Graphite graph generative model - Graphite is a graph generative modeling framework that combines latent node representations with an iterative graph-neural-network decoder.
- Hard attention - Hard attention is an attention mechanism that makes a discrete selection of input information, such as choosing one image region or a small set of positions...
- Hard expectation maximization - Hard expectation maximization, or hard EM, fits a latent-variable model by replacing uncertainty about hidden variables with selected assignments and then...
- Harmonic mean estimator - The harmonic mean estimator is a method for estimating a Bayesian model's marginal likelihood from posterior samples by taking the harmonic mean of their...
Sources
Stan authors, Reference Manual, MCMC Sampling: https://mc-stan.org/docs/reference-manual/mcmc.html
Neal, MCMC Using Hamiltonian Dynamics: https://arxiv.org/pdf/1206.1901
Independent review consulted Stan’s Hamiltonian, leapfrog and Metropolis sections and Neal’s treatment. Original transition arithmetic verified; forecasting case fictional.
Previous: Hallucination Next: Hamming