I am following this course (notebook) and I wonder about how this derivation of the Naive Bayes came to be.
So, there is a $X$ defined to be the term frequency matrix between documents (rows) and words (cols). The documents can be classified to either positive or negative documents. $P_1$ is defined to be the per-word positive count vector (i.e. in how many positive documents did each word appear). Similarly $P_0$ is defined to be the negative count.
in python:
p1 = np.squeeze(np.asarray(x[y.items==positive].sum(0)))
p0 = np.squeeze(np.asarray(x[y.items==negative].sum(0)))
$Prob_1$ is defined to be $P_1$ divided by the total number of positive documents, and likely $Prob_0$ is $P_0$ divided by the total number of negative documents. We add 1 to avoid division by 0.
pr1 = (p1+1) / ((y.items==positive).sum() + 1)
pr0 = (p0+1) / ((y.items==negative).sum() + 1)
Now, we define $r = log(\frac{Prob_1}{Prob_0})$
To predict whether a document is positive or negative, you calculate the dot product between it and the vector $r$.
So far so good, but now comes some bias term $b$ that I don't understand. It is defined in python as:
b = np.log((y.items==positive).mean() / (y.items==negative).mean())
I'm pretty sure they could simply use count instead of mean. But ok.
The next step is to calculate the predictions, which is:
$\hat{y} =$ \begin{cases} 1, & X_{doc} * r + b >0 \\ 0, & X_{doc} * r + b \le 0 \end{cases}
I get it that you might want to correct for the difference ratios of positive to negative documents. If they are similar than $b$ would be 0. But what I don't get is that if you calculate $r$ you see that this bias exists in every-word, so taking the dot product you would also sum it n times (where n is the length of the document), but in the final equation you just correct for it once.
What am I missing?