The Bernoulli Model and Why Naive Bayes Works

The second event model for Naive Bayes, which records only which terms are present and absent. Its generative story and estimates, derived as maximum likelihood, the worked example where it disagrees with the multinomial model term by term, its log odds as a linear classifier whose weights are the Binary Independence Model's, how the two models compare on Reuters as the vocabulary grows, and why a classifier built on a false independence assumption classifies well while its probabilities, measured on Reuters, are badly calibrated.

The Bernoulli event model for Naive Bayes records which terms a document contains and which it lacks, estimating each term's probability as $(N_{ct} + 1)/(N_c + 2)$. Its log odds are a linear classifier whose presence weights are the Binary Independence Model's term weights. On the China/Japan example it counts Chinese once and chooses Japan, giving China 0.191 against the multinomial model's 0.690. On the ten largest Reuters categories, with features chosen by mutual information, it is best with 10 to 20 per category (0.800 micro-F1 at 10) and falls to 0.648 with all 18,135 terms, where the multinomial model reaches 0.835. Naive Bayes classifies well because only the winner must be right, but its probabilities are badly calibrated: Reuters stories given 0.999 to 0.999999 belong to the category 44.9 percent of the time.

In Naive Bayes classification, the test document Chinese Chinese Chinese Tokyo Japan went to China, with probability 0.690. Three occurrences of Chinese, with help from the prior, outvoted one each of Tokyo and Japan. That decision rested on two choices: three occurrences counted three times, and the words the document lacks counted for nothing. A different model of documents makes both choices the other way, and it reaches the opposite decision.

Naive Bayes needs a model of how a class generates a document, called its event model, and there are two natural choices. The multinomial model treats a document as a sequence of words drawn one at a time from the class's distribution over the vocabulary, so repeated words count repeatedly. The Bernoulli model treats a document as the outcome of one coin flip per vocabulary term: heads if the term appears and tails if not. It records which terms are present and also which are absent.

Here is the route. First we set out the Bernoulli model and work the China/Japan example through it. Next we write it as a linear classifier and find the Binary Independence Model's term weights inside it. Then we compare the two models on Reuters. Finally we explain why a classifier built on a false assumption works as well as it does, and measure what the assumption costs.

The Bernoulli model

The coin-flipping story differs from the multinomial one in what is drawn (Manning, Raghavan, and Schütze, 2008, §13.3–13.4). To write a document of class $c$, go through the vocabulary once. For each term $t$, flip a coin that comes up heads with probability $P(t \mid c)$, and include $t$ in the document if it does. The document is the set of terms that came up heads. How many times a term occurs, and in what order, is not part of the model.

For each class $c$ and each term $t$ in the vocabulary $V$, the model therefore has a parameter $P(t \mid c)$, the probability that a document of class $c$ contains $t$ at least once. A document's probability given the class multiplies over the whole vocabulary, one factor per coin:

$$P(d \mid c) = \prod_{t \in V} \begin{cases} P(t \mid c) & \text{if } t \in d, \\ 1 - P(t \mid c) & \text{if } t \notin d. \end{cases}$$

The parameters are estimated from document counts, smoothed by adding one to each of the two outcomes:

$$\hat{P}(t \mid c) = \frac{N_{ct} + 1}{N_c + 2},$$

where $N_{ct}$ is the number of training documents of class $c$ that contain $t$ and $N_c$ the number of training documents of class $c$. The maximum likelihood estimate, without the added counts, is the fraction $N_{ct}/N_c$ of the class's documents that contain the term.

Where the estimates come from

Here is the idea. Each term's coin is flipped once for every training document of the class, and the coins of different terms are independent, so each coin can be estimated by itself. The value that makes a coin's observed flips most probable is the fraction of heads.

The formal version. Fix a class $c$ and a term $t$, and write $p = P(t \mid c)$. Among the $N_c$ training documents of the class, $N_{ct}$ contain the term and $N_c - N_{ct}$ do not. Under the model each document contributes a factor $p$ or $1 - p$ for this term, independently of the other documents and of the other terms. The part of the training set's probability that involves $p$ is therefore

$$L(p) = p^{\,N_{ct}} (1 - p)^{\,N_c - N_{ct}}, \qquad \log L(p) = N_{ct} \log p + (N_c - N_{ct}) \log (1 - p).$$

No constraint links $p$ to any other parameter, so no Lagrange multiplier is needed, unlike in the multinomial derivation, where a class's term probabilities had to sum to 1. Setting the derivative to zero,

$$\frac{d}{dp} \log L(p) = \frac{N_{ct}}{p} - \frac{N_c - N_{ct}}{1 - p} = 0 \;\Longrightarrow\; N_{ct}(1 - p) = (N_c - N_{ct})\,p \;\Longrightarrow\; \hat{p} = \frac{N_{ct}}{N_c}.$$

The second derivative, $-N_{ct}/p^2 - (N_c - N_{ct})/(1 - p)^2$, is negative for $0 < p < 1$, so this is the maximum. When $N_{ct} = 0$ the function is $(1 - p)^{N_c}$, largest at $p = 0$, and when $N_{ct} = N_c$ it is $p^{N_c}$, largest at $p = 1$; both agree with the formula. The priors are the multinomial model's, $N_c/N$, since the first step of the story, choosing the class, is the same.

The unsmoothed estimates fail the way the multinomial ones did. A term that occurs in no training document of a class gets $\hat{p} = 0$ and vetoes the class for every document containing it. A term that occurs in all of them gets $\hat{p} = 1$ and vetoes the class for every document lacking it.

The smoothed estimate is the two-outcome case of the result derived in Naive Bayes classification. A coin has $B = 2$ outcomes, with counts $N_{ct}$ and $N_c - N_{ct}$. Under a uniform prior on $p$, the posterior mean of an outcome's probability is its count plus one, divided by the total count plus the number of outcomes: $(N_{ct} + 1)/(N_c + 2)$. This is Laplace's rule of succession, and the probability primer derives it directly from the Beta distribution.

This is the same document representation as the Binary Independence Model. The BIM's probabilities $p_t$ and $u_t$ are Bernoulli parameters for the relevant and nonrelevant classes. The section on the linear form below shows that the connection is exact.

Where the two models disagree

Back to the China/Japan example. It has three training documents of class China (Chinese Beijing Chinese, Chinese Chinese Shanghai, Chinese Macao) and one of class Japan (Tokyo Japan Chinese), and the test document Chinese Chinese Chinese Tokyo Japan. The multinomial model chose China. The Bernoulli model counts documents, not tokens. Chinese occurs five times in the China documents but in only three documents, so $N_{ct} = 3$. The estimates $(N_{ct} + 1)/(N_c + 2)$, with $N_c = 3$ for China and $N_c = 1$ for Japan (Manning, Raghavan, and Schütze, 2008, §13.3):

Term China: $N_{ct}$ and estimate Japan: $N_{ct}$ and estimate In test?
Chinese 3, 4/5 = 0.8 1, 2/3 yes
Japan 0, 1/5 = 0.2 1, 2/3 yes
Tokyo 0, 1/5 = 0.2 1, 2/3 yes
Beijing 1, 2/5 = 0.4 0, 1/3 no
Macao 1, 2/5 = 0.4 0, 1/3 no
Shanghai 1, 2/5 = 0.4 0, 1/3 no

Example (reading the decision off the table). The Bernoulli model chooses Japan, and the rows for the three terms in the test document already show why. Chinese is only slightly more likely in China (0.8 against 2/3), while Tokyo and Japan are far less likely (0.2 against 2/3). The calculation confirms it:

$$\hat{P}(\textit{China} \mid d) \propto \tfrac{3}{4} \times 0.8 \times 0.2 \times 0.2 \times (1 - 0.4)^3 \approx 0.005,$$

$$\hat{P}(\textit{Japan} \mid d) \propto \tfrac{1}{4} \times \left(\tfrac{2}{3}\right)^3 \times \left(1 - \tfrac{1}{3}\right)^3 \approx 0.022.$$

The same calculation in natural logarithms, one row per coin, shows where the decision comes from (computed by tools/experiments/reuters/china_japan.py, rounded to three decimals):

Factor China Japan Difference, China − Japan
Prior log 0.75 = −0.288 log 0.25 = −1.386 +1.099
Chinese present log 0.8 = −0.223 log 2/3 = −0.405 +0.182
Tokyo present log 0.2 = −1.609 −0.405 −1.204
Japan present −1.609 −0.405 −1.204
Beijing absent log 0.6 = −0.511 log 2/3 = −0.405 −0.105
Macao absent −0.511 −0.405 −0.105
Shanghai absent −0.511 −0.405 −0.105
Total −5.262 −3.819 −1.443

The log odds are −1.443, and normalizing the two scores gives $\hat{P}(\textit{China} \mid d) = 0.191$, against 0.690 under the multinomial model. The decisive difference is that the Bernoulli model counts Chinese once. In the multinomial model its three occurrences multiplied the odds for China by $(27/14)^3 \approx 7.2$. Here its single presence multiplies them only by $0.8/(2/3) = 1.2$, a log contribution of 0.182. That is far too little to offset Japan and Tokyo. Those two are present in the one Japan document but in none of the China documents, and each costs China 1.204.

The model also counts absences. Beijing, Macao, and Shanghai each appear in China documents, so their absence from the test document counts against China, 0.105 each. A natural guess is that the absences decide the case. They do not: the decision would be Japan even without them. Leaving out the absent terms, the scores are 0.024 for China and 0.074 for Japan. Which answer is right depends on whether repetition and absence carry information in the collection at hand.

The linear form, and the BIM's term weights

The multinomial model turned out to be a weighted sum of term counts. Does the Bernoulli model have the same shape? It does, and that connects it to retrieval. Here is the plan: taking logs splits the score into a part that is the same for every document and a sum over the terms the document contains.

The formal version. Let $x_t = 1$ if term $t$ is in the document and 0 otherwise, and write $p_t = \hat{P}(t \mid c)$ and $u_t = \hat{P}(t \mid \bar{c})$. Each coin's factor can be written without cases as $p_t^{x_t} (1 - p_t)^{1 - x_t}$, so

$$\log P(d \mid c) = \sum_{t \in V} \Big[ x_t \log p_t + (1 - x_t) \log (1 - p_t) \Big] = \sum_{t \in V} \log (1 - p_t) + \sum_{t \in V} x_t \log \frac{p_t}{1 - p_t}.$$

The first sum does not depend on the document. Subtracting the same expression for $\bar{c}$ and adding the prior ratio gives the log odds:

$$\log \frac{\hat{P}(c \mid d)}{\hat{P}(\bar{c} \mid d)} = \underbrace{\log \frac{\hat{P}(c)}{\hat{P}(\bar{c})} + \sum_{t \in V} \log \frac{1 - p_t}{1 - u_t}}_{\text{constant}} + \sum_{t \in d} \log \frac{p_t (1 - u_t)}{u_t (1 - p_t)}.$$

The weight on each present term, $\log \frac{p_t (1 - u_t)}{u_t (1 - p_t)}$, is exactly the term weight $c_t$ of the Binary Independence Model, with class $c$ in the role of "relevant". The BIM ranks documents by the sum of these weights over the query terms they contain. It assumes that terms outside the query are equally likely in relevant and nonrelevant documents, which makes their weights zero. Bernoulli Naive Bayes classifies by the sum over all vocabulary terms the document contains, plus a constant, compared with zero. The two are the same model put to different uses. One ranks, so it can drop the constant; the other decides, so it cannot.

Now the check on the running example. The constant is the prior's $\log 3 = 1.099$ plus $\sum_t \log \frac{1 - p_t}{1 - u_t}$ over all six terms: $-0.511$ for Chinese, $-0.105$ for each of the three cities, and $+0.875$ for each of Tokyo and Japan, so 2.023 in total. The presence weights are:

Term Presence weight $\log \frac{p_t(1 - u_t)}{u_t(1 - p_t)}$
Chinese 0.693
Beijing, Macao, Shanghai 0.288
Tokyo, Japan −2.079

The test document contains Chinese, Tokyo, and Japan, so its log odds are $2.023 + 0.693 - 2.079 - 2.079 = -1.443$ (summed before rounding), the same as the table above. The form also suggests why the Bernoulli model suffers with large vocabularies. The constant holds a term for every word in the vocabulary, so every word a document lacks has, in effect, cast a vote. The Reuters experiment below shows the loss of accuracy; it does not isolate this cause.

Which model is better?

The toy example cannot say which model to prefer. Real collections can. McCallum and Nigam (1998) compared the two on five text collections. The Bernoulli model performed well with small vocabularies, but its accuracy fell as the vocabulary grew. The multinomial model improved with larger vocabularies and was better overall. One reason is the absence terms. With a vocabulary of tens of thousands of terms, the product over absent terms dominates the Bernoulli score, and most of those absences are uninformative. Long documents also suffer, for a different reason: the model ignores how often a term occurs, so a single occurrence in a long document counts as much as a document full of the term. The course text's example is an entire book assigned to the class China because the word occurs in it once (Manning, Raghavan, and Schütze, 2008, §13.3). This article does not measure the effect of document length on Reuters.

Example (which model gains from a larger vocabulary). On Reuters, as the number $k$ of terms each binary classifier keeps grows from 10 to all 18,135, the Bernoulli model loses (0.800 to 0.648 micro-F1) and the multinomial model ends higher than it began (0.781 to 0.835). That is what McCallum and Nigam's finding predicts, since every added term is one more coin whose absence the Bernoulli model must count. The multinomial model does not gain at every step; the paragraph after the table describes its dip. Both models were trained on the ten largest categories with the top $k$ terms per category selected by mutual information (see feature selection), for $k$ from 10 to all 18,135:

Notice that the two curves cross. Micro- and macro-averaged F1 of multinomial (circles, solid) and Bernoulli (squares, dashed) Naive Bayes on the Reuters test stories as the number of features per category grows on a log scale; the rightmost points use all 18,135 terms. The Bernoulli model is best with 10 to 20 features. Its macro-F1 falls at every larger size, to 0.396 with the full vocabulary; its micro-F1 falls to 0.629 at 5,000 and 10,000 features and ends at 0.648 with all 18,135 terms. The multinomial model dips between 50 and 100 features and is best with all of them.
Notice that the two curves cross. Micro- and macro-averaged F1 of multinomial (circles, solid) and Bernoulli (squares, dashed) Naive Bayes on the Reuters test stories as the number of features per category grows on a log scale; the rightmost points use all 18,135 terms. The Bernoulli model is best with 10 to 20 features. Its macro-F1 falls at every larger size, to 0.396 with the full vocabulary; its micro-F1 falls to 0.629 at 5,000 and 10,000 features and ends at 0.648 with all 18,135 terms. The multinomial model dips between 50 and 100 features and is best with all of them.
Features per category Bernoulli micro / macro F1 Multinomial micro / macro F1
10 0.800 / 0.713 0.781 / 0.686
50 0.789 / 0.679 0.680 / 0.557
100 0.765 / 0.643 0.676 / 0.548
500 0.673 / 0.536 0.710 / 0.576
1,000 0.664 / 0.530 0.735 / 0.602
5,000 0.629 / 0.468 0.779 / 0.650
All 18,135 0.648 / 0.396 0.835 / 0.721

The Bernoulli model does best with the smallest feature sets and loses most of its advantage as more are added: micro-F1 falls from 0.800 at 10 features to 0.629 at 5,000 and ends at 0.648 with all terms, and macro-F1 falls at every step. The multinomial model is best with all the terms, but it does not gain steadily: it falls from 0.781 micro-F1 at 10 features to 0.676 at 100 before rising. Feature selection traces that dip to the terms mutual information selects. With up to about 200 features it beats the multinomial model with the same features, at 50 features by 0.789 against 0.680 micro-F1. But the multinomial model with all 18,135 features reaches 0.835 micro-F1 and 0.721 macro-F1, better than any Bernoulli setting on micro-F1. With features chosen by the χ² statistic instead, the Bernoulli model's best result is 0.817 micro-F1 and 0.729 macro-F1 at 10 features. That is slightly better than the full multinomial model on macro-F1 and still worse on micro-F1. The Bernoulli model suits short documents and small, carefully chosen vocabularies, and the multinomial model suits everything else.

Why Naive Bayes works

Here is the puzzle. Both models assume that terms occur independently given the class. For text this is badly wrong: words come in phrases, topics bring clusters of related words, and a document that mentions Hong almost certainly mentions Kong. Yet Naive Bayes is competitive with far more elaborate methods on many text tasks. The explanation lies in what classification requires.

Classification needs only the right winner. A classifier is correct if the true class receives the highest score. The scores need not be accurate probabilities. Domingos and Pazzani (1997) showed that Naive Bayes is optimal under zero-one loss, the loss that counts only whether the chosen class is correct, in many situations where the independence assumption is violated. Violations distort the estimated probabilities, often by large factors, but frequently not enough to change which class is largest.

Violations push toward the same answer. Correlated words that indicate a class are counted as independent evidence, so their contribution is counted more than once. When Hong and Kong both appear, a classifier that treats them as independent adds both weights, though the second word carries almost no new information. When the correlated words all point to the true class, the overcounting makes the classifier more confident without changing its decision.

Example (a duplicated feature). Counting one piece of evidence $k$ times leaves the decision unchanged when the classes are equally likely beforehand, and it can change the decision when they are not. Take two classes and one binary feature $x$ with $P(x = 1 \mid c) = 0.8$ and $P(x = 1 \mid \bar{c}) = 0.4$. Observing $x = 1$ multiplies the odds of $c$ by $0.8/0.4 = 2$, a log odds weight of $\log 2 = 0.693$. Now suppose the data hold $k$ copies of the feature that always agree, as Hong and Kong nearly do. The copies carry no more information than one, so the true log odds given that all are 1 are $b + \log 2$, where $b$ is the prior log odds. Naive Bayes treats the copies as independent and computes $b + k \log 2$.

  • With equal priors, $b = 0$. The true posterior probability of $c$ is $2/3 = 0.667$. Naive Bayes reports $2^k/(2^k + 1)$: 0.800 for two copies, 0.889 for three and 0.999 for ten. Both log odds are positive for every $k$, so the decision is always right and the probability is always overstated.
  • With a prior against the class, say $b = -1$, the true log odds are $-1 + 0.693 = -0.307$ and the correct decision is $\bar{c}$, with $P(c \mid x) = 0.424$. With two copies Naive Bayes computes $-1 + 1.386 = 0.386$, reports 0.595, and decides $c$.

So double counting cannot change the decision when the overcounted evidence and everything else point the same way, which is the usual case for a document about one topic, and it always distorts the probability. This is a constructed case that shows the mechanism; Domingos and Pazzani (1997) give the general conditions.

The probabilities are poorly calibrated. The price of double counting is that Naive Bayes's probability estimates are extreme, usually very close to 0 or 1 (Manning, Raghavan, and Schütze, 2008, §13.4).

Example (what a reported 0.999 means). On Reuters, when a Naive Bayes category classifier reports a probability between 0.999 and 0.999999 that a test story belongs to its category, the story belongs 44.9 percent of the time. The face value says at least 99.9 percent. Each of the ten category classifiers produced a posterior probability for each of the 3,019 test stories, 30,190 probabilities in all. Grouping them by the probability reported and counting how many stories in each group actually belong to the category:

Notice that in every group above 0.1 the bar falls far short of its diamond. This is the calibration of multinomial Naive Bayes on Reuters. The 30,190 posterior probabilities (ten categories times 3,019 test stories) are grouped by the value Naive Bayes reported. Bars show the share of each group's stories actually in the category, diamonds the average probability reported, and n the group's size. For a calibrated classifier the bars would reach the diamonds. Of the stories given 0.9 to 0.999, only 20 percent are in the category; of those given 0.999 to 1 − 10⁻⁶, 45 percent.
Notice that in every group above 0.1 the bar falls far short of its diamond. This is the calibration of multinomial Naive Bayes on Reuters. The 30,190 posterior probabilities (ten categories times 3,019 test stories) are grouped by the value Naive Bayes reported. Bars show the share of each group's stories actually in the category, diamonds the average probability reported, and n the group's size. For a calibrated classifier the bars would reach the diamonds. Of the stories given 0.9 to 0.999, only 20 percent are in the category; of those given 0.999 to 1 − 10⁻⁶, 45 percent.

96.5 percent of the probabilities are below 0.001 or above 0.999. Of the 2,866 probabilities above $1 - 10^{-6}$, 85.5 percent belong to stories actually in the category. Of the 314 between 0.999 and $1 - 10^{-6}$, only 44.9 percent do. A probability of 0.999 here means roughly a coin flip. The ranking is still useful. Higher probabilities do mean more stories in the category, so the order of the scores carries information that the values themselves misstate.

Calibrated probabilities matter in two common settings: setting a threshold that trades precision for recall, and combining a classifier's output with other evidence. There the scores must be recalibrated or another method used. Recalibration fits a monotone function, on held-out data, that maps each score to the observed share of positives among stories with that score. Two standard choices are a logistic curve fitted to the scores, known as Platt scaling (Platt, 1999), and a non-decreasing step function, isotonic regression (Zadrozny and Elkan, 2002). Niculescu-Mizil and Caruana (2005) compared the two across learning methods and found the pattern measured here: Naive Bayes pushes its probabilities toward 0 and 1. Because the fitted function never decreases, recalibration changes the reported probabilities without reversing the order of any two stories. Platt scaling, a strictly increasing curve, keeps the ranking exactly; isotonic regression can merge neighboring scores into ties. Neither method was run on the Reuters scores for this article. The library's binary classification metrics treats calibration and reliability diagrams in general, and Calibration and Proper Scoring Rules measures and repairs it with proper scores. Evaluating classifiers shows that moving the threshold well above the nominal probability of 0.5 improves F1 substantially on these categories.

Simple models resist overfitting. With little training data, a model with few parameters, one probability per term per class and nothing for interactions between terms, cannot fit noise in those interactions. Naive Bayes therefore does relatively well on small training sets, which is common in practice.

Its weaknesses are also well understood. Rennie, Shih, Teevan, and Karger (2003) identified several systematic biases in multinomial Naive Bayes for text, such as favoring classes with more training data. They showed that simple corrections brought it close to support vector machines, the linear classifiers that vector space classification introduces. Two of the corrections were training on the complement of each class and transforming term frequencies. On a set of ten Reuters categories, a k-nearest-neighbor classifier with 30 neighbors does better than Naive Bayes on both averages. Yang and Liu's (1999) comparison, described in that article, also ranked Naive Bayes below k nearest neighbors and support vector machines.

Worked problems

Problems 1, 2 and 3 are Exercises 13.9 (its Bernoulli parts), 13.2 and 13.4 of the course text (Manning, Raghavan, and Schütze, 2008), restated in our own words with their data. Problem 4 is posed on the running example. The script tools/experiments/reuters/china_japan.py recomputes every number.

Problem 1. The training set has four documents. Two are in class China: Taipei Taiwan and Macao Taiwan Shanghai. Two are not: Japan Sapporo and Sapporo Osaka Taiwan. Estimate a Bernoulli Naive Bayes classifier and classify the test document Taiwan Taiwan Sapporo. (Naive Bayes classification solves the same problem with the multinomial model, which gives $\hat{P}(c \mid d) = 3/7$.)

Solution. Write $c$ for China. Each class has $N_c = 2$ documents, so both priors are $1/2$ and every estimate is $(N_{ct} + 1)/(2 + 2)$. The Bernoulli model needs all seven terms, because the absent ones count:

Term $N_{ct}$ in $c$ $\hat{P}(t \mid c)$ $N_{ct}$ in $\bar{c}$ $\hat{P}(t \mid \bar{c})$ In test?
Taiwan 2 3/4 1 1/2 yes
Sapporo 0 1/4 2 3/4 yes
Taipei 1 1/2 0 1/4 no
Macao 1 1/2 0 1/4 no
Shanghai 1 1/2 0 1/4 no
Japan 0 1/4 1 1/2 no
Osaka 0 1/4 1 1/2 no

The test document contains Taiwan and Sapporo and lacks the other five. A present term contributes its estimate and an absent term one minus its estimate:

$$\hat{P}(c \mid d) \propto \tfrac{1}{2} \cdot \tfrac{3}{4} \cdot \tfrac{1}{4} \cdot \left(\tfrac{1}{2}\right)^3 \cdot \left(\tfrac{3}{4}\right)^2 = \tfrac{27}{4096} = 0.00659,$$

$$\hat{P}(\bar{c} \mid d) \propto \tfrac{1}{2} \cdot \tfrac{1}{2} \cdot \tfrac{3}{4} \cdot \left(\tfrac{3}{4}\right)^3 \cdot \left(\tfrac{1}{2}\right)^2 = \tfrac{81}{4096} = 0.01978.$$

The classifier assigns the document to $\bar{c}$, not China, with $\hat{P}(c \mid d) = 27/(27 + 81) = 1/4$ and log odds $\log(1/3) = -1.099$. Both models reach the same decision here, and the Bernoulli model is the more confident. It counts Taiwan once, a factor of $3/2$ for China where the multinomial model had $(3/2)^2$. The five absences partly cancel: the three absent China terms contribute $(2/3)^3 = 8/27$ and the two absent terms of the other class $(3/2)^2 = 9/4$, a net factor of $2/3$ against China. That exactly offsets the factor $3/2$ from Taiwan, so the odds are the prior's 1 times Sapporo's $1/3$, which gives $\hat{P}(c \mid d) = 1/4$.

Problem 2. Consider three documents: (1) He moved from London, Ontario, to London, England. (2) He moved from London, England, to London, Ontario. (3) He moved from England to London, Ontario. Which of them have the same representation under the Bernoulli model, and which under the multinomial model?

Solution. Under the Bernoulli model a document is the set of terms it contains. All three documents contain exactly the terms he, moved, from, london, ontario, to and england, so all three have the same representation. Under the multinomial model a document is its bag of terms, with counts. Documents 1 and 2 both have london twice and every other term once, so they are identical. Document 3 has london once, so it differs from the other two in that one count. Neither model can tell document 1 from document 2, although they describe moves in opposite directions: the difference lies in word order, which both models discard. And only the multinomial model can tell document 3 from the others.

Problem 3. The course text's comparison of the two models gives estimates for the word the in a class: about 0.05 under the multinomial model and about 1.0 under the Bernoulli model. Explain the difference.

Solution. The two numbers estimate different probabilities. The multinomial parameter is the probability that a token drawn from the class is the, estimated by the share of the class's tokens that are the. By the value in the text's Table 13.3, about one token in twenty is the, hence 0.05. The Bernoulli parameter is the probability that a document of the class contains the at least once, estimated by the share of the class's documents that contain it. Nearly every document of more than a few words does, hence about 1.0. The running example shows the same contrast on a small scale: Chinese has multinomial estimate $6/14 = 0.429$ in the China class, where it is 5 of 8 tokens, and Bernoulli estimate $4/5 = 0.8$, since all 3 documents contain it.

Problem 4. In the running example, a document is represented by which of the six vocabulary terms it contains, so there are $2^6 = 64$ possible documents. Find how many of them the Bernoulli classifier assigns to China, and describe them.

Solution. The linear form gives the log odds as the constant 2.023 plus the presence weights of the terms the document contains: 0.693 for Chinese, 0.288 for each of Beijing, Macao and Shanghai, and $-2.079$ for each of Tokyo and Japan. Split the documents by how many of Tokyo and Japan they contain.

  • Neither (16 documents). The log odds are at least 2.023, so all 16 go to China, including the empty document.
  • Exactly one (32 documents: two choices of the term, 16 choices of the rest). The log odds are $2.023 - 2.079 = -0.057$ (computed before rounding) plus the weights of the other terms present. With none of the other four terms the document goes to Japan. With any of them it goes to China, since the smallest of those weights, 0.288, exceeds 0.057. That is $2 \times 15 = 30$ documents for China and 2 for Japan.
  • Both (16 documents). The log odds are $2.023 - 4.159 = -2.136$ plus at most $0.693 + 3 \times 0.288 = 1.556$ (again before rounding), so they are negative, and all 16 go to Japan.

In total $16 + 30 = 46$ of the 64 documents are assigned to China and 18 to Japan. The script enumerates all 64 and agrees. The test document of this article is in the third group. The count also shows how strongly the constant leans toward China: a document must contain both Tokyo and Japan, or one of them and nothing else, to be assigned to Japan.

What we have learned

The Bernoulli model describes a document by which terms it contains and which it lacks. Its presence weights are the Binary Independence Model's term weights. It counts Chinese once and chooses Japan. On Reuters it is best with 10 to 20 features, the multinomial model with all 18,135. Both classify well because a classifier needs only the right winner, though their probabilities are badly calibrated.

A classifier that is to use only some of the 18,135 terms must choose which to keep. Feature selection works out the standard measures on grain.

References

  • Domingos, P., and Pazzani, M. (1997). On the optimality of the simple Bayesian classifier under zero-one loss. Machine Learning, 29(2–3), 103–130. record
  • Manning, C. D., Raghavan, P., and Schütze, H. (2008). Introduction to Information Retrieval, sections 13.3–13.4. Cambridge University Press. record · 102 pages
  • McCallum, A., and Nigam, K. (1998). A comparison of event models for Naive Bayes text classification. In Learning for Text Categorization: Papers from the 1998 Workshop, AAAI Technical Report WS-98-05, 41–48. record
  • Niculescu-Mizil, A., and Caruana, R. (2005). Predicting good probabilities with supervised learning. In Proceedings of ICML 2005, 625–632. record · 2 pages
  • Platt, J. C. (1999). Probabilistic outputs for support vector machines and comparisons to regularized likelihood methods. Published as "Probabilities for SV machines" in A. J. Smola, P. Bartlett, B. Schölkopf, and D. Schuurmans (Eds.), Advances in Large Margin Classifiers (2000), 61–74. MIT Press. record
  • Rennie, J. D. M., Shih, L., Teevan, J., and Karger, D. R. (2003). Tackling the poor assumptions of Naive Bayes text classifiers. In Proceedings of ICML 2003, 616–623. record
  • Yang, Y., and Liu, X. (1999). A re-examination of text categorization methods. In Proceedings of SIGIR 1999, 42–49. record · 2 pages
  • Zadrozny, B., and Elkan, C. (2002). Transforming classifier scores into accurate multiclass probability estimates. In Proceedings of KDD 2002, 694–699. record

Reviewed: verified, 16 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 11 revisions