Machine learningLoading progress…

Posterior prediction

Update a prior, compare posterior means with MAP estimates and interpret predictive uncertainty.

Predict by averaging over theta

What is the chance the next flip is heads? Given theta it would be theta, but you do not know theta; you have a posterior. The honest answer averages theta over that posterior, which for a Beta(a, b) is simply its mean: P(heads next) = a / (a + b). With a flat prior and 3 heads, 0 tails the posterior is Beta(4, 1), so the answer is 0.8.

The shortcut of picking one estimate and plugging it in (here the frequency 3/3 = 1) treats theta as known exactly. It says tails are impossible, so it is certain about the next flip when three flips cannot justify that.

012345678910heads among the next 10 flips
posterior predictive (averages over theta)plug in the point estimate
heads seen3tails seen0next flips m10
prior Beta(1, 1) -> posterior Beta(4, 1) next flip is heads: predictive = 4/5 = 0.8 plug-in = 3/3 = 1 all 10 next flips heads: predictive = 0.286 plug-in = 1 spread of the head count (sd): predictive = 2 plug-in = 0

The point estimate says tails are impossible. The posterior still allows them.

Blue: predictive distribution of heads in the next m flips, averaged over theta. Orange: the same question with the point estimate plugged in. Try 3 heads, 0 tails, then 3 and 3, then 40 and 40.

Where the extra spread comes from

A plugged-in binomial only has coin-flip noise. The predictive adds uncertainty about theta itself, so the head count is more spread out. With 3 heads and 3 tails (posterior Beta(4, 4)) and 10 future flips, the predictive standard deviation of the head count is 2.236against 1.581 for the plug-in. With lots of data the two nearly coincide: the parameter becomes known and only flip noise is left.

Predict first. Flat prior, 3 heads and 0 tails. How likely is a run of 10 more heads?

Common mistake

Reporting the plug-in probability as if it were the predictive one. Look at the case of 0 heads in 4 flips: the point estimate says the next flip is heads with probability 0, while the flat-prior predictive says 1/6. Any decision that divides by that probability, or bets everything on it, fails on the first heads.

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.

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:

p(θ∣D)=Beta⁡(a+s,b+f),E[θ∣D]=a+sa+b+s+f.p(\theta\mid D)=\operatorname{Beta}(a+s,b+f),\quad E[\theta\mid D]=\frac{a+s}{a+b+s+f}.

For Beta(alpha,beta) with both parameters greater than one, the MAP is (α−1)/(α+β−2)(\alpha-1)/(\alpha+\beta-2).

For a zero-mean Gaussian-process regression model with independent noise variance sigma-n squared, a test prediction has

μ∗=k∗T(K+σn2I)−1y,\mu_*=k_*^T(K+\sigma_n^2I)^{-1}y, v∗=k∗∗−k∗T(K+σn2I)−1k∗.v_*=k_{**}-k_*^T(K+\sigma_n^2I)^{-1}k_*.

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.