A maintenance desk receives short messages from equipment operators and has to decide quickly which ones are urgent. Past messages show how often words such as stop and heat appear in urgent and in routine messages, but the question about a new message runs the other way, from its words to its class. Naive Bayes reverses the direction with Bayes’ rule and keeps the model small by assuming that the words are independent within each class. This lesson builds the classifier from class priors and word probabilities, smooths the estimates, computes with logarithms, and shows what the independence assumption can break.
Key terms
Generative classifier
Definition A generative classifier models how the features arise within each class. For classes it specifies class probabilities and, for each class, a distribution of the feature vector, a probability mass function for discrete features or a density for continuous ones. Together they give the joint distribution , where , and Bayes’ rule turns it into class probabilities,
for every at which the denominator is positive. The products are unnormalized scores. When all errors cost the same, predicting the class with the largest posterior probability minimizes the probability of an error, provided the model is correct. A discriminative classifier such as logistic regression instead models directly and says nothing about how varies within a class.
In plain words A generative classifier first learns what messages of each class look like, then uses Bayes’ rule to ask which class most plausibly produced the message in hand. The largest score picks the same class as the largest probability, because every score is divided by the same total. A score becomes a probability only after that division.
Example Ten constructed training messages are labeled urgent or routine, and each is described by two binary features, when the word stop appears and when heat appears. A fitted model, whose numbers the following cards derive, has priors and , and gives a message containing both words the probabilities and . The scores are and , which sum to . So
The score on its own is not the probability that the message is urgent.
Class prior
Definition The class prior is the probability of class before any features are seen, with and . If of the training examples belong to class , the maximum likelihood estimate is . That estimate suits prediction only when the training examples were sampled with the class proportions of the population where the classifier will be used. When the classes were collected in fixed numbers or rebalanced, the priors should be set to the deployment proportions instead. Changing the priors multiplies the posterior odds of class against class by the same factor as their prior odds and leaves the likelihoods untouched.
In plain words The prior says how common each class is. The features then move the probability up or down from that starting point. A rare class needs stronger evidence from the features to win.
Example The training set has six urgent and four routine messages, so , and the prior odds of urgent are . For a message with both words, the likelihoods from the first card have ratio , so the posterior odds of urgent are
which again gives . If only percent of the messages in service were urgent, the prior odds would be and the posterior odds , a posterior probability of about . The same message would then be classified as routine under equal costs.
Naive Bayes assumption
Definition The naive Bayes assumption states that the features are conditionally independent given the class. For every class and every feature vector ,
It is a modeling assumption, not a fact about the data, and it says nothing about independence of the features when the classes are pooled. Its value is economy. For binary features a general distribution within one class needs probabilities, while the assumption needs only , one for each feature.
In plain words Once the class is known, learning that one word appears is assumed to say nothing more about whether another word appears. The words can still be associated across all messages, simply because urgent messages contain more of both. The assumption fails when one word tends to bring another along within the same class, for example if writers who mention heat also tend to write stop.
Example Among the six urgent messages, four contain stop and three contain heat. Among the four routine messages, one contains each word. These counts do not say how many messages contain both words. Take the unsmoothed proportions as estimates, for stop and for heat among urgent messages and for each word among routine ones. The assumption then implies that of the six urgent messages are expected to contain both. Under the same fitted model the probability of stop is , the probability of heat is , and the probability of both is
not . The words are dependent overall although the model makes them independent within each class. With binary features a general model would need probabilities per class, against under the assumption.
Laplace smoothing
Definition For a binary feature and a class with training examples, of which have , the smoothed estimate of is
with a smoothing constant . Laplace smoothing is the choice , which adds one imagined example with the feature present and one with it absent to every class. It is the posterior mean of under a uniform prior. With the formula is the maximum likelihood estimate , which equals when the feature never appears in the class and when it always does. For every the estimate lies strictly between and . The constant is a modeling choice, fixed in advance or tuned on validation data, and the counts come from the training data only.
In plain words Without smoothing, a word that never appeared in the urgent training messages would get probability for that class, and no future message containing it could ever be classified as urgent, however strong the other evidence. Smoothing pulls every estimate a little toward , by less when the class has many examples. It is a rule for estimating, not a set of extra observations to report or evaluate.
Example The smoothed estimates are
These give the likelihoods of the first card, and . Now suppose a third word, noise, appeared in none of the six urgent messages and in two of the four routine ones. Unsmoothed, its urgent probability is , and every message containing noise gets posterior probability exactly for urgent. Smoothed, the estimates are for urgent and for routine, so the word counts as evidence for routine without overriding everything else.
Naive Bayes classifier
Definition The Bernoulli naive Bayes classifier works with binary features in for . It combines class priors with the probabilities under the naive Bayes assumption,
so a present feature contributes and an absent one contributes . It is fitted with , or with deployment priors, and smoothed estimates . It predicts the class with the largest score , which is the class with the largest estimated posterior probability, and ties are broken by a fixed rule. Unequal error costs call for a different decision rule applied to the posterior probabilities. Multinomial naive Bayes is a different model, built for word counts, in which the likelihood comes from how often each vocabulary word occurs, so the feature representation must be stated.
In plain words Each feature casts a vote that depends on the class, through its probability when present or its complement when absent, and the votes are multiplied with the prior. With two classes, the posterior odds are the prior odds times one likelihood ratio per feature, which makes every decision easy to inspect. In the Bernoulli model a missing word is evidence too.
Example A message with both words has urgent score and routine score , as in the first card. Its posterior odds factor as the prior odds , the ratio for stop and the ratio for heat, with product . A message with neither word gets
so its urgent probability is and the equal-cost decision is routine. The two absent words moved the odds away from urgent, which a model that ignored absent words could not do.
Log-probability score
Definition The log-probability score of class for a feature vector is the natural logarithm of its unnormalized score. In the Bernoulli naive Bayes model, with all strictly between and , the present features add and the absent ones add ,
The logarithm is strictly increasing, so the class with the largest is the class with the largest score. To recover probabilities, subtract the largest log score before exponentiating,
The common factor cancels, so the result is exactly the normalized posterior, and the largest term in the denominator equals , so the division cannot become .
In plain words A product of many probabilities below quickly becomes smaller than the smallest positive number a computer stores, and then every class scores . A sum of logarithms stays an ordinary number, and only the differences between the class sums matter. Smoothing keeps every logarithm finite.
Example For the message with both words,
The difference equals , and the urgent probability is , the same as before. With features each contributing a factor , the direct product is below the smallest positive double-precision number, about , and rounds to , while the log score is computed without trouble.
Check yourself
- Using the fitted model on this page, with priors for urgent and for routine, smoothed probabilities for stop and for heat among urgent messages and for each word among routine messages, classify a message that contains stop but not heat. Give both scores, the urgent posterior probability and the equal-cost decision.
Show the answer
The absent word contributes the complement of its probability. The urgent score is , unchanged from the message with both words because . The routine score is . So
and the equal-cost decision is urgent. As a check, the posterior odds are , where is the ratio for the absent heat. The evidence for urgent is weaker than for the message with both words, whose probability was about .
- Suppose none of the four routine training messages had contained stop, with all other counts unchanged. Find the urgent posterior probability of a message containing both words without smoothing and with Laplace smoothing.
Show the answer
Without smoothing the routine estimate for stop is , so the routine score is and the urgent posterior probability is exactly . No other feature could change that. With Laplace smoothing the routine estimate is , so the routine score is , while the urgent score stays . The posterior is
Four routine messages without the word are weak evidence that the word can never occur in a routine message, and smoothing keeps the estimate positive.
- A colleague adds a copy of the stop column under a second name, so every message now has three features. What does naive Bayes now give for a message containing both words, and does the agreement between the two identical columns support the conditional independence assumption?
Show the answer
The copy has the same smoothed probabilities as stop, so its ratio enters the posterior odds a second time,
and the urgent probability rises to from , although the message carries no new information. The agreement does not support the assumption. Two identical columns are perfectly dependent within each class, unless the word is always present or always absent there, and the model counts the same evidence twice. Correlated features do the same to a lesser degree, so a naive Bayes classifier can choose labels well while its probabilities are too extreme. Its probabilities should be checked for calibration on held-out messages before anyone relies on them.