Lesson 79 — Deep Dive into Bayes' Theorem
Priors, posteriors, and sequential updating. Odds form, Beta-binomial conjugate prior, base rate fallacy, Naive Bayes. Applications in medical diagnosis, spam filtering, and ML.
Used in: Stochastik LK alemão · H2 Math Statistics singapurense · Math B japonês · Equiv. AP Statistics EUA
Bayes' theorem is the rule for rational belief updating. The prior represents what we believe before seeing the evidence; the likelihood measures how much the evidence favors the hypothesis; the posterior is the updated belief after observing . The denominator normalizes the result so that the probability sums to 1.
Rigorous notation, full derivation, hypotheses
Definitions and theorems
Conditional probability
"The conditional probability , the probability of given , expresses the probability of when we know that has occurred. It can be computed using the formula , assuming ." — Grinstead & Snell, Introduction to Probability, §4.1
Law of total probability
Bayes' theorem
"Bayes' Theorem is just a formula that comes from the definition of conditional probability. Yet it is extremely powerful, and is the key to understanding what it means to rationally revise your beliefs in light of new evidence." — OpenIntro Statistics 4e, §3.2
Odds form
Sequential updating
Beta-binomial conjugate prior
SVG — Bayes diagram in 2×2 table
Absolute frequency diagram. The PPV (Positive Predictive Value) is the Bayesian posterior P(sick | positive test). When prevalence is low, false positives outweigh true positives even with a high-quality test.
Solved examples
Exercise list
40 exercises · 10 with worked solution (25%)
- Ex. 79.1ApplicationAnswer key
, , . Calculate .
Show solution
By definition: . Note that , so and are independent. - Ex. 79.2Application
, . Calculate .
Show solution
By the product rule: . - Ex. 79.3Application
, , . Calculate .
Show solution
Law of total probability: .Show step-by-step (with the why)
- Identify the two hypotheses: (with probability 0.1) and (with probability 0.9).
- Note the likelihoods: and .
- Apply the law of total probability — weighted sum of likelihoods by priors: .
- Tip: the law of total probability is the denominator of Bayes. Calculate it before applying the theorem.
- Ex. 79.4Application
With the data from exercise 79.3, calculate .
Show solution
Using the data from the previous exercise and Bayes: . - Ex. 79.5ApplicationAnswer key
Disease with 0.5% prevalence. Diagnostic test: 95% sensitivity, 95% specificity. Calculate the PPV using frequencies in 10,000 people.
Show solution
In 10,000 people: sick = 50, TP = , FP = . PPV = . Despite the high quality of the test, the PPV is low because the prevalence is very low. - Ex. 79.6ApplicationAnswer key
Same data as exercise 79.5, but with 50% prevalence. Calculate the PPV and compare with the previous result.
Show solution
With 50% prevalence: in 10,000, sick = 5,000, TP = 4,750, FP = 250. PPV = , or 95%. Compare with 8.7% from exercise 79.5: the same test has a radically different PPV depending on prevalence. - Ex. 79.7Application
Spam filter: . Word "FREE" appears in 60% of spams and 5% of legitimate emails. Calculate .
Show solution
Prior odds of spam: . Likelihood ratio: . Posterior odds: . Posterior: .Show step-by-step (with the why)
- Prior: , .
- Likelihoods: , .
- Numerator of Bayes: .
- Denominator: .
- Posterior: .
- Mental shortcut: LR = 12 transforms prior odds of into posterior odds of , i.e., approximately 84% spam.
- Ex. 79.8Application
Urn A: 2 red, 3 blue. Urn B: 5 red, 1 blue. An urn is chosen at random and a red ball is drawn. What is the probability the urn is A?
Show solution
Prior: . Likelihoods: , . Total: . Posterior: . - Ex. 79.9ApplicationAnswer key
3 coins: 2 fair, 1 double-headed. One is chosen at random, flipped once, comes up heads. What is the probability the chosen coin is the double-headed one?
Show solution
3 coins: 2 fair (prob head = 1/2) and 1 double-headed (prob head = 1). Prior: , . Likelihood of head: , . Total: . Posterior: . - Ex. 79.10Application
. . . Given a person has cancer, what is the probability they are a smoker?
Show solution
Prior: . Likelihoods: , . Total: . Posterior: . - Ex. 79.11Application
Sequential updating: two positive tests with 90% sensitivity and 90% specificity, applied to a disease with 1% prevalence. Use the posterior of the 1st test as the prior of the 2nd. What is the PPV after both consecutive positive results?
Show solution
Step 1 (1st positive test, 90% sens, 90% spec, 1% prevalence): . Step 2 (2nd positive test, same test, prior = 8.33%): . After two independent positive tests, the probability rises from 1% to almost 45%.Show step-by-step (with the why)
- Calculate PPV after the 1st positive test with 1% prevalence, sens = spec = 90%: .
- Use 8.33% as the new prior for the 2nd test.
- Calculate PPV: numerator = ; denominator = .
- . With each positive test, the probability grows — but slowly when the prior is very low.
- Curiosity: via odds form, for each test. After two tests: prior odds ; posterior odds ; posterior . Same result, faster.
- Ex. 79.12Application
For a test with 90% sensitivity and 95% specificity, calculate the positive likelihood ratio .
Show solution
. Interpretation: a positive result makes the disease 18 times more likely (in odds scale) than before the test. - Ex. 79.13Application
Prior odds of 1:99 (1% prevalence). (90% sensitivity, 95% specificity). Calculate the posterior odds and the posterior.
Show solution
Prior odds: . . Posterior odds: . Posterior: . - Ex. 79.14Application
Which of the following values is the correct posterior in a context with prior odds 1:99 and ?
Show solution
The correct alternative is 15.4%. The prior odds is 1:99, the = 18, so posterior odds = 18/99 and posterior = 18/117 ≈ 15.4%. The other alternatives map classic errors: confusing PPV with sensitivity (90%), ignoring the test (keeping 1%), or forgetting the prior (50% incorrect). - Ex. 79.15Application
Prior . 7 heads observed in 10 flips. Determine the posterior.
Show solution
Prior Beta(2, 2). Flips: , heads. Posterior: . Posterior mean: . - Ex. 79.16Application
Prior (uniform). 0 heads observed in 5 flips. Determine the posterior and its mean.
Show solution
Posterior: . Posterior mean: . Even without any heads, the posterior does not go to zero because the uniform prior assigned some positive mass to all values of . - Ex. 79.17Application
In exercise 79.15, what is the posterior mean?
Show solution
From exercise 79.15: posterior Beta(9, 5). Mean: . - Ex. 79.18Application
Prior . New batch: 30 parts inspected, 6 defective. Determine the posterior and posterior mean.
Show solution
Prior Beta(2, 8), n = 30, k = 6. Posterior: . Posterior mean: . Compare with MLE = : they coincide because the prior was already calibrated to the same proportion. - Ex. 79.19ModelingAnswer key
COVID-19 in endemic phase: 5% prevalence. Rapid test: 80% sensitivity, 95% specificity. Calculate the PPV using frequencies in 10,000 people. Is it worth automatically isolating all positives?
Show solution
COVID screening: 5% prevalence, 80% sens, 95% spec. In 10,000 people: sick = 500, TP = 400, healthy = 9,500, FP = 475. PPV = 400/875 ≈ 45.7%. Less than half of the positives are truly infected — justifies confirmation with PCR before prolonged isolation decisions.Show step-by-step (with the why)
- Calculate the number of sick people in the population of 10,000: .
- TP = sick × sensitivity = .
- FP = healthy × (1 − specificity) = .
- PPV = TP / (TP + FP) = .
- Observation: with lower sensitivity (80% vs 95%), the PPV falls even more than in the introductory example. Prevalence and specificity are the determining factors of PPV for population screening.
- Ex. 79.20Modeling
Naive Bayes for email: . In training: "FREE" appears in 60% of spams and 5% of hams; "won" appears in 50% of spams and 10% of hams. An email contains both words. Classify assuming conditional independence.
Show solution
Classify: prior odds spam 3:7. LR for "FREE": . LR for "won": , , LR = 5. Posterior odds = . Posterior = . Classifies as spam. - Ex. 79.21Modeling
Three diseases: A (10% in population), B (5%), C (1%). Patient presents symptom S with , , . Which disease is most likely?
Show solution
Priors: , , . Likelihoods: , , . Unnormalized posteriors: A: 0.03; B: 0.045; C: 0.009. Normalize: sum = 0.084. , , . Most likely diagnosis: disease B. - Ex. 79.22Modeling
Prosecutor's fallacy: DNA evidence has a frequency of 1/1000 in the population. The prosecutor claims the probability of innocence is 1/1000. Why is this reasoning wrong? Calculate the correct posterior assuming there are 100,000 plausible suspects in the city.
Show solution
See the referenced source for the detailed solution. - Ex. 79.23ModelingAnswer key
Fraud classifier: 95% sensitivity, 99.9% specificity. Frauds: 0.1% of transactions. Calculate the PPV. How many false positives for every true positive?
Show solution
Fraud: 0.1% prevalence, 95% sens, 99.9% spec. In 1,000,000 transactions: frauds = 1,000, TP = 950, FP = . PPV = . For every genuine alert, there is almost 1 false positive. In practice: automatic screening, but human review before blocking definitively. - Ex. 79.24Modeling
Pregnancy test: 99% sensitivity, 98% specificity. Woman with prior probability of pregnancy of 30%. Calculate the PPV.
Show solution
Prev 30%, sens 99%, spec 98%. PPV = . With a prior of 30%, the PPV is already very high — the high prevalence compensates for the small false positive rate. - Ex. 79.25ModelingAnswer key
Polygraph: 70% sensitivity, 80% specificity. In interrogation with a suspect who has a 5% prior of guilt. Calculate the posterior after a positive result. Is the result admissible as sufficient evidence to convict?
Show solution
Prior odds: . Polygraph: 70% sens, 80% spec. . Posterior odds: . Posterior: . With only 15% posterior probability of guilt, it is not reliable as sufficient evidence for conviction. - Ex. 79.26ModelingAnswer key
Two independent positive tests (sens = 0.9, spec = 0.95; sens = 0.85, spec = 0.90). Prevalence 2%. Calculate the posterior after both positive results via sequential updating.
Show solution
Two independent positive tests: sensitivities 0.9 and 0.85; specificities 0.95 and 0.90; prevalence 2%. After T1+: . After T2+ with prior 0.269: . - Ex. 79.27Modeling
In a lineup, one suspect has red hair (H) with a 70% probability of being the culprit. A witness identifies the red-haired one with 90% probability when the culprit is H, and erroneously 15% of the time when the culprit is not H. Given the witness pointed to H, what is the posterior of guilt?
Show solution
, . Witness identifies suspect H with probability 0.9 when it is H, and errs 0.15 of the time when it is C. . . - Ex. 79.28Modeling
Quality control with 3 lines (A: 40% of production, 2% defect; B: 35%, 3%; C: 25%, 5%). A defective part is found. Determine the probability of each line being the origin.
Show solution
Line A: 40%, 2% defect. Line B: 35%, 3% defect. Line C: 25%, 5% defect. Upon finding a defective part: posterior proportional to defect rate × line proportion. ; ; . Total = 0.031. Posteriors: A ≈ 25.8%, B ≈ 33.9%, C ≈ 40.3%. Line C is the most likely source of the defective part. - Ex. 79.29Understanding
What is the base rate fallacy?
Show solution
The base rate fallacy is specifically the failure to incorporate prevalence (prior) when calculating the posterior probability. The other options describe other types of errors, but not the base rate fallacy. - Ex. 79.30Understanding
Why does the prior matter even in "objective science"? An analysis that ignores the prior is equivalent to what implicit assumption?
Show solution
The prior matters because it is the starting point of Bayesian updating. Without a prior, there is no numerator in Bayes — the formula does not produce a posterior. In practice, even "objective" analyses imply priors: testing with a -value is equivalent to assigning a point prior at and rejecting if the evidence is unlikely under that prior. Saying that a prior is "subjective" while the -value is "objective" is an illusion — the objectivity of the -value lies in the error control procedures, not in the absence of assumptions about the parameters. - Ex. 79.31Understanding
Two independent positive tests with likelihood ratios and . What is the effect on the odds form?
Show solution
When tests are conditionally independent given the hypothesis, the combined likelihood ratio is the product of the individual ratios: . In odds form: posterior odds = prior odds . Adding would be incorrect — ratios multiply, they do not add. The second positive test always increases the posterior (provided ). - Ex. 79.32Understanding
What is the practical difference between using a Beta(1,1) prior and a Beta(10,10) prior for a coin? In which case will the posterior be more sensitive to new data?
Show solution
The Beta(1,1) prior is uniform on — it does not favor any value of . The Beta(,) prior with concentrates mass around . A more "informative" prior (larger ) requires more data to be dominated by the likelihood. With Beta(1,1) prior and observations (k heads), the posterior Beta(1+k, 1+10−k) has mean — slightly pulled toward 0.5 relative to the MLE . - Ex. 79.33Challenge
Show that two conditionally independent positive tests given result in posterior odds equal to prior odds, where .
Show solution
Via odds form: prior odds . After : posterior odds prior odds. After (with posterior as new prior): posterior odds prior odds prior odds. Extension by induction: for conditionally independent pieces of evidence: posterior odds prior odds. The odds form turns sequential updating into multiplication of LRs. - Ex. 79.34Challenge
Demonstrate that the posterior of the Bernoulli-Beta model is Beta(, ) when the prior is Beta(, ) and we observe successes in trials.
Show solution
See the referenced source for the detailed solution. - Ex. 79.35Proof
Demonstrate Bayes' theorem from the definition of conditional probability and the law of total probability.
Show solution
By definition of conditional probability: . By the multiplication theorem: . By the law of total probability (partition ): . Substituting: . - Ex. 79.36Proof
Show that using only the definition of conditional probability. Identify why in general.
Show solution
See the referenced source for the detailed solution. - Ex. 79.37Challenge
Monty Hall problem with 3 doors. Use Bayes to calculate the probability of the car being in each door after Monty (who knows where the car is) opens an empty door. Should you switch?
Show solution
Monty Hall: 3 doors, car in 1. Hypotheses: = car in door 1 (prior 1/3), = door 2, = door 3. You choose door 1. Monty opens door 3 (no car). Evidence = "Monty opens door 3". (Monty chooses between 2 and 3 randomly), (can only open 3), (would not open with car). . Posterior of door 1: . Posterior of door 2: . You should switch. - Ex. 79.38ChallengeAnswer key
In Naive Bayes with binary features, show that the classifier is equivalent to multiplying the individual LRs of each feature. What happens when the conditional independence assumption is violated?
Show solution
Naive Bayes with features independent given the class: . In log form: . For binary features, . This is equivalent to multiplying the LRs of each feature individually (if independent), exactly as in sequential updating. If features are not conditionally independent, Naive Bayes overestimates the confidence of predictions but often still classifies correctly. - Ex. 79.39ProofAnswer key
Demonstrate that the odds form of Bayes, posterior odds = LR prior odds, follows directly from the usual form of Bayes' theorem for two complementary events and .
Show solution
For the partition : and . Dividing: . The first factor is the LR, the second is the prior odds. The normalizing factor cancels in the quotient. - Ex. 79.40Challenge
Show that the mean of the posterior Beta(, ) converges to the maximum likelihood estimator when , for any fixed prior Beta(, ). What does this imply about the relationship between Bayes and frequentism for large samples?
Show solution
With flips and heads, the MLE is . The posterior mean with Beta(,) is . When : (since by the law of large numbers and the terms become negligible). For any fixed prior (with finite), the posterior converges to MLE. Interpretation: with many data, the data "annihilates" the prior — this is the consistency result of Bayesian inference.
Sources
-
Grinstead, C.M. & Snell, J.L. — Introduction to Probability (2nd ed.) · GNU FDL · Dartmouth College. Chapter 4 (§4.1): Conditional probability, independence, Bayes' theorem — primary source for most urn, coin, and proof exercises in this lesson.
-
Diez, D.M., Çetinkaya-Rundel, M., Barr, C.D. — OpenIntro Statistics (4th ed.) · CC-BY-SA · OpenIntro. Sections §3.2–3.4: conditional probability, Bayes, frequency tables, and Bayesian updating — source for PPV, sequential updating, and conjugate prior exercises.
-
Illowsky, B. & Dean, S. — Statistics (OpenStax) · CC-BY · OpenStax. Section §3.4 (Contingency Tables and Probability Trees): medical diagnosis, spam filtering, and probability trees — basis for Naive Bayes and fraud exercises.