← Algorithms & ML

Bayes' Theorem

A positive result from an accurate test can still leave a disease unlikely. Bayes' theorem says how to weigh new evidence against what you believed before, one observation at a time.

You take a medical test and it comes back positive. If the test is 95% accurate, it is tempting to conclude that there is a 95% chance you have the disease. The right answer can be far lower, and Bayes' theorem says how much lower. Part 4 described the shape of a dataset; this part is about uncertainty, and how a belief should change as each new observation arrives.

Base Rates

Suppose a disease affects 1% of the population. A test for it has 95% sensitivity (it correctly identifies 95% of sick people) and 95% specificity (it correctly identifies 95% of healthy people). If you test positive, the probability that you have the disease is about 16%.

The unit square in Figure 1 represents the whole population, divided into four rectangles for the four outcomes: true positives, false negatives, false positives and true negatives.

Figure 1. The area model of Bayes' theorem. Each rectangle's area is proportional to the joint probability of that outcome, and the dashed outlines mark everyone who tested positive. At the default 1% base rate the true positives are only a thin blue column at the left and the false positives an amber strip along the top, so both are labeled from outside.

At the default settings, most of the highlighted area belongs to healthy people. When a condition is rare, even an accurate test produces more false positives than true positives, because the healthy group is so much larger that its small false-positive rate still adds up to more cases. At 10% prevalence the same test gives a posterior above 67%.

Bayes' Theorem

The formula behind the figure is Bayes' theorem:

In words: the posterior (the probability of the hypothesis given the evidence) equals the likelihood (the probability of the evidence under the hypothesis), times the prior (the base rate of the hypothesis), divided by the evidence (the total probability of seeing what we saw).

The denominator P(positive) accounts for all the ways you could test positive, including false positives from the much larger healthy group. That term is what pulls the posterior down when the base rate is low.

Sequential Updates

Bayes' theorem can be applied again each time new evidence arrives. Suppose you know nothing about a coin's bias, so you start with a uniform prior: every bias from 0 to 1 is equally plausible. After each flip, Bayes' theorem updates the distribution, and the posterior after one flip becomes the prior for the next.

0 observations

Figure 2. Sequential updating. Top: the belief distribution over the coin's bias (a beta distribution). Bottom: the posterior mean, the estimated probability of heads, as observations accumulate.

The curve starts wide and flat and narrows as evidence accumulates. Its standard deviation starts at 0.289 for the uniform prior; after 3 heads in 5 flips it is 0.175, and after 30 heads in 50 flips it is 0.067. The width shrinks roughly as one over the square root of the number of flips, so four times as many flips give about half the width.

The Effect of the Prior

A common objection to Bayesian reasoning is that the prior is subjective, since different people start with different beliefs. In Figure 3, three observers watch the same flips from different priors: uniform (no opinion), skeptical (the coin is probably fair) and credulous (it is probably biased toward heads).

0 observations

Figure 3. Three priors, same evidence. Auto-flips are simulated from a coin with bias 0.6; the Heads and Tails buttons add flips of your choosing.

With few observations the three estimates differ; with enough, they converge, but slowly, because the skeptical prior counts as 40 imaginary flips. For a coin with bias 0.6, the expected gap between the skeptical and credulous means is 0.20 before any flips, 0.12 after 20, and 0.03 after 200.

Bayesian vs. Frequentist

The frequentist tradition handles the same uncertainty differently. In Figure 4, the frequentist panel gives a point estimate (the fraction of heads) with a 95% confidence interval, and the Bayesian panel gives a full posterior over the coin's bias, starting from a uniform prior, with its 95% credible interval. The confidence interval here is the Wilson score interval; the simpler "estimate plus or minus 1.96 standard errors" collapses to zero width after a single flip. Both intervals narrow with more data and converge on the truth. The posterior also supports direct probability statements: after 69 heads in 115 flips, for example, it puts 73% probability on the bias being between 0.55 and 0.65. A confidence interval does not support a statement like that.

Frequentist
Bayesian

Figure 4. The same simulated flips analyzed both ways: frequentist panel first (left, or above on a phone), Bayesian second. The slider sets the simulated coin's bias; moving it starts a new coin and clears the flips.

The two approaches answer different questions. The frequentist asks, "What procedure gives intervals that, over many repeats of this experiment, contain the true value 95% of the time?" The 95% belongs to the procedure: any one interval either contains the fixed true bias or it does not. (For a coin with bias 0.6, the Wilson interval's actual coverage is 98% at 10 flips and 94% at 50, close to but not exactly 95%.) The Bayesian asks, "Given what I have seen, what do I believe about the true value now?" That second question treats probability as a description of what you know, so the answer can be updated flip by flip.

Here the evidence arrives the same way whatever the observer does. Part 6 turns to an agent whose actions change what it observes, and which learns from the rewards those actions earn.