Contents
02Bayes’ rule, part by part
We see an image and want its digit. But the training data let us count the reverse more easily: how often each digit has a given feature. Bayes’ rule turns one into the other.
2.1From the product rule
Proposition 1 (Bayes’ rule, Murphy, 2022, eq. 2.51–2.53). For a class c and an observation x with P(x)>0,
The product rule writes the joint probability of the class and the observation in two ways, depending on which one we condition on:
Divide both sides by P(x) to get the first formula. For the second, the events “the class is c'” for the different c' do not overlap and together cover everything, so P(x)=\sum_{c'}P(c',x)=\sum_{c'}P(x\mid c')P(c'), the law of total probability.
Each part has a meaning. The prior P(c) is what we believe before we look: how common class c is. The likelihood P(x\mid c) answers the reverse question: if the class is c, how likely is what we see? The evidence P(x) is how likely the observation is overall, across all classes; it is the same for every c and makes the posteriors add up to one. The posterior P(c\mid x) is what we believe after we look. In words: posterior equals likelihood times prior, divided by the evidence.
Worked example 1 (Is it a zero?). An image’s center pixel is off. How likely is it a zero (Figure 1)? The prior: 124 of the 1{,}257 training images are zeros, 9.9\%. The likelihood: every training zero has the center off, so P(\text{off}\mid0)=124/124=100\%; it is the hole of the zero. The evidence: 400 of the 1{,}257 training images have the center off, 31.8\%. The posterior is
With counts the totals cancel, \frac{(124/124)(124/1257)}{400/1257}=\frac{124}{400}: the posterior is simply the share of zeros among the images with the center off. One pixel moved us from 10\% to 31\%.
Pause and think. Every zero has the center off: the likelihood is 100\%. Why is the posterior only 31\%?
Show the answer
The likelihood answers the reverse question. Many other digits also have an empty center (94 sixes and 63 fours among the 400), so the evidence is large, and the zeros are only about a third of the images that match. Confusing P(\text{off}\mid0) with P(0\mid\text{off}) is the classic mistake that Bayes’ rule corrects; the prior and the evidence are what turn one into the other.
One pixel is not enough: a six can have an empty center too. So let the evidence be the whole image. What kind of model do we get?