On this page
Your spam folder fills up with fake lottery wins you never had to read. Google says Gmail blocks more than 99.9% of spam, phishing attempts and malware from reaching you. The core idea behind spam filtering is a page of school probability. You can run it with a pencil.
How does a spam filter work using probability?
A spam filter is a classifier: it outputs the probability that an email is spam. In 1998, Mehran Sahami, Susan Dumais, David Heckerman and Eric Horvitz described a Bayesian approach to filtering junk e-mail that learns from examples instead of hand-written rules, and treats the cost of each mistake separately.
The filter needs two piles of training email, one marked spam and one marked good. For every word, it counts how often the word turns up in each pile. That gives two numbers per word: P(word | spam) and P(word | not spam).
When a new email arrives, the filter uses Bayes' rule to turn "how often this word appears in spam" into "how likely this email is spam, given that it contains this word". It multiplies the evidence from each word, as if words were independent (they are not: "free" and "offer" travel together). That shortcut is why the method is called naive Bayes.
If Bayes' rule feels shaky, our walk through the medical test example of Bayes' theorem uses the same arithmetic with a different story.
Can you build a mini spam filter by hand?
Yes. Say you have 20 training emails: 10 spam and 10 good. You count three words.
"free" appears in 6 of the 10 spam emails and 1 of the 10 good ones.
"offer" appears in 5 spam emails and 1 good one.
"homework" appears in 1 spam email and 5 good ones.
So P(free | spam) = 0.6 and P(free | good) = 0.1. Half the training emails were spam, so the starting guess is P(spam) = 0.5. The filter marks an email as spam if the final probability is above 0.9.
A new email arrives: "Free offer just for you."
Spam score: 0.5 × 0.6 (free) × 0.5 (offer) = 0.15.
Good score: 0.5 × 0.1 (free) × 0.1 (offer) = 0.005.
Turn the scores into a probability: 0.15 ÷ (0.15 + 0.005) ≈ 0.97.
At 97%, it goes to the spam folder. Notice what happened after one word: "free" alone moved the guess from 50% to about 86%, and "offer" pushed it past the line.
A second email says: "Free homework help."
Spam score: 0.5 × 0.6 × 0.1 = 0.03.
Good score: 0.5 × 0.1 × 0.5 = 0.025.
Probability: 0.03 ÷ 0.055 ≈ 0.55.
At 55%, it stays in the inbox. One "good" word pulled the score back from 86% to 55%.
What do real filters add?
Paul Graham's 2002 essay A Plan for Spam described a working filter built this way. Trained on about 4,000 spam and 4,000 good emails, his version added a few tweaks:
It scored each email using only the fifteen most "interesting" words, the ones whose spam probability was farthest from 0.5.
A word it had never seen got a probability of 0.4, slightly leaning towards good.
It counted words from good email twice, so the filter was slower to call a real message spam.
Mail went to spam only above 0.9; Graham reported missing fewer than 5 in 1,000 spams with zero false positives on his own email.
The last two tweaks reflect a choice: losing a real email (say, a college admission letter) is worse than seeing one extra spam. Our explainer on accuracy, precision and recall shows how to measure that trade-off.
Why does some spam still get through?
The spammer's trick is to stuff the email with words that appear in normal mail. Daniel Lowd and Christopher Meek studied these good word attacks on statistical spam filters. Without any feedback from the filter, adding 150 words or fewer got 50% of blocked spam past naive Bayes and maximum entropy filters; when the attacker could test messages against the filter, 30 words were enough. Their early results suggested that retraining the filter often is an effective defence.
Spammers also misspell trigger words (the same paper mentions "vi@gra") so the filter may not recognise them. And a filter only knows what it has seen: a brand-new scam with fresh wording starts out close to the neutral guess. That is why marking an email as spam matters: in a filter that learns from its users, the emails you mark can become new examples in the spam pile it counts from.
The idea behind all this, updating a belief as evidence arrives, goes back to Thomas Bayes and his famous essay. If conditional probability still feels slippery, Fermi's AI tutor can work out what you already know and build a plan for the session, or you can look up Bayes' theorem in the Fermi Concept Library.



