Bayes' Theorem Explained With a Medical Test Classifier
Bayes' theorem turns a probability you can measure into the probability you actually want: the chance of a hypothesis once you have seen some evidence. In symbols, P(A | B) = P(B | A) × P(A) / P(B). In their chapter on classification, James, Witten, Hastie, and Tibshirani write it for K classes as Pr(Y = k | X = x) = π_k f_k(x) / Σ π_l f_l(x), their Equation 4.15. Here π_k is the prior probability that an observation comes from class k, f_k(x) is the density of the predictor in class k, and the left side is the posterior probability that an observation with X = x belongs to class k.
That is how a classifier uses it. It models how the predictors look within each class, f_k, and combines that with how common each class is, π_k, to get the probability of each class given the observation. The authors note that estimating the priors is easy from a random sample, since you take the fraction of training observations in each class, while estimating the densities is much harder.
Try it by hand. Take 10,000 people. One percent of them, 100 people, are sick, and 9,900 are healthy. A test catches 90 percent of the sick people and false-alarms on 5 percent of the healthy ones. Of the 100 sick people, 90 test positive. Of the 9,900 healthy people, 5 percent, or 495, also test positive. So there are 90 + 495 = 585 positive results, and only 90 of them are real. Given a positive test, the chance of being sick is 90/585 = 0.154, about 15 percent. The formula gives the same thing: 0.9 × 0.01 / (0.9 × 0.01 + 0.05 × 0.99) = 0.009 / 0.0585 = 0.154. The prior of 1 percent became a posterior of about 15 percent: the evidence moved it a lot, but it did not make it 90 percent. The numbers are invented for the example.
The trap: treating P(A | B) as P(B | A). In the example, P(positive | sick) is 0.90 and P(sick | positive) is 0.15, and they are different questions. Most people hear "the test is 90 percent accurate" and conclude a positive result means a 90 percent chance of illness. The missing piece is the prior: when the thing you are looking for is rare, most positives come from the much larger healthy group.
One more distinction from the same chapter. The authors point out in a footnote that the Bayes classifier, which assigns an observation to the class with the largest posterior, is different from Bayes' theorem, which is the rule for manipulating conditional probabilities.
Prof Rod's rule: before you read a positive result, ask how common the thing is and how often the test cries wolf.