MoonSay /Tutorials/Machine Learning Interview #16
Contents
  1. 01Introduction
  2. 02Bayes’ rule, part by part
  3. 03A generative classifier
  4. 04The naive assumption
  5. 05Every pixel votes
  6. 06Why it still works
  7. 07Interview follow-ups
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Naive Bayes/Chapter 02 of 10

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,

\underbrace{P(c\mid x)}_{\text{posterior}}=\frac{\overbrace{P(x\mid c)}^{\text{likelihood}}\;\overbrace{P(c)}^{\text{prior}}}{\underbrace{P(x)}_{\text{evidence}}},\qquad P(x)=\sum_{c'}P(x\mid c')\,P(c'). \tag{1}

The product rule writes the joint probability of the class and the observation in two ways, depending on which one we condition on:

P(c,x)=P(c\mid x)\,P(x)=P(x\mid c)\,P(c). \tag{2}

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.

Bayes’ rule on one pixel, the center pixel (row 4, column 5). (a) A training zero and a training six, both with the center pixel off (ringed). (b) The 400 training images with the center pixel off, by digit: 124 are zeros. Data of Alpaydin and Kaynak (1998); computed for this tutorial, with the same data as the companion video.
Figure 1. Bayes’ rule on one pixel, the center pixel (row 4, column 5). (a) A training zero and a training six, both with the center pixel off (ringed). (b) The 400 training images with the center pixel off, by digit: 124 are zeros. Data of Alpaydin and Kaynak (1998); computed for this tutorial, with the same data as the companion video.
Try itBayes' rule on any pixel

Click a pixel, choose whether it is on or off, and pick a digit. The page counts the 1,257 training images of Figure 1 and fills in the four parts of Bayes' rule. The grid shades how often each pixel is on in the chosen digit; the bars show the posterior of every digit, with its prior as a dark tick.

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

P(0\mid\text{off})=\frac{P(\text{off}\mid0)\,P(0)}{P(\text{off})}=\frac{1\times0.099}{0.318}=0.31 . \tag{3}

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?