skip to content

What does Hamiltonian Monte Carlo's momentum variable buy over random-walk Metropolis proposals?

level: seniorimportance: nice to knowfreq 33%

answer

  1. the proposal gets a direction
  2. an auxiliary variable redrawn each iteration
  3. energy conserved along the path
  4. far away yet still high density
  5. gradients required, so continuous only

basics

~20 s

Momentum lets a proposal travel a long way while following the posterior's shape, so moves are coherent strides rather than a random walk. Distant proposals still get accepted, which is why exploration holds up as the number of parameters grows.

solid answer

~50 s

Hamiltonian Monte Carlo augments the parameters with an auxiliary momentum vector, drawn fresh each iteration, and simulates frictionless motion over the landscape given by the negative log posterior. Picture a puck sliding across that surface: it accelerates down into high-density basins, coasts up the sides, and traces a long path that stays where the posterior mass is instead of stumbling randomly. The trajectory is simulated numerically with a leapfrog integrator, controlled by a step size and a number of steps, and a Metropolis correction at the end accounts for the integrator's energy error — with exact simulation the acceptance probability would be one. Because it uses the gradient of the log posterior, it needs a continuous, differentiable parameter space, so discrete parameters must be marginalised out. Each proposal costs many gradient evaluations, but you buy distant moves that are actually accepted rather than the tiny, timid steps a random walk is forced into.

go deeper

for a junior

Be ready to give the physical picture: an extra momentum variable lets a proposal travel along the posterior's shape rather than stepping randomly, so it can move far and still be accepted.

for a middle

Explain the mechanics: momentum redrawn each iteration, a trajectory simulated over the negative log posterior with a leapfrog integrator, and a final Metropolis test that corrects the integrator's energy error.

for a senior

Demonstrate operating judgment: how step size and trajectory length interact, why a continuous differentiable space is required and what you do with discrete parameters, and how to weigh many gradient evaluations per proposal against the distance gained.

for a principal

Own the platform-level call: whether committing a team to gradient-based inference is worth the modelling constraints it imposes, versus keeping models in a form a simpler sampler can handle with less specialist knowledge.

## The problem with random-walk proposals A random-walk proposal has no idea where it is. It offers `theta + noise` in a direction chosen without reference to the posterior's shape, so to be accepted at a reasonable rate the step must be small relative to the posterior's tightest direction. The chain then diffuses: distance covered grows like the square root of the number of iterations, and in high dimensions almost every proposed direction points out of the thin shell where the posterior mass actually lives. Cranking the step size up only converts crawling into rejection. Hamiltonian Monte Carlo attacks this by giving the proposal a sense of direction. ## The mechanical picture Define a potential energy as the negative log unnormalised posterior, `U(theta) = -log p_unnorm(theta)`. High posterior density is low altitude; the posterior's bulk is a valley, and the tails are the slopes rising away from it. Each iteration: 1. Draw a fresh momentum vector `m`, usually from a multivariate normal, independently of the current position. 2. Simulate the motion of a frictionless particle starting at `theta` with momentum `m`, moving over the surface `U` for some fixed simulated time. 3. Take the end point of that trajectory as the proposed `theta'`, and accept or reject it with a Metropolis test. The intuition is the puck. Shove it and it slides down into the valley, gaining speed, then climbs the far slope, decelerates, and turns back — always tracing a path that follows the terrain instead of ignoring it. Total energy, potential plus kinetic, is conserved along the exact path, which is precisely the statement that the proposal ends somewhere with comparable posterior density to where it started, however far away that is. This is why long moves survive the acceptance test: distance and acceptance stop being in tension, which is exactly the tradeoff that traps a random walk. The momentum being redrawn each iteration is what makes the chain explore rather than orbit forever on one energy level. ## Why there is still an accept-reject step The dynamics cannot be simulated exactly. In practice the trajectory is approximated with a leapfrog integrator that alternates half-steps of momentum update and full steps of position update, governed by a step size and a number of steps. The integrator conserves energy well but not perfectly, so the end point carries a small energy error. The final Metropolis test uses that error: the acceptance probability is one when energy is exactly conserved and drops as the error grows. This is what makes HMC exact despite being built on an approximate simulation, and it is why an implementation with no acceptance test is simply wrong. The two knobs interact. Too large a step size makes the integrator inaccurate, energy error blows up, and acceptance collapses. Too small a step size makes each trajectory expensive in gradient evaluations. Too few steps and the proposal barely leaves the starting point, reducing HMC to an expensive random walk; too many and the trajectory can curve back towards where it started, spending gradients to go nowhere. Adaptive variants exist that pick trajectory length automatically rather than requiring a hand-set number of steps. ## The requirements it imposes HMC needs the gradient of the log posterior with respect to every parameter. Three consequences follow. First, the parameter space must be continuous and the log posterior differentiable. Discrete parameters cannot be moved by these dynamics at all; the standard treatment is to marginalise them out of the model analytically and recover them afterwards if needed, or to update them with a different kind of step. Second, constrained parameters — a variance that must be positive, a probability in the unit interval, a simplex — are handled by transforming to an unconstrained space and carrying the Jacobian term, so the dynamics run on a space with no walls to hit. Third, cost per proposal is high. One HMC proposal costs as many gradient evaluations as it has leapfrog steps, so it may be tens or hundreds of times more expensive than one random-walk proposal. The bargain only pays when those expensive proposals move much further and are accepted, which is generally true in moderate to high dimensions and may not be true for a small, well-behaved, cheap-to-evaluate model. ## When it is not the answer HMC is not universally superior. For a low-dimensional model with conditionally conjugate structure, Gibbs sampling may be simpler, tuning-free and entirely sufficient. For a model with discrete latent structure that cannot be marginalised, HMC does not apply directly. And difficult posterior geometry — regions where curvature changes sharply with position — remains difficult for HMC too; momentum makes exploration efficient where the geometry is reasonable, it does not make pathological geometry disappear. An interviewer asking this question usually wants the physical intuition stated clearly, the reason distant proposals still get accepted, and honest acknowledgment of the gradient requirement and the cost per proposal. Reciting the leapfrog recursion without the intuition, or claiming HMC needs no acceptance step, both read as memorised rather than understood.

  • Why does Hamiltonian Monte Carlo still need an accept-reject step at the end of a trajectory?
    Because the dynamics are simulated approximately. The leapfrog integrator conserves energy well but not exactly, and the Metropolis test at the end uses that energy error, accepting with probability one under exact simulation and less as the error grows. That correction is what keeps the sampler exact despite an approximate trajectory.
  • What are the main tuning knobs of a Hamiltonian Monte Carlo proposal?
    Step size and trajectory length. Too large a step size wrecks the integrator's accuracy and collapses acceptance; too small wastes gradient evaluations. Too few steps reduces the method to an expensive random walk, while too many can curve the trajectory back towards its starting point. Adaptive variants choose the trajectory length automatically.
  • Can Hamiltonian Monte Carlo sample discrete parameters?
    Not directly. The dynamics move a particle using the gradient of the log posterior, which requires a continuous, differentiable parameter space. The usual treatment is to marginalise discrete parameters out of the model analytically and recover their distribution afterwards, or to update them with a separate non-gradient step.
  • When would you prefer a simpler sampler over Hamiltonian Monte Carlo?
    When the model has conditionally conjugate structure and few parameters, Gibbs sampling is tuning-free and cheap per iteration; when gradients are unavailable or the parameters are discrete, HMC does not apply. Each HMC proposal costs many gradient evaluations, so the trade only pays when it buys long, accepted moves.

A random walk is a blindfolded person taking small steps in random directions. Hamiltonian Monte Carlo is a puck given a shove across a smooth bowl: it travels far across the surface without leaving it.

saying these in an interview costs you the question

  • Describes HMC as just a random walk with bigger steps
  • Claims HMC accepts every proposal by construction
  • Applies HMC directly to discrete parameters
  • Assumes more leapfrog steps is always better
  • Ignores that each proposal costs many gradient evaluations

context