The Boltzmann distribution

Lesson 4.2 of Biology Primer for Computer Scientists. Download the code as a notebook.

A molecule in water never sits still in its lowest-energy arrangement. Thermal kicks move it around its energy landscape forever, and it spends more time in some regions than others. The Boltzmann distribution says how much more, turning energies into probabilities. Monte Carlo, molecular dynamics, ensemble-based design, and energy-based generative models are all ways of drawing samples from it.

Probabilities from energies

A microstate is one complete arrangement of the system, every atom placed: for a protein, one conformation. A macrostate such as "folded" or "bound" is a set of microstates. At equilibrium at temperature T, the probability of finding the system in microstate i, with energy Ei, is

pi=e−Ei/kBTZ,Z=∑je−Ej/kBT

(per mole, replace kBT with RT). That is a softmax with logits −Ei/kBT, and what you know about softmax carries over. Only energy differences matter, because pi/pj=e−(Ei−Ej)/kBT, so the zero of an energy function is arbitrary. Temperature sharpens or flattens the distribution: as T→0 all probability goes to the lowest state (an argmax), and as T→∞ every state becomes equally likely. At 298 K, every RTln⁡10≈1.36 kcal/mol of energy difference is a factor of ten in population, so a state 4.1 kcal/mol above another is about 1,000 times rarer.

Loading the interactive figure…

Five microstates with editable energies in kcal/mol, and their Boltzmann probabilities. Cool toward 30 K and the lowest state takes everything; heat toward 1500 K and the bars level out. At 298 K, changing one energy by 1.36 kcal/mol changes its population tenfold.

Partition functions and free energy

The normalizer Z is the partition function. Protein design algorithms compute it directly. The K∗ algorithm, for example, scores how well a mutant protein binds a ligand (any molecule that binds a protein, often a small one such as a drug) by comparing partition functions. It sums over rotamers, the few preferred torsion settings of each side chain (lesson 1.9), and it estimates a binding constant: KA below is large when binding is tight, and KD=1/KA is the dissociation constant that lesson 4.3 is about.

Protein-ligand binding for a predicted mutant protein is modeled using the following K∗ equation:

K∗=qPLqPqL

K∗ is derived to be an approximation to the true association (binding) constant KA=1/KD. Computing exact partition functions for complex molecular species appears to require integrating an exact energy function over a molecule's entire conformational space. We therefore approximate these partition functions with the use of rotamer-based conformational ensembles:

qPL=∑b∈Bexp⁡(−Eb/RT),qP=∑f∈Fexp⁡(−Ef/RT),qL=∑l∈Lexp⁡(−El/RT)

where B, F, and L represent rotamer-based ensembles for the bound protein-ligand complex (PL), the free protein (P), and the free ligand (L), respectively, and Es is the energy of conformation s. Since conformations with large energies are unlikely to be assumed and contribute only a vanishingly small amount to the partition function, it is reasonable to prune high-energy conformations from consideration.

Two facts make the partition function useful. First, −RTln⁡Z is the free energy. It is a log-sum-exp, the smooth maximum from machine learning: when one state dominates, it approaches that state's energy, and when many states share the weight, it falls below the lowest energy by TS. Second, restricting the sum to a macrostate A (folded, unfolded, bound, free) gives that macrostate's free energy,

GA=−RTln⁡∑i∈Ae−Ei/RT

and if A is W microstates all at energy E, this is E−RTln⁡W=E−TS, lesson 4.1's free energy derived rather than asserted. Two macrostates' populations compare by their free energies, and the equilibrium constant for A⇌B is

K=pBpA=e−ΔG/RT

Every equilibrium constant you'll meet, including a folding constant, a Kd, and the protonation fractions of lesson 1.3, is a ratio of partition functions like this, as K∗ is. The cell builds a toy protein, a native basin of 5 deep states and an unfolded basin of 50,000 shallow ones, and computes both basins' free energies with a numerically stable log-sum-exp.

import numpy as np

R = 1.987e-3                                         # kcal/(mol K)
rng = np.random.default_rng(0)
native = rng.normal(-12.0, 0.5, size=5)              # few states, low energy
unfolded = rng.normal(-4.0, 1.0, size=50_000)        # many states, higher energy

def free_energy(E, T):
    """-RT ln sum exp(-E/RT), computed stably: shift by the minimum before exponentiating."""
    beta = 1 / (R * T)
    m = E.min()
    return m - np.log(np.exp(-beta * (E - m)).sum()) / beta

for T in (250, 298, 350, 400, 450):
    Gn, Gu = free_energy(native, T), free_energy(unfolded, T)
    p_native = 1 / (1 + np.exp(-(Gu - Gn) / (R * T)))
    print(f"T = {T} K: G_native = {Gn:6.2f}, G_unfolded = {Gu:6.2f}, "
          f"dG = {Gu - Gn:+5.2f} kcal/mol, native fraction = {p_native:.3f}")
T = 250 K: G_native = -12.82, G_unfolded = -10.39, dG = +2.42 kcal/mol, native fraction = 0.992
T = 298 K: G_native = -12.96, G_unfolded = -11.26, dG = +1.70 kcal/mol, native fraction = 0.947
T = 350 K: G_native = -13.13, G_unfolded = -12.25, dG = +0.88 kcal/mol, native fraction = 0.779
T = 400 K: G_native = -13.28, G_unfolded = -13.23, dG = +0.05 kcal/mol, native fraction = 0.515
T = 450 K: G_native = -13.44, G_unfolded = -14.24, dG = -0.80 kcal/mol, native fraction = 0.291

The unfolded basin's typical state is 8 kcal/mol above the native ones, but it has 10,000 times as many states. That's worth RTln⁡104≈5.5 kcal/mol at room temperature, and more as T rises. Near 400 K the entropy wins and this toy protein melts.

Sampling when you can't enumerate

A real protein has far too many conformations to sum over (lesson 4.4 counts them). What you can do is draw samples from the Boltzmann distribution and average over them. The justification is the ergodic hypothesis.

The ergodic hypothesis is a basic underlying principle in structural bioinformatics and, more generally, in statistical thermodynamics. It claims that over a sufficiently long period, the time spent in a region of phase space of microstates with the same energy is proportional to the size of this region. In the context of structural bioinformatics, the phase space is often the energy landscape, that is, the energy of the investigated macromolecule(s) for each possible conformation.

For sampling algorithms, the ergodic hypothesis implies that after a sufficiently long time, the sampling algorithm makes an equal number of steps in different equal volumes or regions of phase space. In other words, the ensemble, or thermodynamic average, for a phase space region is equivalent to the time average, also termed the dynamic average. In this context, the assumption of equilibrium means that moving from state X to state Y is as likely as moving from state Y to state X; that is, the system obeys the detailed balance criterion.

Often, while the local explored region of the energy landscape is fully ergodic, high-energy barriers prevent the sampling of distant low-energy regions. Approaches to avoid this local minimum trap include raising the energy of the local low-energy basins, for example accelerated molecular dynamics, or the occasional high-energy jumping above barriers.

The algorithm that does the jumping is Monte Carlo with the Metropolis criterion:

Monte Carlo (MC) simulations, a collective name for the usage of an iterative application of random perturbation steps that can be accepted or rejected, has become one of the most common stochastic search procedures. This simple and efficient technique was largely established in Los Alamos by Metropolis et al. In classical MC the direction of the move is random. Next, and most important, the move has to be accepted or rejected. In order to get out of local minima, moves that are less favorable should be occasionally accepted. Classic MC applications simulate an equilibrium state that follows a Boltzmann-type distribution. As such, moves that have a lower minimum will always be accepted, while moves that are less favorable will be accepted with a probability corresponding to the Boltzmann distribution. This simple acceptance criterion not only channels the moves toward the local minima but also allows occasional jumps to bypass local energy barriers, hence exploring other minima.

A conformation can be modeled as a Markov chain: a discrete-time stochastic process in which the next state depends solely on the present state. A new trial conformation α′ is generated from the current one, α. Following the Metropolis criterion, detailed balance is achieved by accepting the move with probability

P(α→α′)=min⁡(1,p(α′)q(α|α′)p(α)q(α′|α))

where p(α) is the equilibrium probability of configuration α and q(α′|α) is the probability of proposing α′ from α. The equilibrium probability has the form p(α)∝exp⁡(−βF(α))/Z(β), where F is an energy-based scoring function and β=1/kBT. In classic MC the proposal is symmetric, q(α′|α)=q(α|α′), so the equation reduces to the Metropolis acceptance criterion:

P(α→α′)=min⁡(1,e−β[F(α′)−F(α)])

Notice what's missing from the acceptance rule: Z. It cancels in the ratio, so Metropolis samples a distribution whose normalizer is an impossible sum. Here it is on a one-dimensional double well, checked against the exact density:

import matplotlib.pyplot as plt

def E(x):
    """A double well: minima near x = ±1, the right one 0.6 kcal/mol lower, a barrier of about 2 kcal/mol."""
    return 2.0 * (x**2 - 1) ** 2 - 0.3 * x

def metropolis(T, steps=200_000, step=0.25, x=-1.0, seed=1):
    rng = np.random.default_rng(seed)
    beta, out = 1 / (R * T), np.empty(steps)
    e = E(x)
    for i in range(steps):
        x_new = x + rng.uniform(-step, step)
        e_new = E(x_new)
        if e_new <= e or rng.random() < np.exp(-beta * (e_new - e)):
            x, e = x_new, e_new
        out[i] = x
    return out

T = 298
xs = np.linspace(-2, 2, 400)
boltzmann = np.exp(-E(xs) / (R * T))
boltzmann /= np.trapezoid(boltzmann, xs)
samples = metropolis(T)

fig, ax = plt.subplots()
ax.hist(samples, bins=120, range=(-2, 2), density=True, color="#9ec5f4", label="Metropolis samples")
ax.plot(xs, boltzmann, color="#333333", lw=1.5, label="exact Boltzmann density")
ax.set_xlabel("coordinate x")
ax.set_ylabel("probability density")
ax.legend()
plt.show()
print(f"fraction of time in the right well: sampled {np.mean(samples > 0):.3f}, "
      f"exact {np.trapezoid(boltzmann[xs > 0], xs[xs > 0]):.3f}")
−2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0 coordinate x 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 probability density Metropolis samples exact Boltzmann density
fraction of time in the right well: sampled 0.713, exact 0.717

The walker started in the left well and had to cross a barrier of several RT to find the right one. With a higher barrier or a lower temperature it might never cross in the time available. That sampling problem, more often than the energy function, limits what simulations can tell you.

Annealing: from sampler to optimizer

To sample the required landscape comprehensively while focusing on the most probable region in greater detail, a simulated annealing (SA) scheme may be applied. SA involves a temperature that is initially high, allowing for large jumps between local minima, and then gradually decreased to carefully investigate the region of interest. The exact cooling rate, which is proportional to the number of MC iterations, is often decided using a trial-and-error procedure in which convergence is assessed on case studies. MC requires a large number of iterations to achieve convergence as it is challenged by the large phase space to be explored as well as by the roughness of the energy landscape. Indeed, to avoid local minima traps a very slow cooling rate is often required.

Rosetta's structure prediction and design protocols are Monte Carlo with annealing. Molecular dynamics samples the same Boltzmann distribution by integrating Newton's equations at a temperature. Generative models for proteins learn a density p(x)∝e−E(x), or its gradient, from data.

Check yourself

Two conformations of a side chain differ in energy by 2.7 kcal/mol at 298 K. Roughly what fraction of the time does the side chain spend in the higher one? What if both had the same energy but the higher one could be realized in 100 slightly different ways?

Reasoning

2.7 kcal/mol is two factors of 1.36, so the ratio is about 10−2, and the side chain spends about 1% of its time in the higher conformation. In the second case the energies are equal but the second state has 100 microstates against one. Its free energy is lower by RTln⁡100=2.7 kcal/mol, so it is populated about 100 times more, roughly 99% of the time.

A Metropolis simulation of a protein at 300 K is run three times from different starting structures. The average energies after a long run differ substantially between runs. Is the energy function wrong?

Reasoning

Not necessarily. A converged sampler reaches the same Boltzmann distribution from any start, so different averages mean the runs haven't converged: each is trapped in a different basin behind barriers too high to cross in the simulated time. The fix is better sampling (longer runs, replicas at higher temperature, smarter moves), not a new energy function. Only after sampling converges can wrong answers be blamed on the energy function.

K∗ prunes high-energy conformations before summing the partition functions. Why is that safe, and when would it fail?

Reasoning

Each conformation contributes e−E/RT to q, so a conformation 5 kcal/mol above the best contributes about e−8.4≈2×10−4 as much. Dropping high-energy terms changes the sum very little, which is why the algorithm can bound the error. It fails when there are so many high-energy conformations that their total weight is large even though each one is tiny. That is exactly the entropy of a floppy state, such as a flexible ligand in solution, where millions of slightly worse conformations can outweigh one good one.