Prior and likelihood
Update a prior, compare posterior means with MAP estimates and interpret predictive uncertainty.
THE IDEA
Two ingredients, and a coin to test them on
You find a coin and want its chance of landing heads, call it theta. Before flipping anything you may already believe something: most coins are close to fair, but a trick coin is possible. That belief, written as a distribution over theta, is the prior. We use a Beta distribution because it lives on the interval from 0 to 1 and has two knobs: Beta(1, 1) is flat (no opinion), Beta(2, 2) leans gently toward fair, and Beta(20, 20) is a firm belief that theta is near 0.5.
Then you flip. The likelihood answers a different question: for each possible theta, how probable is the data you saw? For h heads and t tails in a particular order, it is theta^h (1 - theta)^t. Seven heads and three tails is most probable when theta = 0.7, much less probable when theta = 0.2.
The likelihood curve does not have area 1, so it is not a distribution over theta.
A likelihood is not a distribution over theta
The likelihood is a function of theta with the data held fixed. It tells you how well each theta explains the data, but nothing forces the curve to enclose an area of 1. For 7 heads and 3 tails its area is B(8, 4) = 1/1320 = 0.000758. Reading its height as "the probability that theta is 0.7" would be like reading a thermometer in the wrong units.
Predict first. You see 5 heads and 5 tails. Which of these is true of the likelihood curve theta^5 (1 - theta)^5?
Prior times likelihood, then normalise
Bayes' rule multiplies the two curves point by point: posterior(theta) is proportional to prior(theta) x likelihood(theta). The product still is not a distribution. Its area is the probability of the data averaged over the prior, called the evidence. For a Beta(2, 2) prior and 3 heads, 1 tail in one specific order the evidence is 0.05714 (that is 2/35). Divide the product by it and the area becomes 1: that is the posterior. You will watch it happen in the next lesson.
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 sliders. Tasks are checked from the exact numbers behind the plot and stay ticked once earned.
With no flips the likelihood is flat at 1 and has area 1, by coincidence.
- To do: Choose data of exactly 8 flips whose likelihood curve peaks at 0.25
- To do: Show that a likelihood is not a distribution: pick data with at least 10 flips where the area under the likelihood curve is below 0.001
- To do: With data of 8 heads and 2 tails, choose a prior that pulls the posterior peak down to 0.55 or lower
Part 2. Do it in code
- likelihood(theta, heads, tails): theta^heads * (1 - theta)^tails for a numpy array of candidate biases. Raise ValueError for negative counts.
- log_likelihood(theta, heads, tails): the same on the log scale without producing NaN. A zero count contributes 0 even when theta is 0 or 1; a positive count at theta 0 (or 1) gives -inf.
- unnormalized_posterior(theta, a, b, heads, tails): Beta(a, b) prior kernel theta^(a-1) (1-theta)^(b-1) times the likelihood. Raise ValueError unless a > 0 and b > 0.
- normalize(theta, values): divide by the area under the curve using the trapezoid rule, np.sum((v[1:] + v[:-1]) / 2 * np.diff(theta)). Raise ValueError if the area is not a positive finite number.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Is a likelihood already a probability distribution over parameters?
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.