Hamiltonian Monte Carlo (source code)

= Hamiltonian Monte Carlo
{c}
{title2=$H(q,p)=-\log\pi(q)+p^TM^{-1}p/2$}
{wiki}

Hamiltonian Monte Carlo samples a smooth target density $\pi(q)$ by augmenting position $q$ with independent <Gaussian> momentum $p\sim N(0,M)$, where $M$ is <positive-definite>. Its joint density is proportional to $e^{-H(q,p)}$, with
$$
H(q,p)=-\log\pi(q)+\frac12p^TM^{-1}p.
$$
After refreshing momentum, approximately solve <Hamilton's equations> $\dot q=M^{-1}p$ and $\dot p=\nabla\log\pi(q)$ using <leapfrog integration>. Flip momentum to make the fixed-length proposal reversible, then accept with probability $\min(1,e^{H(q,p)-H(q',p')})$. A unit <Jacobian determinant> and reversibility justify this <Metropolis–Hastings acceptance probability> for integration error.<MCMC Using Hamiltonian Dynamics by Radford M. Neal>[2011, Sections 5.3.1–5.3.2, joint canonical density, algorithm and invariance proof]{ref}

Momentum sustains movement through the target instead of proposing each position by a short random increment. The step size, trajectory length and mass matrix affect efficiency; invariance alone does not guarantee ergodicity or efficient crossing between isolated modes.