Contents
05Every pixel votes
We have the ten probability pictures. To classify a new image, compute the posterior of every digit with (4), and pick the highest.
5.1Drop the evidence, take logs
Picking the class with the highest posterior minimizes the chance of a mistake. Two simplifications make it cheap. The evidence at the bottom of Bayes’ rule is the same for every digit, so it cannot change which one is highest, and we drop it. And a product of 64 probabilities is tiny (for text, with thousands of words, it underflows to zero in floating point), so we take logs, which also cannot change the winner because the logarithm is increasing (Manning et al., 2008, §13.2):
Each pixel adds one term to each digit’s score: \log\theta_{dc} if it is on, \log(1-\theta_{dc}) if it is off. Every pixel casts a vote.
Between two digits a and b, the evidence cancels exactly, and the difference of the scores is the log of the posterior odds:
A positive vote v_d favors a, a negative one favors b.
Worked example 3 (Three or nine?). For the test digit of Figure 5, the highest score is the three’s, -20.17, and the runner-up is the nine’s, -27.65. Three wins by 7.49 in log odds, so its posterior odds against nine are e^{7.49}\approx1{,}800 to 1, and its posterior is 99.93\%. The priors contribute almost nothing (\log(\pi_3/\pi_9)=0.016); the pixels decide. On all 540 test digits, naive Bayes gets 89.6\% right; logistic regression, trained on the same pixels, gets 93.0\%.
5.2A linear classifier
Proposition 3 (Naive Bayes is a linear classifier, Murphy, 2022, eq. 9.61–9.62). The score (11) is a linear function of the pixels,
and the posterior is the softmax of these linear scores, P(c\mid x)=e^{w_c^\top x+b_c}\big/\sum_{c'}e^{w_{c'}^\top x+b_{c'}}.
For one pixel, x_d\log\theta_{dc}+(1-x_d)\log(1-\theta_{dc})=\log(1-\theta_{dc})+x_d\bigl[\log\theta_{dc}-\log(1-\theta_{dc})\bigr]: a constant plus the pixel times a fixed weight, the log-odds that the pixel is on in digit c. Sum over the pixels and add \log\pi_c. For the posterior, put the evidence back: P(c\mid x)=P(x\mid c)P(c)/\sum_{c'}P(x\mid c')P(c'), and P(x\mid c)P(c)=e^{\text{score}_c(x)}.
So each vote is a fixed weight times a pixel, and the boundary between two digits, where their scores are equal, is the flat surface (w_a-w_b)^\top x+(b_a-b_b)=0. This is exactly the form of multinomial logistic regression (Episode 15). The difference is how the weights are chosen: naive Bayes sets each weight from its own pixel’s counts, maximizing the joint likelihood \prod_nP(y_n,x_n), while logistic regression chooses all the weights together to maximize the conditional likelihood \prod_nP(y_n\mid x_n) (Murphy, 2022, §9.3.4).
Pause and think. Both models are linear in the same pixels, yet logistic regression is more accurate here. What can it do that counting cannot?
Show the answer
It chooses the weights jointly. If two pixels almost always switch on together, logistic regression can share the credit between them, giving each about half the weight. Naive Bayes sets each weight from its own pixel as if the other did not exist, so the pair gets full weight twice. The next chapter shows what that does.
Not bad for a false assumption. But is the assumption really false? And why does it still work?