Hamiltonian Monte Carlo

From HandWiki
Short description: Sampling algorithm

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 which converge to being distributed according to a target probability distribution for which direct sampling is difficult. This sequence can be used to estimate integrals with respect to the target distribution (expected values).

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.[1] In 1996, Radford M. Neal showed how the method could be used for a broader class of statistical problems, in particular artificial neural networks.[2] However, the burden of having to provide gradients of the respective densities 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.[3]

Algorithm

Suppose the target distribution to sample is [math]\displaystyle{ f(\mathbf{x}) }[/math] for [math]\displaystyle{ \mathbf{x} \in \mathbb{R}^d }[/math] ([math]\displaystyle{ d \ge 1 }[/math]) and a chain of samples [math]\displaystyle{ \mathbf{X}_0,\mathbf{X}_1,\mathbf{X}_2,\ldots }[/math] is required.

The Hamilton's equations are

[math]\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} }[/math]

where [math]\displaystyle{ x_i }[/math] and [math]\displaystyle{ p_i }[/math] are the [math]\displaystyle{ i }[/math]th component of the position and momentum vector respectively and [math]\displaystyle{ H }[/math] is the Hamiltonian. Let [math]\displaystyle{ M }[/math] be a mass matrix which is symmetric and positive definite, then the Hamiltonian is

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

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

[math]\displaystyle{ U(\mathbf{x}) = -\ln f(\mathbf{x}) }[/math]

which comes from the Boltzmann's factor.

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

[math]\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)} }[/math]
[math]\displaystyle{ \mathbf{x}_n(t+\Delta t) = \mathbf{x}_n(t)+\Delta t \mathbf{p}_n\left(t+\dfrac{\Delta t}{2}\right) }[/math]
[math]\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)} }[/math]

These equations are to be applied to [math]\displaystyle{ \mathbf{x}_n(0) }[/math] and [math]\displaystyle{ \mathbf{p}_n(0) }[/math] [math]\displaystyle{ L }[/math] times to obtain [math]\displaystyle{ \mathbf{x}_n(L\Delta t) }[/math] and [math]\displaystyle{ \mathbf{p}_n(L\Delta t) }[/math].

The leap frog 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 leap frog step, then a Metropolis-Hastings step.

The transition from [math]\displaystyle{ \mathbf{X}_n = \mathbf{x}_n }[/math] to [math]\displaystyle{ \mathbf{X}_{n+1} }[/math] is

[math]\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} }[/math]

where

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

This is repeated to obtain [math]\displaystyle{ \mathbf{X}_{n+1},\mathbf{X}_{n+2},\mathbf{X}_{n+3},\ldots }[/math].

No U-Turn Sampler

The No U-Turn Sampler (NUTS)[5] is an extension by controlling [math]\displaystyle{ L }[/math] automatically. Tuning [math]\displaystyle{ L }[/math] is critical. For example, in the one dimensional [math]\displaystyle{ \text{N}(0,1/\sqrt{k}) }[/math] case, the potential is [math]\displaystyle{ U(x)=kx^2/2 }[/math] which corresponds to the potential of a simple harmonic oscillator. For [math]\displaystyle{ L }[/math] too large, the particle will oscillate and thus waste computational time. For [math]\displaystyle{ L }[/math] 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 [math]\displaystyle{ U_n\sim\text{Uniform}(0,\exp(-H[\mathbf{x}_n(0),\mathbf{p}_n(0)])) }[/math] is sampled. Let [math]\displaystyle{ \mathbf{x}_n^+ }[/math] and [math]\displaystyle{ \mathbf{p}_n^+ }[/math] be the position and momentum of the forward particle respectively. Similarly, [math]\displaystyle{ \mathbf{x}_n^- }[/math] and [math]\displaystyle{ \mathbf{p}_n^- }[/math] 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

[math]\displaystyle{ (\mathbf{x}_n^+ - \mathbf{x}_n^-)\cdot \mathbf{p}_n^- \lt 0 \quad \text{or} \quad. (\mathbf{x}_n^+ - \mathbf{x}_n^-)\cdot \mathbf{p}_n^+ \lt 0 }[/math]

or when the Hamiltonian becomes inaccurate

[math]\displaystyle{ \exp\left[-H(\mathbf{x}_n^{+},\mathbf{p}_n^{+})+\delta\right]\lt U_n }[/math]

or

[math]\displaystyle{ \exp\left[-H(\mathbf{x}_n^{-},\mathbf{p}_n^{-})+\delta\right]\lt U_n }[/math]

where, for example, [math]\displaystyle{ \delta = 1000 }[/math].

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

[math]\displaystyle{ U_n\lt \exp\left[-H(\mathbf{x_{n+1}},\mathbf{p_{n+1})}\right] }[/math]

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. doi:10.1016/0370-2693(87)91197-X. Bibcode1987PhLB..195..216D. 
  2. Neal, Radford M. (1996). "Monte Carlo Implementation". Bayesian Learning for Neural Networks. Lecture Notes in Statistics. 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. 
  4. Betancourt, Michael (2018-07-15). "A Conceptual Introduction to Hamiltonian Monte Carlo". arXiv:1701.02434 [stat.ME].
  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. 

Further reading

External links