Bayes' Rule and Naive Bayes
Bayes' rule turns "how likely is this evidence if the email is spam?" into "how likely is spam, given this evidence?": the posterior is proportional to likelihood times prior. Naive Bayes pretends the features are independent once you know the class, so the likelihood becomes a product of simple per-feature probabilities, which we add up as logs so nothing underflows.
What the symbols mean
- Posterior
How probable class C is after seeing the evidence x.
- Likelihood
How probable the evidence x is if the class is C.
- Prior
How common class C is before looking at any evidence.
- Evidence
The overall probability of x. It's the same for every class, so it only normalizes.
What the symbols mean
- Feature
One piece of evidence, like whether a particular word appears.
- Proportional to
Equal up to a factor that's the same for every class; divide by the total to finish.
Explanation
Bayes' rule is simply the way to turn "how likely is this evidence if it's spam?" around into "how likely is it spam, given this evidence?". You met it in Bayes' Theorem; here it becomes a classifier. Read the formula as a sentence: the posterior (what you believe after the evidence) is the likelihood (how well class explains the evidence) times the prior (how common was to begin with), divided by (a total that's the same for every class and only makes things add to 1). So the working version is posterior ∝ likelihood × prior: score each class, then normalize. Naive Bayes adds one bold simplification, that the features don't influence each other once you know the class, and that's what makes it fast enough to run on every email you get. It's a short chain of ideas; take them one at a time.
Comparing classes, not computing . To decide whether an email is spam, you only need to compare with ("ham" is the field's word for not-spam). Both have the same denominator , so compare the numerators , and if you want actual probabilities, divide each one by their total at the end. In ratio form it's even cleaner, because cancels:
Posterior odds are prior odds times the likelihood ratio. A word that's 8 times more common in spam than in ham multiplies the odds by 8.
Updating as evidence arrives. Evidence can come one piece at a time. Each new piece multiplies the odds by its likelihood ratio, and today's posterior becomes tomorrow's prior. There's a picture further down that follows one email's spam probability as its words come in, and a second one that updates a belief about a coin flip by flip.
The naive assumption. The hard part is when is a whole email. With a vocabulary of 10,000 words, each present or absent, there are possible emails, and you could never count how often each one shows up. Naive Bayes assumes the words are conditionally independent given the class:
Now each class needs just one number per word, , which you estimate by counting: the fraction of spam emails that contain "free". That's 10,000 numbers per class instead of . The assumption is plainly false ("free" and "offer" travel together), so why does it work? Because a classifier only needs the right winner, not exact probabilities. Counting correlated evidence twice makes Naive Bayes overconfident, but it usually still picks the right class.
Work in logs. A long email multiplies hundreds of small numbers: 500 words with probabilities around 0.001 each give about , which underflows to 0 for every class, and then you can't compare them. Take logs and the product becomes a sum, exactly as in Maximum Likelihood and Cross-Entropy:
Pick the class with the biggest score. To turn scores back into probabilities, exponentiate and normalize, which is precisely a softmax of the scores. Now look at the two-class log-odds: . That's a bias plus one weight per word. Naive Bayes is a linear classifier, an artificial neuron whose weights were counted instead of trained.
Never let a count be zero. If "invoice" never appeared in the training ham, then , and one occurrence makes the whole ham product 0, no matter what the other 499 words say (in logs, ). The fix is Laplace smoothing: pretend you saw every outcome once more than you did. For a yes/no word feature, , where is the number of training emails in class .
A common mistake. It's tempting to treat as if it were : "the fraud detector catches 98% of fraud, so a flagged transaction is 98% likely to be fraud". When fraud is rare, that's badly wrong. If 0.5% of transactions are fraud and 3% of honest ones get flagged too, Bayes' rule gives . The prior matters, and ignoring it is called the base-rate fallacy.
See it
Worked examples
A spam filter has learned that . The word "free" appears in 40% of spam and 5% of ham, and the word "meeting" appears in 2% of spam and 10% of ham. An email contains both words. Using Naive Bayes (and ignoring every other word), find the probability that it's spam.
- 1
Write down what we know for each class. Spam: prior , and . Ham: prior , and .
Redo the same email in log-odds form, the way a real filter would, and check that you get the same answer.
- 1
In odds form, the posterior odds are the prior odds times one likelihood ratio per word. Taking turns those products into a sum, so we'll add three numbers.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A bank's fraud detector flags 95% of fraudulent transactions and 2% of legitimate ones. Fraud makes up 1% of all transactions. A transaction gets flagged. What is the probability that it's fraud? Give 4 decimal places.
A Naive Bayes model has three classes with log-scores of , and . What is the posterior probability of the first class? Give 4 decimal places.
The word "refund" appears in 0 of the 50 ham emails in a training set. Using Laplace smoothing , what is ? Give 4 decimal places.
A document classifier multiplies 300 word probabilities, each about . The product is . Find to 2 decimal places.
With 20 yes/no features, how many probabilities per class would it take to store the full joint distribution (one per combination, minus one because they add to 1)? Naive Bayes needs just 20.
A robot's bump sensor is either working (it beeps on 10% of normal steps) or faulty (it beeps on 60% of them). Before testing, the chance it's faulty is 5%. Over three steps it beeps, beeps again, then stays silent. Assuming the steps are independent given the sensor's state, what is the probability that it's faulty now? Give 4 decimal places.
Why does Naive Bayes often classify well even though its independence assumption is false?
The hardest one here. Get it, and you've got this lesson.
A spam filter is trained on 40 spam and 60 ham emails. "offer" appears in 24 spam and 3 ham emails, "lunch" in 2 spam and 18 ham, and "now" in 20 spam and 15 ham. A new email contains "offer" and "now" but not "lunch". Using Naive Bayes with yes/no word features (an absent word counts as evidence too) and Laplace smoothing , find . Give 4 decimal places.
Where it's used
Naive Bayes powered the first spam filters that really worked, and it's still a strong, fast baseline for text classification: training is one pass of counting, and classifying is a sum of looked-up weights. The same posterior-times-prior logic runs medical test interpretation, fault diagnosis in machines that report error codes, and the localization inside a robot vacuum, where each new sensor reading multiplies its likelihood into the belief about where the robot is.