S14 · Lesson 7 · Bayesian inference with conjugate priors
Estimating a probability from coin tosses, the beta prior and how counts update it, posterior mean, mode and variance, the predictive probability and Laplace's rule of succession, and the Dirichlet prior for several outcomes
Three tests of a new chip all pass. Maximum likelihood says the pass probability is 1, which no engineer believes after three trials. Bayesian inference fixes this by starting from a prior belief and updating it with the data. For coin-like data there's a prior, the beta distribution, that stays a beta distribution after every update, so learning reduces to adding counts. The same trick, for several outcomes at once, powers language models' smoothing and spam filters.
- The posterior density for a pass probability μ after m passes in N trials (m set by the fraction slider), starting from a beta(a, b) prior: beta(a + m, b + N − m).
- With a = b = 1 (a uniform prior) and 3 passes in 3, the posterior favors high μ but still allows lower values. More trials sharpen it.
- A stronger prior (larger a + b) needs more data to move.
Predict
A chip passes 3 tests out of 3. Starting from a uniform prior, what probability should you give that it passes the next one?
A uniform prior is beta(1, 1); three passes make the posterior beta(4, 1), whose mean is 4/5 = 0.8. That mean is the predictive probability of the next pass: Laplace's rule of succession, (m + 1)/(N + 2). It never reaches 1 or 0 from finite data.
Name it
The beta prior
For a probability μ, the beta density has mean and variance . The parameters act like prior counts of successes and failures.
Updating
After m successes in N trials (a binomial likelihood ), the posterior is
A prior that keeps its form under updating is conjugate.
Summaries
- Posterior mean , also the predictive probability of the next success.
- Posterior mode (MAP) .
- Maximum likelihood m/N is the limit of weak priors and lots of data.
- Laplace's rule (uniform prior): .
Several outcomes
For K outcomes with probabilities , the conjugate prior is the Dirichlet with parameters ; counts give posterior and posterior means . Adding 1 to every count before normalizing is "add-one smoothing".
Worked example
With a beta(2, 2) prior and 3 passes in 3, find the posterior mean, MAP estimate and standard deviation.
Show the solution
- Posterior.
- Mean and MAP.
- Spread. a standard deviation of 0.160.
- Effect of the prior. The prior's two pseudo-counts on each side pull the estimate away from the extreme.
Answer the posterior is beta(5, 2), with mean 0.714, MAP estimate 0.8 and standard deviation 0.160.
Worked example
A three-symbol source produced counts 3, 0 and 1. With a uniform Dirichlet prior (α = 1 each), what are the predictive probabilities?
Show the solution
- Posterior parameters. They are 4, 1 and 2, a total of 7.
- Predictive probabilities.
- The unseen symbol. It keeps probability 1/7 instead of the zero that maximum likelihood would give it, which would make any later occurrence infinitely surprising.
Answer the predictive probabilities are 0.571, 0.143 and 0.286.
Worked example
A failure rate gets a gamma prior with shape 2 and rate 1000 hours (as if 2 failures had been seen in 1000 hours). A fleet then logs 3 failures in 5000 hours. What's the posterior?
Show the solution
- Update. The gamma prior is conjugate to Poisson counts: shape and rate add the failures and the hours.
- Posterior mean. between the prior's and the data's .
- Standard deviation. The estimate is still uncertain by almost half its value after three failures.
Answer the posterior is gamma(5, 6000 h), with mean per hour and standard deviation per hour.
Check
A beta(2, 1) prior is updated with 7 successes in 20 trials. What's the posterior mean?
Check
With the same kind of update (beta(2, 2) prior, 7 of 20), what's the MAP estimate?
Check
After 3 successes in 10 trials, what does Laplace's rule of succession predict for the next trial?
Check
What's the variance of a beta(5, 3) distribution?
Check
A three-outcome source produced counts 1, 1 and 9. With a Dirichlet prior of 1 on each outcome, what's the predictive probability of the first?
Check
How far does the posterior mean (beta(1, 1) prior, 3 successes in 50) sit from the maximum-likelihood estimate?
Check
A failure rate has a gamma(2, 500 h) prior; then 12 failures occur in 10000 hours.
(a)
What is the posterior mean rate, per thousand hours?
(b)
What is its posterior standard deviation, per thousand hours?
Check
A beta(a, b) prior on a success probability is updated with m successes in N trials.
Step 1
What is the posterior's first parameter?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What probability does it predict for the next trial's success?
Step 3
What probability does it predict for the next two trials both succeeding?
Explain why
Why does maximum likelihood give unreasonable estimates from little data, and how does a prior help?
Maximum likelihood picks whatever parameter makes the observed data most probable, with no regard for how much data there is. Three passes in three trials make "always passes" the most likely explanation, and zero occurrences of a word make its probability zero, so a single later occurrence is treated as impossible. A prior encodes what's believed before the data, such as that a pass probability is unlikely to be exactly 1. With a conjugate beta prior it acts like a few imaginary trials added to the real ones, pulling estimates away from 0 and 1 when data are scarce and fading in influence as real counts accumulate.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.