Machine learningLoading progress…

Posterior inference

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

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.

00.250.50.751coin bias theta
priorlikelihood (rescaled)posterior
prior a2prior b2
flips so far: 0 none yet heads = 0, tails = 0 Beta(2, 2) -> Beta(2 + 0, 2 + 0) = Beta(2, 2) mean = 2/4 = 0.5 95% interval (shaded) = [0.094, 0.906], width 0.811
Flip the seeded coin one flip at a time (same flips every visit). The bottom-right text shows the exact update. The shaded strip is the 95% credible interval.

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.

00.250.50.751coin bias theta
flat Beta(1, 1)confident heads Beta(20, 5)confident tails Beta(2, 20)
shared flips5
priorposteriormean
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.

Three priors, one shared stream of flips. With few flips the means differ by more than 0.5; after 300 flips they are within 0.05.

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.

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.