# Hamiltonian Monte Carlo

In computational physics and statistics, the Hamiltonian Monte Carlo algorithm (also 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.

## Algorithm

Suppose the target distribution to sample is $f(\mathbf {x} )$ and a chain of samples $\mathbf {X} _{0},\mathbf {X} _{1},\mathbf {X} _{2},\ldots$ is required. The Hamilton's equations are

${\frac {{\text{d}}x_{i}}{{\text{d}}t}}={\frac {\partial H}{\partial p_{i}}}$ and

${\dfrac {{\text{d}}p_{i}}{{\text{d}}t}}=-{\dfrac {\partial H}{\partial x_{i}}}$ where $x_{i}$ and $p_{i}$ 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(\mathbf {x} ,\mathbf {p} )=U(\mathbf {x} )+{\dfrac {1}{2}}\mathbf {p} ^{\text{T}}M^{-1}\mathbf {p}$ where $U(\mathbf {x} )$ is the potential energy. The potential energy for a target is given as

$U(\mathbf {x} )=-\ln f(\mathbf {x} )$ which comes from the Boltzmann's factor.

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

$\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)}$ $\mathbf {x} _{n}(t+\Delta t)=\mathbf {x} _{n}(t)+\Delta tM^{-1}\mathbf {p} _{n}\left(t+{\dfrac {\Delta t}{2}}\right)$ $\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 $\mathbf {x} _{n}(0)$ and $\mathbf {p} _{n}(0)$ $L$ times to obtain $\mathbf {x} _{n}(L\Delta t)$ and $\mathbf {p} _{n}(L\Delta t)$ .

Because the leap frog algorithm is a numerical method and does not solve the Hamilton's equations exactly, a Metropolis–Hastings step is used. The transition from $\mathbf {X} _{n}=\mathbf {x} _{n}$ to $\mathbf {X} _{n+1}$ is

$\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

$\alpha \left(\mathbf {x} _{n}(0)_{n},\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)$ This is repeated to obtain $\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$ automatically. Tuning $L$ is critical. For example in the one dimensional ${\text{N}}(0,1/{\sqrt {k}})$ case, the potential is $U(x)=kx^{2}/2$ which corresponds to the potential of a simple harmonic oscillator. For $L$ too large, the particle will oscillate and this 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 $U_{n}\sim {\text{Uniform}}(0,\exp(-H[\mathbf {x} _{n}(0),\mathbf {p} _{n}(0)]))$ is sampled. Let $\mathbf {x} _{n}^{+}$ and $\mathbf {p} _{n}^{+}$ be the position and momentum of the forward particle respectively. Similarly, $\mathbf {x} _{n}^{-}$ and $\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

$(\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 \left[-H(\mathbf {x} _{n}^{+},\mathbf {p} _{n}^{+})+\delta \right] or

$\exp \left[-H(\mathbf {x} _{n}^{-},\mathbf {p} _{n}^{-})+\delta \right] where, for example, $\delta =1000$ .

Once the U-Turn condition is met, the next MCMC sample, $\mathbf {x} _{n+1}$ , is obtained by sampling uniformly the leap frog path traced out by the binary tree $\{\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 \left[-H(\mathbf {x_{n+1}} ,\mathbf {p_{n+1})} \right]$ This is usually satisfied if the remaining HMC parameters are sensible.