Lesson 109 — Introductory Bayesian statistics
Prior, likelihood, posterior. Bayes's rule. Beta-Bernoulli conjugates. MAP versus MLE. Credible interval. Introduction to inference under the Bayesian paradigm.
Used in: Stochastik LK (Alemanha, Klasse 12) · H2 Math Statistics (Singapura) · AP Statistics (EUA)
Bayes's rule updates our belief about the parameter after observing data . The posterior is proportional to the product of the likelihood and the prior , normalized by the evidence .
Rigorous notation, full derivation, hypotheses
Rigorous definition
Bayes's theorem
"Bayes's theorem is a basic result of conditional probability, but its interpretation changes everything: it offers a formal recipe for updating beliefs in light of evidence." — OpenIntro Statistics §3.6
Conjugate priors: the Beta-Bernoulli case
Point estimators
Credible interval
Bayes factor
To compare hypotheses and :
Jeffreys scale: — strong evidence for ; — strong for ; — inconclusive.
Bayesian flow: prior × likelihood → posterior. The posterior becomes the new prior when more data arrives.
Worked examples
Problem. A disease affects 2% of the population. A test has sensitivity 90% and specificity 85%. A patient tests positive. What is the probability that they have the disease?
Strategy. Apply Bayes's formula with partition and calculate .
Solution.
, , , .
Total evidence:
Posterior:
Check. With only 2% prevalence, even a relatively good test generates many false positives. The answer makes sense: most positives come from the huge healthy population.
Source. OpenIntro Statistics §3.6, medical diagnosis example — CC-BY-SA.
Problem. An urn has unknown proportion of red balls. Prior: . Sampling with replacement: 1st sample — 3 red out of 5 draws; 2nd sample — 4 red out of 6 draws. Calculate the posterior after each sample and the final posterior mean.
Strategy. Beta-Bernoulli: after successes in trials, . Apply iteratively.
Solution.
Prior: , mean .
After 1st sample (, ):
After 2nd sample (, ):
Check. Total data: 7 red out of 11 draws, sample proportion = . Posterior mean 0.60 is between the prior (0.50) and sample proportion — makes sense. With weak prior, the posterior converges to the MLE as grows.
Source. Think Bayes §3 — Allen Downey — CC-BY-NC-SA.
Problem. For the Beta-Bernoulli model with , , after 6 successes in 10 trials, calculate the MLE, the MAP, and the posterior mean. Interpret the difference.
Strategy. MLE maximizes the likelihood; MAP maximizes the posterior; posterior mean is .
Solution.
MLE: .
Posterior: .
MAP (mode of Beta with is ):
Posterior mean:
Check. Ordering: mean (0.5625) between MAP (0.571) and the mode of Beta(9,7). MLE (0.60) is largest — the prior "pulls" toward 0.5 (prior symmetric around 0.5). With large , all three converge to the MLE.
Source. Think Bayes §4, §6 — Allen Downey — CC-BY-NC-SA.
Problem. After 12 successes in 20 trials with prior (uniform), calculate the central 95% credible interval for .
Strategy. Posterior . The central 95% interval is given by the 2.5th and 97.5th percentiles of the Beta distribution.
Solution.
Posterior: .
Posterior mean: .
By table or software (R: qbeta(c(0.025, 0.975), 13, 9)):
2.5th percentile: . 97.5th percentile: .
95% credible interval: .
Check. Direct interpretation: "given the uniform prior and the data, the probability that lies between 0.376 and 0.779 is 95%". Note that the interval is not centered at 0.6 — it is asymmetric because Beta is asymmetric in this case.
Source. Introduction to Probability §4.1 — Grinstead & Snell — GNU FDL.
Problem. To test whether (fair coin) versus (biased coin), with equal prior (), calculate the Bayes factor and the posterior probability of after 8 heads in 10 flips.
Strategy. Calculate for each hypothesis, then apply Bayes.
Solution.
Bayes factor:
With prior :
Check. — moderate to strong evidence for (Jeffreys scale: between 3 and 10 is "moderate"). The posterior probability of biased coin increased from 50% to 84%. Coherent with the data (8 of 10 favors ).
Source. OpenIntro Statistics §3.7 — Diez, Çetinkaya-Rundel, Barr — CC-BY-SA.
Exercise list
40 exercises · 10 with worked solution (25%)
- Ex. 109.1Application
Disease prevalence: 1%. Test sensitivity: 95%. False-positive rate: 10%. A patient tests positive. Calculate the probability of having the disease.
Show solution
Using Bayes: . The low prior (1%) generates many false positives even with a reasonable test. - Ex. 109.2Application
A coin is flipped 10 times and gets 4 heads. Prior: Beta(1,1) (uniform). Calculate the posterior, posterior mean, and compare with MLE.
Show solution
Posterior: . Posterior mean: . MLE: . The uniform prior pulls slightly the estimate.Show step-by-step (with the why)
- Uniform prior = Beta(1,1). We know nothing before.
- Data: 4 successes in 10 trials, so .
- Posterior: .
- Posterior mean: .
- MLE: sample proportion . The uniform prior pulls very little because data dominates.
- Ex. 109.3Application
Prior: Beta(4, 6). Sample: 7 successes in 10. Calculate the posterior, posterior mean, and MAP.
Show solution
Posterior: . Mean: . MAP: . - Ex. 109.4Application
Prior: Beta(2, 2). Batch 1: 5 successes in 10. Batch 2: 8 successes in 10. Perform sequential update and calculate final posterior mean.
Show solution
After batch 1 (5 of 10): . After batch 2 (8 of 10): . Mean: . - Ex. 109.5Application
Prevalence: 0.5%. Sensitivity: 99%. False-positive rate: 2%. Patient tests positive. What is the probability of having the disease?
Show solution
Evidence: . Posterior: . Less than 20% even with a very good test. - Ex. 109.6ApplicationAnswer key
3 successes in 10 trials. Compare the posterior mean with priors Beta(1,1) and Beta(5,5). Which prior has greater influence on the posterior?
Show solution
MLE: . With Beta(1,1): posterior mean = 4/12 = 0.333. With Beta(5,5): posterior mean = 8/20 = 0.40. Stronger prior pulls more toward 0.5. - Ex. 109.7ApplicationAnswer key
Three factories produce bolts: E1 (60% of production, 30% defective), E2 (30%, 50% defective), E3 (10%, 10% defective). A defective bolt is withdrawn. What is the probability it came from E1?
Show solution
Bayes table: . - Ex. 109.8Application
Prior: Beta(3, 3) (slight belief in fair coin, mean 0.5). Flip 5 times and get 0 heads. Calculate the posterior and new mean.
Show solution
Posterior after 0 successes in 5: . Mean: . Before: 0.50. The absence of successes reduces belief in a fair coin. - Ex. 109.9Application
Prior: Beta(1,1). Data: 15 successes in 20. Calculate MAP and MLE. Are they equal? Why?
Show solution
MAP of Beta(a,b) with a,b greater than 1 is . Posterior: Beta(1+15, 1+5) = Beta(16,6). MAP: . MLE: . With uniform prior, MAP = MLE. - Ex. 109.10Application
Bag with two coins: one always gives heads (H), the other is fair (J). One is chosen at random. Flipped twice, both heads. What is the probability it is the H coin?
Show solution
. There is 80% chance it's the always-heads coin. - Ex. 109.11Understanding
What does a 95% Bayesian credible interval mean?
Show solution
The Bayesian credible interval has direct interpretation: given the prior and the data, the probability that lies in the interval is exactly . This is possible because, in the Bayesian paradigm, is a random variable with distribution. Option B describes the frequentist CI. - Ex. 109.12Understanding
Which statement about MAP and MLE is INCORRECT?
Show solution
The correct statement is A: MAP = MLE when the prior is uniform, because maximizing (constant) is equivalent to maximizing . Option B is wrong — with non-uniform prior, MAP and MLE differ. Option D describes asymptotic convergence, not equality. - Ex. 109.13Understanding
How does sample size n affect the relationship between prior and posterior?
Show solution
With large , the likelihood dominates — the posterior converges to the MLE regardless of prior (Bernstein–von Mises result). Statement D is correct. Statement A has correct logic for small to moderate n, but when n is large even a strong prior dilutes. - Ex. 109.14ApplicationAnswer key
A student passes the exam (). Known: (studied hard, probability 60%), (didn't study, probability 40%). Given that they passed, what is the probability they studied hard?
Show solution
. . - Ex. 109.15ApplicationAnswer key
A machine has unknown success rate. Prior: Beta(4, 2) (history of 4 successes and 2 failures). New test: 6 consecutive successes. Calculate the posterior, mean, and MAP.
Show solution
Beta(10, 2). Mean: 10/12 ≈ 0.833. MAP: 9/10 = 0.9. The informative prior (Beta(4,2)) weighs less than the data (6 of 6): the posterior reflects the perfect sequence but with memory of earlier failures. - Ex. 109.16ApplicationAnswer key
Calculate the Bayes Factor for versus after 8 heads in 10 flips.
Show solution
. Numerator: . Denominator: . BF ≈ 5.32. Moderate evidence for biased coin. - Ex. 109.17Application
Three batches of 10 trials each: 7 successes, 6 successes, 7 successes. Prior: Beta(1,1). Perform sequential update and calculate final posterior mean.
Show solution
Cumulative posterior after 30 trials (20 successes): Beta(1+20, 1+10) = Beta(21, 11). Mean: 21/32 ≈ 0.656. With n=30, prior Beta(1,1) has minimal weight; posterior mean ≈ MLE = 20/30 = 0.667. - Ex. 109.18Application
Prevalence: 30%. Sensitivity: 95%. False-positive rate: 20%. Patient tests positive. Calculate the probability of having the disease and compare with Exercise 109.1.
Show solution
Evidence: P(+) = 0.95 × 0.30 + 0.20 × 0.70 = 0.285 + 0.14 = 0.425. P(disease | +) = 0.285/0.425 ≈ 67.1%. High prevalence dramatically changes the positive predictive value. - Ex. 109.19Application
Show that the posterior mean of the Beta-Bernoulli model is a weighted average of the prior and sample proportion. Identify the weights.
Show solution
The mean of Beta(a,b) is a/(a+b). After n trials with s successes and prior Beta(α,β): posterior mean = (α+s)/(α+β+n). It is a weighted average of the prior (α/(α+β)) and sample proportion (s/n), with weights (α+β) and n. - Ex. 109.20Application
Prior: Beta(2, 2). Data: 0 successes in 3. Calculate the posterior, MAP, and posterior mean.
Show solution
Beta(2+0, 2+3) = Beta(2,5). Mean: 2/7 ≈ 0.286. MAP: (2-1)/(2+5-2) = 1/5 = 0.20. With 0 of 3, the posterior concentrates at low values of θ. - Ex. 109.21ApplicationAnswer key
Probability of rain in Fortaleza on a given day: 40%. If it rains, there is 85% chance of dark clouds. If it doesn't rain, 30%. There are dark clouds. What is the probability of rain?
Show solution
P(rain | clouds) = P(clouds | rain) × P(rain) / P(clouds). P(clouds) = 0.85 × 0.4 + 0.3 × 0.6 = 0.34 + 0.18 = 0.52. Posterior = 0.34/0.52 ≈ 65.4%. - Ex. 109.22Application
Production history: 10% defect rate (equivalent to 10 defects in 100 items = Beta(10,90)). New inspection: 3 defects in 20. Calculate the posterior and posterior mean.
Show solution
With prior Beta(10, 90) (expected 10%), after 3 defective in 20: Beta(13, 107). Mean: 13/120 ≈ 10.8%. The strong prior (100 earlier observations) absorbs the small new sample. - Ex. 109.23Application
Bag with 3 coins: 1 always gives heads (H), 2 are fair (J). One is withdrawn at random and flipped: heads comes up. What is the probability it is the H coin?
Show solution
P(H) = 1/3, P(J) = 2/3. P(H | heads) = 1, P(J | heads) = 0.5. P(H | heads) = (1 × 1/3) / (1 × 1/3 + 0.5 × 2/3) = (1/3) / (2/3) = 0.5. After 1 head: 50%.Show step-by-step (with the why)
- Set up the table: hypothesis, prior, likelihood, product.
- H (always heads): prior 1/3, likelihood 1, product 1/3.
- J (fair): prior 2/3, likelihood 0.5, product 1/3.
- Sum of products = 2/3. Normalize: P(H|heads) = (1/3)/(2/3) = 0.5.
- Surprising: 1 heads doesn't distinguish between the two coins — J's higher prior balances H's greater likelihood.
- Ex. 109.24Application
Prior Beta(1,1). Data: 10 successes in 20. Describe the posterior and the central 95% credible interval (use the fact that the 2.5th percentile of Beta(11,11) ≈ 0.31).
Show solution
Posterior after 10 successes in 20 with Beta(1,1): Beta(11,11). Central 95% interval: 2.5th and 97.5th percentiles of Beta(11,11). By symmetry (a=b), centered at 0.5. Approx. (0.31; 0.69). - Ex. 109.25ModelingAnswer key
A tutoring center has historically approved 70% of students in ENEM. New class, 20 students: 15 passed. Propose a suitable Beta prior, calculate the posterior, and posterior mean approval rate.
Show solution
Natural prior: Beta(α, β) with α/(α+β) = historical approval rate (e.g., 70%). If history suggests 70%: Beta(7,3). After 15 approved in 20 students from new class: Beta(22,8). Posterior mean: 22/30 ≈ 73.3%. Credible interval (use software or table). - Ex. 109.26Modeling
Pancreatic cancer prevalence: 0.2%. Biopsy: sensitivity 92%, specificity 97%. Exam positive. Calculate P(cancer | positive) and discuss the medical decision.
Show solution
Doctor's inverse Bayes: P(cancer) = 0.002 (rare). Biopsy positive: sensitivity 92%, false-positive 3%. P(cancer | biopsy+) = (0.92 × 0.002)/(0.92 × 0.002 + 0.03 × 0.998) = 0.00184/0.03178 ≈ 5.8%. Nevertheless, biopsy is recommended because the cost of not treating is huge. - Ex. 109.27ModelingAnswer key
A transport company reports 20 delayed deliveries in 50 monitored deliveries. Using prior Beta(1,1), estimate the delay rate with a 90% credible interval.
Show solution
With prior Beta(1,1) and 20 delays in 50 deliveries: Beta(21, 31). Mean: 21/52 ≈ 40.4%. 90% credible interval approx. (0.29; 0.53). The operation maintains rate close to 40%. Bayesian allows saying "there is 90% chance the rate is between 29% and 53%". - Ex. 109.28Modeling
A fintech knows 1% of transactions are fraudulent. An algorithm detects that the current transaction has value outside the client's pattern. P(abnormal value | fraud) = 85%, P(abnormal value | legitimate) = 2%. Calculate the probability of fraud.
Show solution
Before: P(fraud) = 0.01. Transaction of 15,000 reais outside pattern. P(large transaction | fraud) = 0.85, P(large transaction | legitimate) = 0.02. Posterior: (0.85 × 0.01)/(0.85 × 0.01 + 0.02 × 0.99) = 0.0085/0.0283 ≈ 30.0%. One suspicious signal raises from 1% to 30%.Show step-by-step (with the why)
- Identify prior: P(fraud) = 0.01.
- Likelihood: P(high value | fraud) = 0.85; P(high value | legitimate) = 0.02.
- Calculate evidence: P(high value) = 0.85 × 0.01 + 0.02 × 0.99 = 0.0085 + 0.0198 = 0.0283.
- Apply Bayes: posterior = 0.0085/0.0283 ≈ 30%.
- Decision: with 30% chance of fraud, worth investigating (block temporarily).
- Ex. 109.29ProofAnswer key
Show that, for the Bernoulli model with Beta prior, the posterior is also Beta. Identify the parameters.
Show solution
Likelihood for s successes in n Bernoulli(θ): . Prior Beta(α,β): proportional to . Posterior: proportional to , which is exactly the kernel of Beta(α+s, β+n-s). The normalization constant is B(α+s, β+n-s). - Ex. 109.30Proof
Demonstrate that, with prior Beta(1,1) (uniform), the MAP estimator coincides with the MLE for the Bernoulli model.
Show solution
Uniform prior: Beta(1,1), constant density 1 on [0,1]. Posterior with s successes in n: Beta(1+s, 1+n-s), proportional to . This is exactly the likelihood (Bernoulli). So maximizing posterior = maximizing likelihood = MAP = MLE = s/n. - Ex. 109.31Application
Spam filter: 20% of emails are spam. In spam emails, each suspicious keyword appears with probability 60%; in legitimate emails, 5%. An email has 3 keywords. What is the probability it is spam?
Show solution
P(email spam | 3 keywords) applying Naive Bayes: P(spam) = 0.2, P(keywords | spam) = 0.6^3 = 0.216, P(keywords | legitimate) = 0.05^3 = 0.000125. Posterior: (0.216 × 0.2)/(0.216 × 0.2 + 0.000125 × 0.8) = 0.0432/0.04321 ≈ 99.97%. - Ex. 109.32Application
Two groups of rats: lineage 1 (10 animals, 8 developed tumor after exposure) and lineage 2 (10 animals, 3 developed). Prior Beta(1,1) for both rates. Calculate the posterior and posterior mean for each lineage.
Show solution
Posteriors: H1: Beta(1+8, 1+2) = Beta(9,3); H2: Beta(1+3, 1+7) = Beta(4,8). Means: H1 = 9/12 = 0.75; H2 = 4/12 = 0.33. Lineage 1 has much higher estimated transmission probability. 95% credible interval for H1 approx. (0.46; 0.94). - Ex. 109.33Application
An urn has unknown proportion of orange balls. After 100 draws with replacement, 50 are orange. Prior Beta(1,1). Calculate the posterior, mean, and 95% credible interval.
Show solution
After 50 draws of orange ball in 100: Beta(1+50, 1+50) = Beta(51,51). Mean: 0.5. MAP: 50/100 = 0.5. 95% credible interval approx. (0.40; 0.60). With n=100, the credible interval narrows much compared to n=10 case. - Ex. 109.34Challenge
The Jeffreys prior for Bernoulli is Beta(0.5; 0.5). After 6 successes in 10, calculate the posterior. Research what it means for this prior to be "invariant under reparametrization" and compare the posterior mean with prior Beta(1,1).
Show solution
The Beta(0.5; 0.5) prior is the Jeffreys prior for the Bernoulli model. It is invariant under transformations — if θ → φ = g(θ), the resulting prior is also Jeffreys for the new parametrization. It is "objectively non-informative" in the sense of not privileging any parametrization. After 6 successes in 10: Beta(6.5; 4.5). Mean ≈ 0.591, different from 0.60 of MAP with uniform prior. - Ex. 109.35ApplicationAnswer key
A genetic test detects predisposition for thrombosis. It is known that 3% of the population truly has the predisposition. The test is 99% accurate when a person has the predisposition and 98% accurate when they do not. What is the probability that a person with a positive test truly has the predisposition?
Show solution
Prevalence: . Sensitivity: . Specificity: , so false-positive: . Evidence: . Posterior: , or about 60.5%.Show step-by-step (with the why)
- Define events: = have predisposition; = test positive. Prior: .
- Write likelihoods: and .
- Calculate evidence: .
- Apply Bayes: .
- Ex. 109.36Application
Lupus affects 2% of the population. The test is 98% accurate when a person has the disease and 74% accurate when they do not. A patient tests positive. What is the probability of having lupus? Does the TV show phrase "It's always lupus" make Bayesian sense?
Show solution
Prevalence: . Sensitivity: . False-positive rate: . Evidence: . Posterior: . With 74% specificity, many false positives. Dr. House's phrase has Bayesian grounding. - Ex. 109.37Application
An exit poll showed that 53% of voters voted for Scott Walker. Among those who voted for Walker, 37% had a college degree; among those who voted against, 44% had a degree. A voter with a college degree was selected at random. What is the probability they voted for Walker?
Show solution
Let = voted for Walker and = has college degree. Prior: . Likelihoods: and . Evidence: . Posterior: .Show step-by-step (with the why)
- Prior: , so .
- Likelihoods: and .
- Evidence: .
- Posterior: .
- Ex. 109.38Application
Let , , . Calculate and determine if and are independent.
Show solution
, so . For independence we'd need , but — so C and D are not independent. Also: . And . - Ex. 109.39Application
According to United Blood Services: 43% of people have type O blood, 15% have Rh factor, and 52% have type O or Rh factor. Calculate the probability that a person has both type O blood AND Rh factor, and the probability of having neither.
Show solution
By addition: . Substituting: , so . Probability of NOT having both: . - Ex. 109.40Modeling
After an introductory statistics course, 80% of students can correctly construct box plots. Of those, 65% pass the final exam. Of those who cannot construct box plots correctly, only 15% pass. A student passed the exam. What is the probability that they knew how to correctly construct box plots?
Show solution
After an introductory statistics class: 80% of students construct box plots correctly. Of those, 65% pass the exam. Of those who don't construct correctly (20%), only 15% pass. Apply Bayes to find: . Those who passed almost certainly knew how to construct box plots.Show step-by-step (with the why)
- Define: = constructed box plot correctly, = passed exam.
- Prior: , .
- Likelihoods: and .
- Evidence: .
- Posterior: .
Sources
- Think Bayes — Allen B. Downey · CC-BY-NC-SA · Greenteapress · Chapters 1–9.
- Introduction to Probability — Grinstead & Snell · GNU FDL · Dartmouth · §4.1.
- OpenIntro Statistics — Diez, Çetinkaya-Rundel, Barr · CC-BY-SA · OpenIntro · §3.6–3.7.