Posterior inference
Update a prior, compare posterior means with MAP estimates and interpret predictive uncertainty.
THE IDEA
The Beta prior updates by counting
For a coin, multiplying a Beta(a, b) prior by the likelihood theta^h (1 - theta)^t gives a curve proportional to theta^(a-1+h) (1-theta)^(b-1+t), and that is exactly the shape of a Beta(a + h, b + t). No integral is needed: add the heads to a, add the tails to b. You can read a and b as pseudo-counts: Beta(2, 2) behaves like having already seen one head and one tail beyond the flat starting point.
Order does not matter: after the first 10 flips of this coin (6 heads, 4 tails) a Beta(2, 2) prior becomes Beta(8, 6) whether you update ten times or once.
Same data, different priors
Two analysts with different priors disagree at first, since a prior is a starting opinion. As data arrives the likelihood gets sharper, while the prior keeps the same weight of a fixed number of pseudo-flips, so the posteriors converge. Drag the slider from 0 flips upward.
| prior | posterior | mean |
|---|---|---|
| flat Beta(1, 1) | Beta(5, 2) | 0.714 |
| confident heads Beta(20, 5) | Beta(24, 6) | 0.8 |
| confident tails Beta(2, 20) | Beta(6, 21) | 0.222 |
All three see the same 5 flips (4 heads, 1 tails). Spread of the three means: 0.578.
Predict first. A Beta(1000, 1000) prior (a very firm belief in a fair coin) sees 10 heads and 0 tails. Roughly what is the posterior mean?
Common mistake
Updating twice with the same data. If you add the 10 flips to the prior and then add them again, you have claimed 20 observations and your interval is too narrow. Each flip enters once. Also remember that a prior of probability 0 on some theta can never be revived by data: Beta distributions with positive parameters avoid that by giving every theta some weight.
Chapter context
Probabilistic modeling represents uncertainty explicitly instead of presenting every fitted number as certain. Priors, likelihoods and approximation choices are assumptions that should be exposed rather than hidden behind a library call.
TRY IT YOURSELF
Part 1. Do it hands-on
Set the prior, then press the flip buttons. The stream is seeded: you are watching the same flips as in the lesson.
- To do: Start from the pessimistic prior Beta(1, 5) and flip until the posterior mean passes 0.5
- To do: Start from the strong prior Beta(20, 20) and flip until the data pulls the posterior mean to 0.6 or more
- To do: Start from the flat prior Beta(1, 1) and flip until the 95% credible interval is narrower than 0.2
Part 2. Do it in code
- beta_update(a, b, flips): flips is a list of 1 (heads) and 0 (tails). Return the pair (a + heads, b + tails). Raise ValueError if a or b is not positive or a flip is not 0 or 1.
- sequential_updates(a, b, flips): update one flip at a time and return the list of (a, b) pairs: the prior first, then the state after each flip (length len(flips) + 1).
- posterior_mean_var(a, b): return (mean, variance) of a Beta(a, b): a / (a + b) and a b / ((a + b)^2 (a + b + 1)).
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
What happens to a Beta(a,b) prior after s successes and f failures?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For Bernoulli success probability theta and a Beta prior:
For Beta(alpha,beta) with both parameters greater than one, the MAP is .
For a zero-mean Gaussian-process regression model with independent noise variance sigma-n squared, a test prediction has
K is the training covariance matrix, k-star connects the test input with training inputs, and k-star-star is its prior variance. This is latent-function variance; observation noise adds another term for a noisy predicted observation. Practical code solves systems instead of explicitly computing an inverse.
Worked example
Start with Beta(2,2), then observe three successes and one failure. The posterior is Beta(5,3). Its mean is 5/8=0.625. Its MAP is 4/6≈0.6667. Both summarize the same distribution but answer different questions.
Reference code
The following example uses the Python standard library and does not call a remote service.
from math import isclose
a, b = 2+3, 2+1
assert (a,b) == (5,3)
assert a/(a+b) == .625
assert isclose((a-1)/(a+b-2), 2/3)
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
A posterior estimate is conditional on the chosen model. MAP is not the same as a posterior mean. A small uncertainty under a misspecified model need not mean a reliable real-world answer.