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 06 of 10

06Why it still works

Chapter 5 found a linear classifier that reads nine digits in ten. This chapter checks the assumption against the data, finds what breaking it costs, and shows why the decisions survive.

6.1The assumption is false

(a) The probability picture of the training ones, with two pixels in one column ringed: when the upper one is on, the lower one is on 90\% of the time. (b) Copying every pixel m times, so that each clue counts m times with no new information: the accuracy stays the same, while the average posterior of the chosen digit rises. (c) Test digits grouped by the posterior of the chosen digit: the posterior stated on average (blue) and the share right (orange); 86, 70, 90 and 294 digits. Computed for this tutorial, with the same data as the companion video.
Figure 6. (a) The probability picture of the training ones, with two pixels in one column ringed: when the upper one is on, the lower one is on 90\% of the time. (b) Copying every pixel m times, so that each clue counts m times with no new information: the accuracy stays the same, while the average posterior of the chosen digit rises. (c) Test digits grouped by the posterior of the chosen digit: the posterior stated on average (blue) and the share right (orange); 86, 70, 90 and 294 digits. Computed for this tutorial, with the same data as the companion video.

Take the ones and two pixels in one column, row 2 and row 3 of column 6 (Figure 6a). The upper pixel is on in about half the training ones (53.5\%), and so is the lower one (51.2\%). If they were independent given the digit, both would be on in about 0.535\times0.512=27\% of the ones. In fact both are on in 48\%: when the upper pixel is on, the lower one is on 90\% of the time. One stroke runs through both. Naive Bayes multiplies their probabilities as if they were separate pieces of evidence, so it counts the same clue, the stroke, twice.

6.2Double counting stretches the scores

To see the effect in isolation, make the double counting exact: copy every pixel, so each image has 2D features, each pixel twice. Each clue now counts twice, with no new information.

Proposition 4 (Copies multiply the votes). If every pixel is copied m times, naive Bayes trained on the copies gives the scores

\text{score}^{(m)}_c(x)=\log\pi_c+m\,S_c(x),\qquad S_c(x)=\sum_{d=1}^{D}\Bigl[x_d\log\theta_{dc}+(1-x_d)\log(1-\theta_{dc})\Bigr], \tag{14}

so every difference between two digits’ pixel votes is multiplied by m, and the posterior P^{(m)}(c\mid x)\propto\pi_c\,P(x\mid c)^m becomes sharper. If the priors are equal, the highest score, and so the decision, is the same for every m.

A copy of pixel d has the same counts as pixel d, so it gets the same estimate \hat\theta_{dc} (10) and casts the same vote; the m copies add the vote m times, which gives (14). With equal priors, \log\pi_c is the same for every digit, and multiplying all the S_c(x) by the same m>0 does not change their order. Exponentiating and normalizing gives P^{(m)}(c\mid x)\propto\pi_ce^{mS_c(x)}=\pi_cP(x\mid c)^m; raising to the power m pulls the largest further ahead of the rest.

Our priors are almost equal, and copying changes none of the 540 answers.

Worked example 4 (Each clue counted twice). With every pixel copied once (m=2), naive Bayes gives the same answer for all 540 test digits, so the accuracy is still 89.6\%. But the average posterior of the digit it picks climbs from 94.9\% to 97.5\% (Figure 6b). The model is more confident, and no more often right.

Try itCount every clue m times

Try Worked example 4 for any m. Copy every pixel of every image m times and retrain: each clue then counts m times, with no new information, and every pixel vote is multiplied by m (Proposition 4). The bars group the 540 test digits by the posterior of the chosen digit, as in Figure 6c.

Real dependence is a partial version of this. Pixels along one stroke behave like partial copies of each other, so their votes pile up: the scores are stretched, the winner rarely changes.

6.3A decision only needs the highest score

To pick a class, the model does not need the right probabilities; it needs the right class to have the highest score. Domingos and Pazzani (1997) made this precise: under zero-one loss (every mistake costs the same), naive Bayes is optimal whenever the class with the highest true posterior also gets the highest naive score, a far weaker condition than independence. They showed it holds for some problems where the features are strongly dependent, such as learning conjunctions and disjunctions of features. A second reason is the bias–variance tradeoff of Episode 2: with only 640 numbers, each estimated from about 126 images, naive Bayes has low variance and is relatively immune to overfitting (Murphy, 2022, §9.3), (Hand and Yu, 2001). Its bias, the false assumption, costs some accuracy, but often less than the variance a richer model would add.

6.4The catch: overconfident probabilities

A model is calibrated if, among the cases where it says “p”, the share it gets right is about p. Double counting breaks this: the stretched scores push the posteriors toward 0 and 1 (Murphy, 2022, §9.4.1), (Niculescu-Mizil and Caruana, 2005). Figure 6c shows it on our test digits. When naive Bayes says the chosen digit has a posterior between 90\% and 99\% (96.3\% on average), it is right only 80\% of the time; its most confident mistake calls a four a seven at 99.999\%.

In practice, trust its decisions, not its probabilities. If the probabilities matter, for example to set a threshold from costs (Episode 13), calibrate them on held-out data; Niculescu-Mizil and Caruana (2005) show that Platt scaling (a logistic regression on the scores) or isotonic regression repairs naive Bayes well.

That answers the question: naive Bayes is called naive because of its independence assumption, the assumption is false, and the decisions survive it because double counting stretches the scores without changing their order. Now, three follow-ups interviewers love.