Simple Algorithm
Machine Learning · Convergence · 28 min

Bayesian Model Selection

Marginal likelihood, Bayes factors, BIC and Occam’s razor.

A more flexible model always fits the training data at least as well, so fit alone cannot choose between models. The Bayesian answer, the marginal likelihood, penalizes flexibility automatically: a model that could have explained anything gets little credit for explaining what happened. This lesson derives that Occam's razor, its BIC approximation, its famous paradox, and the predictive criteria that compete with it.

What you’ll learn

  • Compare models with the marginal likelihood and Bayes factors, and compute both for a coin.
  • Derive the Laplace approximation to the evidence and the BIC, and identify the Occam factor.
  • Contrast BIC with AIC: consistency versus predictive efficiency, and why no criterion has both.
  • Explain Lindley's paradox and the prior sensitivity of Bayes factors.
  • Estimate predictive accuracy with PSIS-LOO and WAIC, and say when they and the evidence disagree.

The evidence compares models

Treat the model M\mathcal M itself as unknown. Bayes' rule over models needs each model's marginal likelihood, or evidence: the probability of the data averaged over that model's prior.

p(D∣M)=∫p(D∣θ,M) p(θ∣M) dθ,p(M1∣D)p(M2∣D)=p(D∣M1)p(D∣M2)⏟Bayes factor B12⋅p(M1)p(M2).p(\data \mid \mathcal M) = \int p(\data \mid \boldsymbol\theta, \mathcal M)\,p(\boldsymbol\theta \mid \mathcal M)\,d\boldsymbol\theta,\qquad \frac{p(\mathcal M_1 \mid \data)}{p(\mathcal M_2 \mid \data)} = \underbrace{\frac{p(\data \mid \mathcal M_1)}{p(\data \mid \mathcal M_2)}}_{\text{Bayes factor } B_{12}}\cdot\frac{p(\mathcal M_1)}{p(\mathcal M_2)}.

The Bayes factor is the factor by which the data change the odds between two models. By the chain rule, log⁡p(D∣M)=∑ilog⁡p(xi∣x1,…,xi−1,M)\log p(\data \mid \mathcal M) = \sum_i \log p(x_i \mid x_1, \dots, x_{i-1}, \mathcal M): the evidence is the model's cumulative score at predicting each observation from the ones before it, starting from no data at all. Averaged over all orderings, this equals exhaustive leave-pp-out cross-validation with the log predictive score, summed over every pp (Fong & Holmes, 2020).

log⁡10B12\log_{10} B_{12}B12B_{12}Evidence for M1\mathcal M_1
0 to ½1 to 3.2not worth more than a bare mention
½ to 13.2 to 10substantial
1 to 210 to 100strong
above 2above 100decisive
Jeffreys' scale for Bayes factors, as condensed by Kass & Raftery (1995). These are conventions, not thresholds with a theory behind them.
Predict before you look

Model M0\mathcal M_0: the coin is fair. Model M1\mathcal M_1: its pp is unknown, uniform on [0,1][0, 1]. You see 60 heads in 100 flips, which a likelihood-ratio test rejects at the 5% level. What is the Bayes factor B10B_{10} in favor of M1\mathcal M_1?

Show the answer

0.91

p(D∣M1)=∫01p60(1−p)40dp=B(61,41)p(\data \mid \mathcal M_1) = \int_0^1 p^{60}(1-p)^{40}dp = B(61, 41), so log⁡p(D∣M1)≈−69.41\log p(\data \mid \mathcal M_1) \approx -69.41, while log⁡p(D∣M0)=100log⁡12≈−69.31\log p(\data \mid \mathcal M_0) = 100\log\frac12 \approx -69.31. So B10≈0.91B_{10} \approx 0.91: the data slightly favor the fair coin. With 70 heads instead, B10≈427B_{10} \approx 427, decisive. The next step explains how a better-fitting model can lose.

Occam's razor, automatically

A flexible model spreads its prior predictive probability over many possible datasets, so it can give only a little to each; a rigid model concentrates its probability on few datasets, and wins whenever the data land there. Laplace's method makes this quantitative (MacKay, 2003, chapter 28).

DerivationLaplace approximation and the BIC
  1. p(D)=∫ef(θ) dθ≈ef(θ~)∫e−12(θ−θ~)⊤H(θ−θ~) dθ=p(D∣θ~) p(θ~) (2π)d/2det⁡(H)−1/2p(\data) = \int e^{f(\boldsymbol\theta)}\,d\boldsymbol\theta \approx e^{f(\tilde{\boldsymbol\theta})}\int e^{-\frac12(\boldsymbol\theta - \tilde{\boldsymbol\theta})^\top H(\boldsymbol\theta - \tilde{\boldsymbol\theta})}\,d\boldsymbol\theta = p(\data \mid \tilde{\boldsymbol\theta})\,p(\tilde{\boldsymbol\theta})\,(2\pi)^{d/2}\det(H)^{-1/2}

    Write f(θ)=log⁡p(D∣θ)+log⁡p(θ)f(\boldsymbol\theta) = \log p(\data \mid \boldsymbol\theta) + \log p(\boldsymbol\theta), with mode θ~\tilde{\boldsymbol\theta} and negative Hessian HH there. A second-order expansion turns the evidence into a Gaussian integral:

  2. log⁡p(D)≈log⁡p(D∣θ~)⏟best fit+log⁡[p(θ~) (2π)d/2det⁡(H)−1/2]⏟log Occam factor\log p(\data) \approx \underbrace{\log p(\data \mid \tilde{\boldsymbol\theta})}_{\text{best fit}} + \underbrace{\log\Big[p(\tilde{\boldsymbol\theta})\,(2\pi)^{d/2}\det(H)^{-1/2}\Big]}_{\text{log Occam factor}}

    The evidence is the best fit times an Occam factor: in one dimension, with a flat prior of width Δprior\Delta_{\text{prior}} and posterior width σpost=H−1/2\sigma_{\text{post}} = H^{-1/2}, the factor is 2π σpost/Δprior\sqrt{2\pi}\,\sigma_{\text{post}}/\Delta_{\text{prior}}, the fraction of the prior's range that survives the data.

  3. log⁡p(D∣M)=log⁡p(D∣θ^)−d2log⁡n+Op(1)⟹BIC=−2log⁡p(D∣θ^)+dlog⁡n\log p(\data \mid \mathcal M) = \log p(\data \mid \hat{\boldsymbol\theta}) - \frac d2\log n + O_p(1)\quad\Longrightarrow\quad \mathrm{BIC} = -2\log p(\data \mid \hat{\boldsymbol\theta}) + d\log n

    For i.i.d. data, H≈n IˉH \approx n\,\bar I with Iˉ\bar I the per-observation information, so log⁡det⁡H=dlog⁡n+O(1)\log\det H = d\log n + O(1). Dropping every term that stays bounded as nn grows (prior density, 2π2\pi, det⁡Iˉ\det\bar I, and the gap between mode and MLE) leaves Schwarz's criterion (Schwarz, 1978):

  4. log⁡p(D∣M1)≈−67.30−2.10=−69.40(exact: −69.41)\log p(\data \mid \mathcal M_1) \approx -67.30 - 2.10 = -69.40\quad(\text{exact: } -69.41)

    Check it on the coin. M1\mathcal M_1's best fit is 60log⁡0.6+40log⁡0.4≈−67.3060\log0.6 + 40\log0.4 \approx -67.30, two nats better than the fair coin's −69.31-69.31. Its Occam factor is 2π⋅0.24/100\sqrt{2\pi}\cdot\sqrt{0.24/100} over a prior width of 1, about 0.1230.123, or −2.10-2.10 nats. The penalty slightly exceeds the gain:

BIC versus AIC

Two penalized likelihoods dominate practice. The BIC approximates −2-2 times the log evidence. The AIC (Akaike, 1974),

AIC=−2log⁡p(D∣θ^)+2d,\mathrm{AIC} = -2\log p(\data \mid \hat{\boldsymbol\theta}) + 2d,

estimates something else: −2-2 times the expected log-likelihood of the fitted model on fresh data, correcting the training optimism of dd nats that a regular model with dd parameters enjoys. Leave-one-out cross-validation is asymptotically equivalent to AIC (Stone, 1977). Applied to Gaussian mixtures fitted to three well-separated blobs, both criteria pick the right number of components, but with very different margins:

KKparameters d=6K−1d = 6K - 1best log⁡L^\log\hat LBICAIC
15−199.40-199.40424.3408.8
211−86.45-86.45229.1194.9
317−33.52-33.52153.8101.0
423−28.51-28.51174.5103.0
529−22.97-22.97194.0103.9
635−18.26-18.26215.2106.5
Full-covariance mixtures in 2-D on the widget's 'blobs' data (n=165n = 165, so log⁡n≈5.11\log n \approx 5.11): the best log-likelihoods found over many starts. Each component has 2 mean and 3 covariance parameters, and the KK weights add K−1K - 1. The likelihood keeps rising with KK. BIC chooses K=3K = 3 by about 20 points; AIC also chooses 3, but K=4K = 4 and 55 trail it by only 2 to 3 points. Strictly, a mixture with more components than clusters is a singular model, where neither criterion's derivation applies (the last step returns to this); here both still pick the right KK.
Predict before you look

Two nested regular models differ by one parameter, and the smaller one is true. With a large sample, how often does AIC choose the larger model?

Show the answer

15.7 %

AIC prefers the larger model when the likelihood-ratio statistic 2(log⁡L^1−log⁡L^0)2(\log\hat L_1 - \log\hat L_0) exceeds 2, its extra penalty. Under the smaller model that statistic is asymptotically χ12\chi^2_1 (Wilks), and Pr⁡(χ12>2)≈0.157\Pr(\chi^2_1 > 2) \approx 0.157, whatever the sample size. BIC's threshold log⁡n\log n grows: Pr⁡(χ12>log⁡n)\Pr(\chi^2_1 > \log n) is 3.2% at n=100n = 100 and 0.24% at n=104n = 10^4, tending to zero.

Lindley's paradox and prior sensitivity

DerivationA significant result that favors the null
  1. B01=N(xˉ∣0,σ2/n)N(xˉ∣0,τ2+σ2/n)=1+r  exp⁡(−z22⋅r1+r)B_{01} = \frac{\mathcal N(\bar x \mid 0, \sigma^2/n)}{\mathcal N(\bar x \mid 0, \tau^2 + \sigma^2/n)} = \sqrt{1 + r}\;\exp\Big(-\frac{z^2}{2}\cdot\frac{r}{1 + r}\Big)

    Let xˉ∼N(θ,σ2/n)\bar x \sim \mathcal N(\theta, \sigma^2/n), and compare H0:θ=0H_0: \theta = 0 with H1:θ∼N(0,τ2)H_1: \theta \sim \mathcal N(0, \tau^2). Under H1H_1, integrating out θ\theta gives xˉ∼N(0,τ2+σ2/n)\bar x \sim \mathcal N(0, \tau^2 + \sigma^2/n). With z=xˉn/σz = \bar x\sqrt n/\sigma and r=nτ2/σ2r = n\tau^2/\sigma^2:

  2. B01≈0.54 (r=1),1.5 (r=100),14.7 (r=104),146 (r=106)B_{01} \approx 0.54\ (r = 1),\quad 1.5\ (r = 100),\quad 14.7\ (r = 10^4),\quad 146\ (r = 10^6)

    Hold the evidence fixed in the frequentist sense, z=1.96z = 1.96 (a two-sided pp-value of 0.05), and let nn grow. The exponential factor tends to e−z2/2e^{-z^2/2} while 1+r\sqrt{1 + r} grows without bound:

  3. z fixed, n→∞  or  τ→∞:B01→∞z \text{ fixed},\ n \to \infty\ \text{ or }\ \tau \to \infty:\qquad B_{01} \to \infty

    So the same "significant at 5%" result is evidence for the null once rr exceeds about 42, and overwhelming evidence for it in huge samples (Lindley, 1957). Letting τ→∞\tau \to \infty at fixed nn does the same: a vaguer alternative spreads its prior over ever more implausible effects and loses (Bartlett, 1957).

The paradox is not an error on either side. A pp-value asks how surprising the data are under H0H_0; a Bayes factor asks which hypothesis predicted them better, and a diffuse H1H_1 predicts poorly. But it exposes the prior sensitivity of Bayes factors: posteriors within a model forget the prior as nn grows, while the evidence keeps depending on its scale, roughly through the Occam factor σpost/Δprior\sigma_{\text{post}}/\Delta_{\text{prior}}. An improper prior, Δprior=∞\Delta_{\text{prior}} = \infty, makes the Bayes factor undefined.

Remedies calibrate the prior instead of making it vague: Zellner-type gg-priors and their mixtures for regression (Liang, Paulo, Molina, Clyde & Berger, 2008), and intrinsic or fractional Bayes factors, which spend part of the data turning an improper prior into a proper one (Berger & Pericchi, 1996; O'Hagan, 1995).

Check your understanding

In a very large trial, a treatment effect has z=2.5z = 2.5 (p≈0.012p \approx 0.012). The alternative is a diffuse N(0,τ2)\mathcal N(0, \tau^2) prior on the effect. What can the Bayes factor do as nn grows with zz fixed?

  1. It always favors the alternative, since p<0.05p < 0.05
  2. It converges to the likelihood ratio at the MLE
  3. It is unaffected by τ\tau, because priors wash out
  4. It increasingly favors the null: the observed effect xˉ=zσ/n\bar x = z\sigma/\sqrt n shrinks toward 0, which the point null predicts far better than a diffuse alternative
Show the answer

D. It increasingly favors the null: the observed effect xˉ=zσ/n\bar x = z\sigma/\sqrt n shrinks toward 0, which the point null predicts far better than a diffuse alternative

With zz fixed the observed effect is of order 1/n1/\sqrt n, tiny compared with the alternative's spread τ\tau. B01=1+r e−z22r1+rB_{01} = \sqrt{1+r}\,e^{-\frac{z^2}{2}\frac{r}{1+r}} grows like n\sqrt n, so eventually the null wins however small pp is.

Predictive alternatives: PSIS-LOO and WAIC

If the goal is prediction, score each model by its expected log predictive density on new data, elpd=∑iE[log⁡p(y~i∣D)]\mathrm{elpd} = \sum_i \E\big[\log p(\tilde y_i \mid \data)\big], and estimate it without refitting. Leave-one-out uses ∑ilog⁡p(yi∣D−i)\sum_i \log p(y_i \mid \data_{-i}), and a single set of posterior draws θ(1),…,θ(S)\boldsymbol\theta^{(1)}, \dots, \boldsymbol\theta^{(S)} suffices, because

p(yi∣D−i)=(Eθ∣D[1p(yi∣θ)])−1,p(y_i \mid \data_{-i}) = \Big(\E_{\boldsymbol\theta \mid \data}\Big[\frac{1}{p(y_i \mid \boldsymbol\theta)}\Big]\Big)^{-1},

an importance-sampling identity with ratios ri(s)=1/p(yi∣θ(s))r_i^{(s)} = 1/p(y_i \mid \boldsymbol\theta^{(s)}). Raw ratios can have infinite variance. Pareto-smoothed importance sampling fits a generalized Pareto distribution to the largest ratios and replaces them by its quantiles (Vehtari, Gelman & Gabry, 2017); the fitted shape k^\hat k is a diagnostic, and the estimate is considered reliable when k^<min⁡(1−1/log⁡10S, 0.7)\hat k < \min(1 - 1/\log_{10}S,\ 0.7) (Vehtari et al., 2024). WAIC instead subtracts a variance penalty from the log pointwise predictive density:

elpd^WAIC=∑i=1nlog⁡(1S∑s=1Sp(yi∣θ(s)))−∑i=1nVar⁡s[log⁡p(yi∣θ(s))].\widehat{\mathrm{elpd}}_{\text{WAIC}} = \sum_{i=1}^n \log\Big(\frac1S\sum_{s=1}^S p(y_i \mid \boldsymbol\theta^{(s)})\Big) - \sum_{i=1}^n \Var_s\big[\log p(y_i \mid \boldsymbol\theta^{(s)})\big].

Watanabe (2010) proved that WAIC and Bayesian leave-one-out cross-validation are asymptotically equivalent, also for singular models such as mixtures and neural networks, where AIC and BIC lose their justification.

CriterionWhat it estimatesStrengthsWeaknesses
Evidence, Bayes factorprobability of the data under the prior predictivecoherent model probabilities; consistent when a candidate is trueprior-scale sensitive; undefined with improper priors; hard to compute
BIC−2log⁡-2\log evidence, up to O(1)O(1)one fit; consistentignores the prior; invalid for singular models
AICexpected out-of-sample devianceone fit; efficient for predictionregular models only; ignores posterior uncertainty
PSIS-LOO, WAICexpected log predictive density of the Bayesian predictiveuses posterior draws; diagnostics (k^\hat k); valid for singular models (WAIC)needs a good posterior sample; LOO assumes exchangeable data
Computing the evidence itself is a research topic: Laplace and BIC approximations, nested sampling (Skilling, 2006), bridge and path sampling (Gelman & Meng, 1998) and Chib's (1995) method from Gibbs output. The harmonic-mean estimator (Newton & Raftery, 1994) is simple but notoriously unstable.

Key takeaways

  • The evidence p(D∣M)p(\data \mid \mathcal M) averages the likelihood over the prior; Bayes factors multiply prior model odds into posterior odds, and the log evidence is a cumulative one-step-ahead prediction score.
  • Laplace's method gives evidence ≈ best fit × Occam factor; dropping bounded terms gives BIC=−2log⁡L^+dlog⁡n\mathrm{BIC} = -2\log\hat L + d\log n. A model that fits slightly better can lose, as 60 heads in 100 (B10≈0.91B_{10} \approx 0.91) shows.
  • BIC (dlog⁡nd\log n) is consistent when a candidate is true; AIC (2d2d) targets out-of-sample deviance and overfits nested models with probability about 0.157. By Yang's theorem no criterion is both consistent and minimax-rate optimal.
  • Lindley's paradox: at fixed pp-value, the Bayes factor for a point null grows like n\sqrt n. Bayes factors stay sensitive to the prior's scale, so default priors must be calibrated, not merely vague.
  • PSIS-LOO and WAIC estimate predictive accuracy from posterior draws, with a k^\hat k diagnostic for LOO and validity for singular models (Watanabe); in the M-open setting, stacking often beats selecting a single model.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Model M1\mathcal M_1 contains M0\mathcal M_0 as a special case, so its maximized likelihood is at least as high. How can the evidence still favor M0\mathcal M_0?

  1. It cannot: the evidence always favors the model with the higher maximized likelihood
  2. Because the evidence counts parameters directly, subtracting one unit per parameter
  3. Because M1\mathcal M_1's prior spreads over many parameter values that fit the data badly, and the evidence averages the likelihood over that prior
  4. Only if the prior of M0\mathcal M_0 is improper
Show the answer

C. Because M1\mathcal M_1's prior spreads over many parameter values that fit the data badly, and the evidence averages the likelihood over that prior

The evidence is the prior-weighted average of the likelihood, not its maximum. Laplace's method splits it into the best fit times an Occam factor, roughly the fraction of prior volume consistent with the data. A small fit improvement does not pay for a large loss of prior volume.

Question 2

The true model is one of several fixed-dimensional candidates. As n→∞n \to \infty, which statement is correct?

  1. BIC selects the true model with probability tending to one; AIC keeps a fixed positive probability of choosing a larger model
  2. AIC selects the true model with probability tending to one; BIC underfits
  3. Both are consistent, since both penalize parameters
  4. Neither is consistent, since penalties are constants
Show the answer

A. BIC selects the true model with probability tending to one; AIC keeps a fixed positive probability of choosing a larger model

BIC's penalty dlog⁡nd\log n grows with nn, so the chance that noise buys an extra parameter vanishes. AIC's penalty 2d2d does not grow: with one extra parameter it overfits with probability Pr⁡(χ12>2)≈0.157\Pr(\chi^2_1 > 2) \approx 0.157 forever. In exchange, AIC predicts better when no candidate is exactly true (Yang, 2005).

Question 3

What do PSIS-LOO and WAIC estimate?

  1. The posterior probability that each model is true
  2. The marginal likelihood, more cheaply than the Laplace approximation
  3. The expected log predictive density of the model's posterior predictive on new data
  4. The number of parameters in the model
Show the answer

C. The expected log predictive density of the model's posterior predictive on new data

Both estimate out-of-sample predictive accuracy from one set of posterior draws: leave-one-out by Pareto-smoothed importance weights, WAIC by penalizing the in-sample log predictive density with the posterior variance of the log-likelihood. They answer "which model predicts better?", not "which model is true?".

Further reading

  • R. E. Kass & A. E. Raftery, "Bayes factors," Journal of the American Statistical Association 90(430), 773–795 (1995).
  • G. Schwarz, "Estimating the dimension of a model," Annals of Statistics 6(2), 461–464 (1978).
  • D. J. C. MacKay, Information Theory, Inference, and Learning Algorithms (Cambridge University Press, 2003), chapter 28.
  • A. Vehtari, A. Gelman & J. Gabry, "Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC," Statistics and Computing 27(5), 1413–1432 (2017).
  • S. Watanabe, "Asymptotic equivalence of Bayes cross validation and widely applicable information criterion in singular learning theory," Journal of Machine Learning Research 11, 3571–3594 (2010).