Skip to content
LeetQuant
E02 Bayesian Statistics

E02 · Bayesian Statistics · Lesson 4 of 4

Bayesian Model Comparison

Marginal likelihoods and Bayes factors, the automatic Occam penalty shown on a coin, BIC as a Laplace approximation, Lindley's paradox, prior sensitivity, and posterior model averaging.

18 min

Objective

The Bayesian Updating with Repeated Evidence lesson compared hypotheses with no free parameters (which die, which coin) using likelihood ratios. Real models have parameters: "the strategy has zero edge" against "the strategy has some edge of unknown size". This lesson shows how to compare such models, why the comparison automatically penalises flexibility, how this connects to BIC, and why a Bayes factor and a p-value can point in opposite directions. By the end you should be able to compute a Bayes factor for a coin by hand and explain Lindley's paradox in an interview.

Marginal likelihood and Bayes factors

The marginal likelihood (or evidence) of model MM averages the likelihood over that model's prior:

p(y∣M)=∫p(y∣θ,M) π(θ∣M) dθ.p(y \mid M) = \int p(y \mid \theta, M)\, \pi(\theta \mid M)\, d\theta .

Posterior odds between two models are prior odds times the Bayes factor:

P(M1∣y)P(M0∣y)=P(M1)P(M0)×BF10,BF10=p(y∣M1)p(y∣M0).\frac{P(M_1 \mid y)}{P(M_0 \mid y)} = \frac{P(M_1)}{P(M_0)} \times \text{BF}_{10}, \qquad \text{BF}_{10} = \frac{p(y \mid M_1)}{p(y \mid M_0)} .

A common reading (Kass and Raftery, 1995): BF\text{BF} of 1–3 is barely worth mentioning, 3–20 positive, 20–150 strong, above 150 very strong.

The Occam effect. p(y∣M)p(y \mid M) is a probability distribution over possible data sets, so it sums to one. A flexible model that can explain many data sets must spread its probability thinly; a sharp model concentrates it. If the data land where the sharp model predicted, it wins, even though the flexible model fits at least as well at its best parameter. The penalty is not added by hand; it falls out of averaging the likelihood instead of maximising it.

The coin

Compare M0M_0: p=12p = \tfrac12 against M1M_1: p∼U(0,1)p \sim U(0, 1), after a particular sequence of nn flips with kk heads:

p(y∣M0)=2−n,p(y∣M1)=∫01pk(1−p)n−k dp=k! (n−k)!(n+1)!.p(y \mid M_0) = 2^{-n}, \qquad p(y \mid M_1) = \int_0^1 p^k (1-p)^{n-k}\, dp = \frac{k!\,(n-k)!}{(n+1)!} .

(Using the count kk instead of the sequence multiplies both by (nk)\binom nk, so the Bayes factor is the same.) A neat fact: under M1M_1 the number of heads is uniform on 0,1,…,n0, 1, \dots, n, with probability 1/(n+1)1/(n+1) each. M1M_1 commits to nothing, and pays for it.

Worked example

ten flips

For n=10n = 10 flips, compute BF10\text{BF}_{10} for k=5,…,10k = 5, \dots, 10 heads and compare with the two-sided exact p-value for fairness.

Show solution

p(y∣M0)=1/1024p(y \mid M_0) = 1/1024 throughout. For k=8k = 8: p(y∣M1)=8! 2!/11!=2/990=1/495p(y \mid M_1) = 8!\,2!/11! = 2/990 = 1/495, so BF10=1024/495=2.07\text{BF}_{10} = 1024/495 = 2.07. For k=9k = 9: 9! 1!/11!=1/1109!\,1!/11! = 1/110 and BF10=1024/110=9.31\text{BF}_{10} = 1024/110 = 9.31.

Heads kk5678910
BF10\text{BF}_{10}0.370.440.782.079.3193.1
Two-sided p-value1.0000.7540.3440.1090.0210.002

Read the Occam effect from the first columns. At 6 heads the best-fitting p=0.6p = 0.6 fits better than p=0.5p = 0.5, yet the evidence favours the fair coin by 1/0.44=2.31/0.44 = 2.3 to 1, because M1M_1 wasted prior mass on values like p=0.1p = 0.1 that predicted these data badly. Only from about 8 heads does the flexible model pay its way.

Compare the scales: k=9k = 9 gives p=0.021p = 0.021, "significant at 5%", but a Bayes factor of 9: with even prior odds, the posterior probability of a biased coin is 9.31/10.31=0.909.31/10.31 = 0.90, not 0.980.98. A p-value is not the probability of the null, and it typically overstates the evidence against it.

BIC is a Laplace approximation to the evidence

Expand the log integrand around the maximum likelihood estimate θ^\hat\theta (dimension dd). The likelihood is approximately Gaussian with precision matrix H=nI(θ^)H = n I(\hat\theta), so

log⁡p(y∣M)≈ℓ(θ^)+log⁡π(θ^)+d2log⁡(2π)−12log⁡∣nI(θ^)∣=ℓ(θ^)−d2log⁡n+O(1).\log p(y \mid M) \approx \ell(\hat\theta) + \log \pi(\hat\theta) + \frac d2 \log(2\pi) - \frac12 \log\lvert n I(\hat\theta)\rvert = \ell(\hat\theta) - \frac d2 \log n + O(1) .

Dropping the O(1)O(1) terms gives −12BIC-\tfrac12 \text{BIC} with BIC=dlog⁡n−2ℓ(θ^)\text{BIC} = d\log n - 2\ell(\hat\theta). So log⁡BF10≈−12(BIC1−BIC0)\log \text{BF}_{10} \approx -\tfrac12(\text{BIC}_1 - \text{BIC}_0), with an error that does not shrink as nn grows: it converges to a constant that depends on the prior. BIC is consistent for choosing the model, but it is a crude evidence calculator.

The code compares the exact log Bayes factor for the coin with the BIC version when 60% of flips are heads.

from math import lgamma, log

def log_bf10_exact(k, n):           # uniform prior on p vs p = 1/2
    log_m1 = lgamma(k + 1) + lgamma(n - k + 1) - lgamma(n + 2)
    log_m0 = n * log(0.5)
    return log_m1 - log_m0

def log_bf10_bic(k, n):             # max log-lik gain minus (1/2) log n
    p = k / n
    gain = k * log(p) + (n - k) * log(1 - p) - n * log(0.5)
    return gain - 0.5 * log(n)

print("    n   heads   exact    BIC    diff")
for n in (10, 100, 1_000, 10_000):
    k = int(0.6 * n)                # always 60% heads
    e, b = log_bf10_exact(k, n), log_bf10_bic(k, n)
    print(f"{n:6d} {k:6d} {e:7.2f} {b:7.2f} {e - b:6.3f}")

Output:

    n   heads   exact    BIC    diff
    10      6   -0.81   -0.95  0.136
   100     60   -0.09   -0.29  0.198
  1000    600   16.89   16.68  0.205
 10000   6000  196.96  196.75  0.205

The gap settles at log⁡π(p^)+12log⁡(2π)+12log⁡(p^(1−p^))=0+0.919−0.714=0.205\log\pi(\hat p) + \tfrac12\log(2\pi) + \tfrac12\log\big(\hat p(1 - \hat p)\big) = 0 + 0.919 - 0.714 = 0.205, exactly the O(1)O(1) term dropped above. Note also how the evidence moves: at 60 heads in 100 the models are roughly tied, while 600 in 1,000 is overwhelming.

Lindley's paradox

Fix the z-statistic and let nn grow. For the coin, under M1M_1 the count is uniform, p(k∣M1)=1/(n+1)p(k \mid M_1) = 1/(n + 1), while under M0M_0 the normal approximation gives p(k∣M0)≈φ(z)/(n/2)p(k \mid M_0) \approx \varphi(z)/(\sqrt n/2). Hence

BF01≈2n φ(z).\text{BF}_{01} \approx 2\sqrt n\, \varphi(z) .

For a normal mean with xˉ∼N(θ,σ2/n)\bar x \sim N(\theta, \sigma^2/n), M0:θ=0M_0: \theta = 0 and M1:θ∼N(0,τ2)M_1: \theta \sim N(0, \tau^2), the exact version is

BF01=1+nτ2σ2  exp⁡ ⁣(−z22⋅nτ2σ2+nτ2).\text{BF}_{01} = \sqrt{1 + \frac{n\tau^2}{\sigma^2}}\; \exp\!\left(-\frac{z^2}{2}\cdot\frac{n\tau^2}{\sigma^2 + n\tau^2}\right) .

Either way, at fixed zz the Bayes factor for the null grows like n\sqrt n. A result that is "significant" at a fixed level eventually becomes strong evidence for the null. This is Lindley's paradox, and it is not a contradiction: the two methods answer different questions. With zz fixed, the estimated effect shrinks like 1/n1/\sqrt n, and a tiny effect is better predicted by "exactly zero" than by an alternative that spread its prior over effects thousands of times larger.

Two practical consequences:

  • The prior under the alternative matters, and does not wash out. For large nn, BF01\text{BF}_{01} is roughly proportional to τ\tau: doubling the prior SD of the effect roughly doubles the evidence for the null, while the posterior for θ\theta under M1M_1 barely moves. Choose τ\tau on the scale of effects you actually consider plausible. In trading, edges are tiny, so a vague alternative is a poor model of "the strategy works".
  • No improper priors on parameters that differ between models. A flat prior π(θ)=c\pi(\theta) = c makes p(y∣M1)p(y \mid M_1) proportional to an arbitrary cc, and so is the Bayes factor. Parameters common to both models (a shared variance, say) can take improper priors, because the constant cancels.

Worked example

a million flips

A coin lands heads 501,250501{,}250 times in n=1,000,000n = 1{,}000{,}000 flips. Compute the z-statistic and two-sided p-value for fairness, and BF01\text{BF}_{01} against a uniform alternative.

Show solution

Frequentist. Under p=12p = \tfrac12 the count has mean 500,000500{,}000 and SD n/4=500\sqrt{n/4} = 500, so z=1,250/500=2.5z = 1{,}250/500 = 2.5 and the two-sided p-value is 0.01240.0124. Reject fairness at 5%.

Bayesian. BF01≈2n φ(2.5)=2×1000×0.01753=35.1\text{BF}_{01} \approx 2\sqrt n\,\varphi(2.5) = 2 \times 1000 \times 0.01753 = 35.1. The exact calculation with log⁡B(k+1,n−k+1)\log B(k + 1, n - k + 1) gives 35.0635.06. With even prior odds, P(fair∣y)=35.1/36.1=0.97P(\text{fair} \mid y) = 35.1/36.1 = 0.97.

Reconciling them. The estimate p^=0.50125\hat p = 0.50125 is very close to a half. The data are about as consistent with p=0.5p = 0.5 as data can be at this sample size, whereas the uniform alternative said, before seeing anything, that pp was as likely to be in [0.6,0.7][0.6, 0.7] as in [0.45,0.55][0.45, 0.55]. Against a sharper alternative, say p∼N(0.5,0.0012)p \sim N(0.5, 0.001^2), the normal formula with σ2=14\sigma^2 = \tfrac14 gives nτ2/σ2=4n\tau^2/\sigma^2 = 4 and BF01=5 e−3.125×0.8=0.18\text{BF}_{01} = \sqrt5\, e^{-3.125 \times 0.8} = 0.18, about 5 to 1 for bias. The Bayes factor depends on what "biased" was supposed to mean.

Posterior model averaging

Choosing one model and then acting as if it were true ignores the uncertainty about which model is right. Bayesian model averaging (BMA) weights each model's answer by its posterior probability:

p(Δ∣y)=∑kp(Δ∣y,Mk) P(Mk∣y),P(Mk∣y)=P(Mk) p(y∣Mk)∑jP(Mj) p(y∣Mj).p(\Delta \mid y) = \sum_k p(\Delta \mid y, M_k)\, P(M_k \mid y), \qquad P(M_k \mid y) = \frac{P(M_k)\, p(y \mid M_k)}{\sum_j P(M_j)\, p(y \mid M_j)} .

For prediction, BMA has lower expected log-loss than any single model when the model set and priors are right. In return-predictability research it is used to average over subsets of candidate predictors, which tempers the inflated in-sample fit of the single best subset.

Computing evidence is hard outside conjugate models. The harmonic-mean estimator from MCMC output is notoriously unstable; bridge sampling, nested sampling or thermodynamic integration are the reliable options. If the goal is predictive accuracy rather than identifying a true model, cross-validation or approximate leave-one-out (PSIS-LOO, WAIC) is often preferred; it is far less sensitive to the prior width than the Bayes factor.

Worked example

averaging over the coin models

With even prior odds, you see 8 heads in 10 flips. What is the predictive probability that the next flip is heads under each model, under the selected model, and under BMA?

Show solution

Model probabilities. BF10=1024/495=2.069\text{BF}_{10} = 1024/495 = 2.069, so P(M1∣y)=2.069/3.069=0.674P(M_1 \mid y) = 2.069/3.069 = 0.674 and P(M0∣y)=0.326P(M_0 \mid y) = 0.326.

Within-model predictions. M0M_0: 0.50.5. M1M_1: the posterior is Beta(9,3)\text{Beta}(9, 3), so P(heads)=9/12=0.75P(\text{heads}) = 9/12 = 0.75 (Laplace's rule).

Select versus average. Selecting M1M_1 predicts 0.750.75. BMA predicts

0.326×0.5+0.674×0.75=0.669.0.326 \times 0.5 + 0.674 \times 0.75 = 0.669 .

BMA is less extreme because a third of the posterior weight is still on the fair coin. If you bet at even money with the Kelly fraction 2p−12p - 1, the difference matters: 0.750.75 says stake 50% of the bankroll, 0.6690.669 says 34%, so acting on the selected model bets about half as much again as the averaged estimate justifies.

Knowledge check

A coin is flipped 10 times and lands heads 9 times. What is the Bayes factor for M1M_1: p∼U(0,1)p \sim U(0, 1) against M0M_0: p=12p = \tfrac12?

Knowledge check

You test whether a strategy's mean daily return is zero using 2,500 days, daily SD 1%, and get z = 2.2. Analyst A puts a N(0, 0.1%^2) prior on the mean under the alternative; analyst B uses N(0, 1%^2). What do their Bayes factors say?

Knowledge check

Hold the z-statistic of a test at 2.5 and increase the sample size from one million to one hundred million, with a fixed uniform alternative. What happens to the Bayes factor in favour of the null?

Key takeaways

  • The evidence p(y∣M)p(y \mid M) averages the likelihood over the prior; the Bayes factor is the ratio of evidences, and posterior odds = prior odds × Bayes factor.
  • Averaging rather than maximising builds in an Occam penalty: 6 heads in 10 favours the fair coin by 2.3 to 1 even though p^=0.6\hat p = 0.6 fits better.
  • BIC =dlog⁡n−2ℓ= d\log n - 2\ell is a Laplace approximation with an O(1)O(1) error that never vanishes; it is consistent for model choice but crude for the evidence.
  • Lindley's paradox: at fixed zz, BF01\text{BF}_{01} grows like n\sqrt n. P-values and Bayes factors answer different questions.
  • Bayes factors are sensitive to the prior width under the alternative and are undefined with improper priors on model-specific parameters; choose priors on the scale of plausible effects.
  • Model averaging weights predictions by posterior model probabilities and is less over-confident than selecting a single model.

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).