Contents
04The naive assumption
We cannot count whole images. Naive Bayes answers with one bold assumption, which turns a table of 2^{64} entries into 64 numbers per digit.
4.1Independent once you know the class
Definition 2 (The naive Bayes assumption, Murphy, 2022, eq. 9.46). The features are conditionally independent given the class: once you know the digit, knowing some pixels tells you nothing more about the others. Then the likelihood is a product, one factor per feature:
That is the naive part. Each factor is the probability that one pixel is on or off in digit c. With \theta_{dc}=P(x_d=1\mid c), the factor is \theta_{dc} if the pixel is on and 1-\theta_{dc} if it is off, which one formula covers because a power of 0 is 1:
This is the Bernoulli naive Bayes model (Murphy, 2022, §9.3.1). Instead of 2^{64}-1 probabilities per digit, it needs 64: 640 numbers for the ten digits, plus the ten priors.
4.2Training is counting
Proposition 2 (Maximum likelihood is counting, Murphy, 2022, eq. 9.53). The maximum likelihood estimates of the Bernoulli naive Bayes model are
each pixel probability is the share of that digit’s training images with the pixel on.
The training images are independent, so the log-likelihood is a sum over images n, each contributing the log of its prior and of its 64 factors (6):
where y_n is the digit of image n. Collect the terms that contain one parameter \theta_{dc}: the N_c images of digit c, of which N_{dc} have the pixel on, contribute N_{dc}\log\theta_{dc}+(N_c-N_{dc})\log(1-\theta_{dc}). Set the derivative to zero:
a maximum, since the second derivative -N_{dc}/\theta_{dc}^2-(N_c-N_{dc})/(1-\theta_{dc})^2 is negative. For the priors, maximize \sum_cN_c\log\pi_c subject to \sum_c\pi_c=1: with a Lagrange multiplier \lambda, N_c/\pi_c=\lambda for every c, and summing \pi_c=N_c/\lambda over c gives \lambda=N.
Each parameter appears in its own terms, so each is estimated separately, by one division: one pass through the data, no optimization. Figure 3 shows the result as ten probability pictures, one per digit, where the darker a square, the more often that pixel is on. Each picture is the average of that digit’s training images.
Worked example 2 (The center pixel). The center pixel of Chapter 2 is on in 123 of the 128 training threes, so \hat\theta=123/128=0.96 for the threes, and in none of the 124 training zeros, so \hat\theta=0/124=0 for the zeros: that is the hole. Doing the same division for every pixel and every digit gives the 640 numbers of Figure 3.
4.3Zero counts and Laplace smoothing
A count of zero is dangerous. If \hat\theta_{dc}=0 and a new image has pixel d on, the factor \theta_{dc}^{x_d} in (6) is 0, so P(x\mid c)=0 and digit c is ruled out, whatever the other 63 pixels say. The same happens with \hat\theta_{dc}=1 and the pixel off.
Figure 4 shows it happening. The test six has ink at the right edge (row 7, column 8), where none of the 127 training sixes had any, so without a fix it could never be called a six. On our test digits, six of them would get probability zero for their true class. The classic fix is add-one or Laplace smoothing (Murphy, 2022, eq. 9.58): count as if every pixel had been seen once on and once off in two extra images of each digit,
The ringed pixel of the sixes gets (0+1)/(127+2)=1/129: small, but no longer a veto. For a pixel with plenty of data the change is tiny (123/128=0.961 becomes 124/130=0.954). Smoothing is not a hack: it is the estimate under a uniform prior on \theta_{dc}, the posterior mean of Episode 5’s Bayesian view (Murphy, 2022, §9.3.3), and it is the rule of succession that Laplace used for the chance that the sun rises tomorrow (Laplace, 1814). All numbers from here on use it.
Pause and think. Why does a zero count cause trouble for naive Bayes in particular, more than for a single probability estimate?
Show the answer
Because the likelihood is a product of 64 factors. One zero factor makes the whole product zero, and no amount of agreement from the other pixels can bring it back. With words in a spam filter the problem is worse: an e-mail with one word never seen in spam during training could never be called spam (Manning et al., 2008, §13.2).
Six hundred forty numbers, plus ten priors: that is the whole model. So how does it read a new digit?