Back to Lecture 10

Bayes' Rule: Real-World Applications

Master Bayesian inference through practical spam detection, medical diagnosis, and weather forecasting

📧 Application 1: Email Spam Detection

Scenario: Naive Bayes Spam Filter

You receive an email with subject: "CONGRATULATIONS! You've WON a FREE prize - CLICK HERE NOW!"
Your spam filter analyzes three key words: "FREE", "WIN", and "CLICK". Determine if this email is spam.

Prior Knowledge

P(Spam) = 0.40 (40% of emails are spam)

P(Ham) = 0.60 (60% are legitimate)

Naive Bayes Assumption

Words appear independently given the class (simplifying assumption)

Word Likelihood Table
Word P(Word|Spam) P(Word|Ham)
FREE 0.80 0.10
WIN 0.70 0.05
CLICK 0.65 0.15
Step-by-Step Solution (Illustrative Example)
1
Calculate Likelihood for SPAM

Multiply probabilities of all words appearing in spam:

P(FREE, WIN, CLICK | Spam) = P(FREE|Spam) × P(WIN|Spam) × P(CLICK|Spam)
P(Words | Spam) = 0.80 × 0.70 × 0.65 = 0.364
2
Calculate Likelihood for HAM (Legitimate)

Multiply probabilities of all words appearing in legitimate email:

P(FREE, WIN, CLICK | Ham) = P(FREE|Ham) × P(WIN|Ham) × P(CLICK|Ham)
P(Words | Ham) = 0.10 × 0.05 × 0.15 = 0.00075
3
Calculate Evidence P(Words)

Using law of total probability:

P(Words) = P(Words|Spam) × P(Spam) + P(Words|Ham) × P(Ham)
P(Words) = 0.364 × 0.40 + 0.00075 × 0.60 = 0.1456 + 0.00045 = 0.14605
4
Apply Bayes' Theorem

Calculate posterior probability of spam:

P(Spam | Words) = [P(Words|Spam) × P(Spam)] / P(Words)
P(Spam | Words) = (0.364 × 0.40) / 0.14605 = 0.1456 / 0.14605 = 0.997
Final Classification
99.7% SPAM

Verdict: This email is almost certainly spam! Block it immediately.

Why This Works
  • Words like "FREE", "WIN", "CLICK" are highly correlated with spam
  • The combination of all three words makes spam probability overwhelming
  • Even though spam is only 40% of emails initially, the evidence updates our belief to 99.7%
  • This is why Bayesian spam filters are so effective!
Key Insight: Naive Bayes Assumption

We assume words appear independently, which isn't strictly true in reality, but this "naive" assumption makes calculations tractable and works remarkably well in practice!


Exercise 1: Less Obvious Email

Email to Classify

Subject: "Meeting tomorrow at 3pm - Please confirm"
Body snippet: "Hi, just wanted to confirm our meeting tomorrow. Please reply to let me know if you can make it."

The filter analyzes two key words: "meeting" and "confirm"

Prior Probabilities

P(Spam) = 0.35 (35% of emails are spam)

P(Ham) = 0.65 (65% are legitimate)

Word Probabilities
Word P(Word|Spam) P(Word|Ham)
meeting 0.15 0.60
confirm 0.25 0.50
Your Task: Calculate P(Spam | "meeting", "confirm")

Exercise 2: Ambiguous Email

Email to Classify

Subject: "Limited time offer - Exclusive deal for you"
Body snippet: "We noticed you were interested in our products. Here's a special discount just for you."

The filter analyzes three words: "offer", "discount", and "products"

Prior Probabilities

P(Spam) = 0.50 (50% of emails are spam)

P(Ham) = 0.50 (50% are legitimate)

Word Probabilities
Word P(Word|Spam) P(Word|Ham)
offer 0.60 0.30
discount 0.55 0.40
products 0.40 0.35
Your Task: Calculate P(Spam | "offer", "discount", "products")

Challenge Exercise 3: Multi-Word Analysis

Professional Email

Subject: "Invoice #45231 - Payment Due"
Body snippet: "Dear customer, your invoice is attached. Payment is due within 30 days. Thank you for your business."

The filter analyzes four words: "invoice", "payment", "customer", and "business"

Prior Probabilities

P(Spam) = 0.45 (45% of emails are spam)

P(Ham) = 0.55 (55% are legitimate)

Word Probabilities
Word P(Word|Spam) P(Word|Ham)
invoice 0.35 0.70
payment 0.50 0.65
customer 0.40 0.60
business 0.30 0.55
Important Note: Phishing Risk

While these words appear professional, scammers also use them in phishing emails! The Bayesian filter helps us quantify the probability, but users should still verify sender authenticity.

Your Task: Calculate P(Spam | "invoice", "payment", "customer", "business")

🏥 Application 2: COVID-19 Rapid Test

Scenario: Interpreting Test Results

A patient in Riyadh takes a COVID-19 rapid test and tests POSITIVE. What is the actual probability they have COVID-19? Let's use Bayes' theorem to find out.

Disease Prevalence
3%
P(COVID) = 0.03

Current infection rate in Saudi Arabia

Test Sensitivity
95%
P(+|COVID) = 0.95

Correctly detects COVID when present

False Positive Rate
4%
P(+|¬COVID) = 0.04

Incorrectly shows positive when no COVID

Bayesian Calculation (Illustrative Example)
1
Calculate P(Positive Test) - Evidence

Using law of total probability:

P(+) = P(+|COVID) × P(COVID) + P(+|¬COVID) × P(¬COVID)
P(+) = 0.95 × 0.03 + 0.04 × 0.97
P(+) = 0.0285 + 0.0388 = 0.0673

About 6.7% of all people test positive

2
Apply Bayes' Theorem

Calculate posterior probability of actually having COVID:

P(COVID | +) = [P(+|COVID) × P(COVID)] / P(+)
P(COVID | +) = (0.95 × 0.03) / 0.0673
P(COVID | +) = 0.0285 / 0.0673 = 0.423
Diagnosis Result
42.3%

Despite testing positive, there's only a 42% chance of actually having COVID!

Why So Low? The Base Rate Fallacy
Out of 1000 people:
  • 30 actually have COVID (3% prevalence)
  • Test correctly identifies: 30 × 0.95 = 28-29 people
But also:
  • 970 don't have COVID
  • False positives: 970 × 0.04 = ~39 people
Total positive tests: 28 + 39 = 67 people
Only 28 out of 67 actually have COVID → 28/67 ≈ 42%
False positives outnumber true positives when disease is rare!
Clinical Recommendation

A positive rapid test should be confirmed with a PCR test. The combination of two positive tests dramatically increases the probability of true infection. This is Bayesian reasoning in action: sequential evidence updates our beliefs!


Exercise 1: Cancer Screening Test

Clinical Scenario

A 45-year-old patient undergoes a screening test for a rare cancer. The test comes back POSITIVE.

Given Information:
• Cancer prevalence: 0.8% in this age group
• Test sensitivity: 90% (correctly identifies 90% of cancer cases)
• False positive rate: 7% (incorrectly shows positive for 7% of healthy people)

Your Task: Calculate P(Cancer | Positive Test)

Exercise 2: Diabetes Screening

Blood Glucose Test

A patient with risk factors takes a fasting blood glucose test. The result shows elevated levels.

Given Information:
• Type 2 diabetes prevalence in high-risk group: 12%
• Test correctly identifies diabetes: 85%
• Test incorrectly shows elevated glucose in non-diabetics: 15%

Your Task: Calculate P(Diabetes | Elevated Glucose)

🌤️ Application 3: Weather Prediction

Scenario: Will It Rain Tomorrow in Riyadh?

The weather service observes dark clouds forming over Riyadh this afternoon. Using historical data and Bayesian inference, calculate the probability of rain tomorrow.

Historical Data (Riyadh)

P(Rain) = 0.15 (15% of days have rain)

P(No Rain) = 0.85 (85% are dry)

Based on annual average for Riyadh
Cloud Observation Likelihoods

P(Dark Clouds | Rain) = 0.80

P(Dark Clouds | No Rain) = 0.20

From meteorological records
Bayesian Weather Update (Illustrative Example)
1
Calculate P(Dark Clouds) - Evidence

What's the overall probability of seeing dark clouds?

P(Dark Clouds) = P(Clouds|Rain) × P(Rain) + P(Clouds|No Rain) × P(No Rain)
P(Clouds) = 0.80 × 0.15 + 0.20 × 0.85
P(Clouds) = 0.12 + 0.17 = 0.29

Dark clouds appear on 29% of days

2
Apply Bayes' Theorem

Update rain probability given cloud observation:

P(Rain | Dark Clouds) = [P(Dark Clouds|Rain) × P(Rain)] / P(Dark Clouds)
P(Rain | Clouds) = (0.80 × 0.15) / 0.29
P(Rain | Clouds) = 0.12 / 0.29 = 0.414
Weather Forecast
41% Chance of Rain

Recommendation: Carry an umbrella! Dark clouds significantly increased rain probability from 15% to 41%.

Before Evidence
15%
Prior probability
(historical average)
15%
After Seeing Dark Clouds
41%
Posterior probability
(updated with evidence)
41%
Bayesian Learning in Action
  • Prior: Started with 15% (historical average for Riyadh)
  • Evidence: Observed dark clouds forming
  • Likelihood: Dark clouds are 4× more likely when it rains (0.80 vs 0.20)
  • Posterior: Updated belief to 41% chance of rain
  • Result: Evidence increased probability by 2.75×
Modern Weather Forecasting

Professional meteorologists use Bayesian methods with hundreds of variables: satellite imagery, atmospheric pressure, temperature, humidity, wind patterns, and more. Each new observation updates the probability distribution, leading to increasingly accurate forecasts. This is why weather predictions improve as the event gets closer!

Try It Yourself: Adjust the Parameters

See how changing the prior probability or likelihood affects the result:


Exercise 1: Tornado Warning System

Severe Weather Alert

A tornado warning system combines radar data and atmospheric conditions to predict tornadoes.

Given Information:
• Actual tornado occurrence: 2% of warnings
• When tornado present, both indicators activate: 92%
• When no tornado, both indicators activate (false alarm): 8%

Your Task: If both radar and atmospheric indicators activate, what's P(Tornado)?

Exercise 2: Multi-Day Weather Forecasting

Weekend Rain Forecast for Riyadh

Friday morning: Weather models predict 25% chance of rain on Saturday. By Friday evening, satellite data shows approaching moisture.

Given Information:
• Initial rain probability (Friday AM): 25%
• When rain occurs, satellites detect moisture: 80%
• When no rain, satellites falsely detect moisture: 20%

Your Task: Update the rain probability after seeing satellite moisture

🧠 Part 4: Conceptual Understanding

Question 1: The Role of Prior Probability

Scenario: Two cities, City A and City B, both use the same COVID test with 95% sensitivity and 5% false positive rate. City A has 10% infection rate, City B has 1% infection rate.

Question: If both cities report a positive test, which patient is more likely to actually have COVID? Explain your reasoning using Bayesian thinking.

Question 2: Understanding Test Characteristics

Question: A spam filter is designed to have very high specificity (99% - rarely labels legitimate emails as spam). What is the trade-off? Explain why this trade-off exists.

Question 3: Sequential Bayesian Updating

Scenario: A patient tests positive for COVID (42% probability of infection after first test). They take a second independent test and it's also positive. What happens to the probability? Explain the concept of sequential updating.

Question 4: Insurance Fraud Detection (Calculation)

New Application: Insurance Claims

An insurance company uses an AI system to detect fraudulent claims:

  • 5% of claims are actually fraudulent
  • The AI correctly identifies 90% of fraudulent claims
  • The AI incorrectly flags 8% of legitimate claims as fraudulent

Calculate: If the AI flags a claim as fraudulent, what's the probability it's actually fraudulent?

Question 5: Airport Security Scanner (Calculation)

New Application: Security Screening

An airport security scanner detects prohibited items:

  • 0.5% of passengers actually carry prohibited items (very rare)
  • Scanner detects 98% of prohibited items (high sensitivity)
  • Scanner incorrectly alarms on 3% of innocent passengers

Calculate: When the scanner alarms, what's the probability the passenger actually has a prohibited item?

Key Takeaways: Bayes' Rule in Practice

📧 Spam Detection
  • Combines multiple word probabilities
  • Naive Bayes assumption simplifies calculation
  • Very effective despite "naive" assumption
  • Updates as new spam patterns emerge
🏥 Medical Diagnosis
  • Accounts for disease prevalence (base rate)
  • Balances sensitivity vs specificity
  • Explains why confirmation tests are needed
  • Prevents misinterpretation of results
🌤️ Weather Forecasting
  • Updates predictions with new observations
  • Combines historical data with current evidence
  • Quantifies uncertainty in predictions
  • Improves accuracy as event approaches
The Power of Bayesian Thinking

All three applications share the same mathematical framework: start with prior knowledge, observe evidence, update beliefs mathematically. This is the essence of rational reasoning under uncertainty. Bayes' theorem isn't just a formula—it's a way of thinking that helps us make better decisions when information is incomplete!