Chapter 1 established that ensemble averages are intractable integrals and that the path forward is importance sampling: generate configurations distributed according to the Boltzmann distribution , then estimate averages as arithmetic means. This chapter delivers the algorithm that generates those configurations. The method — introduced by Metropolis and colleagues in 1953 — is simple enough to state in five lines of pseudocode. Understanding why it works requires thinking carefully about convergence of random processes, the geometry of configuration space, and a condition called detailed balance that sits at the foundation of equilibrium statistical mechanics. Both the simplicity and the depth matter for using the method well.
The Failure of Naive Sampling
The original Monte Carlo idea — draw independent samples uniformly at random from the integration domain, then average the integrand — fails completely for high-dimensional Boltzmann distributions.
The problem is concentration. The Boltzmann weight is negligible for the overwhelming majority of configurations in . A configuration drawn uniformly from the space will almost certainly contain atomic overlaps — pairs of atoms closer than their van der Waals radii — because close contacts are geometrically unavoidable in high-dimensional random packings. For a hard-core potential, such a configuration has and Boltzmann weight exactly zero. For more realistic potentials, the weight is exponentially suppressed by the large repulsive energy. The integrand is effectively zero almost everywhere, and essentially all computational effort is wasted evaluating the integrand in regions that contribute nothing to the integral.
This is not a problem that more samples can fix. The fraction of configuration space with non-negligible Boltzmann weight does not grow with the number of samples; it remains vanishingly small regardless of . What is needed is a method that concentrates its evaluations where the Boltzmann weight is large — a method that navigates toward the typical configurations rather than searching for them blindly.
Markov Chains
The solution is to construct a dependent sequence of configurations, each obtained by making a small random modification to the previous one, with the modification designed so that the sequence explores configuration space in proportion to the Boltzmann weight.
[!definition] 2.1 — Markov Chain A Markov chain is a sequence of random variables taking values in configuration space , satisfying the Markov property: the conditional distribution of depends only on , not on the earlier history:
The dynamics are entirely encoded in the transition kernel : the probability density of moving to when currently at .
A Markov chain is useful as a sampling algorithm only if its distribution at time converges to as , regardless of where it started. A distribution with this property is called the stationary distribution of the chain. The convergence condition is:
This says that if the chain is started in , it remains in after one step — is preserved. The challenge is to design a transition kernel for which (the Boltzmann distribution) is stationary.
Detailed Balance
A sufficient condition for stationarity — and the one used in almost all molecular simulation algorithms — is detailed balance.
[!definition] 2.2 — Detailed Balance A transition kernel satisfies detailed balance with respect to if, for all pairs of configurations :
$$\pi(\mathbf{r})\, T(\mathbf{r}' \mid \mathbf{r}) = \pi(\mathbf{r}')\, T(\mathbf{r} \mid \mathbf{r}').$$ This condition asserts that the probability flux from to — the rate at which the chain transitions from to in stationarity — exactly equals the reverse flux from to . The chain is in microscopic reversibility: every transition is balanced by its time-reverse.
To verify that detailed balance implies stationarity, integrate both sides of (2.2) over :
where the last step uses the normalization (the chain must go somewhere). This is exactly the stationarity condition (2.1).
The physical intuition behind detailed balance is direct: it is the condition of microscopic reversibility at equilibrium. In an equilibrium molecular system, the rate at which the system transitions from configuration to equals the rate of the reverse transition. Designing an algorithm that satisfies this condition is therefore equivalent to designing an algorithm that respects the symmetry of equilibrium.
[!remark] 2.1 Detailed balance is sufficient but not necessary for stationarity. There exist valid sampling algorithms — notably those based on the skew-detailed balance condition — that do not satisfy (2.2) but still have as their stationary distribution. These algorithms can sometimes mix faster by introducing a net probability flux that circulates through configuration space, analogous to the flow of an incompressible fluid. However, the Metropolis–Hastings algorithm satisfies the stronger detailed balance condition, and we will work within that framework throughout this chapter.
The Metropolis–Hastings Algorithm
Detailed balance provides the design criterion for a sampling kernel. The Metropolis–Hastings algorithm implements it by decomposing each step into a proposal followed by an acceptance decision.
Choose any convenient proposal distribution — a distribution over new configurations given the current configuration . Then accept or reject the proposal with a probability chosen specifically to enforce detailed balance:
This is the Metropolis–Hastings acceptance probability. For the Boltzmann distribution , the normalization constant cancels in the ratio :
where is the energy change of the proposed move. For a symmetric proposal — one satisfying — the proposal ratio cancels and the acceptance probability reduces to:
This is the original Metropolis criterion. It has an immediate physical interpretation: moves that lower the energy () are always accepted. Moves that raise the energy () are accepted with probability , which decreases exponentially with the magnitude of the energy increase. At low temperature ( large), only energetically favorable moves are accepted. At high temperature ( small), uphill moves are accepted more readily, and the system explores more of configuration space at the cost of less discrimination between high- and low-energy regions.
[!definition] 2.3 — The Metropolis–Hastings Algorithm Given a symmetric proposal distribution and current configuration :
- Propose: Draw a candidate .
- Evaluate: Compute .
- Accept: Draw . If , set ; else set .
- Collect: Record for any observable of interest.
- Repeat from step 1.
After an equilibration period, the time average converges to the ensemble average .
The correctness of the algorithm — the fact that it produces samples from — follows directly from detailed balance. The effective transition kernel of the Metropolis chain for is . Checking detailed balance for the case (the uphill direction):
In the reverse direction, the move from to is downhill and accepted with probability 1:
using the symmetry of . The two sides are equal. Detailed balance is satisfied, and is stationary.
Trial Moves in Practice
The proposal distribution determines the efficiency of sampling, while the Metropolis criterion guarantees correctness for any . The art of Monte Carlo in molecular simulation lies in choosing proposals that explore configuration space rapidly.
The canonical trial move is the random displacement: select one particle uniformly at random, then propose
where is drawn uniformly from the cube . All other particle positions are unchanged. This proposal is symmetric by construction.
The maximum displacement controls a fundamental tradeoff. If is too small, proposed moves are nearly always accepted (since for tiny displacements), but each accepted move barely changes the configuration — successive samples are highly correlated and configuration space is explored slowly. If is too large, proposed moves generate large positive energy changes and are almost always rejected — the chain stagnates at the current configuration. The optimal choice balances these: a commonly cited target for simple liquids is an acceptance rate of roughly 30–50%. In practice, is tuned during equilibration by monitoring the running acceptance fraction and adjusting upward if the rate is too high, downward if too low.
[!remark] 2.2 The acceptance rate is not, in itself, a measure of sampling efficiency. A chain with 5% acceptance rate might be exploring configuration space efficiently if successful moves are large and geometrically uncorrelated. A chain with 95% acceptance rate might be making negligibly small, highly correlated steps. The meaningful quantity is the integrated autocorrelation time — the effective number of steps between statistically independent samples of an observable — which depends on both acceptance rate and step size in a coupled way. For a given system and observable, the optimal minimizes , not maximizes the acceptance rate.
For molecular systems — where particles are not single atoms but rigid or flexible molecules — single-atom displacement moves are inefficient because molecular geometry constraints make independent atom moves highly correlated. More effective proposals include:
- Rigid-body moves: translate and rotate an entire molecule simultaneously, preserving its internal geometry.
- Volume moves: for NPT simulations, propose a random change , rescale all positions by , and apply a modified acceptance criterion that includes the pressure-volume work and the entropic term .
- Configurational-bias moves: for chain molecules, build the proposed conformation one monomer at a time, sampling each monomer position from the local Boltzmann distribution of the growing chain. The acceptance criterion is corrected using Rosenbluth weights to account for the asymmetric proposal.
Ergodicity and Convergence
Detailed balance guarantees that is a stationary distribution of the Metropolis chain. Two additional properties are needed to guarantee that the chain converges to and that the ergodic theorem applies.
[!definition] 2.4 — Ergodicity A Markov chain is irreducible with respect to if, for every pair of configurations with , there exists a finite number of steps such that the chain can reach from with positive probability. The chain is aperiodic if it does not return to states with a fixed period. An irreducible, aperiodic Markov chain with stationary distribution is ergodic: for -almost every starting configuration, the marginal distribution of converges to as .
The ergodic theorem for Markov chains then guarantees:
for any observable with finite variance under . The time average along the chain converges to the ensemble average, regardless of the starting configuration, provided the chain is run long enough.
For the Metropolis algorithm with random displacement moves, irreducibility holds as long as and the potential energy is finite on a connected subset of configuration space with positive measure under . Aperiodicity follows from the positive probability of rejection: the chain can stay at for any number of steps, so it cannot be periodic.
There is, however, a practical failure mode that satisfies these formal conditions yet defeats convergence in any finite simulation: metastability. If the configuration space consists of multiple regions separated by barriers of height , the chain is irreducible in principle (it can always cross the barrier given enough steps) but in practice never does so within any accessible simulation time. The formal ergodicity guarantee says nothing about how long convergence takes, only that it eventually occurs. A chain trapped in a single metastable basin will produce biased estimates of any observable that differs between the basins. This is the sampling problem of Chapter 4.
Equilibration and Statistical Analysis
Every Monte Carlo simulation has two phases: an equilibration phase, during which the chain is run without collecting data until it has lost memory of its initial configuration, and a production phase, during which configurations are collected and observables averaged.
There is no general rule for the equilibration length, but two diagnostics are standard. First, monitor the potential energy as a function of step number: equilibration is complete when this quantity has stopped drifting and fluctuates around a stable mean. Second, if computationally feasible, launch multiple independent chains from different initial configurations and verify that their observable averages have converged to the same value.
Even in the production phase, successive configurations are not independent. The integrated autocorrelation time,
measures the effective number of steps between independent samples of . A production run of steps yields an effective sample size of approximately , and the standard error of is rather than .
A practical approach to error estimation is block averaging: divide the production run into non-overlapping blocks of length , compute the block mean for each block, and estimate the variance of as . When the block length , the block means are approximately independent, and this estimator is consistent.
The Partition Function Is Never Needed
One feature of the Metropolis algorithm deserves emphasis, because it has both a practical consequence and a fundamental limitation. The partition function never appears in the acceptance criterion. The ratio requires only the difference in potential energies, and cancels. This is the computational miracle that makes Boltzmann sampling tractable: we can generate samples from a distribution without knowing its normalization.
The limitation is that absolute free energies are inaccessible from a standard Metropolis run. The ensemble average is estimated cheaply from the sample mean. But the Helmholtz free energy requires knowing itself — information that the Metropolis algorithm systematically discards in every acceptance step. Computing free energy differences between two thermodynamic states requires a different strategy entirely — one that connects the two states and carefully tracks the probability weight along the connection. These strategies are the subject of Chapter 6.
[!remark] 2.3 The practical consequence is sharp: Metropolis Monte Carlo computes thermal averages of configurational observables efficiently, but cannot compute the free energy of a single state. To obtain the free energy difference between a folded and unfolded protein, or the binding free energy of a drug, one cannot simply collect configurations in each state and compare their energies — the entropic contribution is invisible to that comparison. One must compute the free energy difference directly, using methods that are covered in Chapter 6.
Brief Extensions
The Metropolis algorithm is the foundation of a much larger family of Monte Carlo methods. Several extensions are important enough to mention here, even if their full development belongs to later chapters.
Gibbs sampling is an alternative update scheme for systems where the conditional distribution of one variable given all others is easy to sample. Rather than proposing a random displacement and accepting or rejecting, one updates each coordinate (or block of coordinates) by drawing exactly from its conditional distribution. No acceptance step is needed; the algorithm always accepts. Gibbs sampling is particularly useful in lattice models and in Bayesian inference problems embedded in molecular simulation.
Parallel tempering (replica exchange Monte Carlo) runs multiple copies of the system simultaneously at different temperatures, periodically proposing swaps of configurations between replicas with a Metropolis criterion applied to the swapping. High-temperature replicas can cross energy barriers freely, passing their configurations to lower-temperature replicas that then equilibrate locally. This dramatically improves sampling of rugged energy landscapes. It is the canonical enhanced sampling method and is treated in Chapter 5.
Wang–Landau sampling estimates the density of states — the number of configurations at each energy level — rather than sampling from the Boltzmann distribution directly. By iteratively flattening the energy histogram, it produces an estimate of across the full energy range, from which free energies at any temperature can be obtained. It is a rare example of an algorithm that makes the partition function computationally accessible.
The Monte Carlo method was developed at Los Alamos in the late 1940s by John von Neumann, Stanislaw Ulam, and Nicholas Metropolis for nuclear calculations, with the name coined to evoke the randomness of the casino at Monte Carlo. Its application to statistical mechanics is due to Metropolis, Arianna Rosenbluth, Marshall Rosenbluth, Augusta Teller, and Edward Teller, whose 1953 paper “Equation of State Calculations by Fast Computing Machines” introduced the acceptance criterion and demonstrated Boltzmann sampling for a hard-disk fluid. The asymmetric generalization — allowing non-symmetric proposals with the correction factor — is due to W.K. Hastings (1970). The theoretical foundations of Markov chain convergence were developed by Markov himself and formalized for continuous state spaces by Meyn and Tweedie (1993). The configurational-bias Monte Carlo method, extending the algorithm to chain molecules, is due to Siepmann and Frenkel (1992). Block averaging for autocorrelated time series is discussed in Flyvbjerg and Petersen (1989). The treatment of Monte Carlo methods in Frenkel and Smit (2002), Chapters 3–4, remains the definitive reference for practical implementation in molecular simulation.