Skip to content

Bayesian updating

Multiply what you believed by how well each possibility explains the data, rescale, and today’s posterior becomes tomorrow’s prior.

Helps to know first
This topic

First, the picture

A curve says how much you believe each possible bias of a coin. Watch it reshape as the flips land, one by one.

Learning from evidence

Belief about a coin's chance of heads, flip by flip: 30 flips of a coin with chance 0.7 (seed 7100).

Before any flips the curve is flat: every chance of heads p from 0 to 1 is equally believable.

flips
0
heads
0
mean of the curve
0.5000
standard deviation
0.2887

Predict first. After three heads in a row, what will the belief curve look like?

0.00 / 14.85 s
Describe this picture

One panel: a curve over the unknown chance of heads pp, from 0 to 1, whose height says how strongly each value is believed; the area under it, lightly filled, is always 1. It starts flat: every value equally believable. Before the clip a question asks what the curve will look like after three heads in a row, with three small drawings: a narrow spike at p=1p=1, a hump centred on 0.5, or a curve rising towards 1 but still spread wide. A tap picks one and starts the clip. In the clip 30 seeded flips of a coin with true p=0.7p=0.7 (seed 7100) land one at a time as H and T marks in a row above the curve, and each one reshapes the curve. The first three are heads and land slowly: the curve becomes 2p2p, then 3p23p^2, then 4p34p^3, and holds there while your guess is drawn as a dotted curve beside it. Then the other 27 flips land quickly, a dashed line marks the true chance 0.7, and the curve narrows and moves towards it. After 30 flips, 20 of them heads, it is Beta(21,11)\mathrm{Beta}(21,11), with mean 0.6563. Readouts give the number of flips, the number of heads, the mean of the curve and its standard deviation. After the clip a slider, “Flips seen”, scrubs the number of flips from 0 to 30.

Belief as a curve

The coin’s chance of heads, pp, is unknown. Instead of one guess, the picture keeps a whole curve: a belief about every possible value. Before any flips that belief is the prior. After the data it is the posterior. The rule that turns one into the other is Bayes’ theorem, used again and again.

Before the clip you guessed what three heads would do. After three heads the curve is 4p34p^3: it rises all the way to p=1p=1, but it is spread wide. Its mean is 0.80.8, not 1, and pp could easily be as low as 0.6: the curve still puts 13 % of its area below 0.6. Three flips are not much evidence.

How sure the curve is that the coin favours heads, p>0.5p>0.5, is its area above 0.5: 1−0.54=0.93751-0.5^4=0.9375.

The clip’s coin then landed 17 more heads in 27 flips: 20 heads in 30 in all. The curve ends narrow, with mean 21/32=0.656321/32=0.6563, near the true chance 0.7.

Three coins in a bag

Start with a case small enough to do by hand. A bag holds three coins. One lands heads with chance 0.3, one with 0.5, one with 0.8. You draw a coin without looking, so each is equally likely: a prior of 13\tfrac13 each. You flip it once, and it lands heads. Which coin is it now likely to be?

Multiply each prior by the chance that coin gives heads, its likelihood from Maximum likelihood, and rescale so the three add to 1:

CoinPriorChance of headsProductPosterior
0.31/30.30.10000.1875
0.51/30.50.16670.3125
0.81/30.80.26670.5000

That is the whole method:

Pr⁡{coin k∣data}=Pr⁡{data∣coin k} Pr⁡{coin k}∑every coinPr⁡{data∣coin} Pr⁡{coin}.\Pr\{\text{coin }k\mid\text{data}\}=\frac{\Pr\{\text{data}\mid\text{coin }k\}\,\Pr\{\text{coin }k\}}{\sum_{\text{every coin}}\Pr\{\text{data}\mid\text{coin}\}\,\Pr\{\text{coin}\}}.

In words: posterior is prior times likelihood, rescaled. The bottom line is only the rescaling.

Three coins in a bag

Belief in each coin, updated by one heads and one tails.

A bag of three coins with heads chances 0.3, 0.5 and 0.8. One is picked at random: each is believed equally, one third.

coin 0.3
0.3333
coin 0.5
0.3333
coin 0.8
0.3333
next flip heads
0.5333
Order of the two flips
0.00 / 7.20 s
Describe this picture

Three bars, one for each coin in the bag, labelled “coin 0.3”, “coin 0.5” and “coin 0.8” with their chances of heads, and heights equal to the belief in each coin, each printed above its bar; they start equal at one third. In the clip a heads lands and the bars move to 0.1875, 0.3125 and 0.5; then a tails lands and they move to 0.3387, 0.4032 and 0.2581, while dashed ticks keep the heights after the first flip. A list beside the bars names the flips as they land. Readouts give the three beliefs and the chance that the next flip is heads: 0.5333 at the start, 0.6125 after the heads, 0.5097 after both. After the clip a switch, “Order of the two flips”, sets tails first, then heads: the dashed ticks move to the heights after a tails, 0.5, 0.3571 and 0.1429, and the bars end at the same three values.

Now a tails. Use the posterior as the new prior, and the chances of tails, 0.70.7, 0.50.5 and 0.20.2, as the likelihoods. The products are 0.131250.13125, 0.156250.15625 and 0.10.1, adding to 0.38750.3875, so the beliefs become 0.33870.3387, 0.40320.4032 and 0.25810.2581.

After the clip, swap the order. Tails first and then heads gives the same three beliefs: either way each coin’s product is its chance of one heads times its chance of one tails, 0.210.21, 0.250.25 and 0.160.16 (times 13\tfrac13). Order does not matter, only what was seen.

Watch out

A prior is not a bias that rules for ever, and the posterior is not the likelihood. The prior is where the belief starts; each piece of evidence multiplies it, and enough evidence outweighs any reasonable start. Only with a flat prior does the posterior have the same shape as the likelihood.

Many coins: a curve

Now let the bag hold coins of every bias from 0 to 1, all equally likely: a flat prior over pp. After hh heads in nn flips, the likelihood of a coin with bias pp is ph(1−p)n−hp^h(1-p)^{n-h}, and with a flat prior the posterior is that curve, rescaled to area 1.

A curve of the shape pa−1(1−p)b−1p^{a-1}(1-p)^{b-1} on [0,1][0,1], rescaled to area 1, is called the beta law, written Beta(a,b)\mathrm{Beta}(a,b). Here aa and bb are two positive shape numbers, not filter coefficients. So the posterior after hh heads in nn flips, from a flat prior, is

Beta(h+1, n−h+1).\mathrm{Beta}(h+1,\ n-h+1).

The flat prior is Beta(1,1)\mathrm{Beta}(1,1), and three heads give Beta(4,1)\mathrm{Beta}(4,1), the 4p34p^3 above. A Beta(a,b)\mathrm{Beta}(a,b) curve has mean a/(a+b)a/(a+b) and, for a,b>1a,b>1, its peak at (a−1)/(a+b−2)(a-1)/(a+b-2). For the posterior that means

mean=h+1n+2,peak=hn.\text{mean}=\frac{h+1}{n+2},\qquad\text{peak}=\frac hn.

The peak is the maximum likelihood estimate; the mean is pulled slightly towards 12\tfrac12.

Predicting the next flip

What is the chance the next flip lands heads? Average each coin’s chance of heads, weighted by the belief in it. For the bag after one heads:

0.1875×0.3+0.3125×0.5+0.5×0.8=0.6125.0.1875\times0.3+0.3125\times0.5+0.5\times0.8=0.6125.

For the curve, the same average is the curve’s mean, (h+1)/(n+2)(h+1)/(n+2). After 7 heads in 10 flips it is 8/12=0.66678/12=0.6667. It is called the rule of succession: after no flips at all it gives 12\tfrac12.

Today’s posterior is tomorrow’s prior

Updating one piece of evidence at a time gives the same answer as updating on all of it at once, as the swap showed. That is why the method suits data that arrive in a stream: keep one belief, and fold in each new observation as it comes.

The repeated medical test of Bayes’ theorem was exactly this. A prior of 1 % became 16.7%16.7\% after one positive test, 79.8%79.8\% after two and 98.7%98.7\% after three, each posterior serving as the next prior.

Worked example

1. The bag, heads then tails. Priors 13\tfrac13 each. After heads: 0.18750.1875, 0.31250.3125, 0.50.5, and the next flip is heads with chance 0.61250.6125. After tails as well: 0.33870.3387, 0.40320.4032, 0.25810.2581, and the next flip is heads with chance 0.50970.5097.

2. Seven heads in ten, from a flat prior. The posterior is Beta(8,4)\mathrm{Beta}(8,4). Its peak is at 0.70.7, the estimate of Maximum likelihood; its mean is 8/12=0.66678/12=0.6667, and its standard deviation 32/(144×13)=0.1307\sqrt{32/(144\times13)}=0.1307.

This curve is the likelihood p7(1−p)3p^7(1-p)^3 divided by its area, 1/13201/1320. So its peak height is 1320×0.002224=2.9351320\times0.002224=2.935.

The curve’s mean sits left of its peak because the curve leans: it has a longer tail towards small pp than towards 1.

3. Two tails from a flat prior. No heads in two flips gives Beta(1,3)\mathrm{Beta}(1,3), with mean 1/41/4: not 0, because two flips cannot rule heads out.

Where you’ll meet this

Spam filters. A filter keeps a belief that a message is spam and updates it word by word, as the bag updated flip by flip.

Experiments with history. An A/B test can start from a prior built from earlier tests, so that a small new test is read in the light of the old ones.

Monitoring. A hospital monitor or a fault detector updates a belief about a patient’s or a machine’s state with every new reading.

In signal processing · the Kalman filter

The Kalman filter keeps a belief about a moving object’s position and updates it with each measurement, weighing the new datum against the old belief by how uncertain each is. It is Bayesian updating for bell-shaped beliefs; RLS and the Kalman filter in the signal processing course builds it.

Reference card

QuantityFormulaNotes
Updateposterior ∝ prior × likelihoodrescale to add (or integrate) to 1
DiscretePr⁡{k∣data}∝Pr⁡{data∣k}Pr⁡{k}\Pr\{k\mid\text{data}\}\propto\Pr\{\text{data}\mid k\}\Pr\{k\}rescaled over every kk; Bayes’ theorem
Ordersame posterior in any ordertoday’s posterior is tomorrow’s prior
Beta lawBeta(a,b)∝pa−1(1−p)b−1\mathrm{Beta}(a,b)\propto p^{a-1}(1-p)^{b-1}mean a/(a+b)a/(a+b)
Coin, flat priorBeta(h+1, n−h+1)\mathrm{Beta}(h+1,\ n-h+1)peak h/nh/n, mean (h+1)/(n+2)(h+1)/(n+2)
Next flipmean of the posteriorrule of succession

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look