- Introduced by
- Matthew D. Hoffman and Andrew Gelman
- Year
- 2014
- Type
- adaptive MCMC sampler
- Family
- Bayesian / computational
What does it do?
Dynamic Hamiltonian Monte Carlo, widely known as the No-U-Turn Sampler (NUTS), is an adaptive extension of Hamiltonian Monte Carlo.
It chooses the number of leapfrog integration steps in each transition automatically, so the user does not have to set the trajectory length by hand. It is the default sampler in Stan and PyMC and targets continuous, differentiable posterior distributions, typically of moderate to high dimension.
The intuition behind the method
Standard HMC simulates the motion of a particle on a potential-energy surface defined by the negative log-posterior, and the user must decide in advance how far the particle travels before a proposal is made. If the trajectory is too short, the sampler moves in a manner closer to a random walk; if it is too long, the particle doubles back and computation is wasted. Dynamic HMC monitors the direction of travel as the trajectory is built. It keeps extending the trajectory by doubling the number of steps until the simulated particle begins to curve back on itself, which is the 'U-turn' criterion. The trajectory length is therefore set by the geometry of the posterior at each transition rather than by the user.
History
Hamiltonian Monte Carlo was brought into mainstream statistics by Radford Neal, whose handbook chapter on MCMC using Hamiltonian dynamics is among the page's references. Hoffman and Gelman introduced the No-U-Turn Sampler in 2014 in the Journal of Machine Learning Research. NUTS is the default sampler in Stan and is also implemented in PyMC and Turing.jl.
When to use it
Choose Dynamic HMC when the parameters are continuous and the log-posterior and its gradient can be computed, as in many Bayesian regression, hierarchical and latent-variable models. It is most useful when the posterior is high-dimensional or strongly correlated, because random-walk samplers such as Metropolis-Hastings tend to explore such posteriors slowly. Do not use it when parameters are discrete (marginalise them or use a Gibbs-type step instead), when the log-posterior is not differentiable, or when each gradient evaluation is too costly for the many evaluations that sampling requires. In those cases, variational inference or sequential Monte Carlo may be worth considering.
Limitations and considerations
- Requires continuous, differentiable parameters; discrete parameters must be marginalised or handled by a separate step.
- Gradient computation can be expensive for large datasets or highly complex models, and the many gradient evaluations per transition add to the cost.
- Posteriors with strongly non-linear or funnel-shaped geometry can produce divergent transitions even with well-chosen tuning.
- The warm-up phase requires many gradient evaluations, and short chains may not adapt the step size and mass matrix adequately.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Removes the need to hand-tune trajectory length, the most sensitive parameter of standard HMC.
- Explores high-dimensional, correlated posteriors more efficiently than random-walk proposals, because each transition moves along the posterior geometry.
- Its transitions are designed to leave the exact target posterior invariant, so the stationary distribution is the posterior itself.
- Diagnostic failures such as divergent transitions, low E-BFMI and elevated R-hat are informative and point to specific parts of the model that may need revision.
- Because the trajectory adapts to the posterior at each step, users typically need to set fewer sampler options than with standard HMC.
Applications
- Bayesian hierarchical and mixed-effects models across social, biological, and clinical sciences.
- Gaussian process regression and classification with differentiable covariance functions.
- Latent-variable and state-space models where posterior geometry is complex.
- Bayesian neural networks and probabilistic deep learning (small to medium scale).
- Phylogenetic inference in computational biology using probabilistic programming frameworks.
- Any probabilistic programming model written in Stan, PyMC, or Turing.jl.
How it works
How it works
Each transition begins by drawing auxiliary momentum variables and then integrating Hamilton's equations numerically with the leapfrog scheme, which takes a half-step momentum update, a full-step position update and a further half-step momentum update. The step size epsilon and the mass matrix M are the two geometric tuning parameters. During warm-up, the step size is adapted with dual averaging toward a target acceptance rate set in the sampler's configuration, which is typically high. The mechanics panel shows the position and momentum updates of a single integration step; a full leapfrog step also includes the half-step momentum updates at its start and end.
Formula
\tilde{q} = q + \epsilon\, M^{-1} p, \quad \tilde{p} = p + \epsilon\, \nabla\log p(\tilde{q})Rather than fixing the trajectory length in advance, the algorithm builds a binary tree of candidate states by repeatedly doubling the number of leapfrog steps, extending the trajectory forward or backward in time at each doubling. After each doubling it checks whether to keep extending, and expansion stops when the stopping criterion is met or a maximum tree depth is reached.
The tree expansion halts when the U-turn condition is detected: the dot product of the displacement vector between the leading and trailing positions and either endpoint's momentum becomes negative, signalling that the particle has begun to retrace its path. This geometric check replaces the fixed-step-count decision of standard HMC.
Formula
(\theta^+ - \theta^-) \cdot r^\pm < 0
Once the tree is complete, the new state is selected from the candidate states it contains, using a selection rule designed to leave the target posterior invariant. NUTS therefore does not apply the single per-transition accept or reject step used in standard HMC. The formula in the mechanics panel is the standard HMC Metropolis correction, which uses the Hamiltonian to account for numerical integration error, and it is shown for comparison with that method.
Formula
\alpha = \min\!\left(1,\, \frac{p(\theta^*,\,r^*)}{p(\theta,\,r)}\right)During warm-up, Stan's implementation uses a dual-averaging algorithm to tune the step size and a windowed scheme to estimate the mass matrix (diagonal or dense). After warm-up these parameters are fixed and sampling begins. Good mass-matrix estimation matters most when posterior dimensions differ greatly in scale.
Common pitfalls
- Ignoring divergent transitions. Each divergence signals a numerical instability in a region of high posterior curvature that the sampler cannot explore reliably.
- Running too few warm-up iterations, so that the mass matrix and step size are poorly estimated.
- Interpreting R-hat below 1.01 as sufficient without also checking bulk and tail effective sample sizes.
- Using default settings for posteriors with extreme geometry, such as Neal's funnel, where reparameterisation is usually needed before sampling is reliable.
Frequently asked questions
What is a divergent transition and why does it matter?
A divergent transition occurs when the leapfrog integrator becomes numerically unstable, usually because the posterior has a region of very high curvature (for example, a funnel). Divergences indicate that samples from that region are unreliable. A reasonable first step is to check whether divergences persist after raising the target acceptance rate in the sampler's settings, which makes the step size smaller. If they persist, reparameterise the model (for example, a non-centred parameterisation for hierarchical models).
How does NUTS differ from standard HMC?
Standard HMC requires the user to specify both a step size and a fixed number of leapfrog steps (the trajectory length). NUTS keeps the step size, which is adapted automatically, but replaces the fixed trajectory length with the U-turn criterion, which determines when to stop integrating. This makes NUTS considerably easier to use and often more efficient.
What is the mass matrix and why does it matter?
The mass matrix M in the Hamiltonian controls how the algorithm moves through parameter space. A diagonal mass matrix set to the inverse marginal variances of the posterior rescales each dimension to a similar scale, which can improve mixing. A dense mass matrix can also capture correlations between parameters. Poor mass-matrix estimation can lead to slow mixing or a collapsed step size.
Can I use Dynamic HMC with discrete parameters?
Not directly. Leapfrog integration requires continuous, differentiable parameters. Discrete parameters must be marginalised analytically, assigned to a separate Gibbs-type step, or handled by reformulating the model with continuous variables. Stan requires all sampled parameters to be continuous for this reason.
How many warm-up and sampling iterations do I need?
A common starting point is 1000 warm-up and 1000 sampling iterations per chain, across 4 chains. Complex posteriors may call for longer warm-up, such as 2000 iterations, to estimate the mass matrix well. Adequacy should be judged from R-hat (below 1.01) and from bulk and tail effective sample sizes, not from the iteration count alone.
How should I read the NUTS-specific diagnostics?
Three signals need attention. Divergent transitions: any non-zero count means the sampler entered regions it could not integrate accurately, so the posterior summaries should not be trusted until the cause is investigated. Tree-depth saturation: if many transitions reach the maximum tree depth, the sampler wanted longer trajectories than it was allowed; this is mainly an efficiency concern, but it can also indicate a posterior that is hard to explore. E-BFMI (energy Bayesian fraction of missing information): low values mean the sampler may be exploring the posterior inefficiently, so effective sample sizes should be checked. Treat any warning from the software as a reason to examine the chains, and consult the software's diagnostic guide for the thresholds it uses.
Related methods
Sources
Hoffman, M. D. & Gelman, A. (2014). The No-U-Turn Sampler: Adaptively setting path lengths in Hamiltonian Monte Carlo. Journal of Machine Learning Research, 15(1), 1593–1623.
Open publication referenceNeal, R. M. (2011). MCMC using Hamiltonian dynamics. In S. Brooks, A. Gelman, G. Jones & X.-L. Meng (Eds.), Handbook of Markov Chain Monte Carlo (pp. 113–162). CRC Press.
ISBN: 978-1420079418