Contents / Statistics / Bayesian Inference
Chapter 10
Bayesian Inference
Prior, likelihood and posterior, and what the two frameworks actually disagree about; conjugate priors with the Beta-binomial and normal-normal updates proved, and the posterior mean shown to be a weighted average; why a flat prior is not uninformative; credible intervals against confidence intervals; the posterior predictive distribution; Bayes factors, Occam's razor and Lindley's paradox; and MCMC, with Metropolis-Hastings proved to have the posterior as its stationary distribution.
Introduction
Every chapter so far has treated the parameter as a fixed unknown constant and the data as random. That is not a mathematical necessity; it is a choice, and it is the choice that makes a confidence interval mean what it means — a statement about the long-run behaviour of a procedure, rather than about the parameter, because a fixed constant has no probability distribution to describe.
This chapter makes the other choice. Treat the parameter as a random variable with a distribution expressing what is known about it, and the whole apparatus rearranges. Bayes' theorem combines that prior distribution with the data to produce a posterior distribution, and every question is then answered by reading it off: a point estimate is a summary of the posterior, an interval is a region containing of it, and — the thing the frequentist framework cannot deliver — the probability that the parameter lies in that interval really is .
Nothing here contradicts the earlier chapters. Both frameworks are internally consistent and answer different questions, and with a large sample and a prior that is not perverse they give nearly identical numbers, which the third section demonstrates. The genuine differences appear where the data are few, where information exists beyond the current sample, and where the question being asked is really about the parameter rather than about a procedure.
The cost is the prior, and it is a real cost. It must come from somewhere, two analysts may choose differently, and with small samples the choice shows in the answer. The second section is about making that choice defensibly and reporting its influence rather than hiding it. The last section covers the computation that made all of this practical: for most models the posterior has no closed form, and it was the arrival of simulation methods rather than any new mathematics that moved Bayesian inference from a philosophical position into a working toolkit.
Notation. is the parameter, the prior, the likelihood as in Statistical Inference: Estimation, and the posterior.
10.1From Bayes' theorem to a posterior distribution
Definition 10.1 (Prior, likelihood, posterior). Given a prior density and data with likelihood , the posterior density is
the denominator being the marginal likelihood, a constant in .
Because the denominator does not involve , it serves only to make the posterior integrate to one. That licenses the notation used throughout this chapter:
and in practice one recognises the shape of the right-hand side and supplies the constant afterwards.
Intuition. The formula is a rule for updating. The prior says what was believed before; the likelihood says how well each value of explains what was observed; the product reweights the prior in favour of the values that explain the data well.
A value of ends up with high posterior probability only if it was plausible beforehand and predicts the data. Either factor can veto: data that a parameter value cannot produce drive its posterior to zero however plausible it seemed, and a parameter value the prior excluded stays excluded however well it would have fitted. That second point is why a prior that assigns zero probability to a region is a strong and often unwise commitment — no amount of data can ever recover from it.
Remark (What the two frameworks actually disagree about). The disagreement is not about Bayes' theorem, which is a theorem, and not about whether it applies to events — both frameworks use it freely for observable events. It is about whether a parameter may be given a probability distribution.
The frequentist position is that is a fixed unknown constant, so is either or and probability statements about it are meaningless; probability describes the sampling procedure instead. The Bayesian position is that probability describes uncertainty of any kind, including uncertainty about a constant, so a distribution over is a legitimate description of what is known.
Each gives a coherent system. The practical consequence is what a interval means: a confidence interval is of the time correct across repeated samples, while a credible interval contains of the posterior probability. The second is what most readers assume the first says, which is the most common misreading in applied statistics and is the subject of a pitfall in the testing chapter.
Example 10.2 (Updating on a single observation). A disease affects of a population. A test has sensitivity and specificity . A randomly chosen person tests positive. Compute the posterior probability of disease.
Solution.
- Prior: , so .
- Likelihoods: and .
- Marginal probability of a positive test:
- Posterior: .
- So a positive result raises the probability from to about — a seventeen-fold update, and still well short of a diagnosis.
- Sanity check on why: the disease is rare, so the of healthy people generate false positives, five times the true positives. This is the same base-rate arithmetic as the post-test probability calculation in Statistical Inference: Hypothesis Testing, with playing the role of specificity and power the role of sensitivity.
Example 10.3 (Two tests in sequence). The same person from the previous example takes a second, independent test of the same accuracy, and it is also positive. Compute the updated probability.
Solution.
- Yesterday's posterior becomes today's prior: , so .
- Marginal probability of the second positive:
- Posterior: .
- Two positives take the probability from to about — the update compounds, and it compounds multiplicatively on the odds scale.
- Sanity check via odds, which is faster: the prior odds are , and each positive test multiplies them by the likelihood ratio . After two tests the odds are , giving a probability of ✓.
- The likelihood-ratio form shows why sequential updating is coherent: the final answer does not depend on the order the tests were processed, or on whether they were processed one at a time or together.
10.2Choosing a prior
The prior is the part that has no counterpart in earlier chapters, and it is where most of the objection to the approach is directed. Three considerations make the choice tractable: mathematical convenience, honest ignorance, and reporting the influence.
Definition 10.4 (Conjugate prior). A family of priors is conjugate to a likelihood if the posterior belongs to the same family. The updating is then a rule for changing the family's parameters.
Theorem 10.5 (Beta is conjugate to the binomial). If and, given , is observed with successes, then
Proof. The Beta density is proportional to , and the binomial likelihood is proportional to as a function of . Multiplying,
which is the kernel of a Beta density. Since a density is determined by its kernel, that is the posterior.∎
The updating rule is as simple as it could be: add the successes to and the failures to . That gives the prior parameters an interpretation — and act like previously-seen successes and previously-seen failures, so measures how much the prior is worth in units of data. A Beta prior carries the weight of four observations; a Beta prior carries a hundred and will not be moved by a small experiment.
Theorem 10.6 (The posterior mean is a weighted average). Under the hypotheses above, with prior mean and sample proportion ,
Proof. The mean of a Beta is , so the posterior mean is . Split the numerator, writing and :
and the two coefficients are and , which sum to .∎
That theorem is the clearest statement of what the prior does and when it stops mattering. The posterior mean is a compromise between the prior mean and the data's own estimate, with the weight determined by how much data there is relative to the prior's strength. As , and the prior washes out entirely; for small relative to the prior dominates. Nothing about this is hidden — the weight is computable before any data arrive.
Definition 10.7 (Uninformative and Jeffreys priors). A prior is uninformative if it is intended to let the data speak. The flat prior on is Beta. The Jeffreys prior is with the Fisher information of the estimation chapter; for a binomial it is Beta.
Remark (Why "flat" is not the same as "uninformative"). A uniform prior looks neutral and is not, because flatness is not preserved under reparametrisation. A prior flat in is not flat in or in the odds — the Jacobian of the transformation reshapes it — so "know nothing" expressed on one scale becomes a definite opinion on another.
The Jeffreys prior is constructed to be invariant under reparametrisation precisely to avoid that, which is its main justification. It also frequently improper, meaning it does not integrate to a finite value; that is tolerable when the resulting posterior is proper, but it must be checked rather than assumed.
The honest practice is not to hunt for a prior with no influence but to state the prior, report how much it moved the answer, and show the result under a reasonable alternative. That is a sensitivity analysis, and it converts the prior from a hidden assumption into a reported one.
Example 10.8 (The same data under three priors). Seven successes in trials. Compute the posterior mean under a Beta flat prior, a Beta weakly informative prior, and a Beta strong prior centred at .
Solution.
- Beta: posterior is Beta, mean . Prior weight , so .
- Beta: posterior is Beta, mean . Here .
- Beta: posterior is Beta, mean . Now — the data barely register.
- Check the second against the weighted-average form: ✓.
- Sanity check on the ordering: all three lie between the prior mean and the MLE , closer to when the prior is weak and closer to when it is strong — exactly as the weight dictates. ✓
- The lesson for reporting: with the answer ranges from to depending on the prior, so the prior must be stated. With successes in proportion, all three would agree to two decimals.
Theorem 10.11 (Normal-normal conjugacy). Let and let be the mean of observations from with known. Then the posterior is normal with precision (reciprocal variance) equal to the sum of the prior and data precisions,
so the posterior mean is the precision-weighted average of the prior mean and the sample mean.
Proof. Drop all factors free of . The likelihood contributes — the other part of the sum of squares does not involve — and the prior contributes . So
Expand the bracket and collect powers of :
A quadratic inside is the kernel of a normal with variance and mean — complete the square to see it. Reading off and gives the stated precision and mean.∎
Intuition. Precision is the natural currency here, and the rule is as simple as addition: precisions add. The prior contributes and observations contribute , and the posterior has their sum, so the posterior is always more precise than either input — information accumulates and never decreases.
The mean is then the average of the two estimates weighted by how precise each is, which is what anyone would do by hand when combining two measurements of different quality. A vague prior has large, contributes almost no precision, and barely shifts the answer; a sharp prior contributes a lot and pulls hard.
10.3Point estimates and credible intervals
The posterior is the complete answer. Everything else in this section is a summary of it, chosen to fit what a reader can absorb.
Definition 10.12 (Posterior summaries and credible interval). The posterior mean, median and mode (the last also called the maximum a posteriori or MAP estimate) summarise the posterior's location. A credible interval is any region with
commonly taken as the equal-tailed interval between the and posterior quantiles, or as the highest posterior density region, which is the shortest such interval.
Pitfall. This is where the two frameworks differ most visibly, and the difference is worth stating in both directions.
A credible interval supports the statement most people want: given the data and the prior, there is a probability that lies in it. A confidence interval does not; as the estimation chapter set out, once computed it either contains or does not, and the describes the procedure across repeated samples.
The numbers are often nearly identical — with a flat prior and a normal model they can coincide exactly — which is why the misinterpretation so rarely causes visible damage. It is still a misinterpretation, and the honest options are to compute a credible interval and make the probability statement, or compute a confidence interval and make the procedural one.
Proposition 10.13 (The posterior mode is the MLE under a flat prior). If the prior is constant over the parameter space, the posterior is proportional to the likelihood, so the MAP estimate equals the maximum likelihood estimate.
Proof. With , the posterior is . Two functions proportional by a positive constant are maximised at the same point.∎
That proposition explains why the two frameworks so often agree numerically. Maximum likelihood, the workhorse of the estimation chapter, is Bayesian estimation with a flat prior and the mode as the summary — so the disagreement is about interpretation far more often than about arithmetic.
Remark (Large samples: the prior stops mattering). The Bernstein-von Mises theorem, which we quote, says that under regularity conditions the posterior converges to a normal distribution centred at the maximum likelihood estimate with variance equal to the inverse Fisher information — whatever the prior, provided it assigns positive density near the true value.
So for large the credible interval and the confidence interval converge to the same numbers, and the frequentist properties of a Bayesian procedure become good automatically. The practical reading: with abundant data the choice of framework is a matter of interpretation and convenience; with scarce data it changes the answer, and the prior must be defended.
The proviso matters. A prior assigning zero density to the true value blocks the convergence entirely, which is the formal version of the earlier warning against excluding regions.
Example 10.14 (A credible interval, and the confidence interval beside it). With the Beta prior and successes in , the posterior is Beta with quantiles and . Compare with the Wilson confidence interval from the estimation chapter.
Solution.
- Credible interval: , and the statement licensed is direct — given the prior and data, .
- Posterior mean ; posterior mode , using the Beta mode formula .
- The three summaries order themselves as mean < mode < MLE . That ordering is the general pattern: the mode tracks the likelihood most closely of the posterior summaries, while the mean is pulled furthest toward the prior, because it averages over the whole posterior including its left tail.
- Wilson interval for , , : centre and half-width , giving .
- Sanity check on the agreement: the two intervals nearly coincide at the lower end and differ by at the upper. With and a weak prior they are describing the same uncertainty by different routes.
- What differs is what may be said. The credible interval carries a probability statement about ; the Wilson interval carries a statement about the procedure's coverage across repeated samples.
Example 10.15 (Combining a prior with data on the normal scale). A quantity has prior . Sixteen measurements with known give . Find the posterior and a credible interval.
Solution.
- Precisions. Prior: . Data: .
- Posterior precision: , so the posterior variance is and the standard deviation is .
- Posterior mean:
- Credible interval: .
- Sanity check on the compromise: the posterior mean lies between the prior mean and the sample mean , and much closer to the sample mean because the data carry four times the precision ( against ) — the weights are and , and ✓.
- Note the posterior standard deviation is smaller than the data's own standard error and smaller than the prior's . Combining information sharpens, as precision addition requires.
Predicting the next observation
A posterior describes the parameter. Often the question is instead about the next data point, and the two are not the same — predicting an observation must account for both uncertainty about and the noise in the observation itself, exactly as the prediction interval of the regression chapter did.
Definition 10.16 (Posterior predictive distribution). The posterior predictive distribution of a future observation is
averaging the sampling distribution over the posterior rather than fixing at an estimate.
Theorem 10.17 (Predicting the next Bernoulli trial). Under the Beta-binomial model, the probability that the next trial is a success equals the posterior mean:
Proof. Apply the definition with :
which is by definition the posterior mean of . The value follows from the theorem The posterior mean is a weighted average.∎
Pitfall. Plugging a point estimate into the sampling distribution — using instead of the integral — understates the uncertainty, because it treats as known when it is not. The error is the same one as confusing a confidence interval for a mean with a prediction interval for an observation, and it is largest exactly when the posterior is wide, which is when the data are few.
For the Bernoulli the two happen to agree on the probability of a success, as the theorem shows, but they diverge immediately for anything involving more than one future trial: the posterior predictive for the number of successes in the next trials is beta-binomial and over-dispersed relative to the binomial that a plug-in estimate would give.
Example 10.18 (Plug-in against posterior predictive). With the Beta posterior from successes in , find the probability that the next trial succeeds, and the variance of the number of successes in the next trials under both the plug-in and the predictive approach.
Solution.
- Next trial. By the theorem, .
- Plug-in for . Fixing gives Binomial with variance .
- Posterior predictive for . The count is beta-binomial, whose variance is
- The predictive variance is larger, because it carries the uncertainty about in addition to the trial-to-trial noise.
- Sanity check on the limit: as the posterior sharpens — larger — the inflation factor tends to and the two agree. With instead of the factor would be .
- This is the same structure as the regression chapter's prediction interval, whose extra term likewise never vanished: knowing perfectly would still leave the coin's own randomness.
10.4Bayesian model comparison
Hypothesis testing has a Bayesian counterpart, and the shape of it is different enough to be worth contrasting carefully with the testing chapter.
Definition 10.19 (Bayes factor). For two models and , the Bayes factor is the ratio of marginal likelihoods
and it converts prior odds into posterior odds:
Proof. Apply Bayes' theorem to each model and divide, the common marginal cancelling:
The Bayes factor is the factor by which the data multiply the odds, and it has three properties that a -value does not.
It can support the null: is ten-to-one evidence for , whereas a large -value is merely a failure to reject. It is symmetric between the two models, neither being privileged. And it automatically penalises complexity, because the marginal likelihood integrates over the parameter space — a model with more parameters spreads its prior predictive mass more thinly, so it must fit substantially better to compensate. That is an Occam's razor that falls out of the arithmetic rather than being added as a penalty term the way AIC adds one.
Remark (Lindley's paradox). For a fixed -value, the Bayes factor can favour the null more and more strongly as grows. A result significant at exactly with typically has a Bayes factor supporting .
The two are answering different questions rather than contradicting each other. The -value asks how surprising the data are if holds, and with a huge sample a trivial departure is surprising. The Bayes factor asks which model predicted the data better, and a tiny effect is predicted well by the null and poorly by an alternative that spread its prior over a wide range of possible effect sizes.
The practical reading: with very large samples, statistical significance and evidential support come apart, and quoting the first without an effect size is uninformative — which the testing chapter said from the frequentist side too.
Example 10.20 (A Bayes factor by hand). A coin is tested for fairness: says exactly, and places a uniform prior on . In tosses, heads are observed. Compute .
Solution.
- Under : the probability of the observed count is the binomial mass at ,
- Under : integrate over the uniform prior, using the Beta integral :
- Bayes factor: .
- So the data mildly favour the null — the fair-coin model — by about to , despite heads out of looking suggestive.
- Sanity check against the frequentist test: the two-sided binomial -value for or more extreme is , also far from significant. Both approaches say the same thing here, but only the Bayes factor states it as positive (if weak) support for fairness.
- Note where 's penalty came from: it spread its prior over all of , so it had to hedge across values like and that the data flatly contradict. A narrower alternative concentrated near would have produced a Bayes factor favouring .
Example 10.21 (Interpreting a Bayes factor against prior odds). Two models have prior odds of against (the analyst thinks it is four times less likely at the outset). The data give . Compute the posterior odds and posterior probability of .
Solution.
- Prior odds: .
- Posterior odds .
- Converting odds to a probability: .
- So the data have moved from a prior probability of to a posterior probability of .
- Sanity check on the arithmetic of the prior: odds of correspond to ✓, and multiplying odds by takes to , which is a probability of ✓.
- The point of separating the two factors: the Bayes factor is what the data contribute and is the same for every reader; the prior odds differ between readers, and each can combine them with their own. Reporting the Bayes factor alone is therefore the more useful thing to publish.
10.5Computation
Conjugate models give closed-form posteriors, and almost nothing else does. For a model with several parameters the normalising constant is a high-dimensional integral with no analytic solution, and numerical quadrature fails past a few dimensions.
The resolution is to stop trying to compute the posterior and instead to sample from it. A large sample from the posterior answers every question one could ask of it: the mean is estimated by the sample mean, a credible interval by sample quantiles, and any function of by applying it to each draw.
Definition 10.22 (Markov chain Monte Carlo). MCMC constructs a Markov chain whose stationary distribution is the posterior. Running it long enough produces draws distributed according to the posterior, from which any summary can be estimated.
Theorem 10.23 (Metropolis-Hastings has the posterior as stationary distribution). Given a current value , propose from a symmetric proposal density and accept with probability
otherwise stay at . The resulting chain has as its stationary distribution.
Proof. We verify detailed balance, which by the reversibility discussion of Markov Chains is sufficient for stationarity. For the transition density is with symmetric. Suppose without loss of generality that , so and . Then
while the reverse flow is , equal to the above by symmetry of . Detailed balance holds, so is stationary.∎
The decisive feature is visible in the acceptance ratio: the posterior appears only as a ratio, so the intractable normalising constant cancels. The algorithm needs only the unnormalised product likelihood prior, which is always computable. That single observation is what made Bayesian inference practical for models of any complexity.
Remark (Gibbs sampling, and checking the chain). Gibbs sampling is the special case in which each parameter is updated in turn by drawing from its full conditional distribution given the others. When those conditionals are standard distributions — which conjugacy within each block often arranges — every proposal is accepted and no tuning is needed.
Because the draws form a Markov chain, they are not independent, and two practical consequences follow. The chain must be run past its burn-in, the initial stretch still influenced by the starting value, which is discarded. And the effective number of independent draws is smaller than the number of iterations, by a factor depending on the autocorrelation — so a chain of highly correlated draws may carry the information of only a few hundred.
Convergence is assessed by running several chains from dispersed starting points and checking that they mix into the same region, formalised by the Gelman-Rubin statistic , which compares between-chain and within-chain variance and should be near .
Example 10.24 (Monte Carlo error in a posterior summary). An MCMC run produces draws from a posterior with standard deviation , but the autocorrelation makes the effective sample size only . Compute the Monte Carlo standard error of the posterior mean estimate, and compare with the naive figure.
Solution.
- Correct. The standard error uses the effective sample size: .
- Naive, using all draws as if independent: .
- The naive figure understates the Monte Carlo error by a factor of .
- Sanity check on whether this matters: the posterior standard deviation is , so even the correct Monte Carlo error of is only of it — the simulation is adding little uncertainty on top of what the data leave. That is the right comparison to make, and it is the one that decides whether to run the chain longer.
- Note the two error sources are different in kind. The posterior standard deviation reflects genuine uncertainty about and does not shrink with more iterations; the Monte Carlo error reflects imperfect simulation and does.
- Reading a confidence interval as a credible interval. Only the second licenses "there is a probability that is in here"; the first is a statement about the procedure.
- Believing a flat prior is uninformative. Flatness is not preserved under reparametrisation, so a prior flat in is opinionated about .
- Assigning zero prior probability to a region. No amount of data can revive it, and the Bernstein–von Mises convergence fails.
- Not reporting the prior, or its influence. With small the prior moves the answer materially; report a sensitivity analysis rather than a single number.
- Treating a large -value as evidence for the null. A Bayes factor can support the null; a -value can only fail to reject.
- Expecting significance and evidence to agree at large . Lindley's paradox: a result at exactly with a huge sample often favours the null on a Bayes factor.
- Using all MCMC draws as if independent. The effective sample size is smaller, often by an order of magnitude, and the Monte Carlo error scales accordingly.
- Failing to discard burn-in or to check convergence. Run several chains from dispersed starts and check is near .
- Assuming a Bayesian answer differs from a frequentist one. With a flat prior the MAP estimate is the MLE, and for large the intervals converge. The differences are largest exactly where the data are weakest.