Week 11 · Bayesian inference with a beta prior
Calculate a posterior and see how prior strength matters.
← Week 10 · Subject contents · Week 12 →
Calculate a posterior and see how prior strength matters.
Work through the problem
For a Beta(a,b) prior, its density is proportional to pᵃ⁻¹(1−p)ᵇ⁻¹. Multiplying by a Bernoulli likelihood with k successes and n−k failures adds the powers. The posterior is Beta(a+k,b+n−k).
With 2 successes in 3 trials, a uniform Beta(1,1) prior gives Beta(3,2), mean 3/5 = 0.60. A Beta(10,10) prior gives Beta(12,11), mean 12/23 ≈ 0.522. An equal-tail 95% credible interval leaves 2.5% of posterior probability on each side given the chosen model and prior.
Three observations under two priors.
Run the Python lab
import numpy as np
from scipy.stats import beta
grid = np.linspace(0, 1, 101)
curves = {}
for a, b in [(1, 1), (10, 10)]:
posterior = beta(a+2, b+1)
print(f"Prior ({a},{b}) -> mean {posterior.mean():.3f}, interval {posterior.ppf([.025,.975]).round(3)}")
curves[f"Prior Beta({a}, {b})"] = posterior.pdf(grid)
assert abs(beta(3,2).mean() - .6) < 1e-12
Output from the lab
Prior (1,1) -> mean 0.600, interval [0.194 0.932]
Prior (10,10) -> mean 0.522, interval [0.322 0.718]
Practice
- Update Beta(2,2) with four successes and one failure.
Check your reasoning
- The posterior is Beta(6,3) with mean 6/9.