Skip to content
BIASED

Bayesian model selection

How to choose between models without holding out data: the evidence as a prediction of the data before seeing them, the automatic Occam's razor, Bayes factors and posterior model probabilities, the dependence on the prior width, empirical Bayes and full Bayesian inference.

Parameters and models: two levels of inference

The topic Conjugate priors and the exponential family gave us posteriors in closed form, but always inside one model chosen in advance. In practice several models fit the same data: a fair coin or a coin with an unknown bias, a polynomial of degree 1, 3 or 9, a narrow or a wide prior. This topic chooses among them with the tool we already have, Bayes' formula.

There are two levels. A parameter θ\theta picks one member of a family. A model MM fixes the whole family: the likelihood p(X∣θ,M)p(X \mid \theta, M), the prior p(θ∣M)p(\theta \mid M) and its hyperparameters. Linear and cubic regression are two models; two sets of cubic coefficients are two parameter values of one model.

Inside a model nothing is new:

p(θ∣X,M)=p(X∣θ,M) p(θ∣M)p(X∣M).p(\theta \mid X, M) = \frac{p(X \mid \theta, M)\, p(\theta \mid M)}{p(X \mid M)}.

The denominator, so far only a normalizer, now takes the lead. It is the evidence, or marginal likelihood, of the model:

p(X∣M)=∫p(X∣θ,M) p(θ∣M) dθ=Eθ∼p(θ∣M)[p(X∣θ,M)].p(X \mid M) = \int p(X \mid \theta, M)\, p(\theta \mid M)\, d\theta = \mathbb E_{\theta \sim p(\theta \mid M)}\big[p(X \mid \theta, M)\big].

The likelihood is averaged over the prior, not maximized.

One level up, the model is just another unknown, a discrete one. Bayes' formula again:

p(Mj∣X)=p(X∣Mj) p(Mj)∑kp(X∣Mk) p(Mk).p(M_j \mid X) = \frac{p(X \mid M_j)\, p(M_j)}{\sum_k p(X \mid M_k)\, p(M_k)}.

With equal prior probabilities of the models, the most probable model is the one with the largest evidence. Otherwise maximize the product p(X∣Mj) p(Mj)p(X \mid M_j)\, p(M_j).

Why not compare the fits? Because models are often nested. A cubic with zero leading coefficients is a straight line, so the best cubic fits at least as well as the best line, and the training error can only go down as the degree grows. A rule built on max⁡θp(X∣θ,M)\max_\theta p(X \mid \theta, M) always votes for the biggest model.

Two practical remarks. In regression the inputs are taken as given, so the evidence is p(y∣X,M)p(y \mid X, M). For continuous data the evidence is a density value: it can exceed 1, and it changes with the units of yy. Compare evidences only for the same data in the same units.

The evidence as a prediction

What does the evidence measure? Feed the data in one point at a time and apply the product rule:

p(X∣M)=p(x1∣M) p(x2∣x1,M)⋯p(xn∣x1,…,xn−1,M)=∏i=1np(xi∣x<i,M).p(X \mid M) = p(x_1 \mid M)\, p(x_2 \mid x_1, M) \cdots p(x_n \mid x_1, \dots, x_{n-1}, M) = \prod_{i=1}^{n} p(x_i \mid x_{<i}, M).

Each factor is a posterior predictive probability: what the model said about the next point after seeing the previous ones. The evidence is the model's track record of predicting the data before seeing them. A model cannot earn evidence by fitting the data afterwards; it has to have bet on them.

Take the coin from the topic The Bayesian approach. In model M1M_1 the probability of heads θ\theta is unknown with a uniform prior Beta⁡(1,1)\operatorname{Beta}(1, 1). After kk heads in mm flips it predicts heads with probability

p(xm+1=H∣x1,…,xm,M1)=k+1m+2,p(x_{m+1} = \text{H} \mid x_1, \dots, x_m, M_1) = \frac{k + 1}{m + 2},

the rule that gave 4/54/5 after three heads (H stands for heads). For the sequence H H H:

p(HHH∣M1)=12⋅23⋅34=14,p(\text{HHH} \mid M_1) = \frac12 \cdot \frac23 \cdot \frac34 = \frac14,

the same as the integral ∫01θ3 dθ=1/4\int_0^1 \theta^3\, d\theta = 1/4. Model M0M_0, a fair coin, predicts 1/21/2 every time and gets 1/81/8. On this sequence M1M_1 predicted better.

Two consequences:

  • When the points are i.i.d. given θ\theta, the order does not matter: the individual factors change, their product does not.
  • log⁡p(X∣M)=∑ilog⁡p(xi∣x<i,M)\log p(X \mid M) = \sum_i \log p(x_i \mid x_{<i}, M) is a sequential validation score. Each point is a test point for the model trained on the points before it; nothing is held out and nothing is used twice.

The automatic Occam's razor

Multiply out the predictive factors for any sequence with kk heads in nn flips and you get

p(X∣M1)=k! (n−k)!(n+1)!.p(X \mid M_1) = \frac{k!\,(n-k)!}{(n+1)!}.

There are (nk)\binom{n}{k} such sequences, so the probability of the count is P(k∣M1)=1/(n+1)P(k \mid M_1) = 1/(n+1): before the data, M1M_1 finds every count from 00 to nn equally likely. The fair coin gives P(k∣M0)=(nk)2−nP(k \mid M_0) = \binom{n}{k} 2^{-n}, a bump around n/2n/2.

Now the key fact. For every θ\theta the likelihood sums to one over all possible datasets, and therefore so does the evidence:

∑Xp(X∣M)=∫p(θ∣M)∑Xp(X∣θ,M) dθ=∫p(θ∣M) dθ=1.\sum_X p(X \mid M) = \int p(\theta \mid M) \sum_X p(X \mid \theta, M)\, d\theta = \int p(\theta \mid M)\, d\theta = 1.

The evidence is a fixed budget of probability, spread over the datasets the model can produce. A flexible model that can explain many datasets must spread it thin. A rigid model puts its budget on a few datasets and wins big if one of them shows up. Nobody added a penalty for complexity; it follows from normalization. This is the automatic Occam's razor.

With n=8n = 8, model M1M_1 gives each of the nine counts 1/9≈0.1111/9 \approx 0.111. The fair coin gives 70/256≈0.27370/256 \approx 0.273 to k=4k = 4 and only 1/2561/256 to k=0k = 0 or k=8k = 8. Where the fair coin placed its bet, it beats the flexible model; far from it, it loses.

The budget picture has a formula too. When the posterior is a narrow peak of width σθ∣X\sigma_{\theta \mid X} inside a prior of width Δθ\Delta\theta,

p(X∣M)≈p(X∣θ^,M)⋅2π σθ∣XΔθ.p(X \mid M) \approx p(X \mid \hat\theta, M) \cdot \frac{\sqrt{2\pi}\, \sigma_{\theta \mid X}}{\Delta\theta}.

The first factor is the best fit. The second, the Occam factor, is the fraction of the prior range that survives the data; the topic The Laplace approximation and Bayesian classification derives it. For 7 heads in 8 flips, θ^=7/8\hat\theta = 7/8 and the best fit is 0.8757⋅0.125≈0.0490.875^7 \cdot 0.125 \approx 0.049, about 12.6 times the fair coin's 2−8≈0.00392^{-8} \approx 0.0039. With σθ∣X≈θ^(1−θ^)/n≈0.117\sigma_{\theta \mid X} \approx \sqrt{\hat\theta(1 - \hat\theta)/n} \approx 0.117 and Δθ=1\Delta\theta = 1, the Occam factor is about 0.290.29. The approximate evidence 0.049⋅0.29≈0.01440.049 \cdot 0.29 \approx 0.0144 is close to the exact 7! 1!/9!=1/72≈0.01397!\,1!/9! = 1/72 \approx 0.0139. Flexibility bought a 12.6 times better fit and cost a factor of about 3.4.

The Bayes factor

To compare two models, divide their posteriors; the common denominator cancels:

p(M1∣X)p(M0∣X)=p(X∣M1)p(X∣M0)⏟K10⋅p(M1)p(M0).\frac{p(M_1 \mid X)}{p(M_0 \mid X)} = \underbrace{\frac{p(X \mid M_1)}{p(X \mid M_0)}}_{K_{10}} \cdot \frac{p(M_1)}{p(M_0)}.

Posterior odds are the Bayes factor times the prior odds. The Bayes factor K10K_{10} is what the data say; the prior odds are what you believed before them.

For 7 heads in 8 flips, K10=(1/72)/(1/256)=256/72≈3.56K_{10} = (1/72) / (1/256) = 256/72 \approx 3.56. It makes no difference whether the data are the sequence or only the count: the binomial coefficient multiplies both evidences and cancels. With equal prior odds, p(M1∣X)=3.56/4.56≈0.78p(M_1 \mid X) = 3.56 / 4.56 \approx 0.78.

A rough scale (Jeffreys; Kass and Raftery): KK from 1 to 3 is barely worth mentioning, 3 to 20 is positive, 20 to 150 strong, above 150 very strong. Our 3.56 is just positive.

Unlike a p-value, a Bayes factor can support the simpler model. But it does so slowly. When exactly half of the flips are heads, K01K_{01} is 2.462.46 at n=8n = 8, 4.624.62 at n=32n = 32 and 9.089.08 at n=128n = 128. It grows like n\sqrt n, because the flexible model's Occam factor shrinks like 1/n1/\sqrt n. Evidence against a wrong simple model, by contrast, grows exponentially in nn. A flexible model is never refuted by data it can also explain; it is only out-predicted.

The evidence remembers the prior width

Inside one model the prior fades as data accumulate. The evidence never forgets it: the Occam factor is the posterior width divided by the prior width, so a prior twice as broad halves the evidence for each parameter it governs.

The running example from the topic Conjugate priors and the exponential family, the Normal mean with known variance. A kitchen scale may have an offset μ\mu. We weigh a reference weight n=4n = 4 times; the noise has σ=2\sigma = 2 g, and the readings minus the true weight average to xˉ=2\bar x = 2 g. Model M0M_0: the scale is calibrated, μ=0\mu = 0. Model M1M_1: μ∼N(0,s02)\mu \sim \mathcal N(0, s_0^2), with the prior variance named as in that topic.

Since xˉ\bar x is sufficient, p(X∣μ)=g(X) N(xˉ∣μ,v)p(X \mid \mu) = g(X)\, \mathcal N(\bar x \mid \mu, v) with v=σ2/n=1v = \sigma^2 / n = 1. The factor g(X)g(X) does not involve μ\mu, is the same in both models and cancels from the Bayes factor. Under M1M_1, xˉ\bar x is μ\mu plus independent noise, so the variances add:

K10=N(xˉ∣0,s02+v)N(xˉ∣0,v)=vs02+v exp⁡ ⁣(xˉ22v−xˉ22(s02+v)).K_{10} = \frac{\mathcal N(\bar x \mid 0, s_0^2 + v)}{\mathcal N(\bar x \mid 0, v)} = \sqrt{\frac{v}{s_0^2 + v}}\, \exp\!\Big(\frac{\bar x^2}{2v} - \frac{\bar x^2}{2(s_0^2 + v)}\Big).

With s02=3s_0^2 = 3 this is 12e1.5≈2.24\tfrac12 e^{1.5} \approx 2.24: mild support for an offset.

Now let s0s_0 grow. The exponential stays below exˉ2/(2v)e^{\bar x^2 / (2v)}, while the square root falls like v/s0\sqrt v / s_0. As s0→∞s_0 \to \infty the calibrated model wins whatever xˉ\bar x is: this is the Jeffreys–Lindley paradox. Meanwhile the posterior of μ\mu under M1M_1 tends to N(xˉ,v)\mathcal N(\bar x, v), which is perfectly sensible. The posterior is fine; the evidence is ruined.

Improper priors make it worse. A "flat prior on the whole line" p(μ)=cp(\mu) = c is not a distribution. The constant cc cancels from the posterior but multiplies the evidence, so the Bayes factor can be made anything. The practical rules: use proper priors on the scale of the problem, report how the conclusion depends on the prior, and when it flips, trust held-out likelihood or cross-validation, which depend on the prior only through the posterior.

Evidence maximization: empirical Bayes

If the evidence is so sensitive to s02s_0^2, why not let the data choose it? Treat the hyperparameter as a knob of the model and pick the value with the largest evidence:

s^02=arg⁡max⁡s02p(X∣s02)=arg⁡max⁡s02∫p(X∣μ) p(μ∣s02) dμ.\hat s_0^2 = \arg\max_{s_0^2} p(X \mid s_0^2) = \arg\max_{s_0^2} \int p(X \mid \mu)\, p(\mu \mid s_0^2)\, d\mu.

This is evidence maximization, also called empirical Bayes or type-II maximum likelihood: μ\mu is integrated out, s02s_0^2 is maximized. After that we use the ordinary conjugate posterior p(μ∣X,s^02)p(\mu \mid X, \hat s_0^2) and its predictive distribution.

For the kitchen scale the integral is already done. Up to terms that do not depend on s02s_0^2,

log⁡p(X∣s02)=−12log⁡(s02+v)−xˉ22(s02+v)+const.\log p(X \mid s_0^2) = -\frac12 \log(s_0^2 + v) - \frac{\bar x^2}{2(s_0^2 + v)} + \text{const}.

One derivative with respect to s02s_0^2 and the constraint s02≥0s_0^2 \ge 0 finish the job; you will do it in the check below.

Why integrate μ\mu out instead of maximizing jointly over μ\mu and s02s_0^2? Because the joint density p(X∣μ) p(μ∣s02)p(X \mid \mu)\, p(\mu \mid s_0^2) has no maximum. Put μ=0\mu = 0 and send s02→0s_0^2 \to 0: the prior becomes an ever taller spike, N(0∣0,s02)=1/2πs02→∞\mathcal N(0 \mid 0, s_0^2) = 1/\sqrt{2\pi s_0^2} \to \infty, while the likelihood stays fixed. Integration replaces the height of the spike by its mass, which is always 1.

Empirical Bayes is the standard way to set the precisions α\alpha and β\beta in Bayesian linear regression and a separate precision per weight in the relevance vector machine. It works well with few hyperparameters and plenty of data; with one hyperparameter per weight it can overfit.

MAP, empirical and full Bayes

We now have three ways to treat the unknowns, from the least to the most Bayesian:

Approach Integrated out Maximized Example
MAP / MLE nothing μ\mu, with s02s_0^2 fixed ridge regression with a fixed penalty
Empirical Bayes μ\mu s02s_0^2 the previous block; the RVM
Full Bayes μ\mu and s02s_0^2 nothing a hyperprior, then sampling or VI

Full Bayes gives the hyperparameter its own prior p(s02)p(s_0^2), a hyperprior, and works with the joint posterior

p(μ,s02∣X)∝p(X∣μ) p(μ∣s02) p(s02),p(x∗∣X)=∬p(x∗∣μ) p(μ,s02∣X) dμ d(s02).p(\mu, s_0^2 \mid X) \propto p(X \mid \mu)\, p(\mu \mid s_0^2)\, p(s_0^2), \qquad p(x_* \mid X) = \iint p(x_* \mid \mu)\, p(\mu, s_0^2 \mid X)\, d\mu\, d(s_0^2).

Empirical Bayes is the shortcut: it is close to full Bayes when the evidence is sharply peaked in a few hyperparameters. It uses the data twice, first to pick s02s_0^2 and then to fit μ\mu, and it treats the chosen s02s_0^2 as certain. A side effect in the scale example: a tuned M1M_1 can never lose to M0M_0, because s02=0s_0^2 = 0 reproduces M0M_0.

The same logic applies to the models themselves. If the model is unknown, you need not pick one; average the predictions with the posterior weights, Bayesian model averaging:

p(x∗∣X)=∑jp(x∗∣X,Mj) p(Mj∣X).p(x_* \mid X) = \sum_j p(x_* \mid X, M_j)\, p(M_j \mid X).

For 7 heads in 8 with equal prior odds, p(M1∣X)≈0.78p(M_1 \mid X) \approx 0.78; M1M_1 predicts heads with (7+1)/(8+2)=0.8(7 + 1)/(8 + 2) = 0.8 and M0M_0 with 0.50.5, so the averaged prediction is 0.78⋅0.8+0.22⋅0.5≈0.730.78 \cdot 0.8 + 0.22 \cdot 0.5 \approx 0.73.

Two limits to keep in mind. The evidence ranks models only within the set of models and priors you wrote down; it does not promise the best test error, especially when none of the models is true. And it is an integral: exact for conjugate and linear-Gaussian models, otherwise approximated by Laplace, variational bounds or sampling, which come later in the course.

Next comes the workhorse of the course, the topic Bayesian linear regression: a Gaussian prior on the weights with precision α\alpha and Gaussian noise with precision β\beta. Its evidence is again a Gaussian integral, and choosing α\alpha and β\beta is exactly the evidence maximization of this topic, with α\alpha in the role of 1/s021/s_0^2.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.