Math for Causal Inference

Day 13. Bayesian Inference

Priors, likelihoods and posteriors; conjugate updating, shrinkage and credible intervals.

Video coming soon
Learning outcomes
  • ●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.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

Parameters as random variables

Frequentists treat θ\theta as fixed and the data as random. Bayesians also put a distribution on θ\theta that describes what is known about it:

p(θ∣x)⏟posterior=p(x∣θ)⏞likelihood  p(θ)⏞priorp(x)  ∝  p(x∣θ) p(θ).\underbrace{p(\theta \mid x)}_{\text{posterior}} = \frac{\overbrace{p(x \mid \theta)}^{\text{likelihood}}\;\overbrace{p(\theta)}^{\text{prior}}}{p(x)} \;\propto\; p(x \mid \theta)\,p(\theta).

The denominator p(x)=∫p(x∣θ)p(θ) dθp(x) = \int p(x \mid \theta)p(\theta)\,d\theta only normalises, so we often work with "∝".

Two conjugate models

Beta–binomial. Prior p∼Beta(α,β)p \sim \text{Beta}(\alpha, \beta), data kk successes and ff failures:

p∣data∼Beta(α+k,  β+f),E[p∣data]=α+kα+β+k+f,mode=α+k−1α+β+k+f−2.p \mid \text{data} \sim \text{Beta}(\alpha + k,\; \beta + f), \qquad E[p \mid \text{data}] = \frac{\alpha + k}{\alpha + \beta + k + f}, \qquad \text{mode} = \frac{\alpha + k - 1}{\alpha + \beta + k + f - 2}.

The prior acts like α\alpha earlier successes and β\beta earlier failures.

Normal–normal. Prior θ∼N(μ0,τ02)\theta \sim N(\mu_0, \tau_0^2) and an estimate θ^∼N(θ,s2)\hat\theta \sim N(\theta, s^2). With precisions (inverse variances) π0=1/τ02\pi_0 = 1/\tau_0^2 and πd=1/s2\pi_d = 1/s^2:

θ∣θ^∼N(π0μ0+πdθ^π0+πd,  1π0+πd).\theta \mid \hat\theta \sim N\Big(\frac{\pi_0\mu_0 + \pi_d\hat\theta}{\pi_0 + \pi_d},\; \frac{1}{\pi_0 + \pi_d}\Big).

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 θ\theta with posterior probability 0.95: the statement people often wrongly make about confidence intervals.
  • As nn 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 p(θ∣x)∝p(x∣θ)p(θ)p(\theta \mid x) \propto p(x \mid \theta)p(\theta), name each term and say which one Day 11 maximised.

Show solution

p(x∣θ)p(x \mid \theta) is the likelihood (Day 11 maximised it), p(θ)p(\theta) the prior, and p(θ∣x)p(\theta \mid x) 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 19/32≈0.59419/32 \approx 0.594; mode 18/30=0.618/30 = 0.6.

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 13/30≈0.43313/30 \approx 0.433; mode 12/28≈0.42912/28 \approx 0.429.

Step 3. The MLE is 8/20=0.408/20 = 0.40, 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

P(next=1∣data)=E[p∣data]=13/30≈0.433P(\text{next} = 1 \mid \text{data}) = E[p \mid \text{data}] = 13/30 \approx 0.433.

Exercise 5 · Prior strength

How many "pseudo-observations" does a Beta(5, 5) prior represent, compared with Beta(50, 50)?

Show solution

α+β\alpha + \beta: 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(α+k\alpha + k, β+f\beta + f).

Show solution

Step 1. Likelihood ∝pk(1−p)f\propto p^k(1 - p)^f; prior ∝pα−1(1−p)β−1\propto p^{\alpha - 1}(1 - p)^{\beta - 1}.

Step 2. Their product is

pα+k−1(1−p)β+f−1,p^{\alpha + k - 1}(1 - p)^{\beta + f - 1},

Step 3. the kernel of a Beta(α+k\alpha + k, β+f\beta + f) 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 E[p∣data]=w⋅αα+β+(1−w)⋅knE[p \mid \text{data}] = w\cdot\frac{\alpha}{\alpha + \beta} + (1 - w)\cdot\frac{k}{n} with n=k+fn = k + f and w=α+βα+β+nw = \frac{\alpha + \beta}{\alpha + \beta + n}.

Show solution α+kα+β+n=α+βα+β+n⋅αα+β+nα+β+n⋅kn.\frac{\alpha + k}{\alpha + \beta + n} = \frac{\alpha + \beta}{\alpha + \beta + n}\cdot\frac{\alpha}{\alpha + \beta} + \frac{n}{\alpha + \beta + n}\cdot\frac kn.

As n→∞n \to \infty, w→0w \to 0 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 ln⁡p(θ∣θ^)=−(θ^−θ)22s2−(θ−μ0)22τ02+c=−πd+π02θ2+(πdθ^+π0μ0)θ+c′.\ln p(\theta \mid \hat\theta) = -\frac{(\hat\theta - \theta)^2}{2s^2} - \frac{(\theta - \mu_0)^2}{2\tau_0^2} + c = -\frac{\pi_d + \pi_0}{2}\theta^2 + (\pi_d\hat\theta + \pi_0\mu_0)\theta + c'.

A quadratic −a2θ2+bθ-\frac a2\theta^2 + b\theta is the log-kernel of N(b/a,1/a)N(b/a, 1/a). So the posterior is normal with precision π0+πd\pi_0 + \pi_d and mean (π0μ0+πdθ^)/(π0+πd)(\pi_0\mu_0 + \pi_d\hat\theta)/(\pi_0 + \pi_d).

Exercise 9 · MAP with a normal prior is ridge

In y=Xβ+εy = X\beta + \varepsilon with ε∼N(0,σ2I)\varepsilon \sim N(0, \sigma^2I) and prior β∼N(0,τ2I)\beta \sim N(0, \tau^2I), show the MAP minimises ∥y−Xβ∥2+λ∥β∥2\|y - X\beta\|^2 + \lambda\|\beta\|^2 with λ=σ2/τ2\lambda = \sigma^2/\tau^2.

Show solution −ln⁡p(β∣y)=12σ2∥y−Xβ∥2+12τ2∥β∥2+c.-\ln p(\beta \mid y) = \frac{1}{2\sigma^2}\|y - X\beta\|^2 + \frac{1}{2\tau^2}\|\beta\|^2 + c.

Multiply by 2σ22\sigma^2: the MAP minimises ∥y−Xβ∥2+(σ2/τ2)∥β∥2\|y - X\beta\|^2 + (\sigma^2/\tau^2)\|\beta\|^2, the ridge objective. A tighter prior (small τ\tau) means more penalisation.

Exercise 10 · Credible versus confidence

With a flat prior and θ^∼N(θ,s2)\hat\theta \sim N(\theta, s^2), 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 θ∣θ^∼N(θ^,s2)\theta \mid \hat\theta \sim N(\hat\theta, s^2), so the credible interval is θ^±1.96s\hat\theta \pm 1.96s, the same numbers as the CI.

Step 2. The meanings differ: the credible interval says "given the data, θ\theta is in here with probability 0.95"; the CI says "this procedure covers the true θ\theta in 95% of samples".

Part 3 · Applied

Exercise 11 · A sceptical prior

A policy evaluation reports τ^=2.5\hat\tau = 2.5 (SE 1). Using a sceptical prior τ∼N(0,1)\tau \sim N(0, 1), find the posterior and P(τ>0∣data)P(\tau > 0 \mid \text{data}).

Show solution

Step 1. Precisions: prior 1/12=11/1^2 = 1; data 1/12=11/1^2 = 1.

Step 2. Posterior precision =2= 2, so posterior variance =0.5= 0.5 and sd ≈0.707\approx 0.707.

Step 3. Posterior mean =(1×0+1×2.5)/2=1.25= (1 \times 0 + 1 \times 2.5)/2 = 1.25.

Step 4. P(τ>0∣data)=Φ(1.25/0.707)=Φ(1.77)≈0.96P(\tau > 0 \mid \text{data}) = \Phi(1.25/0.707) = \Phi(1.77) \approx 0.96.

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 τ2=4\tau^2 = 4 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: τ2/(τ2+s2)=4/(4+12)=0.25\tau^2/(\tau^2 + s^2) = 4/(4 + 12) = 0.25.

Step 2. Posterior mean =0+0.25×(6−0)=1.5= 0 + 0.25 \times (6 - 0) = 1.5.

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(50+600,  50+400)(50 + 600,\; 50 + 400) = Beta(650, 450).

Step 2. Posterior mean =650/1100≈0.591= 650/1100 \approx 0.591; MLE =600/1000=0.600= 600/1000 = 0.600.

Step 3. Prior weight =(α+β)/(α+β+n)=100/1100≈9%= (\alpha + \beta)/(\alpha + \beta + n) = 100/1100 \approx 9\%.

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 p<0.05p < 0.05 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 p<0.05p < 0.05, 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 P(pB>pA∣data)P(p_B > p_A \mid \text{data}).

Show solution

Step 1. Posteriors: pA∼p_A \sim Beta(121, 881) and pB∼p_B \sim Beta(151, 851).

Step 2. Draw many pairs (pA,pB)(p_A, p_B) from these posteriors and count how often pB>pAp_B > p_A (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 ≈0.01032+0.01132≈0.0153\approx \sqrt{0.0103^2 + 0.0113^2} \approx 0.0153, so P≈Φ(0.030/0.0153)=Φ(1.96)≈0.975P \approx \Phi(0.030/0.0153) = \Phi(1.96) \approx 0.975.

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.

02400.20.40.60.81θ (adoption rate)density
prior Beta(5, 5)likelihood (normalised)posterior Beta(13, 17)
Posterior mean
0.433
MAP (mode)
0.429
MLE k/n
0.400
Prior weight
33%
prior mean 0.50
95% credible interval
0.264 – 0.611

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).

Day 12Day 14