Bayes' Theorem Math Example 1

Follow the full solution, then compare it with the other examples linked below.

Example 1

medium
Email spam filter: P(spam)=0.3P(\text{spam})=0.3. The word 'free' appears in 80% of spam emails and 10% of legitimate emails. An email contains 'free'. Find P(spamโˆฃfree)P(\text{spam}|\text{free}) using Bayes' theorem.

Solution

  1. 1
    Prior: P(spam)=0.3P(\text{spam})=0.3, P(legit)=0.7P(\text{legit})=0.7
  2. 2
    Likelihoods: P(freeโˆฃspam)=0.8P(\text{free}|\text{spam})=0.8, P(freeโˆฃlegit)=0.1P(\text{free}|\text{legit})=0.1
  3. 3
    Law of total probability: P(free)=0.8(0.3)+0.1(0.7)=0.24+0.07=0.31P(\text{free}) = 0.8(0.3) + 0.1(0.7) = 0.24 + 0.07 = 0.31
  4. 4
    Bayes: P(spamโˆฃfree)=P(freeโˆฃspam)ร—P(spam)P(free)=0.8ร—0.30.31=0.240.31โ‰ˆ0.774P(\text{spam}|\text{free}) = \frac{P(\text{free}|\text{spam}) \times P(\text{spam})}{P(\text{free})} = \frac{0.8 \times 0.3}{0.31} = \frac{0.24}{0.31} \approx 0.774

Answer

P(spamโˆฃfree)โ‰ˆ0.774P(\text{spam}|\text{free}) \approx 0.774. There's a 77.4% chance the email is spam.
Bayes' theorem updates the prior probability (30% base spam rate) with new evidence (email contains 'free') to get a posterior probability (77.4%). This is how Bayesian spam filters work โ€” each word updates the probability that an email is spam.

About Bayes' Theorem

Bayes' theorem gives the posterior probability of a hypothesis given evidence: P(HโˆฃE)=P(EโˆฃH)โ‹…P(H)P(E)P(H|E) = \frac{P(E|H) \cdot P(H)}{P(E)}.

Learn more about Bayes' Theorem โ†’

More Bayes' Theorem Examples