AI Glossary / Probabilistic machine learning / Advanced
Leapfrog integrator
Definition
The leapfrog integrator is a numerical method that advances position and momentum through alternating half and full steps. For a separable Hamiltonian system, it provides a reversible, volume-preserving approximation to the dynamics and is widely used to construct proposals in Hamiltonian Monte Carlo.
What it means
Hamiltonian Monte Carlo, or HMC, introduces an auxiliary momentum variable so that sampling can follow coordinated trajectories through a probability distribution. The negative log target density acts like a potential-energy function, while momentum contributes kinetic energy.
Those trajectories usually cannot be calculated exactly. Leapfrog approximates them with a sequence of small updates that preserve useful geometric structure. Its name reflects the interleaving of position and momentum updates.
The integrator is not the whole sampler. A complete HMC method also handles momentum generation, trajectory length, proposal construction and acceptance. Leapfrog steps along a trajectory should not automatically be treated as independent posterior draws.
How it works
For potential energy U(q), momentum p and a constant mass matrix M, one common leapfrog step has three parts. Update momentum by half a step using the gradient of U at the current position. Update position by a full step using M inverse times the new momentum. Then update momentum by another half step using the gradient at the new position.
Each substep is a shear-like transformation, helping the composition preserve phase-space volume. The symmetric ordering makes the map reversible in exact arithmetic when the step direction is reversed.
Energy is generally only approximately conserved. In standard HMC, a Metropolis acceptance calculation corrects for the proposal's energy error. Step size, trajectory length and mass matrix affect both computational efficiency and numerical stability.
Three practical examples
- An HMC sampler uses leapfrog trajectories to propose coordinated changes to Bayesian model parameters.
- A physics simulation uses the method to approximate a system with separable kinetic and potential energy.
- A numerical-methods exercise compares leapfrog with a basic Euler integrator on a harmonic oscillator, measuring energy error and reversibility.
Think of it this way
Think of alternating small adjustments to speed and position while following a curved track. Updating speed on both sides of the position move balances the local approximation.
The analogy should not imply perfect motion or exact energy conservation. Numerical steps still introduce error, and an unsuitable step size can make the trajectory unstable. In HMC, the track is a mathematical probability landscape rather than a physical route.
Worked example
Taking one step and checking its error
Consider an original one-dimensional system with U(q) = q squared/2 and kinetic energy p squared/2, so the mass is one. Start at q = 0 and p = 1, and choose step size 0.2.
The first momentum half-step subtracts 0.1 times the current gradient q = 0, leaving momentum 1. The position full-step gives q new = 0 + 0.2 times 1 = 0.2. The final momentum half-step subtracts 0.1 times the new gradient 0.2, giving p new = 0.98.
Initial energy is 0.5. Final energy is (0.2 squared + 0.98 squared)/2 = 0.5002. The energy increase is 0.0002, so a standard HMC acceptance calculation for this proposal gives exp(−0.0002), approximately 0.9998, before any realized accept-or-reject draw.
To check reversibility, apply the same update with step size −0.2 from (0.2, 0.98). The half-step returns momentum to 1, the position returns to 0, and the last half-step leaves momentum at 1. This exact arithmetic calculation demonstrates reversal while the energy calculation demonstrates that reversal does not imply perfect energy conservation.
Illustrative case study
Fictional case: validating an HMC implementation
A fictional statistical-software team builds an educational HMC sampler. Before using a complicated posterior, it checks a Gaussian target whose potential gradient and reference moments are known.
The team verifies one-step arithmetic, forward-and-backward trajectories and energy errors at several step sizes. It then checks sample diagnostics across complete HMC transitions, including rejected proposals, rather than counting every intermediate position as a fresh independent sample.
When a larger step size produces unstable trajectories, the team revisits tuning and the model's geometry. A high acceptance rate alone is not treated as sufficient evidence of efficient exploration, because very small moves can be accepted frequently while exploring slowly.
Common misconceptions and mistakes
Leapfrog does not generally preserve the original Hamiltonian exactly. Its geometric properties support favorable behavior under suitable conditions, but they do not eliminate numerical error.
Drawing a fresh momentum is part of the surrounding HMC algorithm, not a requirement to redraw momentum at every leapfrog substep. Doing that would create a different procedure.
Volume preservation does not mean positions alone occupy an unchanged volume: the property concerns the joint position-momentum space. Reversibility in the mathematical map also does not remove floating-point limitations. Large steps, difficult curvature or incorrect gradients can still undermine the calculation.
Business use
Understanding leapfrog helps teams interpret the computational cost and diagnostics of HMC-based inference. It clarifies why reducing step size increases the number of gradient evaluations needed to cover a similar trajectory length.
Validate gradients and parameter transformations, and inspect energy-related failures alongside sampling diagnostics. Distinguish the numerical integration step size from the number of retained posterior draws. When choosing a mass matrix or reparameterizing a model, assess effective exploration per unit computation. The objective is reliable inference, not merely producing long trajectories or maximizing acceptance.
People also ask
Why are there two half momentum steps?
Their symmetric placement around the position update gives the standard leapfrog composition its time-reversal structure.
Can adjacent half-steps be combined?
Yes, when composing steps under the same setup, adjacent momentum half-updates at the same position can be combined into a full update.
Is a smaller step always better?
It usually reduces integration error but increases computation for a fixed trajectory length. Efficiency requires balancing both effects.
Check your understanding
Does the worked proposal's acceptance probability of approximately 0.9998 mean that the trajectory preserved energy exactly?
Answer: No. Its energy rose by 0.0002. The acceptance calculation explicitly accounts for that small integration error.
Related terms
- Hamiltonian Monte Carlo
- Hamiltonian dynamics
- symplectic integrator
- mass matrix
- potential energy
- Metropolis acceptance
- numerical stability
Learn next
Repeat the harmonic-oscillator calculation with a smaller step and compare energy error over a fixed integration time. Then examine how the Metropolis correction turns approximate trajectories into valid HMC proposals under the algorithm's assumptions.
More in Probabilistic machine learning
- Laplace approximation - A Laplace approximation replaces a probability density near a smooth mode with a Gaussian whose center and curvature match that local peak.
- Latent coincidence analysis - Latent coincidence analysis (LCA) is a supervised latent-variable method for learning a linear transformation in which distances reflect labeled similarity...
- LDA-HMM - LDA-HMM is a composite text model combining latent Dirichlet allocation's document-level topics with a hidden Markov model's local sequence structure.
- Likelihood tempering - Likelihood tempering changes the influence of observed data by raising the likelihood to a power before combining it with the prior.
- Local level model - A local level model represents a time series as noisy observations of an underlying level that changes through a random walk.
- Local linear trend model - A local linear trend model is a state-space time-series model with both a changing level and a changing slope.
Sources
Primary implementation reference: Stan Reference Manual, MCMC Sampling, leapfrog integrator and Metropolis acceptance: https://mc-stan.org/docs/reference-manual/mcmc.html
The one-step oscillator calculation and software-team case are original teaching examples.
Previous: Lean integration Next: Learnable evolution model