Module 19

Bayes' Theorem

Updating beliefs with evidence

Bayesian Reasoning

  • 01

    Prior Probability

    Initial belief before seeing data

  • 02

    Likelihood

    Probability of evidence given hypothesis

  • 03

    Posterior Probability

    Updated belief after seeing data

  • 04

    Base Rate

    Overall prevalence in population

01

Bayes' Theorem Formula

Interactive Bayes Calculator

Bayes' Theorem Calculator
10%
95%
5%
Posterior P(A|B) 67.9%
P(A|B) = P(B|A)·P(A) / P(B)

See Bayes' theorem in action! Adjust the Prior P(A), Sensitivity P(B|A), and False Positive Rate P(B|¬A) to calculate the Posterior P(A|B). This demonstrates the classic medical testing problem: even with a 95% accurate test, a positive result might not mean what you think if the condition is rare!

The Fundamental Equation

P(A|B) = P(B|A) × P(A)P(B)
P(A|B)
Posterior
P(B|A)
Likelihood
P(A)
Prior
P(B)
Evidence

Bayes' theorem relates conditional probabilities. It tells us how to update our belief in A after observing B. The posterior P(A|B) combines our prior belief P(A) with the likelihood P(B|A) of seeing the evidence if A is true, normalized by the total probability of the evidence P(B).

The Logic of Updating

30%
Prior Belief
"I think there's a 30% chance"
+
Data
New Evidence
Observed outcome
=
71%
Posterior Belief
"Now I think 71%"

Bayesian reasoning is about learning from evidence. We start with a prior belief (before data). We observe evidence and ask: how likely is this evidence under our hypothesis? Then we update to a posterior belief. Strong evidence shifts beliefs more; weak evidence shifts them less.

Breaking Down the Parts

Prior P(A)
Your belief before seeing data. Could be based on previous studies, expert opinion, or background knowledge.
Likelihood P(B|A)
How probable is the observed evidence if A is true? This is what connects hypothesis to data.
Evidence P(B)
Total probability of observing B, considering all possible hypotheses. Normalizes the result.
Posterior P(A|B)
Your updated belief after seeing the data. This becomes your new prior for future evidence.

Each component has a specific role: Prior captures existing knowledge, likelihood measures how well the hypothesis predicts the data, evidence normalizes probabilities, and posterior is the result. The process is iterative - today's posterior becomes tomorrow's prior.

02

Medical Testing Example

The Classic Medical Test

Given Information:
• Disease prevalence: 1% (Prior)
• Test sensitivity: 99% (True positive rate)
• Test specificity: 95% (True negative rate)
Question:
You test positive. What's the probability you actually have the disease?

This classic problem demonstrates base rate neglect. A test with 99% sensitivity and 95% specificity sounds excellent. But the answer depends heavily on disease prevalence. Most people guess 95% or 99% probability of having the disease after testing positive. The actual answer surprises them.

Applying Bayes' Theorem

P(Disease | Positive) = ?
P(Disease) = 0.01 (prior)
P(Positive | Disease) = 0.99 (sensitivity)
P(Positive | No Disease) = 0.05 (false positive)
P(Positive) = 0.99×0.01 + 0.05×0.99 = 0.0594
P(Disease | Positive) = (0.99 × 0.01) / 0.0594
= 16.7%

Despite testing positive with a 99% sensitive test, there's only a 16.7% chance you have the disease! Why? Because the disease is rare (1%), most positive tests come from the 5% false positive rate applied to the 99% healthy population. False positives vastly outnumber true positives.

Tree Diagram View

1,000 People
10 Diseased
10 Test+
0 Test-
990 Healthy
50 Test+
940 Test-
60 total positive: only 10 truly diseased → 10/60 = 16.7%

Visualizing with natural frequencies: Of 1,000 people, 10 have the disease (1%). Of these 10, about 10 test positive (99% sensitivity). Of 990 healthy, about 50 test positive (5% false positive). Total positives: 60. Only 10 are true cases. So 10/60 = 16.7%.

Why This Matters

Screening Rare Diseases
Even excellent tests produce mostly false positives when screening for rare conditions. Requires confirmation testing.
Legal Evidence
DNA match doesn't mean guilty. Must consider prior probability of guilt (base rate of being the perpetrator).
Security Screening
Terrorism detection: even 99.9% accurate tests flag mostly innocent people because terrorists are extremely rare.
Research Findings
Published findings may be false positives if testing unlikely hypotheses, even with p < 0.05.

Base rate neglect leads to real-world errors: unnecessary medical procedures from screening, wrongful convictions, ineffective security theater, and non-replicable research. Understanding Bayes' theorem helps evaluate claims critically and make better decisions under uncertainty.

03

Choosing Priors

Types of Prior Distributions

Uniform/Flat
All values equally likely. "I have no idea."
Informative
Based on previous studies or expertise.
Weakly Informative
Rules out impossible values, allows learning.

Priors encode existing knowledge. Uniform priors assume no prior knowledge (rarely appropriate). Informative priors use previous research or expert knowledge. Weakly informative priors provide mild regularization without strong assumptions - often a good default.

Does Prior Choice Matter?

Same data, different priors:
Skeptical
Prior
Flat
Prior
Optimistic
Prior
With enough data, posteriors converge regardless of prior. Prior matters most with small samples.

Prior choice matters most when data is limited. With abundant data, the likelihood dominates and posteriors converge regardless of prior. This is a feature: Bayesian inference is self-correcting. For small samples, check sensitivity to prior choice.

Best Practices for Priors

✓
Use domain knowledge when available
✓
Document and justify your prior choices
✓
Run sensitivity analyses with different priors
✓
Prefer weakly informative over flat priors
✗
Don't use data twice (to set prior AND fit model)
✗
Don't use improper priors without checking

Good prior selection: use existing knowledge, document choices, test sensitivity to alternatives, avoid flat priors (they're not actually non-informative). Bad practice: using the same data to choose priors and fit models (double-dipping), or using improper priors that don't integrate to 1.

04

Sequential Updating

Learning Over Time

Prior
50%
→
Data 1
H
→
Posterior 1
67%
→
Data 2
H
→
Posterior 2
80%

Bayesian updating is sequential: each posterior becomes the prior for the next observation. Flip a coin: start at 50% fair. See heads, update to favor heads-biased. See more heads, confidence grows. This mirrors how we naturally learn from experience over time.

Coin Fairness Example

Flipping a possibly biased coin. P(fair) starts at 50%
FlipResultP(Fair)Trend
0—50.0%Prior
1H45.5%↓
2H41.2%↓
3T44.8%↑
4H40.7%↓
5H36.9%↓

After each flip, we update our belief about the coin being fair. Four heads and one tail makes us more suspicious of bias. Each observation shifts the posterior slightly. With enough data, we'll become highly confident about the coin's true bias.

Eventually Reaching Truth

True value
With enough data, posteriors converge to truth
regardless of starting prior

A key property of Bayesian updating: with sufficient data, posteriors converge to the truth regardless of the starting prior (as long as the prior assigns nonzero probability to the truth). The data eventually overwhelms any reasonable prior.

05

Bayesian vs Frequentist

Two Views of Probability

Frequentist
Probability = long-run frequency
Parameters are fixed but unknown
Data is random (varies across samples)
Bayesian
Probability = degree of belief
Parameters have distributions
Data is fixed (what we observed)

Frequentists see probability as long-run frequency and parameters as fixed unknowns. Bayesians see probability as degree of belief and parameters as uncertain quantities with distributions. Neither is 'right' - they answer different questions and suit different contexts.

Confidence vs Credible Intervals

95% Confidence Interval
"If we repeated this many times, 95% of intervals would contain θ"
Property of the procedure
95% Credible Interval
"There is 95% probability that θ is in this interval"
Probability statement about θ
Credible intervals answer the question people usually want to ask!

Confidence intervals describe procedure reliability. Credible intervals give direct probability statements about parameters. Most people interpret CIs as if they were credible intervals - that's technically wrong for CIs but exactly correct for credible intervals.

P-values vs Bayes Factors

P-value
P(data | H₀)
How surprising is data if null is true?
Cannot confirm H₀
Bayes Factor
P(data | H₁) / P(data | H₀)
How much more likely under H₁ vs H₀?
Can support either hypothesis

P-values only tell us how surprising data is under the null. Bayes factors compare evidence for competing hypotheses directly. BF = 10 means data is 10× more likely under H₁ than H₀. Unlike p-values, Bayes factors can provide evidence FOR the null hypothesis.

Choosing Your Approach

Prefer Bayesian when:
• Prior information is available and relevant
• You want probability of hypothesis
• Small samples require regularization
• Sequential updating makes sense
Prefer Frequentist when:
• No prior information or priors controversial
• Long-run error rates matter (clinical trials)
• Regulatory/publication standards require it
• Computational simplicity needed

Both approaches are valid for different contexts. Bayesian methods excel with prior knowledge, small samples, and complex models. Frequentist methods are standard in regulated fields and when priors would be contentious. Many modern analyses use both.

★

Real-World Examples

Apply your knowledge to practical scenarios

01

Email Spam Classification

A spam filter uses Bayesian classification. Given: 20% of all emails are spam (prior). The word 'FREE' appears in 80% of spam emails but only 5% of legitimate emails. An email contains 'FREE'. What's the probability it's spam?

Sample Data
{
  "prior_spam": 0.2,
  "p_free_given_spam": 0.8,
  "p_free_given_ham": 0.05
}
Q Set up the Bayes' theorem equation for P(Spam | FREE). +
A P(Spam|FREE) = P(FREE|Spam) × P(Spam) / P(FREE). We need to calculate P(FREE) first.
Q Calculate P(FREE) - the total probability of seeing 'FREE' in any email. +
A P(FREE) = P(FREE|Spam)×P(Spam) + P(FREE|Ham)×P(Ham) = 0.80×0.20 + 0.05×0.80 = 0.16 + 0.04 = 0.20
Q Calculate P(Spam | FREE). +
A P(Spam|FREE) = (0.80 × 0.20) / 0.20 = 0.16 / 0.20 = 0.80 or 80%. An email containing 'FREE' has an 80% chance of being spam.
Q The email also contains 'PRIZE'. P(PRIZE|Spam) = 0.70, P(PRIZE|Ham) = 0.01. Update the probability. +
A Use 80% as the new prior. P(PRIZE) = 0.70×0.80 + 0.01×0.20 = 0.562. P(Spam|PRIZE,FREE) = (0.70×0.80)/0.562 = 0.996 or 99.6% spam.
Key Insights
  • Bayesian classification updates beliefs with each piece of evidence
  • Multiple weak signals can combine to give strong classification
  • This is exactly how Naive Bayes spam filters work in practice
02

The Prosecutor's Fallacy

A DNA test has a false positive rate of 1 in 1 million. A suspect's DNA matches evidence from a crime scene. The prosecutor argues: 'The probability of this match by chance is 1 in 1 million, so the suspect is almost certainly guilty.'

Q What's wrong with the prosecutor's argument? +
A The prosecutor confuses P(match|innocent) with P(innocent|match). These are NOT the same! This is the inverse probability fallacy.
Q If the suspect was randomly selected from a city of 5 million, what's the prior probability they're the perpetrator? +
A Prior P(guilty) = 1/5,000,000 = 0.0000002. This is the base rate before considering DNA evidence.
Q Using Bayes' theorem, calculate P(guilty|match) assuming P(match|guilty) = 1. +
A P(guilty|match) = P(match|guilty)×P(guilty) / P(match). P(match) ≈ 1×(1/5M) + (1/1M)×(5M-1)/5M ≈ 5/5M = 1/1M. So P(guilty|match) ≈ (1/5M)/(1M/5M) = 1/5 = 0.20 or 20%.
Q What does this mean for the court case? +
A Despite the 1-in-a-million match, there's only about 20% probability of guilt! About 5 people in the city would match. The DNA alone is far from 'beyond reasonable doubt.'
Key Insights
  • P(evidence|hypothesis) ≠ P(hypothesis|evidence)
  • Base rates matter enormously—even 'rare' events occur in large populations
  • This fallacy has led to wrongful convictions in real cases
03

Breast Cancer Screening Decisions

A 50-year-old woman with no family history gets a positive mammogram. Prevalence in her demographic: 0.8%. Mammogram sensitivity: 90%. Specificity: 93%. What's the probability she actually has breast cancer?

Sample Data
{
  "prevalence": 0.008,
  "sensitivity": 0.9,
  "specificity": 0.93
}
Q Using natural frequencies: In 10,000 women, how many have cancer and how many don't? +
A With 0.8% prevalence: 80 have cancer, 9,920 are healthy.
Q Of these, how many test positive? +
A True positives: 80 × 0.90 = 72. False positives: 9,920 × 0.07 = 694. Total positives: 766.
Q What is P(cancer | positive)? +
A P(cancer|positive) = 72/766 = 9.4%. Even with a positive mammogram, there's only about a 9% chance of cancer.
Q What should the woman do with this information? +
A Not panic! A positive mammogram warrants follow-up testing (biopsy), not immediate cancer treatment. Most positive screening results are false alarms. This is why understanding conditional probability matters.
Key Insights
  • Positive screening tests are often false positives when disease is rare
  • Natural frequencies (out of 10,000) are easier to understand than probabilities
  • Doctors should communicate results using Bayesian reasoning
?

Bayes' Theorem Quiz

Test your understanding • Passing score: 70%

0 of 10 answered
Q1 multiple-choice

In Bayes' theorem P(A|B) = P(B|A)×P(A)/P(B), what is P(A) called?

Explanation: P(A) is the prior probability—your belief in A before seeing the evidence B. It represents prior knowledge or baseline probability.
Q2 multiple-choice

A disease has 1% prevalence. A test is 99% sensitive and 95% specific. If you test positive, the probability you have the disease is approximately:

Explanation: Using Bayes: P(Disease|+) = (0.99×0.01)/(0.99×0.01 + 0.05×0.99) = 0.0099/0.0594 ≈ 16.7%. Base rates matter!
Q3 multiple-choice

What is 'base rate neglect'?

Explanation: Base rate neglect is the tendency to ignore prior probabilities (base rates) when evaluating evidence. People focus on hit rates and ignore how rare the condition is.
Q4 multiple-choice

In Bayesian updating, the posterior from one observation becomes:

Explanation: In sequential Bayesian updating, today's posterior becomes tomorrow's prior. This allows beliefs to be updated incrementally as new data arrives.
Q5 multiple-choice

A Bayesian 95% credible interval means:

Explanation: Unlike frequentist confidence intervals, Bayesian credible intervals allow direct probability statements: there IS a 95% probability the parameter falls within the interval.
Q6 multiple-choice

A Bayes factor of 20 means:

Explanation: A Bayes factor compares how likely the observed data is under competing hypotheses. BF = 20 means data is 20× more likely under H₁ than H₀.
Q7 multiple-choice

Which type of prior assumes all values are equally likely?

Explanation: A flat or uniform prior assigns equal probability to all values, representing 'no prior information.' However, flat priors can be problematic and are rarely truly non-informative.
Q8 multiple-choice

The prosecutor's fallacy involves confusing:

Explanation: The prosecutor's fallacy confuses P(evidence|innocent), which might be tiny, with P(innocent|evidence). A 1-in-a-million DNA match doesn't mean 1-in-a-million chance of innocence.
Q9 multiple-choice

With sufficient data, Bayesian posteriors will:

Explanation: A key property: with enough data, posteriors converge to the truth regardless of starting prior (as long as prior doesn't assign zero probability to the truth). Data overwhelms prior.
Q10 multiple-choice

Which is an advantage of Bayesian methods over frequentist methods?

Explanation: Bayesian methods can formally incorporate prior knowledge and provide direct probability statements about parameters (credible intervals). They're not assumption-free or simpler, but offer different interpretations.

Quiz Results

0 %

← Missing Data Next: Time Series →