Mathematics for science

The mathematics used throughout simulation and machine learning, developed from first principles: functions and calculus, linear algebra and gradients, and probability.

Calculus

Functions, derivatives, Taylor series, integrals and numerical approximation.

1Functions and their graphs

What a function is, how a graph encodes one, and the families of functions that appear throughout physical science: linear, power, exponential, logarithmic, periodic and Gaussian. Includes logarithmic axes and how to read a law of nature from data.

about 30 min read

Functions

Science is largely the study of how one quantity depends on another. The pressure of a gas depends on its temperature, the energy of two atoms depends on the distance between them, the number of undecayed nuclei in a sample depends on how long you have waited. The mathematical object that captures “depends on” is the function.

A function is a rule that takes an input and produces exactly one output. If the rule is called ff and the input is xx, the output is written f(x)f(x), read “f of x”. The insistence on exactly one output is the whole point: if you know the input, the output is determined. The rule “square the number” is a function, because every number has one square. The rule “give me a number whose square is xx” is not, because 4 has two square roots, 22 and −2-2, and the rule does not say which to choose.

It helps to picture a function as a machine. A number goes in on one side, the machine does something to it, and a number comes out of the other side. The machine does not remember previous inputs and does not behave differently on different days. The figure below lets you feed numbers into such a machine. Each time a number comes out, the pair (input, output) is recorded as a point on a graph, and if you feed many inputs the points join up into a curve. That curve is the graph of the function, and it is nothing more than the record of every input–output pair.

A function as a machine, and its graph as a record of what it did

Loading the interactive figure…

Choose a rule, move the input slider and feed values through the machine. Each output is plotted against its input. Feeding 40 inputs traces out the graph.

Some vocabulary makes it possible to talk about functions precisely. The set of inputs a function accepts is its domain. The square root of a negative number is not a real number, so the domain of x\sqrt{x} is x≥0x \ge 0; the function 1/x1/x accepts every number except zero. The set of outputs a function can produce is its range: x2x^2 never produces a negative number, so its range is y≥0y \ge 0. The input is often called the independent variable and the output the dependent variable, because the output depends on the input and not the other way round.

Functions can be described in four equivalent ways, and moving between them is a skill worth practising. A formula such as f(x)=1.8x+32f(x) = 1.8x + 32 gives the rule symbolically. A table lists selected input–output pairs, which is how experimental data usually arrive. A graph shows every pair at once as a curve. And a description in words says what the rule does: “convert a temperature from degrees Celsius to degrees Fahrenheit”. Each form is good for something different. The formula is good for calculation, the table for recording measurements, the graph for seeing behaviour at a glance, and the words for remembering what the function means.

Functions in science carry units. In TF=1.8 TC+32T_F = 1.8\,T_C + 32 the input is in °C and the output in °F, so the number 1.8 has units of °F per °C and the 32 is in °F. Keeping track of units is the single most effective way to catch mistakes in a formula: if two terms that are added together have different units, the formula is wrong.

Reading a graph

A graph is drawn on two perpendicular axes. The horizontal axis carries the input, conventionally xx, and the vertical axis the output, y=f(x)y = f(x). The point where the axes cross is the origin, (0,0)(0, 0). Every point on the curve has coordinates (x,f(x))(x, f(x)).

Several features of a function can be read directly from its graph. Where the curve crosses the vertical axis, the input is zero, so that height is f(0)f(0), the intercept. Where the curve crosses the horizontal axis the output is zero; these inputs are the roots or zeros of the function and solving the equation f(x)=0f(x) = 0 means finding them. Where the curve rises from left to right the function is increasing, and where it falls it is decreasing. The highest and lowest points of a hump or a dip are maxima and minima, and they matter enormously in science: a stable arrangement of atoms is a minimum of the energy, and the barrier a chemical reaction must cross is a maximum along the reaction path. A curve may also approach a straight line without ever reaching it, as 1/x1/x approaches the horizontal axis when xx becomes large; such a line is an asymptote.

Graphs can be transformed in predictable ways, and recognising these transformations makes new functions look familiar. Replacing f(x)f(x) by f(x)+cf(x) + c moves the whole graph up by cc. Replacing f(x)f(x) by f(x−h)f(x - h) moves it to the right by hh, which surprises many people at first: the output that used to occur at x=0x = 0 now occurs at x=hx = h. Multiplying the output, a f(x)a\,f(x), stretches the graph vertically by the factor aa, and a negative aa also flips it upside down. Multiplying the input, f(kx)f(kx), squeezes the graph horizontally by the factor kk. A Gaussian peak centred at μ\mu with width σ\sigma, for example, is just the standard bell curve shifted by μ\mu and stretched by σ\sigma.

Linear functions

The simplest non-trivial function is the straight line,

The slope is the defining property of a straight line: between any two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) on it, the ratio (y2−y1)/(x2−x1)(y_2 - y_1)/(x_2 - x_1) is the same number mm. “Rise over run” is the usual mnemonic. A positive slope means yy grows with xx, a negative slope means it shrinks, and a zero slope gives a horizontal line. The slope has units, output units per input unit, and it often has a physical meaning of its own: in a graph of position against time the slope is a velocity, in a graph of charge against voltage it is a capacitance.

When c=0c = 0 the line passes through the origin and yy is proportional to xx, written y∝xy \propto x: doubling xx doubles yy. Many laws of physics take this form near equilibrium. Hooke’s law says the restoring force of a spring is proportional to its extension, F=−kxF = -kx. Ohm’s law says the current through a resistor is proportional to the voltage across it. Linear behaviour is so common because almost any smooth function looks like a straight line if you only look at a small enough piece of it, an idea that becomes the derivative in the next chapter.

Linear functions are also the workhorse of data analysis. Plotting data so that a law appears as a straight line, and then measuring the slope and intercept, is one of the oldest and most reliable ways of extracting physical constants from experiment. The next sections show how to do this even when the underlying law is not linear at all.

Powers and polynomials

A power function has the form y=axny = a x^n. For whole-number powers this is familiar: x2x^2 is a parabola, symmetric about the vertical axis, and x3x^3 rises steeply on the right and falls steeply on the left. Negative powers describe quantities that fall off with distance. The electrostatic energy of two charges varies as 1/r1/r, the gravitational force as 1/r21/r^2, and the repulsion between two atoms when their electron clouds overlap rises roughly as 1/r121/r^{12} in the Lennard-Jones model. Fractional powers also appear: x1/2=xx^{1/2} = \sqrt x describes, for instance, how far a diffusing molecule typically travels in time xx.

Power functions have a scaling property that makes them special. If y=axny = a x^n and you multiply xx by a factor λ\lambda, then yy is multiplied by λn\lambda^n, whatever the starting value of xx. Doubling the distance between two charges halves their energy, everywhere. This is why power laws describe phenomena that have no built-in length or time scale.

A sum of whole-number powers, p(x)=a0+a1x+a2x2+⋯+aNxNp(x) = a_0 + a_1 x + a_2 x^2 + \dots + a_N x^N, is a polynomial of degree NN. Polynomials are easy to evaluate, differentiate and integrate, and a polynomial of degree NN has at most NN real roots and at most N−1N - 1 turning points. They are the raw material of approximation: Taylor series build complicated functions from polynomials, and the harmonic approximation of a chemical bond replaces its true energy curve by a parabola near the minimum.

Families of functions and what their parameters do

Loading the interactive figure…

Choose a family and move its parameters. The dashed curve is the family’s plain member for comparison. The readouts name the feature each parameter controls: a slope, a half-life, a period, a width.

Exponential functions

Suppose a quantity changes at a rate proportional to how much of it there is. A population of bacteria with plenty of food grows at a rate proportional to the number of bacteria; a sample of radioactive nuclei decays at a rate proportional to the number of nuclei left; the light passing through an absorbing liquid loses intensity at a rate proportional to the intensity. All of these are described by the exponential function:

The defining feature of an exponential is that adding a fixed amount to the input multiplies the output by a fixed factor. For a straight line, each step of one unit in xx adds mm to yy; for an exponential, each step multiplies yy by eke^k. As a result, an exponential always has a fixed doubling time (for growth) or half-life (for decay), equal to ln⁡2/∣k∣\ln 2/|k|. Carbon-14 has a half-life of about 5730 years: after 5730 years half of the original nuclei remain, after 11 460 years a quarter, after 17 190 years an eighth, and so on, regardless of how much you started with. The golden dots in the figure above mark these successive halvings when you select the exponential family.

Why the strange base ee? Any base could be used, since 2x2^x, 10x10^x and exe^x are all exponentials and each can be converted into the others. The number ee is chosen because it makes the mathematics of change simplest: the slope of exe^x at any point equals its height. This is proved in the chapter on derivatives, and it is why nature’s exponentials, which arise from rates proportional to amounts, appear with base ee.

Exponentials are the most important functions in statistical mechanics. The probability that a molecule has energy EE at temperature TT is proportional to the Boltzmann factor e−E/kBTe^{-E/k_BT}, and the rate of a chemical reaction with activation energy EaE_a follows the Arrhenius law k=A e−Ea/kBTk = A\,e^{-E_a/k_BT}. Both are decaying exponentials of energy: each additional kBTk_BT of energy makes a state e≈2.7e \approx 2.7 times less likely. These ideas are developed in the Boltzmann distribution.

Logarithms

The logarithm undoes the exponential. If y=exy = e^x, then x=ln⁡yx = \ln y, the natural logarithm of yy. More generally, log⁡by\log_b y is the power to which the base bb must be raised to give yy, so log⁡101000=3\log_{10} 1000 = 3 because 103=100010^3 = 1000. Since exponentials only produce positive numbers, logarithms are only defined for positive inputs.

Three rules follow directly from the rules for powers, and they are used constantly:

Before electronic calculators, these rules made logarithms the standard tool for multiplying large numbers: look up two logarithms, add them, and look up the number whose logarithm is the sum. Today their importance is different. Logarithms compress enormous ranges into manageable ones. The acidity of a solution spans more than fourteen orders of magnitude in hydrogen-ion concentration, and the pH scale, pH=−log⁡10[H+]\mathrm{pH} = -\log_{10}[\mathrm{H}^+], turns this into numbers between about 0 and 14. Sound intensity in decibels and earthquake magnitudes are logarithmic for the same reason. In statistical mechanics, the entropy S=kBln⁡ΩS = k_B \ln \Omega is the logarithm of a count of microstates that is itself astronomically large, and the free energy F=−kBTln⁡ZF = -k_BT \ln Z is the logarithm of a sum of Boltzmann factors.

A logarithm grows without limit, but extraordinarily slowly: ln⁡x\ln x reaches 10 only when xx exceeds 22 000, and 100 only when xx exceeds 104310^{43}. Each multiplication of xx by ee adds exactly one to ln⁡x\ln x, which is the logarithmic version of the exponential’s constant doubling time.

Logarithmic axes and laws from data

The rules of logarithms give a powerful way of discovering laws in data. Take the logarithm of both sides of a power law, y=Axny = A x^n:

log⁡y=log⁡A+nlog⁡x. \log y = \log A + n \log x .

This is a straight line, not in yy against xx but in log⁡y\log y against log⁡x\log x, with slope nn. Plotting data on log–log axes, where both axes are spaced logarithmically, turns every power law into a straight line, and the slope of the line reveals the power. Similarly, taking the logarithm of an exponential, y=Aekxy = A e^{kx}, gives ln⁡y=ln⁡A+kx\ln y = \ln A + kx, which is a straight line on semi-logarithmic axes with only the vertical axis logarithmic.

The figure below contains real data on the orbits of the eight planets: the size of each orbit (its semi-major axis, in units of the Earth’s) and the time it takes to go round the Sun (in years). On ordinary axes the points form a curve. Switch to log–log axes and they fall on a straight line with slope 1.5. This is Kepler’s third law, T∝a3/2T \propto a^{3/2}, found by Kepler in 1619 by exactly this kind of search for a pattern in numbers, and explained seventy years later by Newton’s law of gravitation. The second data set is a simulated radioactive sample, whose counts fall on a straight line only on semi-log axes, as an exponential should. The third is the repulsive part of the Lennard-Jones potential, a pure power law with slope −12-12.

Straight lines hiding in curved data

Loading the interactive figure…

The dashed line is a least-squares straight-line fit in the axes currently shown; it turns teal when the data really are straight (R² above 0.995). Find the axes that straighten each data set, and read the law from the slope.

This technique is routine in research. The decay of a correlation function is plotted on semi-log axes to extract a relaxation time; the mean-squared displacement of a diffusing particle is plotted on log–log axes to check that it grows linearly with time; the error of a numerical method is plotted against its step size on log–log axes to measure its order of accuracy, as you will see in the chapter on derivatives.

Periodic functions

Many phenomena repeat: vibrations of atoms, alternating currents, the seasons, waves of light. The basic repeating functions are the sine and cosine. Imagine a point moving anticlockwise at a steady rate around a circle of radius 1 centred on the origin. If the angle it has turned through, measured from the positive horizontal axis, is θ\theta, then its height is sin⁡θ\sin\theta and its horizontal position is cos⁡θ\cos\theta. As the point goes round and round, its height rises and falls forever, repeating every full turn of 2π2\pi radians (360°).

A general oscillation has an amplitude, a frequency and a phase:

Angles in science are measured in radians, defined so that an angle of θ\theta radians cuts off an arc of length θ\theta on a circle of radius 1. A full turn is 2π2\pi radians. Radians are not an arbitrary convention: as with the base ee for exponentials, they are the unit for which the derivative of sin⁡θ\sin\theta is exactly cos⁡θ\cos\theta, with no awkward conversion factor.

Sinusoids describe any system that is pulled back towards equilibrium with a force proportional to its displacement, such as a mass on a spring or an atom vibrating in a molecule, as the chapter on motion shows. A remarkable theorem of Fourier says that essentially any periodic signal, however jagged, can be written as a sum of sines and cosines of different frequencies, which is why vibrational spectra, X-ray diffraction and much of signal processing are analysed in terms of sinusoids.

The Gaussian

The last family in this chapter is the bell-shaped Gaussian,

Choose the bell curve in the families figure: the shaded band covers μ±σ\mu \pm \sigma and the teal bar marks the full width at half maximum. The Gaussian is an exponential of a negative square, so it falls to zero much faster than an ordinary exponential. It is the shape of the distribution of velocities of molecules in a gas, of the spread of random measurement errors, of a spectral line broadened by thermal motion, and of the smearing functions used to turn atomic positions into smooth densities in machine-learning descriptors. The chapter on probability explains why it is so common: the sum of many small independent random effects always tends to a Gaussian.

Combining functions

New functions are built from old ones in a few standard ways. They can be added, subtracted, multiplied and divided. They can also be composed: feeding the output of one function into another, written (f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x)). The Gaussian is a composition: first square the input, then multiply by −1/2σ2-1/2\sigma^2, then exponentiate. The Lennard-Jones energy of two atoms, V(r)=4ε[(σ/r)12−(σ/r)6]V(r) = 4\varepsilon[(\sigma/r)^{12} - (\sigma/r)^6], is a difference of two power laws that together produce a potential well with a steep repulsive wall and a gentle attractive tail. Recognising how a complicated function is assembled from simple pieces is usually the key to understanding its graph.

A function that undoes another is its inverse, written f−1f^{-1}. The logarithm is the inverse of the exponential, and the square root (on non-negative numbers) is the inverse of squaring. The graph of an inverse is the mirror image of the original graph in the diagonal line y=xy = x, since inverting a function simply swaps the roles of input and output. A function only has an inverse if it never gives the same output for two different inputs.

Finally, many quantities depend on more than one input. The potential energy of a molecule depends on the positions of all its atoms, and the loss of a neural network on millions of weights. A function of two variables, f(x,y)f(x, y), can be drawn as a surface or as a contour map, and most of the mathematics of this chapter carries over. The chapter on gradients develops these functions of many variables.

Check yourself

  1. Which of these rules is not a function of x?
  2. A sample has a half-life of 3 hours. What fraction remains after 12 hours?
  3. Data plotted on log–log axes fall on a straight line of slope −2. What law do they follow?
  4. The graph of f(x − 3) compared with f(x) is…
  5. What is ln(e⁵ × e²)?

References and further reading

  1. J. Stewart, Calculus: Early Transcendentals, 8th ed., Cengage Learning (2015), chapter 1 (functions and models) and chapter 6 (inverse functions, exponentials and logarithms).
  2. K. F. Riley, M. P. Hobson and S. J. Bence, Mathematical Methods for Physics and Engineering, 3rd ed., Cambridge University Press (2006), chapter 1.
  3. M. L. Boas, Mathematical Methods in the Physical Sciences, 3rd ed., Wiley (2006), chapters 1–2.
  4. J. Kepler, Harmonices Mundi, Linz (1619), book V, where the third law is stated.
  5. S. Strogatz, Infinite Powers: How Calculus Reveals the Secrets of the Universe, Houghton Mifflin Harcourt (2019). A non-technical history of the ideas in this subject.

2Derivatives and Taylor series

The derivative as the slope of a curve seen up close: its definition as a limit, the derivative as a new function, the rules of differentiation, forces as derivatives of energy, optimisation, Taylor series and the harmonic approximation, and how derivatives are computed with finite differences.

about 26 min readBuilds on Functions and their graphs

Average rates of change

A car leaves at noon and has travelled 120 km by 2 pm. Its average speed was 60 km/h. That number is a ratio of two changes, the change in position divided by the change in time, and it says nothing about what happened in between: the car may have waited at traffic lights and then driven at 90 km/h on the motorway. The speedometer, by contrast, shows the speed at this instant. Making the idea of an instantaneous rate precise is the purpose of the derivative, and it is one of the most useful ideas in all of mathematics.

The average rate of change of a function ff between two inputs xx and x+hx + h is

ΔfΔx=f(x+h)−f(x)h. \frac{\Delta f}{\Delta x} = \frac{f(x + h) - f(x)}{h}.

Geometrically this is the slope of the straight line joining the two points (x,f(x))(x, f(x)) and (x+h,f(x+h))(x + h, f(x + h)) on the graph, called a secant line. For a straight-line function the secant slope is the same for every pair of points, which is what makes a line straight. For a curve it depends on which two points you choose.

The derivative as a limit

The way to get an instantaneous rate is to make the interval shorter and shorter. Take the secant line through xx and x+hx + h and slide the second point towards the first. As hh shrinks, the secant line swings round and settles towards a definite line that just grazes the curve at xx: the tangent line. Its slope is the derivative.

There is a second way to see the same thing, and it is the more illuminating one. Zoom in on a smooth curve around a point. As the magnification increases, the curve looks less and less curved, until at high magnification it is indistinguishable from a straight line. The slope of that line is the derivative. A function that has this property at a point, of looking straight when magnified enough, is called differentiable there. Most functions in physics are differentiable almost everywhere; the exceptions are corners (like ∣x∣|x| at zero), jumps and infinite spikes.

A curve seen up close is a straight line

Loading the interactive figure…

Left: a curve with a draggable point and a secant line (rose) through the point and a second point at distance h. Right: the purple box magnified. Increase the magnification and the curve straightens onto its tangent (dashed teal); shrink h and the secant slope converges to the same number.

Try it: with a magnification of a few hundred the orange curve in the right panel lies exactly on the dashed tangent, and as hh is reduced the secant slope agrees with the true derivative to more and more decimal places. Both views describe one fact. Over a small enough region, any smooth function behaves linearly, f(x+h)≈f(x)+f′(x) hf(x + h) \approx f(x) + f'(x)\,h, and the derivative is the coefficient of that linear behaviour.

Let us compute one derivative from the definition. For f(x)=x2f(x) = x^2,

(x+h)2−x2h=x2+2xh+h2−x2h=2x+h  ⟶  2x(h→0). \frac{(x + h)^2 - x^2}{h} = \frac{x^2 + 2xh + h^2 - x^2}{h} = 2x + h \;\longrightarrow\; 2x \quad (h \to 0).

So the slope of the parabola x2x^2 at any point xx is 2x2x: zero at the bottom, positive on the right, negative on the left, and steeper the further you go from the centre.

The derivative as a function

Since the derivative can be computed at every point, it is itself a function, f′(x)f'(x), and it has its own graph. Reading the two graphs together is one of the most important skills in applied mathematics. Where ff is rising, f′f' is positive. Where ff is falling, f′f' is negative. Where ff has a peak or a valley, its tangent is horizontal and f′f' is zero. Such points are called stationary points, and finding them by solving f′(x)=0f'(x) = 0 is how minima and maxima are located.

A function and its derivative

Loading the interactive figure…

Top: the function with a draggable point and its tangent line (teal) and slope triangle. Bottom: the derivative, the slope of the top curve at every x. Violet dots mark stationary points. Tick ‘trace’ and sweep to watch the derivative being drawn as the tangent rolls along the curve.

The derivative of the derivative is the second derivative, f′′(x)=d2f/dx2f''(x) = \mathrm d^2 f/\mathrm dx^2. It measures how the slope itself changes, which is the curvature of the graph. Where f′′>0f'' > 0 the slope is increasing and the curve bends upwards like a cup; where f′′<0f'' < 0 it bends downwards like a cap. This gives a test for stationary points: if f′(x)=0f'(x) = 0 and f′′(x)>0f''(x) > 0, the point is a minimum; if f′′(x)<0f''(x) < 0, it is a maximum. In the figure, tick the box for the second derivative and check that it is positive at the minimum of x3−3xx^3 - 3x and negative at the maximum.

When the variable is time, the derivatives have familiar names. The derivative of position x(t)x(t) is the velocity v=dx/dtv = \mathrm dx/\mathrm dt, and the derivative of velocity is the acceleration a=dv/dt=d2x/dt2a = \mathrm dv/\mathrm dt = \mathrm d^2x/\mathrm dt^2. Newton’s second law, F=maF = ma, is therefore a statement about a second derivative, and the chapter on motion shows how it determines a trajectory.

The rules of differentiation

Computing every derivative from the limit definition would be tedious. Instead, a small number of rules, each proved once from the definition, handle almost every function you will meet. The derivatives of the basic functions are:

function f(x)f(x)derivative f′(x)f'(x)comment
cc (a constant)00a horizontal line has no slope
xnx^nnxn−1n x^{n-1}any real power nn
exe^xexe^xthe exponential is its own slope
ekxe^{kx}kekxk e^{kx}rate proportional to amount
ln⁡x\ln x1/x1/xfor x>0x > 0
sin⁡x\sin xcos⁡x\cos xxx in radians
cos⁡x\cos x−sin⁡x-\sin x

The power rule generalises the calculation done above for x2x^2. The rule for exe^x is the property that singles out the number ee: of all exponentials bxb^x, only exe^x has a slope equal to its height everywhere. For a general base, the derivative of bxb^x is (ln⁡b) bx(\ln b)\,b^x, and ln⁡b=1\ln b = 1 precisely when b=eb = e.

Functions built from these pieces are handled by four combination rules:

The chain rule deserves special emphasis because it is everywhere. If yy depends on uu and uu depends on xx, then a small change in xx changes uu by u′(x)u'(x) times as much, and that change in uu changes yy by y′(u)y'(u) times as much, so dy/dx=(dy/du)(du/dx)\mathrm dy/\mathrm dx = (\mathrm dy/\mathrm du)(\mathrm du/\mathrm dx). For example, the derivative of the Gaussian e−x2/2e^{-x^2/2} is e−x2/2e^{-x^2/2} times the derivative of −x2/2-x^2/2, that is −x e−x2/2-x\,e^{-x^2/2}. The training of every neural network is one enormous application of the chain rule, carried out link by link through the network, which is what backpropagation means.

Forces and equilibrium

In physics the most important derivative is the one connecting energy and force. For a particle moving along a line in a potential energy V(x)V(x), the force is the negative slope of the potential:

Select the atom-pair energy in the derivative figure above. It is the Lennard-Jones potential, in reduced units V(r)=4(r−12−r−6)V(r) = 4(r^{-12} - r^{-6}), which models the interaction of two noble-gas atoms. Using the power rule,

V′(r)=4(−12 r−13+6 r−7), V'(r) = 4\left(-12\,r^{-13} + 6\,r^{-7}\right),

and setting V′(r)=0V'(r) = 0 gives r6=2r^6 = 2, so the equilibrium separation is r0=21/6≈1.122r_0 = 2^{1/6} \approx 1.122 (in units of the atomic diameter σ\sigma), where the energy is V(r0)=−1V(r_0) = -1, the depth of the well. To the left of r0r_0 the slope is negative and the force −V′-V' is positive, pushing the atoms apart; to the right it pulls them together. The second derivative at the minimum, V′′(r0)=72/21/3≈57.1V''(r_0) = 72/2^{1/3} \approx 57.1, is the stiffness of the bond, its effective spring constant. This number sets the frequency at which the atoms vibrate about their equilibrium spacing, which is how derivatives of energy surfaces connect to measured vibrational spectra. The same reasoning in many dimensions gives forces as gradients, developed in the chapter on gradients and used throughout molecular dynamics.

Optimisation

Finding the input that makes a function as small or as large as possible is called optimisation, and it is the central computational task of both chemistry (finding stable structures) and machine learning (finding the best parameters). The calculus answer is to solve f′(x)=0f'(x) = 0 and check the sign of f′′f''. This works beautifully when the derivative can be solved by hand, as for the Lennard-Jones minimum. When it cannot, the derivative still tells you which way to go: if f′(x)>0f'(x) > 0, decreasing xx lowers ff, and if f′(x)<0f'(x) < 0, increasing xx does. Repeatedly stepping a little way in the downhill direction,

xnew=x−η f′(x), x_{\text{new}} = x - \eta\, f'(x),

with a small step size η\eta, walks down to a minimum. This is gradient descent, the optimisation method behind nearly all of deep learning, studied in detail in its own chapter. A faster method for smooth functions is Newton’s method, which uses the second derivative as well: it approximates the function near xx by a parabola and jumps straight to the parabola’s minimum, xnew=x−f′(x)/f′′(x)x_{\text{new}} = x - f'(x)/f''(x). Near a minimum it roughly doubles the number of correct digits at every step.

Taylor series

The idea that a smooth function looks straight up close can be pushed further. A straight line matches a function’s value and slope at a point. A parabola can also match its curvature, a cubic the rate of change of curvature, and so on. Matching all the derivatives at a point aa gives the Taylor series:

To see why the coefficients must be f(n)(a)/n!f^{(n)}(a)/n!, differentiate the polynomial nn times and set x=ax = a: every term except the nn-th vanishes, and the nn-th gives n!n! times its coefficient, which must equal f(n)(a)f^{(n)}(a).

Building a function out of polynomial pieces

Loading the interactive figure…

Orange: the function. Dashed violet: its Taylor polynomial of order N about the blue point a. The teal band marks where the two agree to within 0.01. Press ‘add terms one by one’ and watch the band grow; for ln(1 + x) the series can never reach beyond the singularity at x = −1, however many terms are used.

Some Taylor series are worth knowing by heart, all expanded about a=0a = 0:

ex=1+x+x22!+x33!+⋯ ,sin⁡x=x−x33!+x55!−⋯ ,ln⁡(1+x)=x−x22+x33−⋯ . e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots, \qquad \sin x = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots, \qquad \ln(1 + x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots .

The first two converge for every xx: with enough terms the polynomial matches the function on any finite interval, as the figure shows. The third converges only for −1<x≤1-1 < x \le 1. The distance from the expansion point within which a series converges is its radius of convergence, and it is limited by the nearest point where the function misbehaves, here the logarithm’s singularity at x=−1x = -1.

Truncated Taylor series are behind a large fraction of physical approximations. The small-angle approximation sin⁡θ≈θ\sin\theta \approx \theta is the first-order series, and it turns the pendulum’s nonlinear equation into the simple harmonic oscillator. The harmonic approximation of a chemical bond expands the potential energy about its minimum, where V′(r0)=0V'(r_0) = 0:

V(r)≈V(r0)+12V′′(r0) (r−r0)2, V(r) \approx V(r_0) + \tfrac12 V''(r_0)\,(r - r_0)^2 ,

a parabola with spring constant k=V′′(r0)k = V''(r_0). Choose the bond energy in the figure and set the order to 2: the parabola hugs the true curve near the minimum but misses the soft attractive tail and the steep wall. Adding the cubic term captures the asymmetry, which is what makes real crystals expand when heated. Normal-mode analysis of molecules, phonons in crystals and vibrational spectroscopy are all built on the second-order Taylor expansion of the energy.

Derivatives on a computer

Simulations and data analysis often need derivatives of functions known only as numbers, or as code too complicated to differentiate by hand. The definition suggests an approximation: choose a small but finite hh and compute the forward difference [f(x+h)−f(x)]/h[f(x + h) - f(x)]/h. Taylor’s theorem tells us how accurate this is. Since f(x+h)=f(x)+f′(x)h+12f′′(x)h2+…f(x + h) = f(x) + f'(x) h + \tfrac12 f''(x) h^2 + \dots, the forward difference equals f′(x)+12f′′(x)h+…f'(x) + \tfrac12 f''(x) h + \dots, so its error is proportional to hh. A cleverer formula, the central difference [f(x+h)−f(x−h)]/2h[f(x + h) - f(x - h)]/2h, cancels the h2h^2 terms of the two expansions and has an error proportional to h2h^2, which is far smaller for small hh.

It would seem, then, that the best choice is the smallest possible hh. The figure below shows why this is wrong. It plots the actual error of both formulas for the derivative of sin⁡x\sin x at x=1x = 1, computed in the same floating-point arithmetic a real program uses, against hh on log–log axes.

The error of finite differences against the step size

Loading the interactive figure…

Each curve is computed live in your browser’s double-precision arithmetic (or emulated single precision). Drag across the plot to read off the errors at a chosen h.

For large hh the errors fall along straight lines of slope 1 (forward) and 2 (central): this is the truncation error predicted by the Taylor series. But below a certain hh the curves turn round and the error grows again, becoming erratic. The cause is rounding. A computer stores about 16 significant decimal digits in double precision (the chapter on numbers in a computer explains why). When hh is tiny, f(x+h)f(x + h) and f(x)f(x) agree in almost all their digits, and subtracting them leaves only the few digits that differ, most of which are rounding noise. Dividing by the tiny hh then magnifies that noise. The round-off error grows like ϵ/h\epsilon/h, where ϵ≈2.2×10−16\epsilon \approx 2.2 \times 10^{-16} is the machine precision.

The best step balances the two errors. For the forward difference the optimum is near h≈ϵ≈10−8h \approx \sqrt{\epsilon} \approx 10^{-8}, giving an error around 10−810^{-8}; for the central difference it is near h≈ϵ1/3≈10−5h \approx \epsilon^{1/3} \approx 10^{-5}, giving an error of 10−1010^{-10} or better. Tick the single-precision box, as used on many GPUs, and the optimum moves to much larger hh with an error of only around 10−410^{-4} to 10−710^{-7}. Finite differences therefore never reach full machine precision, and they need two or more function evaluations per input variable, which is prohibitive for a function of a million variables.

Modern machine-learning software sidesteps both problems with automatic differentiation. Rather than approximating, it applies the chain rule exactly to every elementary operation the program performs, carrying derivatives along with values. The result is exact up to rounding, and in its reverse mode the full gradient of a function of any number of variables costs only a small multiple of one function evaluation. It is how neural networks are trained and how differentiable molecular dynamics computes the derivatives of a whole simulation.

Check yourself

  1. What is the derivative of f(x) = 3x⁴ − 2x + 7?
  2. At a point, f′(x) = 0 and f″(x) = −4. What is the point?
  3. Using the chain rule, what is the derivative of e^(−3x²)?
  4. The second-order Taylor expansion of a potential about its minimum gives…
  5. Why does making h extremely small make a finite-difference derivative worse?

References and further reading

  1. J. Stewart, Calculus: Early Transcendentals, 8th ed., Cengage Learning (2015), chapters 2–4 (limits, derivatives, applications) and 11.10 (Taylor series).
  2. S. P. Thompson, Calculus Made Easy, Macmillan (1910); revised edition with M. Gardner, St. Martin’s Press (1998). Still one of the clearest introductions to differentiation.
  3. K. F. Riley, M. P. Hobson and S. J. Bence, Mathematical Methods for Physics and Engineering, 3rd ed., Cambridge University Press (2006), chapters 2 and 4.
  4. W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery, Numerical Recipes: The Art of Scientific Computing, 3rd ed., Cambridge University Press (2007), section 5.7 (numerical derivatives and the choice of step size).
  5. A. G. Baydin, B. A. Pearlmutter, A. A. Radul and J. M. Siskind, “Automatic differentiation in machine learning: a survey”, Journal of Machine Learning Research 18(153), 1–43 (2018).
  6. G. Sanderson (3Blue1Brown), Essence of Calculus, video series (2017), on the derivative as a zoomed-in slope.

3Integrals

The integral as an accumulated total: Riemann sums, signed area, the fundamental theorem of calculus that links integrals to derivatives, standard techniques and the Gaussian integral, integrals in physics such as work and averages, numerical quadrature, and Monte Carlo integration in many dimensions.

about 21 min readBuilds on Derivatives and Taylor series

Accumulation

The derivative answers the question “how fast is this changing?”. The integral answers the reverse question: “if I know how fast something changes at every moment, how much has accumulated in total?”. If a car travels at a constant 60 km/h for 2 hours, it covers 60×2=12060 \times 2 = 120 km. On a graph of speed against time, that product is the area of a rectangle 2 hours wide and 60 km/h tall. The units confirm it: (km/h) × h = km. Area under a rate curve is accumulated quantity.

When the speed varies, the region under the graph is no longer a rectangle, but the principle is the same. Over a short enough time interval the speed is nearly constant, so the distance covered in that interval is nearly speed × duration, the area of a thin vertical strip. Adding up the strips gives the total distance. The integral is the exact value this sum approaches as the strips become infinitely thin.

The same idea appears whenever a total is built from a varying rate or density. The mass of a rod whose density varies along its length is the integral of density over length. The charge that has flowed through a wire is the integral of the current over time. The amount of product formed in a chemical reaction is the integral of the reaction rate. The work done by a varying force is the integral of force over distance. And, as later chapters show, every average and every probability in statistical mechanics is an integral.

Riemann sums and the definite integral

To make this precise, divide the interval from aa to bb into nn strips of equal width Δx=(b−a)/n\Delta x = (b - a)/n. In each strip pick a sample point xix_i, for instance its left edge, and form the sum of rectangle areas:

The figure below draws the sum. With a handful of strips the rectangles overshoot in some places and undershoot in others, and the total is visibly wrong. Press “Double n” a few times and the staircase hugs the curve ever more closely while the error shrinks. For any continuous function the sum converges to the same limit whichever sample points are used, which is why the integral is well defined. The choice still matters for a finite number of strips, as comparing the left-edge, right-edge, midpoint and trapezoid rules shows.

Area as a sum of thin strips

Loading the interactive figure…

Gold strips add positive area, rose strips (where the function is negative) subtract. The exact area is computed separately to high precision. Compare how quickly the error falls as n doubles for each rule.

Notice that where the function is negative, the strips hang below the axis and contribute negative amounts. The definite integral is a signed area: area above the axis counts positively and area below counts negatively. For sin⁡x\sin x over a full period from 00 to 2π2\pi the positive and negative lobes cancel exactly and the integral is zero. Physically this is the right behaviour. If a velocity is negative, the object is moving backwards and the distance it covers should be subtracted from its net displacement.

The fundamental theorem of calculus

Computing integrals as limits of sums is laborious. The great discovery of Newton and Leibniz in the 1660s and 1670s was that integration is the reverse of differentiation, which turns many integrals into a lookup in a table of derivatives.

Fix the lower limit aa and let the upper limit vary. The accumulated area up to xx is then a function of xx,

A(x)=∫axf(t) dt. A(x) = \int_a^x f(t)\,\mathrm dt .

How fast does AA grow as xx increases? Moving the upper limit from xx to x+hx + h adds a thin strip of width hh and height approximately f(x)f(x), so A(x+h)−A(x)≈f(x) hA(x + h) - A(x) \approx f(x)\,h. Dividing by hh and letting h→0h \to 0 gives the theorem:

The figure below makes the theorem visible. The upper panel shades the area under a rate curve up to a moving point xx. The lower panel plots that accumulated area A(x)A(x) as a function of xx. Drag the point and compare the teal tangent to the lower curve with the height of the upper curve: they are always equal. Where the rate is large, AA climbs steeply; where the rate is zero, AA is momentarily flat; where the rate is negative, AA goes down.

An integral accumulating, and its slope

Loading the interactive figure…

Top: the rate f with the area accumulated from the left edge up to x (gold above the axis, rose below). Bottom: the accumulated total A(x); the teal line is its tangent. The readouts confirm that the slope of A at x equals f(x).

With the theorem, integrals become calculations. To find ∫03x2 dx\int_0^3 x^2\,\mathrm dx, look for a function whose derivative is x2x^2: the power rule run backwards gives F(x)=x3/3F(x) = x^3/3. The integral is F(3)−F(0)=9F(3) - F(0) = 9. Every row of the table of derivatives, read from right to left, is an integral:

integrand f(x)f(x)antiderivative F(x)F(x)
xnx^n (n≠−1n \ne -1)xn+1/(n+1)x^{n+1}/(n + 1)
1/x1/xln⁡∣x∣\ln \lvert x\rvert
ekxe^{kx}ekx/ke^{kx}/k
sin⁡x\sin x−cos⁡x-\cos x
cos⁡x\cos xsin⁡x\sin x

Techniques and the Gaussian integral

Two techniques extend the table a long way. Substitution is the chain rule in reverse: if the integrand has the form f(g(x)) g′(x)f(g(x))\,g'(x), then it is the derivative of F(g(x))F(g(x)), so ∫f(g(x))g′(x) dx=F(g(x))\int f(g(x)) g'(x)\,\mathrm dx = F(g(x)). For example, ∫x e−x2/2 dx=−e−x2/2\int x\,e^{-x^2/2}\,\mathrm dx = -e^{-x^2/2}, because the factor xx is (up to sign) the derivative of the exponent. Integration by parts is the product rule in reverse: ∫u v′ dx=uv−∫u′ v dx\int u\,v'\,\mathrm dx = uv - \int u'\,v\,\mathrm dx. It moves a derivative from one factor to the other and is used, among many other places, to derive the virial expression for pressure in molecular simulation.

Not every integral has an antiderivative that can be written with elementary functions. The most important example in all of science is the Gaussian integral. The function e−x2e^{-x^2} has no elementary antiderivative, yet its integral over the whole line has an exact value, found by a beautiful trick: square the integral, write the square as a double integral over the plane, and switch to polar coordinates, where the extra factor of rr makes the integrand the derivative of −12e−r2-\tfrac12 e^{-r^2}.

The normalisation of the normal distribution, the partition function of an ideal gas, the momentum integrals that give the Maxwell–Boltzmann distribution and the zero-point properties of a harmonic oscillator all reduce to this one result. In the strips figure, choose e−x2e^{-x^2}: the area between −3-3 and 33 is already 1.77241.7724, within 0.003 %0.003\,\% of π=1.77245\sqrt\pi = 1.77245, because the tails beyond ∣x∣=3|x| = 3 are tiny.

Integrals in physics

Several integrals recur so often in the physical sciences that they are worth recognising on sight.

The work done by a force F(x)F(x) that moves an object from aa to bb is W=∫abF(x) dxW = \int_a^b F(x)\,\mathrm dx. When the force comes from a potential, F=−dV/dxF = -\mathrm dV/\mathrm dx, the fundamental theorem gives W=−[V(b)−V(a)]W = -[V(b) - V(a)]: the work done by the force equals the drop in potential energy. This is the integral form of the statement that force is the slope of energy, and it is why energy is conserved for such forces. Choose the spring force in the accumulation figure: the accumulated work is a downward parabola, minus the familiar 12kx2\tfrac12 kx^2.

The average of a function over an interval is its integral divided by the length of the interval, fˉ=1b−a∫abf(x) dx\bar f = \frac{1}{b - a}\int_a^b f(x)\,\mathrm dx: the height of the rectangle with the same area. A time average from a simulation is exactly this, with the integral replaced by a sum over saved frames.

A probability density p(x)p(x) describes a continuous random quantity: the probability that it lies between aa and bb is ∫abp(x) dx\int_a^b p(x)\,\mathrm dx, and the total integral is 1. The average of any quantity g(x)g(x) is then ⟨g⟩=∫g(x) p(x) dx\langle g \rangle = \int g(x)\,p(x)\,\mathrm dx. The chapter on probability develops this, and statistical mechanics is built on it: the average energy of a system is an integral of energy weighted by the Boltzmann probability over all positions and momenta.

Integrals extend naturally to several dimensions. The volume under a surface f(x,y)f(x, y) is the double integral ∬f dx dy\iint f\,\mathrm dx\,\mathrm dy, the limit of a sum of thin columns, and the mass of a three-dimensional object is the triple integral of its density over its volume. The partition function of NN classical atoms is an integral over all 3N3N position coordinates and 3N3N momentum coordinates, a 6N6N-dimensional integral. Computing such integrals is the central difficulty of statistical mechanics, and it is the reason for the last section of this topic.

Numerical integration

When an antiderivative is unavailable, or the integrand is known only as data, integrals are computed numerically by a quadrature rule: a weighted sum of function values. The rules in the strips figure are the simplest. Their accuracy can be read off from Taylor’s theorem, just as for finite differences. With nn strips of width hh, the left- and right-edge rules have errors proportional to hh; the midpoint and trapezoid rules, which are symmetric within each strip, have errors proportional to h2h^2; and Simpson’s rule, which fits a parabola through each pair of strips, has error proportional to h4h^4. Doubling nn therefore halves the error of the left-edge rule, quarters that of the trapezoid rule and divides that of Simpson’s rule by sixteen. You can check the first two in the figure: compare the errors before and after pressing “Double n”.

The same ideas, applied to the equations of motion, give the integrators of molecular dynamics. Advancing a trajectory by one time step means integrating the acceleration to update the velocity and integrating the velocity to update the position. The velocity Verlet algorithm used in nearly every MD code is a carefully symmetrised version of the midpoint idea, as the chapter on integrators explains.

Monte Carlo integration

Grid-based quadrature has a fatal weakness in many dimensions. A grid with 10 points along each axis needs 10d10^d function evaluations in dd dimensions: 100 in two dimensions, but 103010^{30} in thirty, far beyond any computer. Phase-space integrals over hundreds of atoms are hopeless on a grid. This exponential cost is called the curse of dimensionality.

The way out is to sample at random. To estimate the area of a region, enclose it in a box of known area, scatter NN random points uniformly in the box, and count the fraction that fall inside. That fraction times the box area estimates the region’s area. The figure below estimates π this way: the quarter-circle of radius 1 has area π/4\pi/4, so four times the fraction of random points inside it approaches π.

Estimating π by throwing random darts

Loading the interactive figure…

Left: random points in the unit square, teal inside the quarter circle and rose outside. Right: the error of the estimate against the number of darts on log–log axes. The dashed line is the 1/√N law predicted by the central limit theorem.

The error falls as 1/N1/\sqrt N: a hundred times more darts buys one more correct digit. That is slow compared with Simpson’s rule in one dimension. The crucial point is that the 1/N1/\sqrt N law does not depend on the dimension at all. The same number of random samples gives the same statistical error whether the integral is over two variables or six thousand. This is why the integrals of statistical mechanics are evaluated by sampling, either with Monte Carlo methods that draw configurations at random with the right probabilities, or with molecular dynamics, which generates them by following the motion. The error bar on every simulated average is ultimately this 1/N1/\sqrt N law, with NN the number of effectively independent samples, as the chapter on probability explains.

Check yourself

  1. A reaction produces product at a rate r(t) in mol/s. What does ∫₀¹⁰ r(t) dt represent?
  2. What is ∫₁³ 2x dx?
  3. ∫₀^{2π} sin x dx equals…
  4. Doubling the number of strips in the trapezoid rule reduces the error by about…
  5. Why is Monte Carlo integration preferred for a 3000-dimensional phase-space integral?

References and further reading

  1. J. Stewart, Calculus: Early Transcendentals, 8th ed., Cengage Learning (2015), chapters 5 and 7 (integrals, the fundamental theorem, techniques and numerical integration).
  2. M. L. Boas, Mathematical Methods in the Physical Sciences, 3rd ed., Wiley (2006), chapter 5 (multiple integrals) and chapter 11 (the gamma and error functions).
  3. W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery, Numerical Recipes: The Art of Scientific Computing, 3rd ed., Cambridge University Press (2007), chapter 4 (integration of functions) and chapter 7 (Monte Carlo integration).
  4. N. Metropolis and S. Ulam, “The Monte Carlo method”, Journal of the American Statistical Association 44, 335–341 (1949).
  5. D. Frenkel and B. Smit, Understanding Molecular Simulation, 3rd ed., Academic Press (2023), chapter 3, on why statistical-mechanical integrals require importance sampling.

Linear algebra and many variables

Vectors, matrices as linear maps, eigenvectors, partial derivatives, gradients and the Hessian.

4Vectors and matrices

Vectors as arrows and as lists of numbers, linear combinations and bases, length and the dot product, matrices as linear transformations of space, matrix multiplication as composition, the determinant as an area factor, linear systems, and eigenvectors and eigenvalues with their uses in vibrations, PCA and neural networks.

about 27 min readBuilds on Functions and their graphs

Vectors

Some quantities are fully described by a single number: a temperature, a mass, an energy. These are scalars. Others need a direction as well as a size. A displacement of 3 m means nothing until you say which way; a force of 10 N pushes in some particular direction; the velocity of an atom has a speed and a heading. Such quantities are vectors.

A vector can be pictured as an arrow. Its length is the vector’s size, or magnitude, and its direction is where it points. Two arrows with the same length and direction represent the same vector wherever they are drawn. The same vector can also be written as a list of numbers. Draw the arrow starting at the origin of a coordinate system; the coordinates of its tip are the vector’s components. In two dimensions,

v=(vxvy), \mathbf v = \begin{pmatrix} v_x \\ v_y \end{pmatrix},

and in three dimensions a third component vzv_z is added. Vectors are written in bold, v\mathbf v, or with an arrow, v⃗\vec v.

The two pictures, arrow and list, are equally important. The arrow is how we think about vectors geometrically; the list is how we compute with them, and how a computer stores them. The power of linear algebra comes from moving freely between the two.

The list picture also frees vectors from the three dimensions of physical space. The positions of all NN atoms in a molecule form a single vector with 3N3N components, a point in a 3N3N-dimensional configuration space. A material can be described by a vector of features: its composition, density, band gap and so on. An image of 28×2828 \times 28 pixels is a vector with 784 components. We cannot draw arrows in 784 dimensions, but every rule in this chapter works in any number of dimensions, and the two-dimensional pictures are faithful guides to what happens there.

Adding and scaling vectors

Two operations define what a vector is. Addition is done component by component, a+b=(ax+bx, ay+by)\mathbf a + \mathbf b = (a_x + b_x,\, a_y + b_y), and geometrically it means placing the arrows tip to tail: walk along a\mathbf a, then along b\mathbf b, and the sum is the arrow from start to finish. Because it does not matter which arrow is walked first, the two routes form a parallelogram whose diagonal is the sum. Forces combine this way: the net force on an atom is the vector sum of the forces from all its neighbours.

Scalar multiplication multiplies every component by the same number, c a=(c ax, c ay)c\,\mathbf a = (c\,a_x,\, c\,a_y). Geometrically it stretches the arrow by the factor cc, and a negative cc also reverses its direction. Subtraction combines the two: a−b=a+(−1)b\mathbf a - \mathbf b = \mathbf a + (-1)\mathbf b, the arrow from the tip of b\mathbf b to the tip of a\mathbf a. The vector from atom ii to atom jj is rj−ri\mathbf r_j - \mathbf r_i, and its length is the interatomic distance.

Vectors as arrows and as numbers

Loading the interactive figure…

Drag the tips of a and b. Choose an operation and compare the arrow construction with the components listed below. In dot-product mode the thick bar is the projection of b onto a’s direction: teal when it points along a, rose when it points against it.

A sum of scaled vectors, c1a+c2bc_1\mathbf a + c_2\mathbf b, is called a linear combination. Every vector in the plane can be written as a linear combination of the two unit vectors i^=(1,0)\hat{\mathbf i} = (1, 0) and j^=(0,1)\hat{\mathbf j} = (0, 1): the components of a vector are simply the coefficients, v=vxi^+vyj^\mathbf v = v_x\hat{\mathbf i} + v_y\hat{\mathbf j}. A set of vectors from which every vector can be built in exactly one way is a basis, and the number of vectors in a basis is the dimension of the space. The pair i^,j^\hat{\mathbf i}, \hat{\mathbf j} is the standard basis, but any two vectors that do not point along the same line also form a basis of the plane. Crystallographers use the three lattice vectors of a unit cell as a basis, so that atomic positions are given as fractional coordinates along the cell edges.

Length and the dot product

The length of a vector follows from Pythagoras’ theorem: ∣v∣=vx2+vy2|\mathbf v| = \sqrt{v_x^2 + v_y^2}, with a third term vz2v_z^2 in three dimensions and nn terms in nn dimensions. A vector of length 1 is a unit vector, and any non-zero vector can be turned into one by dividing it by its length.

The most useful way of combining two vectors into a number is the dot product:

The equality of the two formulas, one algebraic and one geometric, is the law of cosines in disguise. It means the angle between two vectors can be computed from their components: cos⁡θ=a⋅b/(∣a∣∣b∣)\cos\theta = \mathbf a \cdot \mathbf b / (|\mathbf a||\mathbf b|). Bond angles in a molecule are computed exactly this way from atomic coordinates.

The geometric meaning is a projection. a⋅b/∣a∣\mathbf a \cdot \mathbf b / |\mathbf a| is the length of the shadow that b\mathbf b casts onto the line through a\mathbf a. In the figure, select the dot product and rotate b\mathbf b around: the shadow is longest when the vectors are aligned, vanishes when they are perpendicular, and becomes negative when b\mathbf b points backwards. Physics uses this constantly. The work done by a force F\mathbf F over a displacement d\mathbf d is F⋅d\mathbf F \cdot \mathbf d, since only the component of the force along the motion does work. Machine learning uses it as a measure of similarity: two feature vectors with a large positive dot product point in similar directions, and the cosine similarity cos⁡θ\cos\theta compares them regardless of their lengths. The attention mechanism of transformers decides how much one word should attend to another by taking dot products of vectors that represent them.

Matrices as transformations

A matrix is a rectangular array of numbers. Matrices can be thought of as tables of data, but their real meaning is as linear transformations: functions that take a vector in and give a vector out, while keeping straight lines straight, keeping the origin fixed and keeping evenly spaced grid lines evenly spaced. Rotations, stretches, reflections and shears are all linear transformations.

The key observation is that a linear transformation is completely determined by what it does to the basis vectors. Since every vector is v=xi^+yj^\mathbf v = x\hat{\mathbf i} + y\hat{\mathbf j}, and a linear transformation respects addition and scaling, the image of v\mathbf v must be xx times the image of i^\hat{\mathbf i} plus yy times the image of j^\hat{\mathbf j}. Write the images of i^\hat{\mathbf i} and j^\hat{\mathbf j} as the two columns of a matrix, and the rule for multiplying a matrix by a vector follows:

The figure below shows a matrix acting on the entire plane at once. The faint grey grid is the original plane; the blue grid is where every grid line goes. Drag the green and red arrows, which are the two columns of the matrix, and the whole grid follows. Try the presets and press play to watch the plane morph from its original state into the transformed one.

A matrix moves the whole plane

Loading the interactive figure…

Green î′ and red ĵ′ are the matrix columns; blue grid lines show where the original grid goes; the gold parallelogram is the image of the unit square, labelled with its area. The blue vector v and its image Mv (orange) show the column construction: x steps along î′ then y steps along ĵ′.

Several transformations are worth recognising by their matrices. The identity (1001)\begin{pmatrix}1&0\\0&1\end{pmatrix} does nothing. A rotation by an angle θ\theta is (cos⁡θ−sin⁡θsin⁡θcos⁡θ)\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix}, since it sends i^\hat{\mathbf i} to (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta). A diagonal matrix (sx00sy)\begin{pmatrix}s_x&0\\0&s_y\end{pmatrix} stretches each axis separately. The shear (1101)\begin{pmatrix}1&1\\0&1\end{pmatrix} slides each horizontal line sideways by an amount proportional to its height, as when a deck of cards is pushed; it is the transformation of a simulation box under shear flow. And a matrix whose two columns lie on the same line squashes the whole plane onto that line.

Composition and matrix multiplication

Applying one transformation and then another is again a linear transformation, so it has a matrix of its own. That matrix is the product of the two. If A\mathbf A is applied first and B\mathbf B second, the combined transformation is BA\mathbf B\mathbf A (read from right to left, like function composition f(g(x))f(g(x))). To find its columns, apply B\mathbf B to each column of A\mathbf A; the familiar rule “row times column” for each entry follows directly.

Two consequences are important. Matrix multiplication is not commutative: in general AB≠BA\mathbf A\mathbf B \ne \mathbf B\mathbf A, because rotating and then shearing is not the same as shearing and then rotating. Try it with a book on a table. And a transformation that can be undone has an inverse matrix A−1\mathbf A^{-1} with A−1A=I\mathbf A^{-1}\mathbf A = \mathbf I, the identity. For a 2×22 \times 2 matrix,

(abcd)−1=1ad−bc(d−b−ca), \begin{pmatrix} a & b \\ c & d \end{pmatrix}^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix},

which exists only when ad−bc≠0ad - bc \ne 0. That quantity has a geometric meaning of its own.

The determinant

Watch the gold square in the transformation figure. It starts as the unit square with area 1, and the matrix turns it into a parallelogram. Every other region of the plane is scaled by the same factor, since the transformed grid cells are all identical parallelograms. That factor is the determinant:

In the figure, the rotations have determinant 1 (areas are preserved), the stretch has determinant 1 because it doubles one axis and halves the other, the reflection has −1-1, and “squash flat” has 0: the parallelogram collapses to a line. In three dimensions the determinant of the matrix whose columns are the lattice vectors of a crystal is the volume of the unit cell, which is how simulation codes compute the volume of a triclinic box. In statistical mechanics, determinants appear whenever variables are changed in an integral, as the factor that accounts for how the change stretches volume elements.

Linear systems

Many problems ask the reverse question: given the output b\mathbf b, which input x\mathbf x produced it? That is, solve Mx=b\mathbf M\mathbf x = \mathbf b. Written out, this is a set of simultaneous linear equations. If det⁡M≠0\det\mathbf M \ne 0 the transformation can be undone and there is exactly one solution, x=M−1b\mathbf x = \mathbf M^{-1}\mathbf b. If det⁡M=0\det\mathbf M = 0, the plane has been squashed, so most b\mathbf b cannot be reached at all and those that can are reached by infinitely many x\mathbf x.

In practice, inverses are rarely computed. Computers solve linear systems by Gaussian elimination, subtracting multiples of equations from one another until the system becomes triangular and can be solved from the bottom up; in matrix language this is a factorisation M=LU\mathbf M = \mathbf L\mathbf U into lower- and upper-triangular pieces. Fitting a straight line or a polynomial to data by least squares, the first algorithm of machine learning, comes down to solving a linear system, the normal equations XTX w=XTy\mathbf X^{\mathsf T}\mathbf X\,\mathbf w = \mathbf X^{\mathsf T}\mathbf y. Gaussian processes, Kriging and kernel methods require solving systems with thousands of unknowns, and the Cholesky factorisation used for them is a specialised version of elimination for symmetric matrices.

Eigenvectors and eigenvalues

Most vectors are knocked off their line by a transformation: in the figure, the blue vector v\mathbf v and its image Mv\mathbf M\mathbf v usually point in different directions. But for many matrices there are special directions that are only stretched or shrunk, never turned. A vector u\mathbf u along such a direction satisfies

Tick “show eigenvectors” in the transformation figure. For the symmetric matrix (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix}, the two dashed purple lines along the diagonals are the eigen-directions: vectors on the line y=xy = x are stretched by 3, vectors on the line y=−xy = -x are left alone (eigenvalue 1), and every other vector is a mixture and gets turned. The shear has only one eigen-direction, the horizontal axis. A rotation by 90° has none at all: every direction is turned, and the eigenvalues are complex numbers. Tick “what happens to a circle” as well, and you will see the unit circle become an ellipse whose axes, for a symmetric matrix, lie exactly along the eigenvectors, with semi-axes equal to the eigenvalues.

Symmetric matrices, those equal to their own transpose (Mij=MjiM_{ij} = M_{ji}), are especially well behaved, and they are the ones that matter most in science. Their eigenvalues are always real, and their eigenvectors can always be chosen to be mutually perpendicular. Using the eigenvectors as a new basis, the matrix becomes diagonal: the transformation is just a stretch along each eigen-direction. This spectral decomposition, M=QΛQT\mathbf M = \mathbf Q\boldsymbol\Lambda\mathbf Q^{\mathsf T} with Q\mathbf Q the matrix of eigenvectors and Λ\boldsymbol\Lambda the diagonal matrix of eigenvalues, appears across the sciences:

The normal modes of a molecule are the eigenvectors of its Hessian, the matrix of second derivatives of the energy with respect to atomic positions (mass-weighted), and the eigenvalues give the squared vibrational frequencies. A negative eigenvalue signals a saddle point rather than a minimum, which is how transition states are recognised, as the chapter on gradients shows.

Principal component analysis finds the eigenvectors of the covariance matrix of a data set. The first eigenvector is the direction in which the data vary most, and projecting onto the first few eigenvectors gives the best low-dimensional summary of high-dimensional data, whether those are atomic trajectories or materials properties.

In quantum mechanics the allowed energies of a system are the eigenvalues of its Hamiltonian matrix, and the stationary states are its eigenvectors. Every electronic-structure calculation, from a Hückel model of benzene to density-functional theory, is at its heart a large symmetric eigenvalue problem.

Matrices in machine learning and simulation

A single layer of a neural network takes an input vector x\mathbf x, multiplies it by a weight matrix W\mathbf W, adds a bias vector and applies a nonlinear function to each component: h=σ(Wx+b)\mathbf h = \sigma(\mathbf W\mathbf x + \mathbf b). The matrix rotates, stretches and shears the input space; the nonlinearity bends it. Stacking such layers lets a network reshape space until classes that were tangled become separable, as the chapter on how networks bend space shows with the same kind of grid picture used above. Training a network means adjusting the entries of these matrices, and running one means doing matrix multiplications, which is why graphics processors, built to multiply matrices quickly, became the engines of deep learning.

In molecular simulation, the simulation box is described by a 3×33 \times 3 cell matrix whose columns are the box vectors; converting between Cartesian and fractional coordinates is multiplication by this matrix or its inverse, and the box volume is its determinant. Rotation matrices orient molecules, the stress and strain of a material are 3×33 \times 3 symmetric matrices whose eigenvectors are the principal axes, and the moment of inertia tensor, diagonalised, gives a molecule’s principal axes of rotation. In all of these the same few ideas recur: a matrix is a transformation, its columns are the images of the basis, its determinant scales volume, and its eigenvectors are the directions it leaves unturned.

Check yourself

  1. What is (1, 2, 3) · (4, −5, 6)?
  2. The matrix [[0, −1], [1, 0]] sends î = (1, 0) to…
  3. A 2 × 2 matrix has determinant −2. What does it do to the unit square?
  4. For M = [[3, 0], [0, 0.5]], which vectors are eigenvectors?
  5. Why must the vibrational frequencies from a Hessian be computed from its eigenvalues rather than its diagonal elements?

References and further reading

  1. G. Strang, Introduction to Linear Algebra, 5th ed., Wellesley-Cambridge Press (2016). The standard introduction, built around the column picture of matrix–vector multiplication.
  2. G. Sanderson (3Blue1Brown), Essence of Linear Algebra, video series (2016). The geometric picture of matrices as transformations used in this topic.
  3. M. P. Deisenroth, A. A. Faisal and C. S. Ong, Mathematics for Machine Learning, Cambridge University Press (2020), chapters 2–4. Free online.
  4. S. Axler, Linear Algebra Done Right, 3rd ed., Springer (2015). A rigorous treatment centred on linear maps and eigenvalues.
  5. G. H. Golub and C. F. Van Loan, Matrix Computations, 4th ed., Johns Hopkins University Press (2013), on how linear systems and eigenvalue problems are solved numerically.
  6. E. B. Wilson, J. C. Decius and P. C. Cross, Molecular Vibrations: The Theory of Infrared and Raman Vibrational Spectra, McGraw-Hill (1955); Dover reprint (1980), for normal modes as eigenvectors.

5Gradients and the Hessian

Calculus for functions of many variables: contour maps, partial derivatives as slopes of slices, the gradient vector and directional derivatives, forces as negative gradients of the energy, the Hessian and the classification of minima, maxima and saddle points, steepest-descent paths, and the multivariable chain rule.

about 21 min readBuilds on Derivatives and Taylor series, Vectors and matrices

Functions of several variables

The energy of a diatomic molecule depends on one number, the bond length. The energy of a water molecule depends on two bond lengths and an angle, and the energy of a protein on the positions of thousands of atoms. The loss of a neural network depends on every one of its weights, often millions of them. Almost every function that matters in simulation and machine learning has many inputs, and this chapter extends the calculus of derivatives to them.

A function of two variables, f(x,y)f(x, y), assigns a number to each point of the plane. It can be drawn as a surface hovering over the plane, with height f(x,y)f(x, y) above each point, which is why such functions are often called landscapes. A surface is hard to read on a flat page, so the more useful picture is a contour map, like the maps hikers use. Each contour line joins points of equal height, f(x,y)=constantf(x, y) = \text{constant}. Where contours crowd together the ground is steep; where they are far apart it is nearly flat. Closed rings of contours surround summits and valley floors, and a figure-of-eight pattern marks a mountain pass.

An energy landscape and its gradient

Loading the interactive figure…

Colour shows height (pale is low, intense is high) and dark lines are contours. Drag the blue walker. Teal: the gradient ∇f, pointing straight uphill. Orange: −∇f, the steepest way down. Violet: a compass direction u, whose slope is the dot product ∇f · u.

The landscapes in the figure are chosen because they have the features found on real potential energy surfaces. The bowl has a single minimum. The long curved valley is gently sloped along its floor and very steep across it, a shape that defeats simple optimisation methods. The landscape with two valleys has two stable states separated by a pass, the simplest model of a chemical reaction or a structural transition. The three-basin landscape is a sum of Gaussian wells in the spirit of the Müller–Brown surface, a standard two-dimensional test problem for reaction-path methods.

Partial derivatives

The ordinary derivative measures how a function changes when its single input changes. With two inputs, there are many directions in which to move. The simplest are along the coordinate axes. Hold yy fixed and let only xx vary: the function becomes an ordinary function of one variable, whose graph is a slice through the landscape along the xx direction. Its slope is the partial derivative with respect to xx:

For example, if f(x,y)=x2y+3y2f(x, y) = x^2 y + 3y^2, then treating yy as a constant gives ∂f/∂x=2xy\partial f/\partial x = 2xy, and treating xx as a constant gives ∂f/∂y=x2+6y\partial f/\partial y = x^2 + 6y. Tick “slices” in the figure: the two small panels show the landscape cut along xx and along yy through the walker, each with its tangent line. The slopes of those tangents are the two partial derivatives.

Partial derivatives can themselves be differentiated, giving second partial derivatives such as ∂2f/∂x2\partial^2 f/\partial x^2, ∂2f/∂y2\partial^2 f/\partial y^2 and the mixed derivative ∂2f/∂x ∂y\partial^2 f/\partial x\,\partial y. For any smooth function the mixed derivatives do not depend on the order of differentiation, ∂2f/∂x ∂y=∂2f/∂y ∂x\partial^2 f/\partial x\,\partial y = \partial^2 f/\partial y\,\partial x. This innocent-looking fact, Schwarz’s theorem, is the origin of the Maxwell relations of thermodynamics, which relate measurable quantities such as (∂S/∂V)T=(∂P/∂T)V(\partial S/\partial V)_T = (\partial P/\partial T)_V.

The gradient

Collect the partial derivatives into a vector and something remarkable appears. The gradient of ff is

The directional derivative formula comes from the linear approximation. Near a point, a smooth landscape looks like a tilted plane: moving by a small displacement (Δx,Δy)(\Delta x, \Delta y) changes ff by approximately ∂f∂xΔx+∂f∂yΔy=∇f⋅Δr\frac{\partial f}{\partial x}\Delta x + \frac{\partial f}{\partial y}\Delta y = \nabla f \cdot \Delta\mathbf r. Walking a distance ss in the direction u\mathbf u means Δr=s u\Delta\mathbf r = s\,\mathbf u, so the slope is ∇f⋅u\nabla f \cdot \mathbf u.

Three geometric facts follow from ∇f⋅u=∣∇f∣cos⁡θ\nabla f \cdot \mathbf u = |\nabla f|\cos\theta, and you can verify each in the figure by turning the violet compass arrow. The slope is largest when u\mathbf u points along ∇f\nabla f (θ=0\theta = 0): the gradient points in the direction of steepest ascent, and its length ∣∇f∣|\nabla f| is that steepest slope. The slope is most negative in the opposite direction, −∇f-\nabla f, the direction of steepest descent. And the slope is zero when u\mathbf u is perpendicular to ∇f\nabla f: walking at right angles to the gradient keeps you at constant height, so the gradient is perpendicular to the contour lines. Tick “arrow field” to see −∇f-\nabla f drawn across the whole map: the arrows cut every contour at right angles and grow longer where the contours crowd together.

Forces as gradients

For a system of atoms with potential energy V(r1,…,rN)V(\mathbf r_1, \dots, \mathbf r_N), the force on atom ii is the negative gradient of the energy with respect to that atom’s position:

This is the many-dimensional version of F=−dV/dxF = -\mathrm dV/\mathrm dx, and it is the single most important formula in molecular simulation: every MD step computes it. For a pair potential that depends only on the distance rij=∣rj−ri∣r_{ij} = |\mathbf r_j - \mathbf r_i| between two atoms, the chain rule gives Fi=V′(rij) (rj−ri)/rij\mathbf F_i = V'(r_{ij})\,(\mathbf r_j - \mathbf r_i)/r_{ij}, a force along the line joining the atoms, and the force on atom jj is exactly opposite: Newton’s third law follows automatically from the energy depending only on distances. It is also why machine-learned interatomic potentials are built to predict an energy and then obtain forces by differentiating it, which guarantees that the forces are consistent with energy conservation, as the chapter on MLIPs explains.

Stationary points and the Hessian

Points where the gradient vanishes are stationary points. In one dimension they are minima or maxima. In two or more dimensions there is a third possibility, the saddle point: a minimum along one direction and a maximum along another, like the middle of a horse’s saddle or the highest point of a mountain pass on the lowest route between two valleys. Drag the walker to the middle of the two-valley landscape, at the origin: the gradient arrows disappear, yet the point is clearly neither a summit nor a valley floor.

To tell these apart one needs the second derivatives, arranged in a symmetric matrix called the Hessian:

The second formula is the Taylor series in several variables. At a stationary point the gradient term vanishes and the landscape is locally a quadratic bowl, dome or saddle described by H\mathbf H. Because H\mathbf H is symmetric, it has real eigenvalues and perpendicular eigenvectors (the chapter on matrices explains why). Tick “curvature” in the figure: the walker now carries two perpendicular bars along the Hessian’s eigenvectors, gold for positive curvature and rose for negative. At the bottom of either valley of the two-valley landscape both bars are gold, with eigenvalues 8 and 2.4. At the pass the bar across the valley is rose with eigenvalue −4-4 and the bar along it is gold: one downhill direction, the signature of a saddle.

In chemistry these ideas have direct physical meaning. Minima of the potential energy surface are stable or metastable structures. The eigenvalues of the mass-weighted Hessian at a minimum are the squares of the vibrational frequencies, and the eigenvectors are the normal modes. A first-order saddle point, with exactly one negative eigenvalue, is a transition state: the highest point on the lowest-energy path between two minima. Its energy above the reactant minimum is the activation barrier that enters the Arrhenius law, and the eigenvector of its negative eigenvalue points along the reaction coordinate.

Following the gradient downhill

A ball released on a landscape, with enough friction that it never builds up speed, follows the steepest-descent direction −∇f-\nabla f at every moment. Its path is a steepest-descent path, and it ends at a minimum (or, if released exactly on a ridge, at a saddle). Press “roll downhill” in the figure from several starting points. Every start on one side of the pass ends in one valley and every start on the other side ends in the other: the set of starting points that drain into a minimum is that minimum’s basin of attraction, and the boundary between basins runs through the saddle.

Turning this into an algorithm gives gradient descent: repeat r←r−η ∇f(r)\mathbf r \leftarrow \mathbf r - \eta\,\nabla f(\mathbf r) with a small step η\eta. It is the method used to minimise the energy of a structure before a simulation, and, with the energy replaced by a loss function, the method by which every neural network is trained (the chapter on gradient descent). Its weakness shows on the long curved valley. There the gradient points mostly across the valley rather than along it, because the walls are much steeper than the floor, so steepest descent zigzags from wall to wall and crawls along the floor. The cause is visible in the Hessian: its eigenvalues differ by a large factor, a situation called ill-conditioning. Better methods either rescale the directions (Newton’s method uses H−1∇f\mathbf H^{-1}\nabla f, and quasi-Newton methods such as L-BFGS build up an approximation to it) or add momentum, which averages out the zigzag.

The steepest-descent path from a transition state down into the two minima it connects is the minimum energy path, also called the intrinsic reaction coordinate. Methods such as the nudged elastic band find it by relaxing a chain of images of the system towards the path, and it provides the reaction coordinate for many rate calculations.

The multivariable chain rule

Suppose a quantity ff depends on xx and yy, and both depend on time: f(x(t),y(t))f(x(t), y(t)). The rate of change of ff along the path collects a contribution from each input:

dfdt=∂f∂xdxdt+∂f∂ydydt=∇f⋅drdt. \frac{\mathrm d f}{\mathrm d t} = \frac{\partial f}{\partial x}\frac{\mathrm dx}{\mathrm dt} + \frac{\partial f}{\partial y}\frac{\mathrm dy}{\mathrm dt} = \nabla f \cdot \frac{\mathrm d\mathbf r}{\mathrm dt}.

The rate of change is the gradient dotted with the velocity. Applied to the potential energy of a moving system, dV/dt=∇V⋅v=−F⋅v\mathrm dV/\mathrm dt = \nabla V\cdot\mathbf v = -\mathbf F\cdot\mathbf v, the negative of the power delivered by the forces, which is half of the proof that total energy is conserved in classical mechanics.

When many variables depend on many others, the partial derivatives of the outputs with respect to the inputs form a matrix, the Jacobian, and the chain rule becomes multiplication of Jacobians. A neural network is a long chain of such dependencies, layer by layer, and the gradient of its loss with respect to the weights of an early layer is a product of Jacobians of all the later layers. Computing that product efficiently from the output end backwards is backpropagation, developed in its own chapter. Every gradient used in modern machine learning, and every force in a machine-learned potential, is computed this way.

Check yourself

  1. For f(x, y) = x²y + 3y², what is ∂f/∂y at (1, 2)?
  2. On a contour map, in which direction does the gradient point?
  3. The gradient at a point is (3, 4). What is the slope in the direction u = (0, 1)?
  4. At a stationary point the Hessian has eigenvalues 5.2 and −1.3. The point is…
  5. Why does steepest descent zigzag in a long narrow valley?

References and further reading

  1. J. Stewart, Calculus: Early Transcendentals, 8th ed., Cengage Learning (2015), chapter 14 (partial derivatives, gradients, directional derivatives, extrema).
  2. K. F. Riley, M. P. Hobson and S. J. Bence, Mathematical Methods for Physics and Engineering, 3rd ed., Cambridge University Press (2006), chapters 5 and 10.
  3. D. J. Wales, Energy Landscapes: Applications to Clusters, Biomolecules and Glasses, Cambridge University Press (2003). Stationary points, basins and pathways on potential energy surfaces.
  4. K. Müller and L. D. Brown, “Location of saddle points and minimum energy paths by a constrained simplex optimization procedure”, Theoretica Chimica Acta 53, 75–93 (1979).
  5. G. Henkelman, B. P. Uberuaga and H. Jónsson, “A climbing image nudged elastic band method for finding saddle points and minimum energy paths”, Journal of Chemical Physics 113, 9901–9904 (2000).
  6. J. Nocedal and S. J. Wright, Numerical Optimization, 2nd ed., Springer (2006), chapters 2–3 and 6–7 (line search, Newton and quasi-Newton methods, L-BFGS).

Probability and statistics

Random variables, distributions, expectation and variance, the central limit theorem and Bayes' theorem.

6Probability and statistics

Probability as long-run frequency and as degree of belief; the sum and product rules; random variables, binomial, normal, Poisson and exponential distributions; expectation and variance; the central limit theorem and error bars; Bayes' theorem; and estimating averages and their uncertainty from correlated simulation data.

about 24 min readBuilds on Integrals

Chance and frequency

Toss a coin and you cannot say whether it will land heads. Toss it ten thousand times and you can say with great confidence that close to half the tosses will be heads. This combination, unpredictability in each case and regularity in the aggregate, is what probability theory describes, and it is everywhere in science. A single molecule in a gas moves erratically, but the pressure of 102310^{23} molecules is steady. A single radioactive nucleus decays at an unpredictable moment, but a sample decays with a precise half-life. A single run of a stochastic training algorithm gives a slightly different network each time, but the distribution of results is reproducible.

The probability of an outcome is the fraction of times it occurs in a long sequence of identical, independent trials. This frequency interpretation is made precise by the law of large numbers: as the number of trials grows, the observed fraction converges to the probability. The convergence is not smooth. The figure below tracks the running fraction of heads, or of sixes, as trials accumulate. Early on it swings wildly; later it settles into a funnel that narrows steadily.

Frequencies settling down to probabilities

Loading the interactive figure…

Left: how often each outcome has occurred, with gold marks at the true probabilities. Right: the running fraction of the watched outcome against the number of trials on a logarithmic axis. The violet funnel is ±2 standard deviations, √(p(1 − p)/n), which narrows as 1/√n.

The width of the funnel is the first appearance of a law that governs all statistical estimates: the typical deviation of an observed fraction from the true probability is p(1−p)/n\sqrt{p(1 - p)/n}, shrinking in proportion to 1/n1/\sqrt n. Ten times more data narrows the uncertainty by only a factor of 10≈3.2\sqrt{10} \approx 3.2. The loaded die in the figure shows why this matters. After 30 rolls, the extra sixes may not yet be distinguishable from chance; after 1000 they are unmistakable.

Probability has a second interpretation, as a degree of belief given the available information. “The probability that this candidate material is stable is 30 %” is not a statement about repeated trials, since the material either is or is not stable; it expresses uncertainty. This Bayesian interpretation obeys exactly the same mathematical rules, and it is the natural language for inference from data, for uncertainty in machine-learning predictions (Gaussian processes) and for deciding which experiment to do next (active learning).

The rules of probability

Probabilities are numbers between 0 (impossible) and 1 (certain). Two rules generate everything else.

For a fair die, the probability of rolling a 1 or a 2 is 1/6+1/6=1/31/6 + 1/6 = 1/3, because the outcomes are exclusive. The probability of two sixes in two independent rolls is 1/6×1/6=1/361/6 \times 1/6 = 1/36. The probability that a roll is even given that it is greater than 3 is 2/32/3, because of the three outcomes 4, 5 and 6, two are even.

When all outcomes are equally likely, a probability is a count: the number of favourable outcomes divided by the total number. Counting is therefore central, and its basic tool is the number of ways of choosing kk items from nn without regard to order,

(nk)=n!k! (n−k)!. \binom{n}{k} = \frac{n!}{k!\,(n - k)!}.

There are (103)=120\binom{10}{3} = 120 ways to choose which 3 of 10 coin tosses are heads. Statistical mechanics is built on this kind of counting: the entropy of an Einstein solid is the logarithm of the number of ways of distributing energy quanta among oscillators.

Random variables and distributions

A random variable is a number whose value is determined by chance: the number of heads in ten tosses, the speed of a randomly chosen molecule, the error in a measurement. Its behaviour is described by its distribution, which says how probable each value is.

For a random variable that takes separate values, the distribution is a list of probabilities P(k)P(k) that add up to 1. The most important example is the binomial distribution: the number of successes kk in nn independent trials, each succeeding with probability pp, has

A Galton board makes the binomial distribution physical. Balls fall through rows of pegs; at each peg a ball bounces left or right at random. The bin a ball lands in is the number of rightward bounces, a binomial random variable with nn equal to the number of rows. Each individual path is unpredictable, yet the bins fill into a precise shape.

A Galton board

Loading the interactive figure…

Each ball makes one left-or-right choice per row. The histogram shows the fraction of balls in each bin; the gold line is the binomial prediction and the dashed rose curve a bell curve with the same mean and spread. Change the bias p and the number of rows.

As the number of rows grows, the binomial histogram approaches a smooth bell curve, even when p≠1/2p \neq 1/2 and the histogram is lopsided at first. This is a first glimpse of the central limit theorem, discussed below.

Many random variables are continuous: a speed or a position can take any value in a range. The probability of any one exact value is then zero, and the distribution is described by a probability density p(x)p(x), defined so that the probability of a value between xx and x+dxx + \mathrm dx is p(x) dxp(x)\,\mathrm dx. Probabilities become areas under the density, P(a≤X≤b)=∫abp(x) dxP(a \le X \le b) = \int_a^b p(x)\,\mathrm dx, and the total area is 1, as described in the chapter on integrals. A density can exceed 1, since it is a probability per unit of xx; only its integral is bounded.

Expectation and variance

Two numbers summarise most distributions. The expectation or mean is the probability-weighted average of the values, and the variance is the expected squared distance from the mean:

The mean is linear: ⟨aX+bY⟩=a⟨X⟩+b⟨Y⟩\langle aX + bY\rangle = a\langle X\rangle + b\langle Y\rangle for any random variables, independent or not. The variance is not linear, but for independent variables the variances add: Var(X+Y)=Var(X)+Var(Y)\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y). This one rule explains the 1/n1/\sqrt n law. The sum of nn independent copies of a variable with variance σ2\sigma^2 has variance nσ2n\sigma^2, so their average, which is the sum divided by nn, has variance nσ2/n2=σ2/nn\sigma^2/n^2 = \sigma^2/n and standard deviation σ/n\sigma/\sqrt n. Check this against the Galton board: the observed spread of the bins converges to np(1−p)\sqrt{np(1-p)}, the square root of the summed variances of nn independent bounces.

In statistical mechanics, means are the observable quantities and variances carry physics too. The average energy of a system in contact with a heat bath is its internal energy, and the variance of the energy is proportional to its heat capacity, ⟨E2⟩−⟨E⟩2=kBT2CV\langle E^2\rangle - \langle E\rangle^2 = k_BT^2 C_V. Fluctuations measured in a simulation are therefore a route to thermodynamic response functions, explored in the chapter on fluctuations.

The normal distribution and the central limit theorem

The bell curve, or normal distribution, has the density

The normal distribution owes its ubiquity to the central limit theorem: the sum (or average) of many independent random variables with finite variance is approximately normally distributed, whatever the distribution of the individual variables. The figure below lets you test this. Choose a distribution for single measurements that looks nothing like a bell: flat, sharply skewed, two separate humps, or just 0 and 1. Then average nn of them, many times over, and histogram the averages.

Averages become bell-shaped

Loading the interactive figure…

Top: the distribution of a single measurement. Bottom: a histogram of averages of n measurements, collected continuously, with the normal curve of mean μ and standard deviation σ/√n predicted by the central limit theorem. Increase n and watch any starting shape turn into the same bell, narrowing as 1/√n.

For n=1n = 1 the histogram simply reproduces the original shape. By n=5n = 5 the averages of uniform numbers already look Gaussian; the skewed exponential needs perhaps 30; the coin needs enough terms for its discrete steps to blur. In every case the width shrinks as σ/n\sigma/\sqrt n.

This theorem is why measurement errors, which are the sum of many small independent disturbances, are usually Gaussian; why the velocity of a heavy particle buffeted by many collisions is Gaussian; and why the bins of the Galton board fill into a bell. It is also the basis of every error bar. When you report the average of nn independent measurements as xˉ±σ/n\bar x \pm \sigma/\sqrt n, the standard error of the mean, you are using the central limit theorem to say that the true mean lies within that range with about 68 % confidence, and within twice that range with about 95 %.

Other distributions in science

A few other distributions appear so often that they are worth recognising.

The Poisson distribution, P(k)=λke−λ/k!P(k) = \lambda^k e^{-\lambda}/k!, describes the number of independent random events in a fixed interval when they occur at an average rate λ\lambda per interval: radioactive decays per minute, photons arriving at a detector, defects in a length of wire. Its mean and variance are both λ\lambda, so the relative uncertainty of a count is 1/λ1/\sqrt\lambda. A detector that registers 10 000 counts has a statistical uncertainty of 1 %.

The exponential distribution, p(t)=λe−λtp(t) = \lambda e^{-\lambda t} for t≥0t \ge 0, describes the waiting time until the next such event. It is memoryless: having waited a long time does not make the event more imminent. Kinetic Monte Carlo simulations draw the waiting time between rare events from this distribution.

The uniform distribution, constant on an interval, is what random-number generators produce. Every other distribution used in a simulation is made from uniform random numbers by a transformation, such as −ln⁡(1−u)/λ-\ln(1 - u)/\lambda for an exponential, or by an accept–reject scheme. Monte Carlo methods are built on this.

The Boltzmann distribution p(s)∝e−Es/kBTp(s) \propto e^{-E_s/k_BT} is the central distribution of statistical mechanics. Its consequence for velocities is the Maxwell–Boltzmann distribution, in which each velocity component is normally distributed with variance kBT/mk_BT/m.

Bayes’ theorem

The product rule can be written in two ways, P(A and B)=P(A∣B)P(B)=P(B∣A)P(A)P(A \text{ and } B) = P(A \mid B)P(B) = P(B \mid A)P(A). Equating them gives Bayes’ theorem, which turns a conditional probability around:

The theorem is simple, but its consequences routinely surprise people. Suppose a fast computational screen is used to search a large set of candidate materials, of which only 5 % are truly good. The screen flags 90 % of the good ones and, wrongly, 10 % of the bad ones. If the screen flags a candidate, what is the probability that it is good? Intuition suggests something close to 90 %. The figure shows a thousand candidates.

Bayes’ theorem with a thousand candidates

Loading the interactive figure…

Teal dots are truly good candidates; orange rings mark those flagged by the screen. The posterior probability is the fraction of ringed dots that are teal. Lower the prior and watch false positives swamp the true ones.

Of 1000 candidates, 50 are good and the screen flags 45 of them. Of the 950 bad ones it flags 95. So 140 are flagged, and only 45 of them, 32 %, are good. When the thing being looked for is rare, even a fairly accurate test produces mostly false positives, because the few true positives are outnumbered by the small fraction of a huge number of negatives. The lesson applies to high-throughput screening, to medical tests and to any classifier deployed on imbalanced data: the precision of a prediction depends on the base rate, not only on the accuracy of the test.

Bayes’ theorem is also the foundation of a whole approach to inference. To fit a model to data, one treats the model’s parameters as the hypothesis, specifies a prior, and computes the posterior distribution of the parameters given the data. The posterior says not only which parameters fit best but how uncertain they are. Gaussian processes, Bayesian optimisation and Bayesian neural networks are all applications of this idea.

Estimating averages from data

Every simulation ends with the same task: estimate an average from a finite number of samples and say how uncertain the estimate is. The sample mean xˉ=1n∑xi\bar x = \frac1n\sum x_i estimates μ\mu, and the sample variance s2=1n−1∑(xi−xˉ)2s^2 = \frac{1}{n - 1}\sum (x_i - \bar x)^2 estimates σ2\sigma^2 (the n−1n - 1 corrects for having used the data to locate the mean). If the samples are independent, the standard error of xˉ\bar x is s/ns/\sqrt n.

Simulation data, however, are almost never independent. Consecutive frames of a molecular dynamics trajectory are separated by a few femtoseconds and are nearly identical; consecutive configurations in a Markov chain Monte Carlo run differ by a single move. Treating them as independent grossly underestimates the error. The correct statement replaces nn by an effective number of independent samples, neff=n/(2τ)n_{\text{eff}} = n/(2\tau), where τ\tau is the integrated autocorrelation time in units of the sampling interval: roughly the number of steps it takes for the system to forget its past. A practical way of measuring the true error without computing τ\tau explicitly is block averaging: divide the trajectory into blocks, average within each block, and treat the block averages as independent samples. As the blocks are made longer than the correlation time, the estimated error rises and then levels off at the correct value. The chapter on trajectory analysis develops this in detail.

Finally, the link between probability and fitting. If measurements scatter around a model’s predictions with independent Gaussian errors of equal width, the probability of the data given the parameters, the likelihood, is a product of Gaussians. Maximising it is the same as minimising the sum of squared residuals. Least squares is therefore maximum-likelihood estimation under Gaussian noise, and the loss functions of machine learning can be read as negative log-likelihoods, an idea developed in learning from data.

Check yourself

  1. Two independent events have probabilities 0.3 and 0.5. What is the probability that both occur?
  2. A measurement has standard deviation 2.0. What is the standard error of the mean of 100 independent measurements?
  3. Averages of n = 50 exponential waiting times are histogrammed. Their distribution is…
  4. A test for a condition affecting 1 % of samples has sensitivity 99 % and false-positive rate 1 %. A sample tests positive. The probability it has the condition is about…
  5. A trajectory of 100 000 frames has an integrated autocorrelation time of 500 frames. Roughly how many independent samples does it contain?

References and further reading

  1. J. K. Blitzstein and J. Hwang, Introduction to Probability, 2nd ed., CRC Press (2019). Free online.
  2. D. S. Sivia and J. Skilling, Data Analysis: A Bayesian Tutorial, 2nd ed., Oxford University Press (2006).
  3. W. Feller, An Introduction to Probability Theory and Its Applications, vol. 1, 3rd ed., Wiley (1968).
  4. F. Galton, Natural Inheritance, Macmillan (1889), where the quincunx (Galton board) is described.
  5. H. Flyvbjerg and H. G. Petersen, “Error estimates on averages of correlated data”, Journal of Chemical Physics 91, 461–466 (1989). The block-averaging method.
  6. A. Grossfield, P. N. Patrone, D. R. Roe, A. J. Schultz, D. W. Siderius and D. M. Zuckerman, “Best practices for quantification of uncertainty and sampling quality in molecular simulations”, Living Journal of Computational Molecular Science 1, 5067 (2019).