Misplaced Pages

Hamiltonian Monte Carlo

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Sampling algorithm
Hamiltonian Monte Carlo sampling a two-dimensional probability distribution

The Hamiltonian Monte Carlo algorithm (originally known as hybrid Monte Carlo) is a Markov chain Monte Carlo method for obtaining a sequence of random samples whose distribution converges to a target probability distribution that is difficult to sample directly. This sequence can be used to estimate integrals of the target distribution, such as expected values and moments.

Hamiltonian Monte Carlo corresponds to an instance of the Metropolis–Hastings algorithm, with a Hamiltonian dynamics evolution simulated using a time-reversible and volume-preserving numerical integrator (typically the leapfrog integrator) to propose a move to a new point in the state space. Compared to using a Gaussian random walk proposal distribution in the Metropolis–Hastings algorithm, Hamiltonian Monte Carlo reduces the correlation between successive sampled states by proposing moves to distant states which maintain a high probability of acceptance due to the approximate energy conserving properties of the simulated Hamiltonian dynamic when using a symplectic integrator. The reduced correlation means fewer Markov chain samples are needed to approximate integrals with respect to the target probability distribution for a given Monte Carlo error.

The algorithm was originally proposed by Simon Duane, Anthony Kennedy, Brian Pendleton and Duncan Roweth in 1987 for calculations in lattice quantum chromodynamics. In 1996, Radford M. Neal showed how the method could be used for a broader class of statistical problems, in particular artificial neural networks. However, the burden of having to provide gradients of the Bayesian network delayed the wider adoption of the algorithm in statistics and other quantitative disciplines, until in the mid-2010s the developers of Stan implemented HMC in combination with automatic differentiation.

Algorithm

Suppose the target distribution to sample is f ( x ) {\displaystyle f(\mathbf {x} )} for x R d {\displaystyle \mathbf {x} \in \mathbb {R} ^{d}} ( d 1 {\displaystyle d\geq 1} ) and a chain of samples X 0 , X 1 , X 2 , {\displaystyle \mathbf {X} _{0},\mathbf {X} _{1},\mathbf {X} _{2},\ldots } is required.

The Hamilton's equations are

d x i d t = H p i and d p i d t = H x i {\displaystyle {\frac {{\text{d}}x_{i}}{{\text{d}}t}}={\frac {\partial H}{\partial p_{i}}}\quad {\text{and}}\quad {\dfrac {{\text{d}}p_{i}}{{\text{d}}t}}=-{\dfrac {\partial H}{\partial x_{i}}}}

where x i {\displaystyle x_{i}} and p i {\displaystyle p_{i}} are the i {\displaystyle i} th component of the position and momentum vector respectively and H {\displaystyle H} is the Hamiltonian. Let M {\displaystyle M} be a mass matrix which is symmetric and positive definite, then the Hamiltonian is

H ( x , p ) = U ( x ) + 1 2 p T M 1 p {\displaystyle H(\mathbf {x} ,\mathbf {p} )=U(\mathbf {x} )+{\dfrac {1}{2}}\mathbf {p} ^{\text{T}}M^{-1}\mathbf {p} }

where U ( x ) {\displaystyle U(\mathbf {x} )} is the potential energy. The potential energy for a target is given as

U ( x ) = ln f ( x ) {\displaystyle U(\mathbf {x} )=-\ln f(\mathbf {x} )}

which comes from the Boltzmann's factor. Note that the Hamiltonian H {\displaystyle H} is dimensionless in this formulation because the exponential probability weight exp ( H ) {\displaystyle \exp \left(-H\right)} has to be well defined. For example, in simulations at finite temperature T {\displaystyle T} the factor k B T {\displaystyle k_{\text{B}}T} (with the Boltzmann constant k B {\displaystyle k_{\text{B}}} ) is directly absorbed into U {\displaystyle U} and M {\displaystyle M} .

The algorithm requires a positive integer for number of leapfrog steps L {\displaystyle L} and a positive number for the step size Δ t {\displaystyle \Delta t} . Suppose the chain is at X n = x n {\displaystyle \mathbf {X} _{n}=\mathbf {x} _{n}} . Let x n ( 0 ) = x n {\displaystyle \mathbf {x} _{n}(0)=\mathbf {x} _{n}} . First, a random Gaussian momentum p n ( 0 ) {\displaystyle \mathbf {p} _{n}(0)} is drawn from N ( 0 , M ) {\displaystyle {\text{N}}\left(\mathbf {0} ,M\right)} . Next, the particle will run under Hamiltonian dynamics for time L Δ t {\displaystyle L\Delta t} , this is done by solving the Hamilton's equations numerically using the leapfrog algorithm. The position and momentum vectors after time Δ t {\displaystyle \Delta t} using the leapfrog algorithm are:

p n ( t + Δ t 2 ) = p n ( t ) Δ t 2 U ( x ) | x = x n ( t ) {\displaystyle \mathbf {p} _{n}\left(t+{\dfrac {\Delta t}{2}}\right)=\mathbf {p} _{n}(t)-{\dfrac {\Delta t}{2}}\nabla \left.U(\mathbf {x} )\right|_{\mathbf {x} =\mathbf {x} _{n}(t)}}
x n ( t + Δ t ) = x n ( t ) + Δ t M 1 p n ( t + Δ t 2 ) {\displaystyle \mathbf {x} _{n}(t+\Delta t)=\mathbf {x} _{n}(t)+\Delta tM^{-1}\mathbf {p} _{n}\left(t+{\dfrac {\Delta t}{2}}\right)}
p n ( t + Δ t ) = p n ( t + Δ t 2 ) Δ t 2 U ( x ) | x = x n ( t + Δ t ) {\displaystyle \mathbf {p} _{n}(t+\Delta t)=\mathbf {p} _{n}\left(t+{\dfrac {\Delta t}{2}}\right)-{\dfrac {\Delta t}{2}}\nabla \left.U(\mathbf {x} )\right|_{\mathbf {x} =\mathbf {x} _{n}(t+\Delta t)}}

These equations are to be applied to x n ( 0 ) {\displaystyle \mathbf {x} _{n}(0)} and p n ( 0 ) {\displaystyle \mathbf {p} _{n}(0)} L {\displaystyle L} times to obtain x n ( L Δ t ) {\displaystyle \mathbf {x} _{n}(L\Delta t)} and p n ( L Δ t ) {\displaystyle \mathbf {p} _{n}(L\Delta t)} .

The leapfrog algorithm is an approximate solution to the motion of non-interacting classical particles. If exact, the solution will never change the initial randomly-generated energy distribution, as energy is conserved for each particle in the presence of a classical potential energy field. In order to reach a thermodynamic equilibrium distribution, particles must have some sort of interaction with, for example, a surrounding heat bath, so that the entire system can take on different energies with probabilities according to the Boltzmann distribution.

One way to move the system towards a thermodynamic equilibrium distribution is to change the state of the particles using the Metropolis–Hastings algorithm. So first, one applies the leapfrog step, then a Metropolis-Hastings step.

The transition from X n = x n {\displaystyle \mathbf {X} _{n}=\mathbf {x} _{n}} to X n + 1 {\displaystyle \mathbf {X} _{n+1}} is

X n + 1 | X n = x n = { x n ( L Δ t ) with probability  α ( x n ( 0 ) , x n ( L Δ t ) ) x n ( 0 ) otherwise {\displaystyle \mathbf {X} _{n+1}|\mathbf {X} _{n}=\mathbf {x} _{n}={\begin{cases}\mathbf {x} _{n}(L\Delta t)&{\text{with probability }}\alpha \left(\mathbf {x} _{n}(0),\mathbf {x} _{n}(L\Delta t)\right)\\\mathbf {x} _{n}(0)&{\text{otherwise}}\end{cases}}}

where

α ( x n ( 0 ) , x n ( L Δ t ) ) = min ( 1 , exp [ H ( x n ( L Δ t ) , p n ( L Δ t ) ) ] exp [ H ( x n ( 0 ) , p n ( 0 ) ) ] ) . {\displaystyle \alpha \left(\mathbf {x} _{n}(0),\mathbf {x} _{n}(L\Delta t)\right)={\text{min}}\left(1,{\dfrac {\exp \left}{\exp \left}}\right).}

A full update consists of first randomly sampling the momenta p {\displaystyle \mathbf {p} } (independently of the previous iterations), then integrating the equations of motion (e.g. with leapfrog), and finally obtaining the new configuration from the Metropolis-Hastings accept/reject step. This updating mechanism is repeated to obtain X n + 1 , X n + 2 , X n + 3 , {\displaystyle \mathbf {X} _{n+1},\mathbf {X} _{n+2},\mathbf {X} _{n+3},\ldots } .

No U-Turn Sampler

The No U-Turn Sampler (NUTS) is an extension by controlling L {\displaystyle L} automatically. Tuning L {\displaystyle L} is critical. For example, in the one dimensional N ( 0 , 1 / k ) {\displaystyle {\text{N}}(0,1/{\sqrt {k}})} case, the potential is U ( x ) = k x 2 / 2 {\displaystyle U(x)=kx^{2}/2} which corresponds to the potential of a simple harmonic oscillator. For L {\displaystyle L} too large, the particle will oscillate and thus waste computational time. For L {\displaystyle L} too small, the particle will behave like a random walk.

Loosely, NUTS runs the Hamiltonian dynamics both forwards and backwards in time randomly until a U-Turn condition is satisfied. When that happens, a random point from the path is chosen for the MCMC sample and the process is repeated from that new point.

In detail, a binary tree is constructed to trace the path of the leap frog steps. To produce a MCMC sample, an iterative procedure is conducted. A slice variable U n Uniform ( 0 , exp ( H [ x n ( 0 ) , p n ( 0 ) ] ) ) {\displaystyle U_{n}\sim {\text{Uniform}}(0,\exp(-H))} is sampled. Let x n + {\displaystyle \mathbf {x} _{n}^{+}} and p n + {\displaystyle \mathbf {p} _{n}^{+}} be the position and momentum of the forward particle respectively. Similarly, x n {\displaystyle \mathbf {x} _{n}^{-}} and p n {\displaystyle \mathbf {p} _{n}^{-}} for the backward particle. In each iteration, the binary tree selects at random uniformly to move the forward particle forwards in time or the backward particle backwards in time. Also for each iteration, the number of leap frog steps increase by a factor of 2. For example, in the first iteration, the forward particle moves forwards in time using 1 leap frog step. In the next iteration, the backward particle moves backwards in time using 2 leap frog steps.

The iterative procedure continues until the U-Turn condition is met, that is

( x n + x n ) p n < 0 or . ( x n + x n ) p n + < 0 {\displaystyle (\mathbf {x} _{n}^{+}-\mathbf {x} _{n}^{-})\cdot \mathbf {p} _{n}^{-}<0\quad {\text{or}}\quad .(\mathbf {x} _{n}^{+}-\mathbf {x} _{n}^{-})\cdot \mathbf {p} _{n}^{+}<0}

or when the Hamiltonian becomes inaccurate

exp [ H ( x n + , p n + ) + δ ] < U n {\displaystyle \exp \left<U_{n}}

or

exp [ H ( x n , p n ) + δ ] < U n {\displaystyle \exp \left<U_{n}}

where, for example, δ = 1000 {\displaystyle \delta =1000} .

Once the U-Turn condition is met, the next MCMC sample, x n + 1 {\displaystyle \mathbf {x} _{n+1}} , is obtained by sampling uniformly the leap frog path traced out by the binary tree { x n , , x n ( Δ t ) , x n ( 0 ) , x n ( Δ t ) , , x n + } {\displaystyle \{\mathbf {x} _{n}^{-},\ldots ,\mathbf {x} _{n}(-\Delta t),\mathbf {x} _{n}(0),\mathbf {x} _{n}(\Delta t),\ldots ,\mathbf {x} _{n}^{+}\}} which satisfies

U n < exp [ H ( x n + 1 , p n + 1 ) ] {\displaystyle U_{n}<\exp \left}

This is usually satisfied if the remaining HMC parameters are sensible.

See also

References

  1. Duane, Simon; Kennedy, Anthony D.; Pendleton, Brian J.; Roweth, Duncan (1987). "Hybrid Monte Carlo". Physics Letters B. 195 (2): 216–222. Bibcode:1987PhLB..195..216D. doi:10.1016/0370-2693(87)91197-X.
  2. Neal, Radford M. (1996). "Monte Carlo Implementation". Bayesian Learning for Neural Networks. Lecture Notes in Statistics. Vol. 118. Springer. pp. 55–98. doi:10.1007/978-1-4612-0745-0_3. ISBN 0-387-94724-8.
  3. Gelman, Andrew; Lee, Daniel; Guo, Jiqiang (2015). "Stan: A Probabilistic Programming Language for Bayesian Inference and Optimization". Journal of Educational and Behavioral Statistics. 40 (5): 530–543. doi:10.3102/1076998615606113. S2CID 18351694.
  4. Betancourt, Michael (2018-07-15). "A Conceptual Introduction to Hamiltonian Monte Carlo". arXiv:1701.02434 .
  5. Hoffman, Matthew D; Gelman, Andrew (2014). "The No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte Carlo". Journal of Machine Learning Research. 15 (1): 1593–1623. Retrieved 2024-03-28.

Further reading

  • Betancourt, Michael; Girolami, Mark (2015). "Hamiltonian Monte Carlo for Hierarchical Models". In Upadhyay, Satyanshu Kumar; et al. (eds.). Current Trends in Bayesian Methodology with Applications. CRC Press. pp. 79–101. ISBN 978-1-4822-3511-1.
  • Betancourt, Michael (2018). "A Conceptual Introduction to Hamiltonian Monte Carlo". arXiv:1701.02434 .
  • Barbu, Adrian; Zhu, Song-Chun (2020). "Hamiltonian and Langevin Monte Carlo". Monte Carlo Methods. Singapore: Springer. pp. 281–326. ISBN 978-981-13-2970-8.
  • Neal, Radford M (2011). "MCMC Using Hamiltonian Dynamics" (PDF). In Steve Brooks; Andrew Gelman; Galin L. Jones; Xiao-Li Meng (eds.). Handbook of Markov Chain Monte Carlo. Chapman and Hall/CRC. ISBN 9781420079418.

External links

Categories: