Understanding the Hamiltonian Monte Carlo through its Physics Fundamentals and Examples
Mario Molina, Abraham Granados, Jorge González Cázares, Isaías Bañales · 2025
Read Understanding the Hamiltonian Monte Carlo through its Physics Fundamentals and Examples by Mario Molina, Abraham Granados, Jorge González Cázares, Isaías Bañales (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2501-13932.txt.
Introduction
A sampling method that borrows from physics can explore complex probability shapes far faster than blind random walks. Mario Molina and co-authors explain that method from its roots in this 2025 paper. The method is Hamiltonian Monte Carlo. It treats a sampling problem like a particle gliding over a landscape shaped by the target distribution.
Readers with strong math but little physics get the needed concepts built up step by step. Working Python code accompanies the text. Examples pit the method against a classic random-walk sampler. Modern variants get their own tour, including an adaptive version that tunes its own path length. Clear guidance shows where the physics approach shines and where it strains.
Catalog record
| CREATOR | Mario Molina, Abraham Granados, Jorge González Cázares, Isaías Bañales |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 82 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2501-13932
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
Understanding the Hamiltonian Monte Carlo through its Physics Fundamentals and Examples Mario Molina, Abraham Granados, Jorge González Cázares, Isaías Bañales Abstract The Hamiltonian Monte Carlo (HMC) algorithm is a powerful Markov Chain Monte Carlo (MCMC) method that uses Hamiltonian dynamics to generate samples from a target distribution. To fully exploit its potential, we must understand how Hamiltonian dynamics work and why they can be used in a MCMC algorithm. This work elucidates the Monte Carlo Hamiltonian, providing comprehensive explanations of the underlying physical concepts. It is intended for readers with a solid foundation in mathematics who may lack familiarity with specific physical concepts, such as those related to Hamiltonian dynamics. Additionally, we provide Python code for the HMC algorithm, examples and comparisons with the Random Walk Metropolis-Hastings (RWMH) algorithm, alongside an exploration of modern variants such as the No-U-Turn Sampler (NUTS), the Dual Averaging (DA) scheme and the repelling-attracting HMC (raHMC), to highlight HMC’s strengths and weaknesses when applied to Bayesian Inference. keywords [class=MSC] Primary 65C05 keywords Hamiltonian Monte Carlo , Markov Chain Monte Carlo , Hamiltonian Dynamics , and , ?? Department of Probability and Statistics, IIMAS–UNAM, ?? ; ?? ?? Department of Mathematics, University of Chicago, ?? ?? Faculty of Science, UNAM, ?? 1 Introduction The solutions offered by Bayesian statistics, while appealing and sufficient, could not be implemented due to computational constraints in the past. However, these limitations have been reduced with the evolution of programming languages, the development of efficient algorithms, the capacity of computers to store large amounts of data, and their ability to handle more complex models, as explained [ 1 ] . One of the most widely used algorithms used to address Bayesian statistics models is HMC, originally known as Hybrid Monte Carlo and introduced in [ 13 ] in the context of lattice field theory, before being further developed in the statistical literature. The term “Hamiltonian Monte Carlo” was first introduced in [ 29 ] where the method is discussed in a statistical context. A detailed treatment of HMC is provided in [ 26 ] , where the algorithm is presented within the broader framework of Monte Carlo methods, highlighting its computational advantages. In addition, [ 35 ] offers a comprehensive exposition of HMC, including both theoretical insights and practical implementation strategies, and remains a standard reference in the literature. More recently, the understanding of HMC has been significantly refined. The work of [ 4 ] provides a conceptual introduction to HMC, developing geometric intuition and offering insight into why the method performs well, as well as the conditions under which it succeeds or fails, while [ 7 ] establishes a rigorous connection between HMC and geometric numerical integration. These developments have led to further methodological advances, including adaptive and randomized variants that aim to improve efficiency and robustness in high-dimensional settings [ 23 , 44 ] . To mention a few examples, in recent years HMC has been used in credit risk to predict potential loan defaults [ 2 ] ; in astrophysics to detect low-frequency gravitational waves using pulsar timing arrays (PTAs) [ 15 ] ; in structural engineering, to address the inverse problem of damage identification in structures [ 10 ] ; in seismology, to model time-varying seismicity rates using deep Gaussian processes [ 34 ] ; in traffic safety, to investigate factors influencing the severity of injuries to car drivers, car passengers, and truck occupants in car-truck collisions [ 22 ] ; in forensic genetics, to address the challenges of analyzing mixed DNA profiles [ 42 ] ; in statistics, to infer and predict nonparametric probability density functions (PDFs) using constrained Gaussian processes [ 28 ] ; in epidemiology, to develop a simulation model to understand the spread of the COVID-19 virus immediately after an infected person coughs or sneezes [ 12 ] ; and in medical imaging, to improve the reliability of deep neural networks used for medical image segmentation [ 45 ] . This paper introduces the theory and Python codes of HMC, and provides examples and comparisons. Section 2 introduces the fundamental principles of Hamiltonian mechanics. Section 3 presents the construction of the HMC algorithm, important theoretical results and its sensitivity to the choice of hyperparameters \epsilon (leapfrog step size) and L (trajectory length). Section 4 reviews modern extensions of the algorithm, such as the No-U-Turn Sampler (NUTS) and the Dual Averaging (DA), which adaptively tune \epsilon and L , and the repelling-attracting HMC (raHMC), which helps the chain visit all local modes. Section 5 presents illustrative examples and comparative analyses between HMC (and its variants) and RWMH in terms of execution time, convergence, effective sample size (ESS) and seconds per ESS. The Python code for these examples can be accessed via the GitHub repository at https://github.com/unabrahams/Hamiltonian-Monte-Carlo- . Finally, Section 6 presents the conclusions of this work. 2 An introduction to Hamiltonian Mechanics This section introduces the fundamental elements of Hamiltonian mechanics, which are essential for comprehending the underlying principles of the HMC algorithm. This content is presented in a concise manner and is organized in a way that does not presuppose any prior knowledge of mechanics. 2.1 Analytical Mechanics Analytical mechanics provides an abstract formulation of Newtonian mechanics. In contrast to the approach taken in classical mechanics, where the equations of motion are derived from vector functions and the positions of particles and the forces acting on them are defined in three-dimensional space, the equations of motion in this approach are derived from scalar functions. Moreover, their mathematical formulation is independent of any change of coordinates, thus facilitating their application to a variety of problems [ 17 ] . 2.1.1 Basic concepts In the context of physics, the degrees of freedom of a system are defined as the minimum number of independent scalar values that are required to determine the position of the particles within a given space. In Newtonian mechanics, the position of n particles moving without constraints can be determined by using n position vectors, resulting in a total of 3n degrees of freedom. This is discussed in greater detail in the Appendix . The generalized coordinates are a set of parameters q_{1},q_{2},...,q_{s} that allow us to determine in an uniquely way the position of the particles in space, generally these parameters are typically measurable physical quantities, including positions along coordinate axes, angles, and distances. In a particle system described by s\in\mathbb{Z}_{+} generalized coordinates, the function q:\mathbb{R}\to\mathbb{R}^{s} represents the generalized coordinates of the system at time t . Specifically, q(t)=(q_{1}(t),q_{2}(t),\dots,q_{s}(t)) , where q_{i}(t) denotes the position along the i -th coordinate at time t . The objective of employing generalized coordinates is to derive the equations of motion for a system in a more efficient way. In particular, we seek a function \phi_{i}:\mathbb{R}^{s+1}\rightarrow{\mathbb{R}}^{3} such that r_{i}\left(t\right)=\phi_{i}\left(q\left(t\right),t\right), (1) where r_{i}\left(t\right)\in\mathbb{R}^{3} represents the position of the i -th particle in cartesian coordinates, thus the complete position of the system in three-dimensional space can also be known, given the value of q\left(t\right) . We define generalized velocities at time t as the vector of derivatives of the generalized coordinates, denoted by \frac{dq\left(t\right)}{dt} . We will refer to the phase space of velocities, which we denote by M , as the subset of \mathbb{R}^{2s} such that