Tuesday , December 1 2026
Bayes theorem with prior, likelihood and posterior, a tested spam-filter example, sequential updating, the base-rate fallacy and Naive Bayes.

Bayes’ Theorem Explained with a Spam Filter Example

Updated: 29 November 2026Level: BeginnerReading time: about 8 minutesPython · tested

Bayes’ theorem tells us how to update a probability when we get new evidence. It powers spam filters, medical test interpretation, Naive Bayes classifiers and Bayesian machine learning.

What You Will Learn

  • The formula and each of its parts
  • A spam-filter example step by step
  • Updating beliefs with several pieces of evidence
  • The base-rate fallacy
  • How Naive Bayes uses the theorem

Prerequisites

Probability.

The Formula

P(A | B) = P(B | A) × P(A) / P(B)

Part Name Spam example
P(A) Prior Share of all emails that are spam
P(B | A) Likelihood How often spam contains the word “free”
P(B) Evidence How often any email contains “free”
P(A | B) Posterior Probability an email is spam given it contains “free”

Example: Is This Email Spam?

Example
p_spam = 0.20                 # 20% of emails are spam
p_free_given_spam = 0.60      # 60% of spam contains "free"
p_free_given_ham = 0.05       # 5% of normal mail contains "free"

p_free = p_free_given_spam * p_spam + p_free_given_ham * (1 - p_spam)
print(round(p_free, 3), round(p_free_given_spam * p_spam / p_free, 3))
Output
0.16 0.75

Explanation: “free” appears in 16% of all emails (law of total probability). Seeing it raises the spam probability from the prior 20% to a posterior of 75%.

Updating with More Evidence

Example
post = 0.20
for p_word_spam, p_word_ham in [(0.6, 0.05), (0.3, 0.1)]:     # "free", then "winner"
    post = p_word_spam * post / (p_word_spam * post + p_word_ham * (1 - post))
    print(round(post, 4))
Output
0.75
0.9

Explanation: Each posterior becomes the next prior. After a second spammy word, the probability rises to 90%. Naive Bayes does exactly this across all words, assuming words are independent given the class.

The Base-Rate Fallacy

People often ignore the prior. In the medical-test example, a 95%-sensitive test for a disease that only 1% of people have gives a positive result that is right only about 9% of the time, because false positives from the large healthy group outnumber the true positives. Rare events need very specific tests.

Bayes in Machine Learning

  • Naive Bayes classifiers for text and spam – see the spam detection project.
  • Bayesian optimisation for hyperparameter tuning.
  • Bayesian inference to express uncertainty in model parameters.
  • Interpreting classifier probabilities when classes are imbalanced.

Series

Maths & Statistics series: Introduction to Statistics · Descriptive Statistics · Probability · Bayes’ Theorem · Distributions · Inferential Statistics · Hypothesis Testing · P-Values · Correlation & Covariance · Linear Algebra · Eigenvalues · Calculus · Optimization · Interview Questions

Interview Questions

Q: What is a prior and a posterior?
The prior is the belief before evidence; the posterior is the updated belief after the evidence.

Q: Why is Naive Bayes called naive?
It assumes features are independent given the class, which is rarely exactly true.

Practice Exercises

  1. A test catches 99% of fraud and wrongly flags 2% of normal transactions; 0.5% of transactions are fraud. What is P(fraud | flagged)?
  2. Add a third word to the spam example and update again.

Summary

Bayes’ theorem turns a prior into a posterior using the likelihood of the evidence. Chaining updates is the core of Naive Bayes; ignoring base rates is a common mistake.

Next: Statistical Distributions

About Machine Learning

Check Also

P-Values Explained Simply (with Python Simulation)

What p-values mean, computed exactly and by simulation, the 0.05 threshold, statistical vs practical significance and common mistakes.