S147 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

The beta prior

For a probability μ, the beta density Beta(μ|a,b)∝μa−1(1−μ)b−1 has mean a/(a+b) and variance ab(a+b)2(a+b+1). The parameters act like prior counts of successes and failures.

Updating

After m successes in N trials (a binomial likelihood μm(1−μ)N−m), the posterior is

Beta(μ|a+m,b+N−m)

A prior that keeps its form under updating is conjugate.

Summaries

  • Posterior mean a+ma+b+N, also the predictive probability of the next success.
  • Posterior mode (MAP) a+m−1a+b+N−2.
  • Maximum likelihood m/N is the limit of weak priors and lots of data.
  • Laplace's rule (uniform prior): (m+1)/(N+2).

Several outcomes

For K outcomes with probabilities μk, the conjugate prior is the Dirichlet with parameters αk; counts mk give posterior αk+mk and posterior means (αk+mk)/∑j(αj+mj). 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
  1. Posterior. beta(5,2)
  2. Mean and MAP. mean=5/7=0.714MAP=5−17−2=0.8
  3. Spread. var=5×272×8=0.0255 a standard deviation of 0.160.
  4. 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
  1. Posterior parameters. They are 4, 1 and 2, a total of 7.
  2. Predictive probabilities. p=(4,1,2)/7=(0.571,0.143,0.286)
  3. 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
  1. Update. The gamma prior is conjugate to Poisson counts: shape and rate add the failures and the hours. gamma(2+3,1000+5000)=gamma(5,6000h)
  2. Posterior mean. E[λ]=5/6000=8.33×10−4per hour between the prior's 2×10−3 and the data's 6×10−4.
  3. Standard deviation. σ=5/6000=3.7×10−4per hour The estimate is still uncertain by almost half its value after three failures.

Answer the posterior is gamma(5, 6000 h), with mean 8.33×10−4 per hour and standard deviation 3.7×10−4 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°.

Explain why

Why does maximum likelihood give unreasonable estimates from little data, and how does a prior help?

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.