Bayesian Inference
Priors, posteriors and predictions that average over uncertainty.
Maximum likelihood picks one parameter value and forgets the rest. Bayesian inference keeps all of them, weighted by how well they explain the data and how plausible they were to begin with. This lesson works the coin end to end: prior, posterior and evidence, predictions that average over what you do not know, credible intervals, and why the prior eventually stops mattering.
What you’ll learn
- Apply Bayes' rule to parameters, name the prior, likelihood, posterior and evidence, and update sequentially.
- Derive the Beta–Bernoulli posterior, write its mean as a shrinkage estimate, and compute the evidence.
- Compute posterior predictive probabilities, and explain why they differ from plug-in predictions.
- Distinguish credible intervals from confidence intervals, and state the Bernstein–von Mises theorem with its conditions.
- Choose priors (informative, weakly informative, Jeffreys) and recognize them as regularizers.
Bayes' rule for parameters
Treat the unknown parameter as a random variable. Before the data it has a prior density ; the model supplies the likelihood ; Bayes' rule combines them into the posterior:
The denominator is the evidence, or marginal likelihood: the probability of the data averaged over the prior. It does not depend on , so for most purposes posterior ∝ likelihood × prior, and the evidence returns only when you compare models (Bayesian Model Selection).
If two batches of data are conditionally independent given , the likelihood factorizes:
Group the last two factors: they are proportional to the posterior after the first batch. So yesterday's posterior is today's prior, and the order of the batches does not matter.
For two hypotheses the normalizer cancels in a ratio, giving the odds form: each independent piece of evidence multiplies the odds by its likelihood ratio.
The Beta–Bernoulli model, worked fully
Take the prior , a Beta distribution, and observe heads and tails in independent flips of a particular sequence:
Multiply and collect powers. The result is again a Beta kernel, so the posterior is a Beta distribution: the prior is a conjugate prior for the Bernoulli likelihood.
The normalizing constants give the evidence for free:
The posterior mean is a weighted average of the MLE and the prior mean, with weight on the data. That is shrinkage: the estimate is pulled toward the prior, less and less as grows.
The posterior variance shrinks like ; for large it approaches , the squared standard error of the MLE.
Prior ; you observe 7 heads and 3 tails. What is the posterior mean of ?
Show the answer
0.643
The posterior is , with mean : the MLE with weight and the prior mean with weight . Its mode, the MAP estimate, is , and its standard deviation is about : ten flips leave a lot of uncertainty.
Predicting: average over what you do not know
To predict a new observation , the Bayesian answer averages the model's predictions over the posterior, the posterior predictive distribution:
For the coin, . With the flat prior this is Laplace's rule of succession, : after 3 heads in 3 flips it says 0.8, where the plug-in MLE says 1.
Think first After 3 heads in 3 flips with a flat prior, what is the probability that the next two flips are both heads? Is it ?
No. The posterior is and the answer is . Since , uncertainty about makes runs more likely than squaring the one-step prediction suggests: future flips are correlated through the shared unknown . The plug-in MLE would say .
| If the loss is… | …the best point estimate is | For |
|---|---|---|
| squared error | the posterior mean | |
| absolute error | the posterior median | about |
| 0–1 loss, in the limit of a vanishing tolerance | the posterior mode (MAP) |
A rare-event model is fitted to 50 trials with no events. Which prediction for the next trial is more defensible, and why?
- The plug-in MLE, probability 0, because the data show the event never happens
- The posterior predictive under a flat prior, , because it averages over the event rates still compatible with the data
- Probability 0.5, because with no events we know nothing
- Neither: no prediction is possible without at least one event
Show the answer
B. The posterior predictive under a flat prior, , because it averages over the event rates still compatible with the data
The posterior is and the predictive probability is its mean, . Fifty clean trials make a high rate implausible, but they cannot rule out a small one. The plug-in estimate of zero would be refuted by the first event.
Credible intervals, confidence intervals and the vanishing prior
A 95% credible interval satisfies : a probability statement about the parameter, given these data and the prior. The equal-tailed interval cuts 2.5% from each side of the posterior; the highest-posterior-density interval is the shortest one. A 95% confidence interval is a procedure that, over repeated samples, covers the fixed true parameter 95% of the time; for the data in hand it either covers it or not.
They can disagree sharply on small samples. With 0 heads in 10 flips, the textbook Wald interval collapses to the single point . The flat-prior posterior gives the equal-tailed interval and the highest-density interval , which honestly reflects that rates up to about a quarter are still plausible.
Choosing a prior
A prior is a modeling choice like the likelihood, and it plays the role a penalty plays elsewhere: a Gaussian prior on regression weights is ridge, a Beta prior adds pseudo-counts (Maximum Likelihood & MAP). Three broad kinds are used:
- Informative priors encode real knowledge, such as earlier studies or physical constraints.
- Weakly informative priors rule out absurd values without favoring any reasonable one. Simulating fake datasets from the prior, a prior predictive check, shows whether "vague" choices imply absurd data: a logistic-regression prior wide enough to allow odds ratios of a billion per standard deviation of a feature is not neutral.
- Reference or "noninformative" priors aim to let the data speak. Flatness is not a neutral choice, though, because it depends on the parameterization: a flat prior on is a non-flat (logistic) density on the log-odds.
Jeffreys (1946) proposed a prior proportional to the square root of the Fisher information determinant:
Under a one-dimensional reparameterization , the information transforms as , so : exactly the change-of-variables rule for densities. The same rule applied in any parameterization describes the same prior beliefs.
For the coin , so Jeffreys' prior is : half an imaginary head and half an imaginary tail, with more weight near 0 and 1 than the flat prior. It is the "Jeffreys" preset of the widgets.
Improper priors, such as a flat density on all of for a location parameter, are allowed when the posterior they produce is proper, but they cannot be used to compare models through the evidence, which they define only up to an arbitrary constant (Bayesian Model Selection). And when nobody knows what prior to use for many related parameters, such as one success rate per hospital, a hierarchical model gives them a common prior whose own parameters are learned from all the groups at once.
Your prior for a coin's is uniform on and zero above. The true is , and you observe 10,000 flips. What does the posterior look like?
- Concentrated near 0.7, because 10,000 flips overwhelm any prior
- Still uniform on , because the data cannot move a uniform prior
- Spread evenly between 0.5 and 0.7
- Piled up against 0.5: it can never put mass where the prior is zero
Show the answer
D. Piled up against 0.5: it can never put mass where the prior is zero
The posterior is the prior times the likelihood, and zero times anything is zero. On the likelihood keeps increasing toward 0.5, so the posterior concentrates at the boundary, as close to the truth as the prior allows. Never give zero prior probability to anything you cannot rule out (sometimes called Cromwell's rule).
Key takeaways
- Posterior ∝ likelihood × prior; the evidence normalizes. With conditionally independent data, yesterday's posterior is today's prior, and each piece of evidence multiplies the odds by its likelihood ratio.
- prior plus heads and tails gives , with evidence and posterior mean , a shrinkage of the MLE toward the prior mean.
- Predictions average over the posterior. They are less extreme than plug-in predictions and capture correlations between future observations; the optimal point estimate depends on the loss (mean, median or mode).
- Credible intervals are probability statements about the parameter; confidence intervals are guarantees about a procedure. Under the Bernstein–von Mises conditions the posterior approaches , the prior washes out, and the two nearly coincide.
- Priors are modeling choices and regularizers. Flat is not neutral across parameterizations; Jeffreys' is invariant ( for a coin); and zero prior mass can never be overturned by data.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
With a flat prior you observe 6 heads and 2 tails. What are the posterior and its mean?
- , mean
- , mean
- , mean
- , mean
Show the answer
B. , mean
Add the counts to the prior's parameters: , with mean . Its mode is , the MLE, as always with a flat prior.
With a flat prior, a coin shows 2 heads in 2 flips. What is the posterior predictive probability that the next flip is heads?
Show the answer
D.
The posterior is , and the predictive probability is its mean, : Laplace's .
A 95% credible interval for is . Which statement is a correct reading?
- Given the data and the prior, lies in with probability 0.95
- If the experiment were repeated many times, 95% of the estimates would fall in
- The true is certainly in
- 95% of the flips will have outcomes between 0.62 and 0.78
Show the answer
A. Given the data and the prior, lies in with probability 0.95
A credible interval is a statement about the posterior distribution of . A confidence interval makes a statement about a procedure across repeated samples instead; with a flat prior and enough data the two kinds of interval nearly coincide numerically (Bernstein–von Mises), but they still answer different questions.
Two analysts use the priors and and observe the same 10,000 flips, 5,000 of them heads. According to the Bernstein–von Mises theorem, what are their posteriors approximately?
- Their own priors, slightly narrowed
- for both
- Both approximately
- Different, because the second prior is ten times stronger
Show the answer
C. Both approximately
The posterior approaches with and standard deviation . The priors are worth 4 and 40 imaginary flips, against 10,000 real ones.
Go deeper
- Conjugate Priors & Exact Posteriors · Beta–binomial, normal–normal and Dirichlet–multinomial.
- Bayesian Linear Regression · Posterior predictive variance and the evidence.
- Bayesian Model Selection · Marginal likelihood, Bayes factors, BIC and Occam’s razor.
Further reading
- A. Gelman, J. B. Carlin, H. S. Stern, D. B. Dunson, A. Vehtari & D. B. Rubin, Bayesian Data Analysis, 3rd ed. (CRC Press, 2013), chapters 1–3.
- C. M. Bishop, Pattern Recognition and Machine Learning (Springer, 2006), section 2.1.
- D. J. C. MacKay, Information Theory, Inference, and Learning Algorithms (Cambridge University Press, 2003), chapters 2–3.