Hierarchical Hamiltonian Monte Carlo

Statistics
Introduced by
Betancourt & Girolami
Year
2015
Type
Bayesian sampling algorithm
Family
Bayesian / computational

What does it do?

Hierarchical Hamiltonian Monte Carlo (Hierarchical HMC) applies Hamiltonian Monte Carlo sampling to Bayesian hierarchical models, whose joint posteriors often contain narrow, curved funnel-shaped regions that random-walk samplers handle poorly.

It pairs HMC's gradient-guided proposals with a non-centered parameterization of the hierarchy. The combination lets the sampler move through these geometries in larger, more coherent steps than random-walk methods.

The intuition behind the method

In a hierarchical model, the global parameters set the scale of the unit-level parameters, so the joint posterior narrows sharply where the group-level scale is small and stays wide elsewhere. This produces a funnel: a region that is wide in some directions and very narrow in others. Random-walk proposals either overshoot the narrow neck or make little progress across the wide part. HMC simulates Hamiltonian dynamics along the posterior's gradient, so proposals follow the shape of the distribution over longer distances. Non-centered parameterization then removes much of the coupling between the group-level scale and the unit-level parameters, so the sampler does not need an impractically small step size to stay stable.

History

Hamiltonian Monte Carlo originated in physics and was later adapted to Bayesian statistics. Its use for hierarchical models, including funnel geometry and the non-centered reparameterization, has been studied in the Bayesian computation literature. The No-U-Turn Sampler (NUTS) automates the choice of path length and is implemented in Stan and PyMC.

When to use it

Choose Hierarchical HMC for Bayesian hierarchical or multilevel models with continuous parameters, especially when a random-walk or Gibbs sampler mixes poorly, shows high autocorrelation, or produces divergent transitions, which are common signs of funnel geometry. Testing the non-centered parameterization is worthwhile when group-level data are sparse and the unit-level parameters are weakly identified by their own data. Do not use it when the model contains discrete latent variables, since HMC requires a continuous, differentiable log-posterior. It is unnecessary for low-dimensional, simple posteriors where gradient overhead gains nothing, and it is a poor fit when each gradient evaluation is prohibitively expensive.

Limitations and considerations

  • Requires a continuous, differentiable log-posterior; it cannot sample discrete latent variables directly.
  • Gradient cost grows with model complexity and dataset size, so each iteration is more expensive than a simple Gibbs or random-walk update.
  • Step size and path length (or NUTS adaptation) are tuned during warm-up, and this phase can be long for difficult geometries.
  • Non-centering does not remove every funnel: when the group-level scale is poorly identified, for example with few groups or weakly informative data, problematic geometry can persist and must be checked through the diagnostics.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Gradient-guided trajectories can traverse the narrow funnels of hierarchical posteriors, where random-walk proposals tend to overshoot or stall.
  • With a non-centered parameterization, the sampler is often much more efficient on hierarchical models than with the centered form, which couples the group-level scale tightly to the unit-level parameters.
  • Divergent transitions give a direct diagnostic: when the integrator's energy error spikes, the problematic region of the posterior is flagged before the results are trusted.
  • Automatic differentiation in Stan and PyMC removes the need to derive gradients by hand.
  • The extra cost per iteration can be offset by fewer iterations needed when parameters are strongly correlated through the hierarchy.

Applications

  • Multilevel regression models in social science, education, and epidemiology with many grouping levels.
  • Longitudinal and panel data models with random effects estimated via Stan or PyMC.
  • Ecological models with hierarchically structured species or site parameters.
  • Pharmacokinetic/pharmacodynamic (PK/PD) nonlinear mixed-effects models.
  • Hierarchical item-response theory (IRT) models in psychometrics.
  • Bayesian meta-analysis with heterogeneous study-level effects.

How it works

How it works

1

The prior factorizes into unit-level parameters theta conditional on hyperparameters phi, together with a hyperprior on phi. In the joint posterior this two-level structure produces the funnel: as phi moves toward small values, the admissible spread of theta shrinks with it, so the sampler faces a region that is wide in some directions and narrow in others.

Formula
p(\theta, \phi) = p(\theta \mid \phi)\, p(\phi)
2

The non-centered form writes each unit-level parameter as the group-level mean mu, one of the hyperparameters in phi, plus a scale sigma times a standard normal deviate. Sigma is the group-level scale and controls how widely the unit-level parameters spread around mu. The sampler then works with standardized variables rather than the strongly coupled pair of unit-level and group-level parameters.

Formula
\theta = \phi + \sigma \cdot \tilde{\theta}, \quad \tilde{\theta} \sim \mathcal{N}(0, I)
3

HMC introduces auxiliary momentum variables p and defines a Hamiltonian energy as the sum of potential energy (the negative log-posterior) and kinetic energy. Simulating the Hamiltonian dynamics moves the proposal along contours of roughly equal posterior density, so the sampler can reach distant, high-probability states in a single proposal.

Formula
H(q, p) = -\log p(q \mid y) + \tfrac{1}{2} p^\top M^{-1} p
4

The leapfrog integrator approximates the Hamiltonian trajectory by repeating a half-step in momentum, a full step in position, and a closing half-step in momentum. The step size and the number of steps (the path length) must be set by hand or adapted automatically, as in NUTS, which stops a trajectory when it begins to double back on itself. Each leapfrog step requires one gradient evaluation of the log-posterior.

5

The end state of each trajectory is accepted with a Metropolis probability based on the change in energy between the start and end of the trajectory. This correction removes the bias introduced by numerical integration error. When the reparameterized posterior is well conditioned, integration error stays small and most proposals are accepted.

Formula
\alpha = \min\!\left(1,\, e^{-H(q^*,p^*)+H(q,p)}\right)

Common pitfalls

  • Ignoring divergent transitions rather than diagnosing and fixing the underlying geometry (wrong parameterization or prior).
  • Using centered parameterization when non-centered is appropriate, causing severe funnel geometry and poor mixing.
  • Interpreting R-hat near 1.01 as sufficient without also checking bulk and tail ESS, especially in hierarchical models.
  • Setting too few warm-up iterations so step size and mass matrix adaptation is incomplete.
  • Reporting posterior summaries from chains that show obvious non-stationarity in trace plots.

Frequently asked questions

What is a divergent transition and why does it matter?

A divergent transition occurs when the leapfrog integrator fails to conserve energy closely, typically because of extreme curvature in the posterior, such as at the neck of a funnel. Divergences indicate that the sampler may not be exploring that region accurately, so estimates should not be trusted until the cause is addressed. The usual remedies are a non-centered reparameterization, tighter or more informative priors, or a smaller step size.

When should I use non-centered vs. centered parameterization?

Non-centered parameterization decouples the geometry and is usually the better choice when group-level sample sizes are small and the data provide little information about individual groups. Centered parameterization can be better when groups are large and well observed, because the posterior of each group parameter is then tightly determined by its data. When in doubt, fit both forms and compare divergences, R-hat, and effective sample size.

How does Hierarchical HMC differ from standard HMC?

The underlying sampler is the same, but Hierarchical HMC specifically addresses the parameterization and tuning challenges that arise from hierarchical posterior geometry. Key additions are the non-centered reparameterization to flatten funnels, and monitoring divergent transitions as a geometry diagnostic unique to hierarchical settings.

Can I use Hierarchical HMC with discrete parameters?

No. HMC requires continuous, differentiable log-posteriors because it relies on gradients to simulate Hamiltonian dynamics. Discrete latent variables must be marginalized out analytically before applying HMC, or handled with a separate discrete sampler in a Gibbs-within-HMC scheme.

What software implements Hierarchical HMC?

Stan (via its NUTS sampler) is the primary tool and handles non-centered parameterization through model code. PyMC also implements NUTS with similar capabilities. Both provide R-hat, ESS, and divergence diagnostics automatically.

Related methods

Sources

  1. Betancourt, M. & Girolami, M. (2015). Hamiltonian Monte Carlo for hierarchical models. In S. K. Upadhyay, U. Singh, D. K. Dey & A. Loganathan (Eds.), Current Trends in Bayesian Methodology with Applications (pp. 79-101). CRC Press.

    Open publication reference
  2. 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