Hamiltonian Monte Carlo
Hamiltonian Monte Carlo Sampling · Also known as: HMC, Hybrid Monte Carlo, NUTS, No-U-Turn Sampler, gradient-based MCMC
Hamiltonian Monte Carlo (HMC) is a gradient-based Markov chain Monte Carlo algorithm that uses the geometry of the log-posterior surface to make large, informed jumps through parameter space instead of the small random steps of classical MCMC. Originally introduced for lattice field theory by Duane, Kennedy, Pendleton, and Roweth (1987) under the name Hybrid Monte Carlo, and brought into mainstream statistics by Radford Neal's authoritative 2011 chapter, HMC is today the default sampler in Stan and PyMC and is widely regarded as the state-of-the-art engine for Bayesian posterior inference in high-dimensional models.
Read the full method
Sign in with a free account to read this section.
Method map
The neighbourhood of related methods — select a node to explore.
+15 more
When to use it
HMC is the preferred sampler when (1) the posterior is continuous and differentiable with respect to all parameters — a necessary condition for gradient computation; (2) the parameter space is moderately to highly dimensional, typically d > 5, where random-walk samplers become geometrically inefficient; and (3) accurate posterior geometry matters, such as when parameters are correlated or the posterior has complex curvature. HMC is unsuitable for discrete parameters (which have no gradient) and requires more computation per proposal than simpler samplers, so it is overkill for low-dimensional or easily-factored posteriors. The No-U-Turn Sampler (NUTS) extension, the default in Stan and PyMC, removes the need to hand-tune the number of leapfrog steps L, making HMC practical without expert tuning.
Strengths & limitations
- Suppresses random-walk behaviour: gradient-guided proposals traverse the posterior in large, informed steps, yielding low autocorrelation and high effective sample size per gradient evaluation.
- Scales well with dimension: empirical and theoretical results show HMC mixing time grows as O(d^{1/4}) versus O(d) for random-walk Metropolis, making it the method of choice for high-dimensional posteriors.
- Metropolis correction guarantees exact sampling from the target posterior regardless of step-size errors in the leapfrog integrator.
- NUTS eliminates manual tuning of the leapfrog trajectory length while preserving the statistical guarantees of HMC.
- Fully implemented and production-tested in Stan, PyMC, NumPyro, and TensorFlow Probability with rich convergence diagnostics.
- Requires continuous, differentiable log-posteriors; cannot be applied directly to models with discrete latent variables without additional techniques such as marginalisation or embedding.
- Each leapfrog step requires one gradient evaluation of the full log-posterior, which can be expensive for large datasets without mini-batching (stochastic gradients break detailed balance and require careful correction).
- Performance is sensitive to the mass matrix M and step size ε; poorly scaled posteriors (e.g. highly correlated or funnel-shaped geometries) require re-parameterisation or adaptation.
- Warm-up (adaptation) phase can be long for complex geometries, consuming a significant fraction of the total computational budget.
Frequently asked
What is the difference between HMC and the Metropolis–Hastings algorithm?
Both are MCMC algorithms that construct a Markov chain converging to the target posterior, but they differ in how proposals are generated. Metropolis–Hastings uses a simple random-walk proposal centred on the current state, which leads to diffusive exploration and high autocorrelation in high dimensions. HMC uses the gradient of the log-posterior to guide proposals along near-constant-energy trajectories, producing distant, high-probability proposals and much lower autocorrelation. The Metropolis acceptance step in HMC corrects for numerical integration error, so both algorithms share the same theoretical guarantee of exact sampling.
What is NUTS and how does it relate to HMC?
The No-U-Turn Sampler (NUTS), introduced by Hoffman and Gelman (2014), is an adaptive extension of HMC that automatically determines how many leapfrog steps to take by building a binary tree of trajectory segments and stopping when the trajectory starts to double back on itself (the 'U-turn' criterion). This removes the need to manually tune the number of steps L, which is the most sensitive HMC hyperparameter. NUTS is the default sampler in Stan and PyMC and delivers HMC-quality samples without manual trajectory tuning.
What do divergent transitions mean and what should I do about them?
A divergent transition occurs when the leapfrog integrator makes a large numerical error, typically because it enters a region of very high curvature in the posterior — a sign that the step size is too large relative to the local geometry. Divergences are not merely a numerical issue; they indicate that the sampler is not faithfully exploring part of the posterior. The recommended remedy is to re-parameterise the model (e.g. use a non-centred parameterisation for hierarchical models) rather than simply reducing the step size, because the underlying geometric problem will persist.
Can HMC be used with discrete parameters?
Standard HMC requires gradients and therefore cannot be applied directly to discrete parameters. The recommended approach is to marginalise discrete parameters out of the model analytically before sampling, as Stan requires. Alternatively, continuous relaxations or auxiliary variable schemes can embed discrete structure in a differentiable framework. PyMC supports some discrete variables through Gibbs steps mixed with NUTS for the continuous block.
Sources
- Duane, S., Kennedy, A. D., Pendleton, B. J., & Roweth, D. (1987). Hybrid Monte Carlo. Physics Letters B, 195(2), 216–222. DOI: 10.1016/0370-2693(87)91197-X ↗
- Neal, R. M. (2011). MCMC using Hamiltonian dynamics. In S. Brooks, A. Gelman, G. L. Jones, & X.-L. Meng (Eds.), Handbook of Markov Chain Monte Carlo (pp. 116–162). Chapman and Hall/CRC. ISBN: 978-1420079418 ↗
- Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A., & Rubin, D. B. (2013). Bayesian Data Analysis (3rd ed.). CRC Press. ISBN: 978-1439840955
How to cite this page
ScholarGate. (2026, June 3). Hamiltonian Monte Carlo Sampling. ScholarGate. https://scholargate.app/en/bayesian/hamiltonian-monte-carlo
Which method?
Set this method beside its closest kin and read them side by side — the library lays the books on the table; the choice is yours.
- Bayesian RegressionBayesian↔ compare
- MCMCBayesian↔ compare
- Variational InferenceBayesian↔ compare