Day 13. Bayesian Inference
Priors, likelihoods and posteriors; conjugate updating, shrinkage and credible intervals.
- ●Apply Bayes' theorem to parameters and compute Beta–binomial and normal–normal posteriors.
- ●Show that the posterior mean is a precision-weighted average of the prior mean and the data.
- ●Explain empirical-Bayes shrinkage and its link to ridge regression.
- ●Contrast credible intervals with confidence intervals and compute a Bayesian A/B comparison.
Theory
Parameters as random variables
Frequentists treat as fixed and the data as random. Bayesians also put a distribution on that describes what is known about it:
The denominator only normalises, so we often work with "∝".
Two conjugate models
Beta–binomial. Prior , data successes and failures:
The prior acts like earlier successes and earlier failures.
Normal–normal. Prior and an estimate . With precisions (inverse variances) and :
Precisions add, and the posterior mean shrinks the estimate toward the prior mean in proportion to how noisy the estimate is.
Summaries
- Posterior mean (minimises expected squared loss), MAP (posterior mode).
- A 95% credible interval contains with posterior probability 0.95: the statement people often wrongly make about confidence intervals.
- As grows, the likelihood dominates any reasonable prior and Bayesian and frequentist answers converge.
Causal link. Shrinkage is everywhere in applied causal work: teacher and hospital value-added, many-site experiments, and heterogeneous treatment effects all borrow strength across units this way.
Part 1 · Warm-up
Exercise 1 · The three ingredients
In the posterior , name each term and say which one Day 11 maximised.
Show solution
is the likelihood (Day 11 maximised it), the prior, and the posterior.
Exercise 2 · A flat prior
With a Beta(1, 1) (uniform) prior and 18 adoptions in 30 households, find the posterior, its mean and its mode.
Show solution
Step 1. Posterior Beta(19, 13).
Step 2. Mean ; mode .
Step 3. With a flat prior, the MAP equals the MLE.
Exercise 3 · An informative prior
Prior Beta(5, 5); data 8 successes and 12 failures. Find the posterior mean and mode.
Show solution
Step 1. Posterior Beta(13, 17).
Step 2. Mean ; mode .
Step 3. The MLE is , pulled toward the prior mean 0.5.
Exercise 4 · Posterior predictive
Using Exercise 3, what is the probability that the next household adopts?
Show solution
.
Exercise 5 · Prior strength
How many "pseudo-observations" does a Beta(5, 5) prior represent, compared with Beta(50, 50)?
Show solution
: 10 versus 100. Both centre on 0.5, but the second takes ten times as much data to move.
Part 2 · Proofs
Exercise 6 · Beta–binomial conjugacy
Prove the posterior is Beta(, ).
Show solution
Step 1. Likelihood ; prior .
Step 2. Their product is
Step 3. the kernel of a Beta(, ) density.
Step 4. The normalising constant must be the Beta one, because a density integrates to 1.
Exercise 7 · The posterior mean as a weighted average
Show with and .
Show solution
As , and the posterior mean approaches the MLE: the data dominate.
Exercise 8 · Normal–normal updating
Derive the normal–normal posterior by completing the square.
Show solution
A quadratic is the log-kernel of . So the posterior is normal with precision and mean .
Exercise 9 · MAP with a normal prior is ridge
In with and prior , show the MAP minimises with .
Show solution
Multiply by : the MAP minimises , the ridge objective. A tighter prior (small ) means more penalisation.
Exercise 10 · Credible versus confidence
With a flat prior and , show that the 95% credible interval equals the 95% confidence interval numerically. Do they mean the same thing?
Show solution
Step 1. A flat prior gives posterior , so the credible interval is , the same numbers as the CI.
Step 2. The meanings differ: the credible interval says "given the data, is in here with probability 0.95"; the CI says "this procedure covers the true in 95% of samples".
Part 3 · Applied
Exercise 11 · A sceptical prior
A policy evaluation reports (SE 1). Using a sceptical prior , find the posterior and .
Show solution
Step 1. Precisions: prior ; data .
Step 2. Posterior precision , so posterior variance and sd .
Step 3. Posterior mean .
Step 4. .
Answer: the sceptical prior halves the estimate to 1.25, but there is still a 96% posterior probability that the policy helps.
Exercise 12 · Empirical-Bayes shrinkage
School effects vary with around a mean of 0. One school's estimate is 6 with sampling variance 12. Find its shrunken estimate.
Show solution
Step 1. Shrinkage factor: .
Step 2. Posterior mean .
Answer: 1.5. The school's noisy estimate (sampling variance 12, three times the true spread of school effects) is pulled three-quarters of the way toward the average. Value-added rankings use exactly this adjustment.
Exercise 13 · Vaccination uptake: the data dominate
A health agency's prior for the share of adults who will take a new vaccine is Beta(50, 50). A survey of 1,000 adults finds 600 intend to get it. Compare the posterior mean with the MLE, and give the prior's weight.
Show solution
Step 1. Posterior: Beta = Beta(650, 450).
Step 2. Posterior mean ; MLE .
Step 3. Prior weight .
Answer: 59.1% versus 60.0%; the prior carries only 9% weight, and less with more data.
Exercise 14 · Peeking at an A/B test
Why can a frequentist who checks a p-value every day and stops at the first inflate the false-positive rate, and how does a Bayesian analysis treat stopping?
Show solution
Step 1 (frequentist). Each daily look is another chance for noise to cross , so stopping at the first crossing makes the overall false-positive rate well above 5%. Sequential designs (e.g. alpha-spending) adjust the thresholds.
Step 2 (Bayesian). The posterior depends on the data only through the likelihood, which is the same whatever the stopping rule, so the posterior is unchanged by when you stopped.
Step 3 (caveat). Stopping on a lucky streak still yields optimistic estimates when the prior is weak.
Answer: peeking invalidates fixed-level p-values; Bayesian summaries remain coherent, but a sensible prior is still needed.
Exercise 15 · Which vaccination reminder works better?
A clinic randomises two SMS reminders. Message A: 120 of 1,000 patients book a vaccination; message B: 150 of 1,000. With Beta(1, 1) priors, estimate .
Show solution
Step 1. Posteriors: Beta(121, 881) and Beta(151, 851).
Step 2. Draw many pairs from these posteriors and count how often (code below): about 0.975.
Step 3. Check with a normal approximation. Posterior means ≈ 0.121 and 0.151; sds ≈ 0.0103 and 0.0113. The difference has mean ≈ 0.030 and sd , so .
Answer: a 97.5% posterior probability that message B works better, with a 95% credible interval for the gain of roughly 0 to 6 points.
Verify in Python
import numpy as np
from scipy import stats
rng = np.random.default_rng(13)
a = rng.beta(121, 881, 1_000_000); b = rng.beta(151, 851, 1_000_000)
print("P(pB > pA | data) ~", round((b > a).mean(), 4))
print("95% credible interval for pB - pA:", np.percentile(b - a, [2.5, 97.5]).round(4))
# Normal-normal update (Exercise 11)
prior_mu, prior_var, est, se2 = 0.0, 1.0, 2.5, 1.0
post_var = 1 / (1 / prior_var + 1 / se2)
post_mu = post_var * (prior_mu / prior_var + est / se2)
print(f"posterior N({post_mu:.2f}, {post_var:.2f}); P(tau>0) = {stats.norm.sf(0, post_mu, np.sqrt(post_var)):.3f}")
# Beta posterior summaries (Exercise 3)
post = stats.beta(13, 17)
print("mean", round(post.mean(), 3), " 95% credible interval", np.round(post.ppf([0.025, 0.975]), 3))
Key takeaways
- Posterior ∝ likelihood × prior; conjugate priors give closed-form updates.
- Posterior means are precision-weighted averages, so noisy estimates are shrunk toward the prior.
- With enough data the prior matters little; with little data it matters a lot, so state it openly.
Simulation Lab: Beta–Binomial Updating
Open full screen →Prior × likelihood = posterior. Change prior strength and data and watch the posterior move.
Prior (dashed) × likelihood (normalised, thin) ∝ posterior (filled). The posterior mean is a weighted average of the prior mean and the MLE, with the prior's weight (α+β)/(α+β+n).