Adapted from an appendix of my MS thesis.
Hamiltonian Monte Carlo
Hamiltonian Monte Carlo (HMC) is a type of MCMC method that makes use of gradients to generate new proposed states. HMC attempts to avoid the random walk behavior typical of Metropolis-Hastings by using the gradient to propose new positions far from the current one with high acceptance probability. This allows HMC to better scale to higher dimensions and in principle more complex geometries [1].
In physical systems, the Hamiltonian is a description of the total energy. The total energy can be decomposed into two terms, the kinetic and the potential energy. We can write the Hamiltonian of such a system as the following where is the kinetic energy and is the potential energy [1].
The probability of being at a particular position with a particular momentum is given as follows [1]. The exponential is a non-negative quantity and can therefore be viewed as an unnormalized probability distribution. The introduction of the minus sign in the exponential is simply a convention, meaning that higher values of energy correspond to lower values of probability [2].
To simulate such systems the Hamiltonian equations must be solve [1].
The potential energy is given by the probability we wish to sample from . For the momentum we can invoke an auxiliary variable, and by choosing we can write . This ensures we can recover the target distribution by marginalizing out the momentum. By introducing the auxiliary variable we can rewrite the equation [1].
We are free to choose the kinetic energy, and if we choose it to be Gaussian and drop the normalization constant, then we have the following where is the precision matrix [1].
If we choose to be the identity matrix we have , making and , and then we can simplify the Hamiltonian equations [1].
Therefore, the HMC algorithm can be summarized as follows [1].
Sample
Simulate and for some amount of time
is the new proposed state
Use the Metropolis acceptance criterion to accept or reject .
In an idealized model, energy is perfectly conserved. However, we use the Metropolis acceptance criterion to correct for errors introduced by the numerical simulation of the Hamiltonian equations. To compute the Hamiltonian equations we have to compute a trajectory. That is, all the intermediate points between one state and the next. In practice this involves computing a series of small integration steps. The most popular method is the leapfrog integrator. Leapfrog integration updates positions and momentums at interleaved time points, staggered in such that they leapfrog over each other [1].
An efficient HMC run requires proper tuning of its hyperparameters [1]:
The time discretization (step size of the leapfrog)
The integration time (number of leapfrog steps)
The precision matrix that parametrizes the kinetic energy.
For example, if the step size is too large then the leapfrog integrator will be inaccurate and too many proposals will be rejected. However, if it is too small we waste computation resources. If the number of steps is too small, the simulated trajectory at each iteration will be too short and sampling will fall back to random walk. But if too large the trajectory might run in circles and we again waste computation resources. If the estimated covariance (inverse of the precision matrix) is too different from the posterior covariance, the proposal momentum will be suboptimal and movement in the position space will be too large or too small in some dimension [1].
References
- Martin, Osvaldo A., Kumar, Ravin, Lao, Junpeng (2021) Bayesian Modeling and Computation in Python. Chapman and Hall/CRC.
- Bishop, Christopher M., Bishop, Hugh (2023) Deep Learning: Foundations and Concepts. Springer Cham.
![Example of the circular leapfrog. Three different trajectories around the same 2D normal distribution. For practical sampling we want to move as far as possible from the starting point while avoiding U-turns in the trajectory. Hence, the name of one of the most popular dynamic HMC methods No U-Turn Sampling (NUTS) [1].](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnecj5utsfenxu43kfk8n.png)
![Example of the funnel leapfrog. Three different trajectories around the same Neal’s funnel, a common geometry arising in (centered) hierarchical models. These trajectories fail to properly simulate following the correct distribution resulting in divergences. When the exact trajectories lie on regions of high curvature, the numerical trajectories generated can rapidly get off towards the boundaries of the distribution we are trying to explore [1].](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjmucqgk1m4ft7c3dx8rp.png)
Top comments (0)