Hamiltonian Monte Carlo Explained

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 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.[3]

Algorithm

Suppose the target distribution to sample is

f(x)

for

x\inRd

(

d\ge1

) and a chain of samples

X0,X1,X2,\ldots

is required.

The Hamilton's equations are

dxi=
dt
\partialH
\partialpi

and\dfrac{dpi}{dt}=-\dfrac{\partialH}{\partialxi}

where

xi

and

pi

are the

i

th component of the position and momentum vector respectively and

H

is the Hamiltonian. Let

M

be a mass matrix which is symmetric and positive definite, then the Hamiltonian is

H(x,p)=U(x)+\dfrac{1}{2}pTM-1p

where

U(x)

is the potential energy. The potential energy for a target is given as

U(x)=-lnf(x)

which comes from the Boltzmann's factor. Note that the Hamiltonian

H

is dimensionless in this formulation because the exponential probability weight

\exp\left(-H\right)

has to be well defined. For example, in simulations at finite temperature

T

the factor

kBT

(with the Boltzmann constant

kB

) is directly absorbed into

U

and

M

.

The algorithm requires a positive integer for number of leapfrog steps

L

and a positive number for the step size

\Deltat

. Suppose the chain is at

Xn=xn

. Let

xn(0)=xn

. First, a random Gaussian momentum

pn(0)

is drawn from

N\left(0,M\right)

. Next, the particle will run under Hamiltonian dynamics for time

L\Deltat

, this is done by solving the Hamilton's equations numerically using the leapfrog algorithm. The position and momentum vectors after time

\Deltat

using the leapfrog algorithm are:[4]

pn\left(t+\dfrac{\Deltat}{2}\right)=pn(t)-\dfrac{\Deltat}{2}\nabla

\left.U(x)\right|
x=xn(t)

xn(t+\Deltat)=xn(t)+\Deltatpn\left(t+\dfrac{\Deltat}{2}\right)

pn(t+\Deltat)= pn\left(t+\dfrac{\Deltat}{2}\right) -\dfrac{\Deltat}{2}\nabla

\left.U(x)\right|
x=xn(t+\Deltat)

These equations are to be applied to

xn(0)

and

pn(0)

L

times to obtain

xn(L\Deltat)

and

pn(L\Deltat)

.

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

Xn=xn

to

Xn+1

is

Xn+1|Xn=xn =\begin{cases} xn(L\Deltat)&withprobability\alpha\left(xn(0),xn(L\Deltat)\right)\\ xn(0)&otherwise \end{cases}

where

\alpha\left(xn(0),xn(L\Deltat)\right)= min\left(1, \dfrac{ \exp\left[ -H(xn(L\Deltat),pn(L\Deltat) ) \right] } { \exp\left[ -H(xn(0),pn(0) ) \right] } \right).

A full update consists of first randomly sampling the momenta

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

Xn+1,Xn+2,Xn+3,\ldots

.

No U-Turn Sampler

The No U-Turn Sampler (NUTS)[5] is an extension by controlling

L

automatically. Tuning

L

is critical. For example, in the one dimensional

N(0,1/\sqrt{k})

case, the potential is

U(x)=kx2/2

which corresponds to the potential of a simple harmonic oscillator. For

L

too large, the particle will oscillate and thus waste computational time. For

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

Un\simUniform(0,\exp(-H[xn(0),pn(0)]))

is sampled. Let
+
x
n
and
+
p
n
be the position and momentum of the forward particle respectively. Similarly,
-
x
n
and
-
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

or when the Hamiltonian becomes inaccurate
+
\exp\left[-H(x
n
+
,p
n

)+\delta\right]<Un

or
-
\exp\left[-H(x
n
-
,p
n

)+\delta\right]<Un

where, for example,

\delta=1000

.

Once the U-Turn condition is met, the next MCMC sample,

xn+1

, is obtained by sampling uniformly the leap frog path traced out by the binary tree
-,\ldots,x
\{x
n(-\Delta

t),xn(0),xn(\Delta

+\}
t),\ldots,x
n
which satisfies
U
n<\exp\left[-H(xn+1,pn+1)\right]
This is usually satisfied if the remaining HMC parameters are sensible.

See also

Further reading

External links

Notes and References

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