Bayes’ theorem – learning from data

How likely is it that you are ill when the test is positive? Should you switch doors in the Monty Hall game? How biased is a coin after 20 tosses? All three questions are answered by a formula that an English minister wrote down around 1760, and which today is used in everything from spam filters and self-driving cars to the search for missing aircraft and the interpretation of gravitational waves. Bayes’ theorem tells us how we ought to change our beliefs when we get new data.

The Reverend Bayes and Laplace

Thomas Bayes (c. 1701–1761) was minister of a Nonconformist congregation in Tunbridge Wells in Kent and an amateur mathematician. After his death his friend Richard Price found a manuscript among his papers and had it printed in the Royal Society’s Philosophical Transactions in 1763 under the title An Essay towards solving a Problem in the Doctrine of Chances. Bayes asked: if an event has occurred $h$ times and failed $t$ times, what can we say about its unknown probability? Independently of him, Pierre-Simon Laplace from 1774 developed “inverse probability” into a general tool, using it among other things to show that more boys than girls are born in Paris and to estimate the uncertainty in the masses of the planets.

The theorem

From the definition of conditional probability, $P(A \cap B) = P(A)\,P(B\,|\,A) = P(B)\,P(A\,|\,B)$. Divide by $P(B)$:

$$P(A\,|\,B) = \frac{P(B\,|\,A)\;P(A)}{P(B)},\qquad P(B) = P(B\,|\,A)\,P(A) + P(B\,|\,\bar A)\,P(\bar A)$$

Think of $A$ as a hypothesis and $B$ as data. Then

The theorem is pure mathematics and entirely uncontroversial. The 20th-century debate between “Bayesians” and “frequentists” was about whether one may assign a probability to a hypothesis at all – and where the prior should come from.

The positive test

A disease has a prevalence of 1 %. A test detects 90 % of the ill (the sensitivity) and is negative in 91 % of the healthy (the specificity). You test positive. How likely is it that you are ill? When the psychologist Gerd Gigerenzer put exactly this question to 160 gynaecologists, almost half answered “9 in 10”, and only about one in five chose the correct answer: about 1 in 10.

Try it: Each figure is one of 1000 people tested. The red are ill with a positive test, the gold are healthy with a false alarm. Because there are so many more healthy than ill people, the false alarms outnumber the true ones, even though the test is good. Try “Rare disease, good test”: with 99 % sensitivity and specificity a positive test is right in only about 9 % of cases. Then press “Test again”: the posterior becomes the prior for the next test – and two independent positive tests give convincing evidence. That is why a positive screening is always followed by a further examination.

The odds form – the easiest version

Write probabilities as odds, $O = P/(1-P)$. Then Bayes’ theorem becomes a simple multiplication:

$$O(A\,|\,B) = O(A)\times\frac{P(B\,|\,A)}{P(B\,|\,\bar A)}$$

The fraction is the likelihood ratio: how many times more probable the data are if the hypothesis is true than if it is false. For the test above it is $0.90/0.09 = 10$. The prior odds are 1 : 99; the posterior odds are 10 : 99, i.e. a probability of $10/109 \approx 9\ \%$. Each new independent observation simply multiplies the odds by its likelihood ratio. Alan Turing and I.J. Good used precisely this form, measured in logarithmic units (“decibans”), to break the German Enigma code at Bletchley Park during the Second World War.

The Monty Hall problem

In an American TV game show, Let’s Make a Deal hosted by Monty Hall, you must choose one of three doors. Behind one is a car, behind the other two a goat. When you have chosen, the host – who knows where the car is – opens one of the other doors and reveals a goat. He offers you the chance to switch. Should you? When Marilyn vos Savant wrote in Parade magazine in 1990 that you should switch, she received thousands of letters, including from mathematicians who thought she was wrong. She was right: switching wins with probability $2/3$.

Try it: Play a few times and then press the simulation. With Bayes: let $B_i$ be “the car is behind door $i$”, and suppose you chose door 1 and the host opened door 3. The host opens door 3 with probability ½ if the car is behind door 1, with probability 1 if it is behind door 2, and 0 if it is behind door 3. Bayes gives $P(B_1\,|\,\text{opens }3) = \tfrac{\frac12\cdot\frac13}{\frac12} = \tfrac13$ and $P(B_2\,|\,\text{opens }3) = \tfrac23$. The host’s choice carries information, because he never opens the car’s door.

Bayesian updating – how biased is the coin?

Bayes’ original problem: a coin shows tails with an unknown probability $p$. We describe our uncertainty about $p$ by a distribution. After $h$ tails and $t$ heads the posterior distribution is proportional to the prior times the likelihood $p^h(1-p)^t$. Starting from a beta distribution $\text{Beta}(a,b)$, one ends with $\text{Beta}(a+h,\,b+t)$. With a uniform prior ($a = b = 1$) the best estimate for the next toss is Laplace’s rule of succession $(h+1)/(n+2)$.

Try it: The dashed curve is the prior, the gold curve the posterior, and the shaded region a 95 % credible interval. With few tosses the prior matters a lot: someone who believes in a fair coin takes longer to be convinced. But after 100 tosses everyone agrees – the data overwhelm the prior. This is the Bayesian version of the law of large numbers, and it is the answer to those who think priors make the method subjective.

The prosecutor’s fallacy

Confusing $P(\text{data}\,|\,\text{innocent})$ with $P(\text{innocent}\,|\,\text{data})$ is called the prosecutor’s fallacy. If a DNA trace matches one in a million, and the city has five million inhabitants, about five innocent people will also match – so a match alone does not make the probability of guilt 999,999 in a million. In the British case against Sally Clark (convicted in 1999, acquitted on appeal in 2003) an expert witness stated that the probability of two cot deaths in the same family was 1 in 73 million. The figure was wrongly calculated (it assumed independence), and it was presented as if it were the probability that she was innocent. The Royal Statistical Society protested publicly in 2001.

Bayes in practice

Spam filters

A filter learns how often each word appears in spam and in ordinary mail, and combines the words’ likelihood ratios into a probability of spam. Paul Graham popularised the method in 2002.

Search

The submarine USS Scorpion (1968) and the wreck of Air France flight 447, which crashed into the Atlantic in 2009, were found by Bayesian search: the seabed is divided into cells with a probability of containing the wreck, updated each time a cell is searched without success. AF447 was found in April 2011 after a new analysis.

Physics and astronomy

The masses of the black holes behind gravitational waves, the cosmological parameters from the Planck satellite and the orbits of exoplanets are determined with Bayesian methods, computing the whole distribution of parameter values consistent with the data.

The brain

An influential theory in neuroscience holds that the senses work in a Bayesian way: the brain combines expectation (prior) with sensory input (likelihood). Many optical illusions can be explained like this – for instance our assumption that light comes from above.

Further reading