Bayesian modelling · All chapters
Bayesian modellingBonus chapter

How computers actually do this

Every chapter so far had a tidy formula. Real models don't. So computers do something brilliantly simple instead: they go for a walk.

AuthorJack Maughan
MethodMCMC
Figures2, interactive
Reading time7 minutes
The series so farEvery chapter used the same recipe: prior × likelihood → posterior. In each one, a neat formula (Beta, Gamma, Normal) or a grid of a thousand points gave us the posterior directly.

Real models aren't that tidy. A proper football model might have an attack and a defence rate for every team, home advantage, and form that drifts through the season: fifty unknowns at once. A grid of a thousand points for each of fifty unknowns would need 1,000⁵⁰ points, far more than there are atoms in the universe.

So computers cheat, brilliantly. Instead of mapping the whole posterior, they send a walker to wander around it and keep a diary of where it went. This is called Markov chain Monte Carlo, or MCMC, and it's how almost every serious Bayesian model is fitted today.

§ 01 — A walk on a hill

Spend more time where the hill is high

Picture the posterior as a hill. Its height at any spot is prior × likelihood, which is easy to work out for one spot at a time. What's hard is the total size of the hill. The simplest walker, the Metropolis algorithm (from a 1953 paper by Nicholas Metropolis and colleagues at Los Alamos), never needs it. Its rules fit on a postcard:

1. Stand somewhere. 2. Propose a random step. 3. If the new spot is higher, go there. 4. If it's lower, go with a probability equal to new height ÷ old height; otherwise stay put and count your current spot again. 5. Repeat thousands of times.

The walker ends up spending time in each place in proportion to the hill's height. Its diary is the posterior. Below, the hill is the penalty taker from Chapter 2 after three from three, so we know the right answer and can check the computer's work. The walker starts somewhere silly on purpose.

Fig. 13 — The Metropolis walkerHill: the penalty taker from Chapter 2
01  The hill
02  The walk
Speed

Press start, or take one step at a time to see each decision.

Walker Step accepted Step rejected
The walker's diary: where it has been, against the true posterior
The trace: position at each step (most recent 400)
Steps0walked so far
Accepted–of proposed steps
Walker's average–ignoring the first 100 steps
True average–from the exact maths

Watch the start. The walker begins far down the slope, and the first few dozen steps are just it climbing towards the high ground. That early stretch says more about where it started than about the hill, so it's thrown away. This is called the burn-in, and it's why the walker's average ignores the first 100 steps.

Then switch to Fast and let it run. The diary fills in, and its shape locks onto the true curve. After a few thousand steps the walker's average agrees with the exact answer from Chapter 2 to about two decimal places, and it got there without ever knowing the size of the hill.

§ 02 — Tuning the walk

Too timid, too bold, just right

The one setting that matters is the step size. Drag it all the way left: almost every step is accepted, but the walker shuffles along so slowly that its trace looks like a lazy river. Drag it all the way right: it keeps proposing leaps off the edge of the hill, almost all are rejected, and the trace turns into a staircase of long flat stretches. Theory suggests accepting very roughly a quarter to a half of proposed steps is a good place to be.

Now switch to Two hills: a posterior with two separate explanations for the data. With small steps, the walker finds one hill and never discovers the other, and its diary confidently reports the wrong answer. With bigger steps it hops between them. This is the hardest problem in MCMC, and it's why practitioners run several walkers from different starting points and check that they agree.

Modern tools such as Stan and PyMC use a much smarter walker called Hamiltonian Monte Carlo. It uses the slope of the hill to glide along it like a marble rather than stumbling randomly, which lets it handle models with thousands of unknowns. But the idea is the one you've just watched: wander in proportion to the posterior, and keep a diary.

Key findings

  1. Real posteriors have too many unknowns to map on a grid, so computers sample them instead.
  2. The Metropolis walker only ever compares two heights, so it never needs the hill's total size.
  3. Throw away the burn-in, tune the step size, and run several walkers to check they agree.
  4. The series in one line: start with what you believe, weigh the evidence honestly, and report a range, not a number.
Appendix — the maths

Let p(θ) be the posterior up to an unknown constant: prior × likelihood. From the current position θ, propose θ′ = θ + ε with ε drawn from a normal with spread equal to the step size. Accept with probability:

a = min(1, p(θ′) ÷ p(θ))

The unknown constant cancels in the ratio, which is the whole trick. Because the proposal is symmetric (stepping from θ to θ′ is as likely as the reverse), the chain's long-run distribution is exactly p. The penalty hill is the Beta(18.2, 4.8) posterior from Chapter 2 (league-average prior plus three from three), with an exact mean of 18.2 ÷ 23 ≈ 0.791. The two-hill example is a 60:40 mix of two normal curves.

Next · Series complete

Back to all chapters

That's the whole course: from one referee's decision to a computer mapping a posterior on its own. Pick any chapter to revisit.

→