Bayesian inference
The parameter as a random variable — prior times likelihood, the Beta–Binomial update with pseudo-counts, posterior mean versus MAP (and ridge regression as MAP), credible intervals, the posterior predictive, and why the prior fades as data accumulate.
Three heads and a single point
The topic Maximum likelihood gave a general recipe: choose the parameter under which the observed data are most probable. For a coin with unknown probability of heads and heads in tosses, the recipe gives , so 7 heads in 10 tosses give .
Now borrow a coin, toss it three times and get three heads. Maximum likelihood answers : tails is impossible. Nobody believes that. Before the first toss we already knew something about coins: most land heads about half the time, and two-headed ones are rare. The likelihood has no slot for this knowledge.
There is a second gap. The answer is a single number. It looks just as sure after 3 tosses as after 3000, and the asymptotic normality from the previous topic only speaks about large samples.
Bayesian inference closes both gaps with one move: the unknown parameter is treated as a random variable. Its distribution before the data, the prior, holds what we knew. The data turn it into the posterior, and every answer is read off the posterior: a point estimate, an interval, a forecast for the next toss. This is the degree-of-belief reading of probability from the topic What probability is. The rules of probability stay exactly the same.
From prior to posterior
The topic Conditional probability and Bayes' formula turned a base rate of for a disease into a probability of about after a positive test. The base rate was a prior, and the test result updated it. Here the hypothesis is a value of , and the evidence is the whole sample . Bayes' formula for densities reads
Each piece has a name.
- is the prior: which values of were plausible before the data.
- is the likelihood, the same function that maximum likelihood maximized.
- is the posterior.
- is the evidence, or marginal likelihood: the probability of the data averaged over the prior.
The evidence does not depend on . So the shape of the posterior comes from the numerator alone:
In words: the posterior is likelihood times prior, rescaled to integrate to one.
A toy example shows the mechanics. Suppose the coin can only have , and the prior gives these values probabilities , and . One toss lands heads, so .
| prior | prior × likelihood | posterior | ||
|---|---|---|---|---|
The fourth column sums to , and dividing by it gives the posterior. One head shifted the mass toward larger but ruled nothing out.
Beta in, Beta out
A real coin may have any , so the prior needs a density on . The topic Continuous distributions introduced the natural family, the Beta distribution:
The constant only makes the density integrate to one. For whole and it equals . is the uniform prior, and is a low hump centred at : "probably near one half, but not sure".
Multiply the prior by the likelihood of heads in tosses, , and keep only the factors that depend on :
The posterior is a Beta again. A prior family with this property for a given likelihood is called conjugate. The update needs no integral: every head adds one to , every tail adds one to . The uniform prior turns into . With the prior, three heads in three tosses give . Seventy heads in a hundred tosses give .
Data often arrive in batches. If the tosses are independent given , the posterior after one batch serves as the prior for the next, and the end result is the same as updating on all the data at once.
Why conjugate pairs exist, and for which likelihoods, is the subject of the topic Conjugate priors and the exponential family in the course Bayesian Methods in Machine Learning.
Pseudo-counts
The mean of is . So the posterior mean of is
The right-hand side is a weighted average of the prior mean and the MLE . The prior weighs as much as tosses, of them heads, so and act as pseudo-counts. The real data weigh .
With the prior, three heads in three tosses give the posterior mean instead of . Four imagined tosses still outweigh three real ones. After 70 heads in 100 the posterior mean is , close to the MLE . The pattern is general: the prior matters when data are few and fades as grows.
There is a second way to count, and it is worth knowing so as not to be confused. The density has exponents and . Compared with the uniform , whose exponents are zero, the prior multiplies in the likelihood of one extra head and one extra tail. Both readings are correct; they count from different baselines. In the posterior mean the prior's weight is .
Posterior mean or MAP
Sometimes one number is needed after all, and the posterior offers several. The posterior mean minimizes the expected squared error. The posterior mode is the maximum a posteriori (MAP) estimate. Taking the logarithm of likelihood times prior gives
the log-likelihood plus the log-prior. The evidence drops out because it does not depend on . With a flat prior, is constant, and MAP coincides with the MLE.
For with the mode is . After 70 heads in 100 with the prior, the posterior has mode and mean . With many data the estimates nearly coincide. With few data the posterior is skewed, and its mean and mode drift apart.
The same formula explains a standard trick of machine learning. Take a linear model with inputs , targets and Gaussian noise of variance , and put the prior on the weights. The negative log-posterior is
Minimizing it is ridge regression, the least-squares loss plus a penalty . Weight decay in neural networks is the same Gaussian prior. A Laplace prior gives the L1 penalty of the lasso. Regularization is a prior in disguise. A MAP estimate is still a single point, though: it throws away the width of the posterior.
Credible intervals
A point estimate hides how sure we are; the posterior shows it. A credible interval is a range that contains with a stated posterior probability. The usual choice is central: cut off posterior mass in each tail. For , after 70 heads in 100 tosses, the central 95% credible interval is
The endpoints are quantiles of the posterior, and any statistics library computes them, for example scipy.stats.beta(72, 32).ppf([0.025, 0.975]). After only three heads in three, the posterior gives the much wider interval : three tosses say little.
The reading is direct. Given this prior and these data, lies in the interval with probability . Since is a random variable here, the sentence makes sense as stated.
The same posterior answers any other question about . Does the coin favor heads? Compute , the posterior mass to the right of one half. For it is about .
Predicting the next toss
Often the question is not about at all but about the next observation. The posterior predictive distribution averages the model's prediction over all values of , each weighted by its posterior density:
For one toss , so the probability that the next toss is heads is , the posterior mean. After three heads in three with the prior it is . The plug-in forecast, which substitutes the MLE for , says and calls tails impossible. Averaging keeps the uncertainty about inside the forecast. A forecast for several future tosses works the same way: the integral then contains the probability of the whole future sequence given .
You may have used this average without the name. A spam filter that estimates the share of spam emails containing a word as uses the posterior mean under a uniform prior. This is add-one, or Laplace, smoothing: a word never seen in spam does not get probability zero.
Conjugate pairs are the exception, not the rule. For logistic regression or a neural network, the integrals in and in the predictive have no formula. Then the posterior is sampled, which the last topic of this course, Markov chain Monte Carlo, shows how to do, or it is approximated by variational inference or the Laplace approximation. The course Bayesian Methods in Machine Learning covers all of these.
When the prior fades
The prior is the most criticized part of the method: two analysts with different priors get different posteriors from the same data. With few data this effect is real, so the prior has to be stated and checked. A useful check is to vary it and see how far the conclusions move. A weakly informative prior encodes only scale and plausibility, such as "a coin is not two-headed".
With many data the prior fades. Its pseudo-counts stay fixed while grows. Under regularity conditions the Bernstein–von Mises theorem makes this precise: the posterior approaches the normal distribution
the same one as in the asymptotic normality of the MLE from the topic Maximum likelihood. For the coin . After 70 heads in 100 the standard deviation is , and the interval is close to the credible interval .
The numbers nearly agree, but the readings do not. The credible interval is a probability statement about , given these data and a prior. The interval around the MLE can be read with no prior at all, as a statement about a procedure: over repeated samples, intervals built this way cover the true in 95% of cases. The next topic, Confidence intervals and the bootstrap, builds such intervals, explains this reading carefully, and shows how the bootstrap builds them from the data alone when no formula is at hand.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.