Chapter 9

Linear Regression

Regression asks a conditional question: how does the distribution of YY change as 𝐱\mathbf{x} changes? Up to now every observation in a group shared the same mean. Here each observation carries its own mean, tied to the others through a short coefficient vector 𝜷\boldsymbol{\beta}.Peter D. Hoff. A First Course in Bayesian Statistical Methods. Springer Texts in Statistics. Springer, New York, 2009. ISBN 978-0-387-92299-7. doi: 10.1007/978-0-387-92407-6 The Bayesian treatment of estimation turns out to be the multivariate normal update of Chapter 7 in a new costume. The genuinely new idea is what comes next: treating which variables belong in the model as one more unknown with a posterior distribution.

9.1The Normal Linear Model

A linear regression model asserts that the conditional mean is linear in the parameters, E[Y∣𝐱]=𝜷T𝐱E[Y \mid \mathbf{x}] = \boldsymbol{\beta}^T\mathbf{x}; the regressors themselves may be squares, products, or indicators of anything we measured. Stacking nn observations, with 𝐗\mathbf{X} the n×pn \times p matrix whose ii-th row is 𝐱iT\mathbf{x}_i^T, the normal linear model is

𝐲∣𝐗,𝜷,σ2∼𝒩n(𝐗𝜷, σ2In),p(𝐲∣𝐗,𝜷,σ2)=(2πσ2)−n/2exp⁡{−SSR⁡(𝜷)2σ2}, \mathbf{y} \mid \mathbf{X}, \boldsymbol{\beta}, \sigma^2 \sim \mathcal{N}_n(\mathbf{X}\boldsymbol{\beta},\, \sigma^2 I_n), \qquad p(\mathbf{y} \mid \mathbf{X}, \boldsymbol{\beta}, \sigma^2) = (2\pi\sigma^2)^{-n/2}\exp\left\{-\frac{\operatorname{SSR}(\boldsymbol{\beta})}{2\sigma^2}\right\}, (9.1)

where SSR⁡(𝜷)=(𝐲−𝐗𝜷)T(𝐲−𝐗𝜷)\operatorname{SSR}(\boldsymbol{\beta}) = (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T(\mathbf{y} - \mathbf{X}\boldsymbol{\beta}). Setting its gradient −2𝐗T𝐲+2𝐗T𝐗𝜷-2\mathbf{X}^T\mathbf{y} + 2\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} to zero gives the ordinary least squares estimate 𝜷^ols=(𝐗T𝐗)−1𝐗T𝐲\hat{\boldsymbol{\beta}}_{\text{ols}} = (\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}, whose sampling variance is σ2(𝐗T𝐗)−1\sigma^2(\mathbf{X}^T\mathbf{X})^{-1}. Completing the square around it yields the regression analogue of the sum-of-squares decomposition from Chapter 5:

SSR⁡(𝜷)=SSR⁡ols+(𝜷−𝜷^ols)T𝐗T𝐗 (𝜷−𝜷^ols), \operatorname{SSR}(\boldsymbol{\beta}) = \operatorname{SSR}_{\text{ols}} + (\boldsymbol{\beta} - \hat{\boldsymbol{\beta}}_{\text{ols}})^T \mathbf{X}^T\mathbf{X}\,(\boldsymbol{\beta} - \hat{\boldsymbol{\beta}}_{\text{ols}}), (9.2)

the cross term vanishing because the OLS residual is orthogonal to the columns of 𝐗\mathbf{X}. Hence (𝜷^ols,SSR⁡ols)(\hat{\boldsymbol{\beta}}_{\text{ols}}, \operatorname{SSR}_{\text{ols}}) is sufficient for (𝜷,σ2)(\boldsymbol{\beta}, \sigma^2).

Running example. Twelve men were randomized to twelve weeks of flat-terrain running or step aerobics; yy is the change in maximal oxygen uptake. With x2=1x_2 = 1 for aerobics and x3x_3 equal to age, Hoff fits

E[Y∣𝐱]=β1+β2 group+β3 age+β4 (group×age), E[Y \mid \mathbf{x}] = \beta_1 + \beta_2\,\text{group} + \beta_3\,\text{age} + \beta_4\,(\text{group} \times \text{age}), (9.3)

that is, two unrelated lines: β1+β3 age\beta_1 + \beta_3\,\text{age} for runners and (β1+β2)+(β3+β4) age(\beta_1 + \beta_2) + (\beta_3 + \beta_4)\,\text{age} for aerobics.Page 151 of the 2009 printing indexes the two lines by x1=0,1x_1 = 0, 1; since x1≡1x_1 \equiv 1 is the intercept column, the switch is x2x_2. See Appendix C. Least squares gives 𝜷^ols=(−51.29,13.11,2.09,−0.32)T\hat{\boldsymbol{\beta}}_{\text{ols}} = (-51.29, 13.11, 2.09, -0.32)^T with standard errors (12.25,15.76,0.53,0.65)(12.25, 15.76, 0.53, 0.65) and σ^ols2=8.54\hat{\sigma}^2_{\text{ols}} = 8.54. Read coefficient by coefficient, the group effect looks negligible. We shall see that this reading is wrong.

9.2Semiconjugate Priors and the Gibbs Sampler

As a function of 𝜷\boldsymbol{\beta}, the likelihood (9.1) is an exponentiated quadratic, so a normal prior is conditionally conjugate.

Proposition 9.1 (Semiconjugate Full Conditionals). Under independent priors 𝜷∼𝒩p(𝜷0,Σ0)\boldsymbol{\beta} \sim \mathcal{N}_p(\boldsymbol{\beta}_0, \Sigma_0) and 1/σ2∼gamma⁡(ν0/2, ν0σ02/2)1/\sigma^2 \sim \operatorname{gamma}(\nu_0/2,\, \nu_0\sigma_0^2/2),

𝜷∣𝐲,𝐗,σ2∼𝒩p(𝐦,V),V=(Σ0−1+𝐗T𝐗/σ2)−1,𝐦=V(Σ0−1𝜷0+𝐗T𝐲/σ2),1/σ2∣𝐲,𝐗,𝜷∼gamma⁡ ⁣(ν0+n2, ν0σ02+SSR⁡(𝜷)2). \begin{aligned} \boldsymbol{\beta} \mid \mathbf{y}, \mathbf{X}, \sigma^2 &\sim \mathcal{N}_p(\mathbf{m}, V), \qquad V = \bigl(\Sigma_0^{-1} + \mathbf{X}^T\mathbf{X}/\sigma^2\bigr)^{-1}, \quad \mathbf{m} = V\bigl(\Sigma_0^{-1}\boldsymbol{\beta}_0 + \mathbf{X}^T\mathbf{y}/\sigma^2\bigr), \\ 1/\sigma^2 \mid \mathbf{y}, \mathbf{X}, \boldsymbol{\beta} &\sim \operatorname{gamma}\!\left(\frac{\nu_0 + n}{2},\, \frac{\nu_0\sigma_0^2 + \operatorname{SSR}(\boldsymbol{\beta})}{2}\right). \end{aligned} (9.4)

Proof. Collecting the terms in 𝜷\boldsymbol{\beta} from prior and likelihood gives the exponent

−12𝜷T(Σ0−1+𝐗T𝐗/σ2)𝜷+𝜷T(Σ0−1𝜷0+𝐗T𝐲/σ2), -\frac{1}{2}\boldsymbol{\beta}^T(\Sigma_0^{-1} + \mathbf{X}^T\mathbf{X}/\sigma^2)\boldsymbol{\beta} + \boldsymbol{\beta}^T(\Sigma_0^{-1}\boldsymbol{\beta}_0 + \mathbf{X}^T\mathbf{y}/\sigma^2),

whose precision matrix and linear term are read off directly. With γ=1/σ2\gamma = 1/\sigma^2, the second conditional is proportional to γν0/2−1e−γν0σ02/2⋅γn/2e−γSSR⁡(𝜷)/2\gamma^{\nu_0/2 - 1}e^{-\gamma\nu_0\sigma_0^2/2} \cdot \gamma^{n/2}e^{-\gamma\operatorname{SSR}(\boldsymbol{\beta})/2}, a gamma kernel. □

Alternating these two draws is a complete Gibbs sampler. Precisions add and means average by precision, exactly as in one dimension: a vague prior (Σ0−1→0\Sigma_0^{-1} \to 0) returns 𝜷^ols\hat{\boldsymbol{\beta}}_{\text{ols}}, and a noisy experiment (σ2→∞\sigma^2 \to \infty) returns 𝜷0\boldsymbol{\beta}_0. The obstacle is not computation but elicitation: Σ0\Sigma_0 has pp variances and (p2)\binom{p}{2} correlations, and few of us hold opinions about the prior correlation between an intercept and an interaction.

9.3Default Priors: Unit Information and the gg-Prior

Two principles let the design matrix supply the prior covariance for us.

One observation’s worth of information. The data contribute precision 𝐗T𝐗/σ2\mathbf{X}^T\mathbf{X}/\sigma^2, the work of nn observations. A unit information priorRobert E. Kass and Larry Wasserman. A Reference Bayesian Test for Nested Hypotheses and Its Relationship to the Schwarz Criterion. Journal of the American Statistical Association, 90(431):928–934, 1995 therefore uses Σ0−1=𝐗T𝐗/(nσ2)\Sigma_0^{-1} = \mathbf{X}^T\mathbf{X}/(n\sigma^2). Kass and Wasserman also center it at 𝜷0=𝜷^ols\boldsymbol{\beta}_0 = \hat{\boldsymbol{\beta}}_{\text{ols}}, so it borrows a little from 𝐲\mathbf{y} and is not a prior in the strict sense; (ν0,σ02)=(1,σ^ols2)(\nu_0, \sigma_0^2) = (1, \hat{\sigma}^2_{\text{ols}}) plays the same role for the variance.

Invariance to units. Recording age in months rather than years should not change what we believe about the effect of aging. Suppose the regressors are transformed as 𝐗~=𝐗H\tilde{\mathbf{X}} = \mathbf{X}H with HH invertible. The same mean vector is 𝐗~𝜷~=𝐗H𝜷~\tilde{\mathbf{X}}\tilde{\boldsymbol{\beta}} = \mathbf{X}H\tilde{\boldsymbol{\beta}}, so coherent priors must satisfy 𝜷=dH𝜷~\boldsymbol{\beta} \stackrel{d}{=} H\tilde{\boldsymbol{\beta}}.

Proposition 9.2 (Invariance of the g-Prior). Let the prior rule assign 𝒩p(𝟎, k(𝐗T𝐗)−1)\mathcal{N}_p(\mathbf{0},\, k(\mathbf{X}^T\mathbf{X})^{-1}) to the coefficients of any full-rank design 𝐗\mathbf{X}, with k>0k > 0 not depending on the units. Then analysts using 𝐗\mathbf{X} and 𝐗~=𝐗H\tilde{\mathbf{X}} = \mathbf{X}H reach the same prior, and hence the same posterior, for 𝜷=H𝜷~\boldsymbol{\beta} = H\tilde{\boldsymbol{\beta}}.

Proof. The second analyst has Var⁡(𝜷~)=k(HT𝐗T𝐗H)−1=kH−1(𝐗T𝐗)−1H−T\operatorname{Var}(\tilde{\boldsymbol{\beta}}) = k(H^T\mathbf{X}^T\mathbf{X}H)^{-1} = kH^{-1}(\mathbf{X}^T\mathbf{X})^{-1}H^{-T}, so Var⁡(H𝜷~)=k(𝐗T𝐗)−1\operatorname{Var}(H\tilde{\boldsymbol{\beta}}) = k(\mathbf{X}^T\mathbf{X})^{-1}, and the mean is 𝟎\mathbf{0} in both cases. The likelihood depends on the coefficients only through 𝐗𝜷=𝐗~𝜷~\mathbf{X}\boldsymbol{\beta} = \tilde{\mathbf{X}}\tilde{\boldsymbol{\beta}}, so the posteriors agree as well. □

Choosing k=gσ2k = g\sigma^2 gives the zero-centered Zellner gg-prior:Arnold Zellner. On Assessing Prior Distributions and Bayesian Regression Analysis with gg-Prior Distributions. In Bayesian Inference and Decision Techniques, pages 233–243. North-Holland, Amsterdam, 1986

𝜷∣𝐗,σ2∼𝒩p(𝟎, gσ2(𝐗T𝐗)−1),1/σ2∼gamma⁡(ν0/2, ν0σ02/2), \boldsymbol{\beta} \mid \mathbf{X}, \sigma^2 \sim \mathcal{N}_p\bigl(\mathbf{0},\, g\sigma^2(\mathbf{X}^T\mathbf{X})^{-1}\bigr), \qquad 1/\sigma^2 \sim \operatorname{gamma}(\nu_0/2,\, \nu_0\sigma_0^2/2), (9.5)

with g=ng = n carrying one observation’s information. Scaling the prior by σ2\sigma^2 restores full conjugacy, so Gibbs sampling is unnecessary.

Theorem 9.3 (Exact Posterior under the g-Prior). Under (9.1) and (9.5), define

SSR⁡g=𝐲T(In−gg+1𝐗(𝐗T𝐗)−1𝐗T)𝐲=gg+1SSR⁡ols+1g+1𝐲T𝐲. \operatorname{SSR}_g = \mathbf{y}^T\Bigl(I_n - \frac{g}{g+1}\mathbf{X}(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\Bigr)\mathbf{y} = \frac{g}{g+1}\operatorname{SSR}_{\text{ols}} + \frac{1}{g+1}\mathbf{y}^T\mathbf{y}. (9.6)

Then

p(𝐲∣𝐗,σ2)=(2πσ2)−n/2(1+g)−p/2exp⁡{−SSR⁡g/(2σ2)}, p(\mathbf{y} \mid \mathbf{X}, \sigma^2) = (2\pi\sigma^2)^{-n/2}(1 + g)^{-p/2}\exp\{-\operatorname{SSR}_g/(2\sigma^2)\},

and

1/σ2∣𝐲,𝐗∼gamma⁡ ⁣(ν0+n2, ν0σ02+SSR⁡g2),𝜷∣𝐲,𝐗,σ2∼𝒩p ⁣(gg+1𝜷^ols,  gg+1σ2(𝐗T𝐗)−1). \begin{aligned} 1/\sigma^2 \mid \mathbf{y}, \mathbf{X} &\sim \operatorname{gamma}\!\left(\frac{\nu_0 + n}{2},\, \frac{\nu_0\sigma_0^2 + \operatorname{SSR}_g}{2}\right), \\ \boldsymbol{\beta} \mid \mathbf{y}, \mathbf{X}, \sigma^2 &\sim \mathcal{N}_p\!\left(\frac{g}{g+1}\hat{\boldsymbol{\beta}}_{\text{ols}},\; \frac{g}{g+1}\sigma^2(\mathbf{X}^T\mathbf{X})^{-1}\right). \end{aligned} (9.7)

Proof. Let 𝐦=gg+1𝜷^ols\mathbf{m} = \frac{g}{g+1}\hat{\boldsymbol{\beta}}_{\text{ols}}. Completing the square in the combined exponent,

(𝐲−𝐗𝜷)T(𝐲−𝐗𝜷)+1g𝜷T𝐗T𝐗𝜷=g+1g(𝜷−𝐦)T𝐗T𝐗(𝜷−𝐦)+SSR⁡g. (\mathbf{y} - \mathbf{X}\boldsymbol{\beta})^T(\mathbf{y} - \mathbf{X}\boldsymbol{\beta}) + \frac{1}{g}\boldsymbol{\beta}^T\mathbf{X}^T\mathbf{X}\boldsymbol{\beta} = \frac{g+1}{g}(\boldsymbol{\beta} - \mathbf{m})^T\mathbf{X}^T\mathbf{X}(\boldsymbol{\beta} - \mathbf{m}) + \operatorname{SSR}_g .

The first term gives the conditional distribution of 𝜷\boldsymbol{\beta}. Integrating it over 𝜷\boldsymbol{\beta} contributes (2πσ2)p/2(gg+1)p/2∣𝐗T𝐗∣−1/2(2\pi\sigma^2)^{p/2}\bigl(\frac{g}{g+1}\bigr)^{p/2}\lvert\mathbf{X}^T\mathbf{X}\rvert^{-1/2}, while the prior’s normalizing constant is (2πgσ2)−p/2∣𝐗T𝐗∣1/2(2\pi g\sigma^2)^{-p/2}\lvert\mathbf{X}^T\mathbf{X}\rvert^{1/2}; their product is (1+g)−p/2(1 + g)^{-p/2}. What remains, as a function of γ=1/σ2\gamma = 1/\sigma^2, is γn/2e−γSSR⁡g/2\gamma^{n/2}e^{-\gamma\operatorname{SSR}_g/2}, which multiplies the gamma⁡(ν0/2,ν0σ02/2)\operatorname{gamma}(\nu_0/2, \nu_0\sigma_0^2/2) prior to give the stated gamma posterior. □

The shrinkage factor is the same scalar g/(g+1)g/(g+1) in every direction, and SSR⁡g\operatorname{SSR}_g interpolates between the residual sum of squares and the total sum of squares.On p. 157 of Hoff (2009) the normalizing constant of the gg-prior is printed as ∣2πgσ2(𝐗T𝐗)−1∣−1\lvert 2\pi g\sigma^2(\mathbf{X}^T\mathbf{X})^{-1}\rvert^{-1}; the exponent should be −1/2-1/2, as the factor (1+g)−p/2(1+g)^{-p/2} on the next page requires. See Appendix C. To sample the joint posterior, draw σ2\sigma^2 and then 𝜷\boldsymbol{\beta} given σ2\sigma^2: every pair is an independent exact draw.

9.3.1Collinearity and the Right Contrast

For the oxygen data with g=n=12g = n = 12 and (ν0,σ02)=(1,8.54)(\nu_0, \sigma_0^2) = (1, 8.54), (9.6) gives SSR⁡g≈124.0\operatorname{SSR}_g \approx 124.0, the posterior mean is 1213𝜷^ols=(−47.35,12.10,1.93,−0.29)T\frac{12}{13}\hat{\boldsymbol{\beta}}_{\text{ols}} = (-47.35, 12.10, 1.93, -0.29)^T, and the exact posterior standard deviations are about (13.98,17.98,0.60,0.74)(13.98, 17.98, 0.60, 0.74).Marginally 𝜷\boldsymbol{\beta} is multivariate tt with ν0+n=13\nu_0 + n = 13 degrees of freedom, and E[σ2∣𝐲]=(ν0σ02+SSR⁡g)/(ν0+n−2)≈12.05E[\sigma^2 \mid \mathbf{y}] = (\nu_0\sigma_0^2 + \operatorname{SSR}_g)/(\nu_0 + n - 2) \approx 12.05. Hoff’s Monte Carlo values (14.41,18.62,0.62,0.77)(14.41, 18.62, 0.62, 0.77) come from 1,000 draws of a heavy-tailed quantity and agree within simulation error. Both marginal intervals for β2\beta_2 and β4\beta_4 comfortably contain zero.

But β2\beta_2 is the group difference at age zero, an extrapolation of more than two decades, and it is almost perfectly confounded with β4\beta_4: the posterior correlation is −0.993-0.993. The estimand a scientist cares about is the group contrast at an attainable age, δ(a)=β2+β4a\delta(a) = \beta_2 + \beta_4 a, and its posterior follows from the same joint draws. At age 24 its posterior mean is close to 5.05.0 with a standard deviation near 22, not 1818. The exact 95% intervals exclude zero at ages 22 through 25, where both groups contributed data, and widen on either side (Figure 9.1, right). Two coefficients that are individually uncertain can have a sharply determined linear combination; never judge a joint hypothesis from marginal intervals.

Three panels for the oxygen uptake data: scatter of uptake change against age with posterior mean lines for running and aerobics groups; scatter of joint posterior draws of beta2 and beta4 lying on a thin negatively sloped ridge; posterior medians and 95 percent intervals of beta2 plus beta4 times age for ages 20 to 31, narrowest near age 24
Figure 9.1: Oxygen uptake under the gg-prior with g=n=12g = n = 12 (exact Monte Carlo draws). Left: data and posterior mean lines for the two programs. Center: joint posterior draws of (β2,β4)(\beta_2, \beta_4) fall on a thin ridge with correlation −0.993-0.993. Right: posterior median and 95% interval of the contrast β2+β4 age\beta_2 + \beta_4\,\text{age}; evidence for a group effect is strongest at ages 22–25 even though neither coefficient is separately distinguishable from zero.

9.4Model Selection and the Backward-Elimination Trap

Hoff’s second example has 442 diabetes patients and p=64p = 64 candidate regressors (10 main effects, 45 interactions, 9 squares), split into 342 training and 100 test cases with all variables standardized. Full OLS attains test error 0.670.67, against 0.970.97 for predicting zero. Backward elimination, which repeatedly drops the regressor with the smallest ∣tj∣\lvert t_j \rvert until every survivor exceeds 1.651.65, keeps 20 variables and lowers the test error to 0.530.53. So far, so good.

Now permute 𝐲\mathbf{y} at random, so that no regressor has any true association with the response. Initially only one tt-statistic exceeds 1.651.65. Yet after elimination Hoff is left with 18 regressors, 17 of them with ∣t∣>2\lvert t \rvert > 2 and four with ∣t∣>3\lvert t \rvert > 3. The procedure manufactures significance: each deletion discards a noise direction whose estimate happened to be small, and the survivors are exactly those whose noise happened to be large. A tt-statistic computed after selection no longer has a tt distribution, and nothing in the output warns us.

9.5Spike-and-Slab: Models as Parameters

The Bayesian response is to place the question inside the model. Write βj=zjbj\beta_j = z_j b_j with zj∈{0,1}z_j \in \{0, 1\} and bj∈ℝb_j \in \mathbb{R}, so that each 𝐳∈{0,1}p\mathbf{z} \in \{0,1\}^p indexes one regression model. The induced prior on βj\beta_j has an atom at zero and a continuous part, a spike-and-slab prior; George and McCulloch pioneered Gibbs sampling over such indicators.Edward I. George and Robert E. McCulloch. Variable Selection via Gibbs Sampling. Journal of the American Statistical Association, 88:881–889, 1993Their original spike is a narrow normal rather than a point mass at zero. The point-mass version used here is Hoff’s, and it permits the exact integration below. Let 𝐗z\mathbf{X}_z be the n×pzn \times p_z matrix of included columns and apply the gg-prior to them: 𝜷z∣𝐗z,σ2∼𝒩pz(𝟎,gσ2(𝐗zT𝐗z)−1)\boldsymbol{\beta}_z \mid \mathbf{X}_z, \sigma^2 \sim \mathcal{N}_{p_z}(\mathbf{0}, g\sigma^2(\mathbf{X}_z^T\mathbf{X}_z)^{-1}).

Proposition 9.4 (Exact Marginal Likelihood of a Model). With 1/σ2∼gamma⁡(ν0/2, ν0σ02/2)1/\sigma^2 \sim \operatorname{gamma}(\nu_0/2,\, \nu_0\sigma_0^2/2) and SSR⁡gz\operatorname{SSR}_g^z as in (9.6) computed from 𝐗z\mathbf{X}_z (and SSR⁡gz=𝐲T𝐲\operatorname{SSR}_g^z = \mathbf{y}^T\mathbf{y} when pz=0p_z = 0),

p(𝐲∣𝐗,𝐳)=π−n/2 Γ(ν0+n2)Γ(ν02) (1+g)−pz/2 (ν0σ02)ν0/2(ν0σ02+SSR⁡gz)(ν0+n)/2. p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}) = \pi^{-n/2}\,\frac{\Gamma\bigl(\frac{\nu_0 + n}{2}\bigr)}{\Gamma\bigl(\frac{\nu_0}{2}\bigr)}\, (1 + g)^{-p_z/2}\, \frac{(\nu_0\sigma_0^2)^{\nu_0/2}}{(\nu_0\sigma_0^2 + \operatorname{SSR}_g^z)^{(\nu_0 + n)/2}}. (9.8)

Proof. Multiply p(𝐲∣𝐗,𝐳,γ)p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}, \gamma) from Theorem 9.3 by the gamma prior density and integrate over γ=1/σ2\gamma = 1/\sigma^2:

(1+g)−pz/2(2π)n/2 (ν0σ02/2)ν0/2Γ(ν0/2)∫0∞γν0+n2−1e−γ(ν0σ02+SSR⁡gz)/2 dγ=(1+g)−pz/2(2π)n/2 (ν0σ02/2)ν0/2Γ(ν0/2) Γ(ν0+n2)(ν0σ02+SSR⁡gz2)(ν0+n)/2, \frac{(1+g)^{-p_z/2}}{(2\pi)^{n/2}}\,\frac{(\nu_0\sigma_0^2/2)^{\nu_0/2}}{\Gamma(\nu_0/2)} \int_0^\infty \gamma^{\frac{\nu_0 + n}{2} - 1} e^{-\gamma(\nu_0\sigma_0^2 + \operatorname{SSR}_g^z)/2}\,d\gamma = \frac{(1+g)^{-p_z/2}}{(2\pi)^{n/2}}\,\frac{(\nu_0\sigma_0^2/2)^{\nu_0/2}}{\Gamma(\nu_0/2)}\, \frac{\Gamma\bigl(\frac{\nu_0+n}{2}\bigr)}{\bigl(\frac{\nu_0\sigma_0^2 + \operatorname{SSR}_g^z}{2}\bigr)^{(\nu_0+n)/2}},

and the powers of 22 combine to 2n/22^{n/2}, turning (2π)−n/2(2\pi)^{-n/2} into π−n/2\pi^{-n/2}. □

Two details deserve care. First, the exponent of the bracket is exactly (ν0+n)/2(\nu_0 + n)/2.The intermediate gamma-normalizer on p. 165 of Hoff (2009) is printed with exponent (ν0+n)/2−1(\nu_0 + n)/2 - 1 (and SSR⁡gz\operatorname{SSR}_g^z there lacks a transpose on its last 𝐗z\mathbf{X}_z); the final formula on the same page is correct. See Appendix C. Second, keep the factor (ν0σ02)ν0/2(\nu_0\sigma_0^2)^{\nu_0/2}. It cancels from ratios only when σ02\sigma_0^2 is the same for all models. Under Hoff’s model-specific choice ν0=1\nu_0 = 1, σ02=sz2\sigma_0^2 = s_z^2 (the OLS residual variance of model 𝐳\mathbf{z}), g=ng = n, it survives, and the Bayes factor is

p(𝐲∣𝐗,𝐳a)p(𝐲∣𝐗,𝐳b)=(1+n)(pzb−pza)/2(sza2szb2)1/2(szb2+SSR⁡gzbsza2+SSR⁡gza)(n+1)/2. \frac{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_a)}{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_b)} = (1 + n)^{(p_{z_b} - p_{z_a})/2} \left(\frac{s_{z_a}^2}{s_{z_b}^2}\right)^{1/2} \left(\frac{s_{z_b}^2 + \operatorname{SSR}_g^{z_b}}{s_{z_a}^2 + \operatorname{SSR}_g^{z_a}}\right)^{(n+1)/2}. (9.9)

The first factor charges about 12log⁡(1+n)\frac{1}{2}\log(1+n) per extra regressor, the same penalty rate as BIC; the last rewards fit. No test and no stopping rule is involved.

Table 9.1: Oxygen uptake: marginal likelihoods and posterior probabilities of five models under equal prior weights (Hoff 2009, Table 9.1).
𝐳\mathbf{z}Model for E[Y∣𝐱]E[Y \mid \mathbf{x}]log⁡p(𝐲∣𝐗,𝐳)\log p(\mathbf{y} \mid \mathbf{X}, \mathbf{z})p(𝐳∣𝐲,𝐗)p(\mathbf{z} \mid \mathbf{y}, \mathbf{X})
(1,0,0,0)(1,0,0,0)constant−44.33-44.330.000.00
(1,1,0,0)(1,1,0,0)group−42.35-42.350.000.00
(1,0,1,0)(1,0,1,0)age−37.66-37.660.180.18
(1,1,1,0)(1,1,1,0)group + age (parallel lines)−36.42-36.420.630.63
(1,1,1,1)(1,1,1,1)group + age + interaction−37.60-37.600.190.19

Age is essentially certain to matter, and parallel lines beat the interaction model by a factor of e1.18≈3.3e^{1.18} \approx 3.3. The three models containing a group effect total 0.820.82, up from a prior 0.600.60: modest evidence, honestly reported (Table 9.1).

Remark 9.5 (Two paradoxes, and what uniform model priors really say). Hold σ02\sigma_0^2 common to all models.

  1. Lindley’s paradox. For fixed data, the Bayes factor of a larger model against a nested one is O((1+g)−(pb−pa)/2)O\bigl((1+g)^{-(p_b - p_a)/2}\bigr) as g→∞g \to \infty. A “noninformative” slab is therefore maximally informative: it forces the smaller model. Diffuse proper priors are harmless for estimation but not for comparison, and improper ones leave the Bayes factor undefined.
  2. The information paradox. For fixed gg, SSR⁡gz≥𝐲T𝐲/(1+g)\operatorname{SSR}_g^z \ge \mathbf{y}^T\mathbf{y}/(1+g), so the Bayes factor of 𝐳\mathbf{z} against the empty model never exceeds (1+g)(ν0+n−pz)/2(1+g)^{(\nu_0 + n - p_z)/2}, no matter how perfectly the model fits. Placing a prior on gg can remove this defect; Liang et al. study such mixtures of gg-priors and their consistency.Feng Liang, Rui Paulo, German Molina, Merlise A. Clyde, and James O. Berger. Mixtures of gg Priors for Bayesian Variable Selection. Journal of the American Statistical Association, 103(481):410–423, 2008
  3. Multiplicity. Setting p(𝐳)=2−pp(\mathbf{z}) = 2^{-p} is a binomial prior on model size: for p=64p = 64 it expects 32±432 \pm 4 active regressors. Letting a common inclusion probability be uniform⁡(0,1)\operatorname{uniform}(0,1) instead gives p(𝐳)=[(p+1)(ppz)]−1p(\mathbf{z}) = \bigl[(p+1)\binom{p}{p_z}\bigr]^{-1}, which penalizes the number of opportunities for a false discovery automatically.James G. Scott and James O. Berger. Bayes and Empirical-Bayes Multiplicity Adjustment in the Variable-Selection Problem. The Annals of Statistics, 38(5):2587–2619, 2010 The factor (1+g)−pz/2(1+g)^{-p_z/2} penalizes complexity within a model; it is not a correction for how many models were tried.

9.6Collapsed Gibbs Sampling and Model Averaging

With p=64p = 64 there are 264≈1.8×10192^{64} \approx 1.8 \times 10^{19} models. But (9.8) evaluates any one of them in closed form, so we can run a Markov chain on 𝐳\mathbf{z} alone with 𝜷\boldsymbol{\beta} and σ2\sigma^2 integrated out, a collapsed Gibbs sampler.

Algorithm 9.6 (Collapsed Gibbs Sampler for Variable Selection). Given 𝐳(s)\mathbf{z}^{(s)}, set 𝐳=𝐳(s)\mathbf{z} = \mathbf{z}^{(s)} and proceed as follows.

  1. For each j∈{1,…,p}j \in \{1, \dots, p\} in random order, compute the conditional odds

    oj=Pr⁡(zj=1∣𝐳−j)Pr⁡(zj=0∣𝐳−j)×p(𝐲∣𝐗,𝐳−j,zj=1)p(𝐲∣𝐗,𝐳−j,zj=0) o_j = \frac{\Pr(z_j = 1 \mid \mathbf{z}_{-j})}{\Pr(z_j = 0 \mid \mathbf{z}_{-j})} \times \frac{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_{-j}, z_j = 1)}{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_{-j}, z_j = 0)} (9.10)

    and set zj=1z_j = 1 with probability oj/(1+oj)o_j / (1 + o_j), otherwise zj=0z_j = 0.

  2. Set 𝐳(s+1)=𝐳\mathbf{z}^{(s+1)} = \mathbf{z} and draw 1/σ2(s+1)1/\sigma^{2(s+1)} from (9.7) using 𝐗z\mathbf{X}_z.

  3. Draw 𝜷z(s+1)\boldsymbol{\beta}_z^{(s+1)} from (9.7) and set the excluded coefficients to 00.

The draws of (σ2,𝜷)(\sigma^2, \boldsymbol{\beta}) never feed back into the chain: they are ancestral samples given 𝐳\mathbf{z}, so only 𝐳\mathbf{z} needs to mix. (A sampler that alternated zjz_j with βj\beta_j would be stuck, since βj≠0\beta_j \neq 0 forces zj=1z_j = 1.) Averaging 𝜷(s)\boldsymbol{\beta}^{(s)} over the chain gives the Bayesian model averaged estimateDavid Madigan and Adrian E. Raftery. Model Selection and Accounting for Model Uncertainty in Graphical Models Using Occam’s Window. Journal of the American Statistical Association, 89:1535–1546, 1994

𝜷^bma=1S∑s=1S𝜷(s)≈E[𝜷∣𝐲,𝐗]=∑𝐳p(𝐳∣𝐲,𝐗) E[𝜷∣𝐲,𝐗,𝐳]. \hat{\boldsymbol{\beta}}_{\text{bma}} = \frac{1}{S}\sum_{s=1}^S \boldsymbol{\beta}^{(s)} \approx E[\boldsymbol{\beta} \mid \mathbf{y}, \mathbf{X}] = \sum_{\mathbf{z}} p(\mathbf{z} \mid \mathbf{y}, \mathbf{X})\,E[\boldsymbol{\beta} \mid \mathbf{y}, \mathbf{X}, \mathbf{z}]. (9.11)

On the diabetes data, 10,00010{,}000 scans find six regressors with inclusion probability above 1/21/2, and the model-averaged test error is 0.4520.452, better than full OLS (0.670.67) and backward elimination (0.530.53). On the permuted response, every inclusion probability is below 1/21/2 and all but two are below 1/41/4: the trap does not spring. A sobering count accompanies this success. Only 32 models were visited more than once, so the chain estimates one-dimensional margins of p(𝐳∣𝐲,𝐗)p(\mathbf{z} \mid \mathbf{y}, \mathbf{X}), not the probabilities of individual models.

Remark 9.7 (An inclusion probability is not an effect size). The posterior inclusion probability Pr⁡(zj=1∣𝐲,𝐗)\Pr(z_j = 1 \mid \mathbf{y}, \mathbf{X}) measures evidence that βj≠0\beta_j \neq 0 within the considered family and given the slab width gg. A negligible effect measured on a million cases can have probability near 11; two nearly duplicate regressors can share one strong effect and each show a probability near 1/21/2. Report it beside the posterior of βj\beta_j itself, and inspect joint inclusion for correlated regressors.

import itertools
import numpy as np
from scipy.special import gammaln

rng = np.random.default_rng(9)
age = np.array([23, 22, 22, 25, 27, 20, 31, 23, 27, 28, 22, 24], dtype=float)
grp = np.repeat([0.0, 1.0], 6)                       # 0 = running, 1 = aerobics
y = np.array([-0.87, -10.74, -3.27, -1.97, 7.50, -7.25,
              17.05, 4.96, 10.40, 11.05, 0.26, 2.51])
X = np.column_stack([np.ones(12), grp, age, grp * age])
n, p = X.shape
g, nu0 = float(n), 1.0

def summaries(X):
    """Return OLS coefficients, residual variance, and SSR_g for design X."""
    if X.shape[1] == 0:
        return np.zeros(0), y @ y / n, y @ y
    b = np.linalg.solve(X.T @ X, X.T @ y)
    fit = y @ X @ b                                  # y' H y
    return b, (y @ y - fit) / (n - X.shape[1]), y @ y - g / (g + 1) * fit

# 1. Exact i.i.d. posterior sampling under the g-prior (no Markov chain)
b_ols, s20, ssr_g = summaries(X)
S = 20_000
sig2 = 1.0 / rng.gamma((nu0 + n) / 2, 2.0 / (nu0 * s20 + ssr_g), size=S)  # scale = 1/rate
L = np.linalg.cholesky(g / (g + 1) * np.linalg.inv(X.T @ X))
beta = g / (g + 1) * b_ols + np.sqrt(sig2)[:, None] * (rng.normal(size=(S, p)) @ L.T)
print("posterior mean", beta.mean(0).round(2), " corr(b2,b4) =",
      np.corrcoef(beta[:, 1], beta[:, 3])[0, 1].round(3))
for a in (20, 24, 30):
    print(f"contrast at age {a}:", np.quantile(beta[:, 1] + beta[:, 3] * a, [.025, .5, .975]).round(2))

# 2. Log marginal likelihood, retaining (nu0 * s20)^(nu0/2) since s20 varies by model
def log_marg(z):
    _, s2, ssr = summaries(X[:, z])
    return (-0.5 * n * np.log(np.pi) - 0.5 * z.sum() * np.log1p(g)
            + 0.5 * nu0 * np.log(nu0 * s2) - 0.5 * (nu0 + n) * np.log(nu0 * s2 + ssr)
            + gammaln((nu0 + n) / 2) - gammaln(nu0 / 2))

# 3. Collapsed Gibbs over all 2^4 models (uniform prior), checked by enumeration
z = np.ones(p, dtype=bool)
lp, pip, bma = log_marg(z), np.zeros(p), np.zeros(p)
for s in range(5_000):
    for j in rng.permutation(p):
        zp = z.copy()
        zp[j] = not zp[j]
        lpp = log_marg(zp)
        if np.log(rng.uniform()) < -np.logaddexp(0.0, lp - lpp):  # Pr(flip) = 1/(1+e^(lp-lpp))
            z, lp = zp, lpp
    pip += z / 5_000
    bma[z] += g / (g + 1) * summaries(X[:, z])[0] / 5_000         # Rao-Blackwellized mean
models = [np.array(m, dtype=bool) for m in itertools.product([0, 1], repeat=p)]
lps = np.array([log_marg(m) for m in models])
w = np.exp(lps - lps.max())
w /= w.sum()
print("inclusion (Gibbs):", pip.round(3), " (exact):", (w @ np.array(models)).round(3))
print("model-averaged beta:", bma.round(2))

9.7Modern Developments: Shrinkage, Correlated Selection, and Stacking

9.7.1The Horseshoe and Regularized Horseshoe Priors

The gg-prior shrinks every coefficient by the same factor g/(g+1)g/(g+1) in (9.7), whereas a global–local prior gives each coefficient a shrinkage factor of its own.Beyond Hoff (2009). This material is not in Hoff’s book; the horseshoe of Carvalho, Polson, and Scott (2010) and its regularized form (Piironen and Vehtari, 2017) offer a continuous alternative to the spike-and-slab of Section 9.5. The horseshoeC. M. Carvalho, N. G. Polson, and J. G. Scott. The horseshoe estimator for sparse signals. Biometrika, 97(2):465–480, 2010. doi: 10.1093/biomet/asq017 takes half-Cauchy scales; for an orthogonal design with 𝐗T𝐗=nI\mathbf{X}^T\mathbf{X} = nI,

βj∣λj,τ,σ2∼normal⁡(0, σ2τ2λj2),λj∼i.i.d.C+(0,1),τ∼C+(0,τ0),E[βj∣𝐲,λj,τ]=(1−κj) β^j,ols,κj=11+nτ2λj2. \begin{aligned} \beta_j \mid \lambda_j, \tau, \sigma^2 &\sim \operatorname{normal}(0,\, \sigma^2\tau^2\lambda_j^2), \qquad \lambda_j \stackrel{\text{i.i.d.}}{\sim} C^+(0,1), \quad \tau \sim C^+(0, \tau_0), \\ E[\beta_j \mid \mathbf{y}, \lambda_j, \tau] &= (1 - \kappa_j)\,\hat{\beta}_{j,\text{ols}}, \qquad \kappa_j = \frac{1}{1 + n\tau^2\lambda_j^2}. \end{aligned} (9.12)

Why it holds. Each coordinate is a normal update with data precision n/σ2n/\sigma^2 and prior precision 1/(σ2τ2λj2)1/(\sigma^2\tau^2\lambda_j^2), and κj\kappa_j is the prior’s share. At nτ2=1n\tau^2 = 1 the half-Cauchy makes κj∼beta⁡(12,12)\kappa_j \sim \operatorname{beta}(\frac{1}{2}, \frac{1}{2}): mass piles up at 00 (keep) and 11 (discard). Writing each λj2\lambda_j^2 as a mixture of inverse-gammas restores Gibbs sampling,Enes Makalic and Daniel F. Schmidt. A Simple Sampler for the Horseshoe Estimator. IEEE Signal Processing Letters, 23(1):179–182, 2016. doi: 10.1109/LSP.2015.2503725 and 𝜷\boldsymbol{\beta} can be drawn in O(n2p)O(n^2p) operations.Anirban Bhattacharya, Antik Chakraborty, and Bani K. Mallick. Fast sampling with Gaussian scale mixture priors in high-dimensional regression. Biometrika, 103(4):985–991, 2016. doi: 10.1093/biomet/asw042 A guess of p0p_0 relevant regressors suggests τ0=p0/{(p−p0)n}\tau_0 = p_0/\{(p - p_0)\sqrt{n}\}, and replacing λj2\lambda_j^2 by c2λj2/(c2+τ2λj2)c^2\lambda_j^2/(c^2 + \tau^2\lambda_j^2) caps the slab at width cσc\sigma, the regularized horseshoe.Juho Piironen and Aki Vehtari. Sparsity information and regularization in the horseshoe and other shrinkage priors. Electronic Journal of Statistics, 11(2):5018–5051, 2017. doi: 10.1214/17-EJS1337SI Beware the Bayesian lasso, with its double-exponential prior of rate ρ\rho: its mode (the lassoRobert Tibshirani. Regression Shrinkage and Selection via the Lasso. Journal of the Royal Statistical Society, Series B, 58(1):267–288, 1996) is sparse, but its full posterior is not. In the sequence model (𝐗=In\mathbf{X} = I_n, σ2=1\sigma^2 = 1) with true 𝜷=𝟎\boldsymbol{\beta} = \mathbf{0} and ρ=2log⁡n\rho = \sqrt{2\log n}, each coordinate keeps posterior spread of order 1/ρ1/\rho, so ∥𝜷∥\lVert\boldsymbol{\beta}\rVert is of order n/log⁡n\sqrt{n/\log n} while the mode’s error vanishes.Ismaël Castillo, Johannes Schmidt-Hieber, and Aad van der Vaart. Bayesian linear regression with sparse priors. The Annals of Statistics, 43(5):1986–2018, 2015. doi: 10.1214/15-AOS1334

When to use it. Prefer the horseshoe for prediction or estimation with pp large or comparable to nn and no need for exact zeros. Keep Hoff’s Algorithm 9.6 when model probabilities are themselves the question.

9.7.2Sum of Single Effects (SuSiE)

Remark 9.7 warned that two nearly duplicate regressors share one strong effect, each showing an inclusion probability near 1/21/2; SuSiE turns that warning into a reportable answer, the set of regressors that holds the effect.Beyond Hoff (2009). This material is not in Hoff’s book; the sum-of-single-effects model is due to Wang, Sarkar, Carbonetto, and Stephens (2020) and complements per-variable inclusion probabilities. It writes 𝜷\boldsymbol{\beta} as LL vectors with one nonzero entry each:Gao Wang, Abhishek Sarkar, Peter Carbonetto, and Matthew Stephens. A Simple New Approach to Variable Selection in Regression, with Application to Genetic Fine Mapping. Journal of the Royal Statistical Society Series B: Statistical Methodology, 82(5):1273–1300, 2020. doi: 10.1111/rssb.12388

𝜷=∑l=1Lbl 𝜸l,𝜸l∼multinomial⁡(1,𝝅),bl∼normal⁡(0,σ0l2),αlj=πjBF⁡lj∑kπkBF⁡lk,BF⁡lj=sj2sj2+σ0l2 exp⁡ ⁣{b^lj22sj2⋅σ0l2sj2+σ0l2}, \begin{aligned} \boldsymbol{\beta} &= \sum_{l=1}^L b_l\,\boldsymbol{\gamma}_l, \qquad \boldsymbol{\gamma}_l \sim \operatorname{multinomial}(1, \boldsymbol{\pi}), \quad b_l \sim \operatorname{normal}(0, \sigma_{0l}^2), \\ \alpha_{lj} &= \frac{\pi_j\operatorname{BF}_{lj}}{\sum_k \pi_k\operatorname{BF}_{lk}}, \qquad \operatorname{BF}_{lj} = \sqrt{\frac{s_j^2}{s_j^2 + \sigma_{0l}^2}}\, \exp\!\left\{\frac{\hat{b}_{lj}^2}{2s_j^2}\cdot\frac{\sigma_{0l}^2}{s_j^2 + \sigma_{0l}^2}\right\}, \end{aligned} (9.13)

with b^lj=𝐱jT𝐫l/𝐱jT𝐱j\hat{b}_{lj} = \mathbf{x}_j^T\mathbf{r}_l/\mathbf{x}_j^T\mathbf{x}_j, sj2=σ2/𝐱jT𝐱js_j^2 = \sigma^2/\mathbf{x}_j^T\mathbf{x}_j, and 𝐫l\mathbf{r}_l the residual after subtracting the other L−1L - 1 expected effects.

Why it holds. With a single effect there are only pp models, so Bayes’ rule enumerates them exactly, and BF⁡lj\operatorname{BF}_{lj} is a ratio of normal marginals. Cycling over ll (iterative Bayesian stepwise selection) is coordinate ascent on a variational objective in which the LL effects are independent but the choice within each is not. If 𝐱1=𝐱2\mathbf{x}_1 = \mathbf{x}_2 and π1=π2\pi_1 = \pi_2, then BF⁡l1=BF⁡l2\operatorname{BF}_{l1} = \operatorname{BF}_{l2}. When they carry a strong effect, 𝜶l≈(12,12,0,… )\boldsymbol{\alpha}_l \approx (\frac{1}{2}, \frac{1}{2}, 0, \dots) and the 95% credible set is {1,2}\{1, 2\}: we are sure there is an effect, and unsure which column carries it.

When to use it. Choose SuSiE for sparse problems with strongly correlated regressors, such as genetic fine mapping. Because it is variational, dependence between effects is lost and local optima are possible; for moderate pp, or dense signals, Algorithm 9.6 targets the exact posterior.

9.7.3Bayesian Stacking in ℳ\mathcal{M}-Open Settings

The average (9.11) weights models by p(𝐳∣𝐲,𝐗)p(\mathbf{z} \mid \mathbf{y}, \mathbf{X}), the right weights when one of the candidates is true (ℳ\mathcal{M}-closed) but not when all are wrong (ℳ\mathcal{M}-open).Beyond Hoff (2009). This material is not in Hoff’s book; stacking is a much older idea for point predictions, extended to full predictive distributions by Yao, Vehtari, Simpson, and Gelman (2018) as an alternative to model averaging. As nn grows, model averaging typically puts all its weight on the single model closest to the truth in Kullback–Leibler divergence, even if a mixture predicts better. Stacking chooses the mixture directly by its leave-one-out log score:Yuling Yao, Aki Vehtari, Daniel Simpson, and Andrew Gelman. Using Stacking to Average Bayesian Predictive Distributions (with Discussion). Bayesian Analysis, 13(3):917–1007, 2018. doi: 10.1214/17-BA1091

𝐰^=arg max⁡wk≥0,  ∑kwk=1  1n∑i=1nlog⁡∑k=1Kwk p(yi∣𝐲−i,ℳk). \hat{\mathbf{w}} = \operatorname*{arg\,max}_{w_k \ge 0,\; \sum_k w_k = 1}\; \frac{1}{n}\sum_{i=1}^n \log \sum_{k=1}^K w_k\, p(y_i \mid \mathbf{y}_{-i}, \mathcal{M}_k). (9.14)

Why it holds. Each held-out term estimates the mixture’s expected log score on new data without using yiy_i twice, and the objective is concave on the simplex. Under the gg-prior, the leave-one-out terms need no refitting. Hold the prior, including σ02\sigma_0^2, fixed and let A=In−gg+1𝐗z(𝐗zT𝐗z)−1𝐗zTA = I_n - \frac{g}{g+1}\mathbf{X}_z(\mathbf{X}_z^T\mathbf{X}_z)^{-1}\mathbf{X}_z^T be the matrix in (9.6). Marginally 𝐲\mathbf{y} is multivariate tt with scale matrix σ02A−1\sigma_0^2A^{-1}, so

yi∣𝐲−i,𝐳∼tν0+n−1 ⁣(yi−(A𝐲)iAii,  ν0σ02+SSR⁡gz−(A𝐲)i2/AiiAii (ν0+n−1)), y_i \mid \mathbf{y}_{-i}, \mathbf{z} \sim t_{\nu_0 + n - 1}\!\left(y_i - \frac{(A\mathbf{y})_i}{A_{ii}},\; \frac{\nu_0\sigma_0^2 + \operatorname{SSR}_g^z - (A\mathbf{y})_i^2/A_{ii}}{A_{ii}\,(\nu_0 + n - 1)}\right), (9.15)

with location and squared scale; the location does not depend on yiy_i, which cancels. The choice σ02=sz2\sigma_0^2 = s_z^2 behind Table 9.1 uses all of 𝐲\mathbf{y}, so it would have to be recomputed, or fixed, for an honest hold-out score.

When to use it. Stack when the goal is prediction and no model is believed. Keep Hoff’s averaging when a true model is plausible or the posterior probabilities answer the scientific question: stacking weights are not probabilities, and near-identical models split theirs arbitrarily.

9.7.4Computational & Symbolic Check

The script compares draws of the shrinkage factor in (9.12) with beta⁡(12,12)\operatorname{beta}(\frac{1}{2}, \frac{1}{2}), and evaluates the single-effect probabilities of (9.13) for two identical regressors and one unrelated regressor.Beyond Hoff (2009). This computational check and its Wolfram symbolic counterpart below verify the modern methods in this section and are not in Hoff’s book.

import numpy as np
from scipy import stats

rng = np.random.default_rng(91)
# 1. Horseshoe: lambda ~ C+(0,1) with n tau^2 = 1 gives kappa ~ Beta(1/2, 1/2)
kappa = 1.0 / (1.0 + rng.standard_cauchy(100_000) ** 2)
print("KS distance:", round(stats.kstest(kappa, stats.beta(0.5, 0.5).cdf).statistic, 4),
      " mean, var:", kappa.mean().round(3), kappa.var().round(3), "(exact 0.5, 0.125)")
# 2. SuSiE single effect: columns 1 and 2 are identical, column 3 is noise
n, sig2, v0 = 200, 1.0, 1.0
x = rng.normal(size=n)
X = np.column_stack([x, x, rng.normal(size=n)])
y = 0.8 * x + rng.normal(size=n)
xx = (X ** 2).sum(axis=0)
bhat, s2 = X.T @ y / xx, sig2 / xx
lbf = 0.5 * np.log(s2 / (s2 + v0)) + bhat ** 2 / (2 * s2) * v0 / (s2 + v0)
alpha = np.exp(lbf - lbf.max())
print("PIPs:", (alpha / alpha.sum()).round(3), "(exact 1/2, 1/2, ~0)")
Invert κ=1/(1+λ2)\kappa = 1/(1+\lambda^2) on λ>0\lambda > 0: the local scale as a function of the shrinkage factor.
// Horseshoe: the half-Cauchy prior on lambda induces kappa = 1/(1 + lambda^2) ~ Beta(1/2, 1/2)
const lamK = await show`Sqrt[1 - k]/Sqrt[k]`
λ(κ)=1−κκ \lambda(\kappa) = \frac{\sqrt{1-\kappa}}{\sqrt{\kappa}} Change variables in the half-Cauchy density 2/(π(1+λ2))2/(\pi(1+\lambda^2)) from λ\lambda to κ\kappa (a decreasing map, hence the minus sign).
const densK = await show`Assuming[0 < k < 1, FullSimplify[-D[${lamK}, k] * 2/(Pi*(1 + (${lamK})^2))]]`
pκ(κ)=pλ(λ(κ)) ∣dλdκ∣=1π(1−κ)κ p_\kappa(\kappa) = p_\lambda(\lambda(\kappa))\,\left\lvert\frac{d\lambda}{d\kappa}\right\rvert = \frac{1}{\pi \sqrt{(1-\kappa) \kappa}} Divide the induced density by the Beta⁡(12,12)\operatorname{Beta}(\tfrac12, \tfrac12) density: the ratio is identically 11.
await show`Assuming[0 < k < 1, FullSimplify[(${densK}) / (k^(-1/2)*(1 - k)^(-1/2)/Beta[1/2, 1/2])]]`
pλ(λ(κ)) ∣dλ/dκ∣κ−1/2(1−κ)−1/2/B ⁣(12,12)=1 \begin{aligned} &\frac{p_\lambda(\lambda(\kappa))\,\lvert d\lambda/d\kappa\rvert}{\kappa^{-1/2}(1-\kappa)^{-1/2}/B\!\left(\frac{1}{2}, \frac{1}{2}\right)} \\ &= 1 \end{aligned} Integrate the induced density to get 𝔼[κ]\mathbb{E}[\kappa] and Var⁡[κ]\operatorname{Var}[\kappa], the exact values printed by the script.
await show`{Integrate[k*(${densK}), {k, 0, 1}], Integrate[(k - 1/2)^2*(${densK}), {k, 0, 1}]}`
(𝔼[κ],  Var⁡[κ])={12,18} \bigl(\mathbb{E}[\kappa],\; \operatorname{Var}[\kappa]\bigr) = \left\{\frac{1}{2},\frac{1}{8}\right\} Integrate the effect b∼𝒩(0,σ02)b \sim \mathcal{N}(0, \sigma_0^2) out of the normal likelihood of b^\hat{b} and divide by its value at b=0b = 0.
// SuSiE: Bayes factor as a ratio of normal marginals, then PIPs of two duplicate regressors
const bfOne = await show`Assuming[s2 > 0 && v > 0 && Element[bh, Reals], FullSimplify[Exp[bh^2/(2*s2)]
  *Integrate[Exp[-(bh - b)^2/(2*s2)]*Exp[-b^2/(2*v)]/Sqrt[2*Pi*v], {b, -Infinity, Infinity}]]]`
BF⁡lj=∫p(b^∣b) p(b) dbp(b^∣b=0)=eb^2σ022s2(s2+σ02)s2s2+σ02 \operatorname{BF}_{lj} = \frac{\int p(\hat{b} \mid b)\,p(b)\,db}{p(\hat{b} \mid b = 0)} = e^{\frac{\hat{b}^2 \sigma_0^2}{2 s^2 (s^2+\sigma_0^2)}} \sqrt{\frac{s^2}{s^2+\sigma_0^2}} Divide the integrated Bayes factor by the closed form of BF⁡lj\operatorname{BF}_{lj} stated above.
await show`Assuming[s2 > 0 && v > 0 && Element[bh, Reals],
  FullSimplify[(${bfOne}) / (Sqrt[s2/(s2 + v)]*Exp[bh^2/(2*s2)*v/(s2 + v)])]]`
∫p(b^∣b) p(b) db / p(b^∣b=0)s2s2+σ02 exp⁡ ⁣{b^22s2⋅σ02s2+σ02}=1 \frac{\int p(\hat{b} \mid b)\,p(b)\,db \,\big/\, p(\hat{b} \mid b = 0)}{\sqrt{\frac{s^2}{s^2+\sigma_0^2}}\,\exp\!\left\{\frac{\hat{b}^2}{2s^2}\cdot\frac{\sigma_0^2}{s^2+\sigma_0^2}\right\}} = 1 Duplicate columns have the same Bayes factor, so it cancels; equal prior weights leave α1=α2=12\alpha_1 = \alpha_2 = \tfrac12.
await show`FullSimplify[{w1*(${bfOne}), w2*(${bfOne})} / (w1*(${bfOne}) + w2*(${bfOne}))] /. w2 -> w1`
(α1,α2)∣𝐱1=𝐱2,  π1=π2={12,12} (\alpha_1, \alpha_2)\big|_{\mathbf{x}_1 = \mathbf{x}_2,\; \pi_1 = \pi_2} = \left\{\frac{1}{2},\frac{1}{2}\right\}

9.8Symbolic Check in Wolfram Language

Integrates γ=1/σ2\gamma = 1/\sigma^2 symbolically in wolfram-ws-bridge JavaScript mode to derive p(𝐲∣𝐗,𝐳)p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}) and the general symbolic Bayes factor BF⁡a,b\operatorname{BF}_{a,b} (retaining model-specific sa2,sb2s_a^2, s_b^2), verifies both the Lindley limit (g→∞g \to \infty) and the information-paradox supremum, and instantiates margZ directly to reproduce Table 9.1.

Integrate precision γ=1/σ2\gamma = 1/\sigma^2 out of the gg-prior joint model to derive the closed-form marginal likelihood p(𝐲∣𝐗,𝐳)p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}).
// Chapter 9: Symbolic g-prior marginal likelihood, general Bayes factor & exact Table 9.1
const margZ = await show`Assuming[nu0 > 0 && s20 > 0 && ssr > 0 && g > 0 && n > 0,
  FullSimplify[Integrate[(1 + g)^(-pz/2)*(2*Pi)^(-n/2)*((nu0*s20/2)^(nu0/2)/Gamma[nu0/2])
    *gam^((nu0 + n)/2 - 1)*Exp[-gam*(nu0*s20 + ssr)/2], {gam, 0, Infinity}]]]`
p(𝐲∣𝐗,𝐳)=(1+g)−pz/2π−n/2(ν0σ02)ν0/2(ν0σ02+SSR⁡g)12(−n−ν0)Γ(n+ν02)Γ(ν02) \begin{aligned} &p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}) \\ &= \frac{(1+g)^{-p_z /2} \pi^{-n/2} (\nu_0 \sigma_0^2)^{\nu_0 /2} (\nu_0 \sigma_0^2 +\operatorname{SSR}_g)^{\frac{1}{2} (-n-\nu_0)} \Gamma \left(\frac{n+\nu_0}{2}\right)}{\Gamma \left(\frac{\nu_0}{2}\right)} \end{aligned} Take the ratio of two marginal likelihoods to obtain the general symbolic Bayes factor BF⁡a,b\operatorname{BF}_{a,b}.
// General symbolic Bayes factor BF_{a,b} retaining model-specific s20_a, s20_b
await show`Assuming[nu0 > 0 && sa > 0 && sb > 0 && ssra > 0 && ssrb > 0 && g > 0 && n > 0,
  FullSimplify[(${margZ} /. {pz -> pa, s20 -> sa, ssr -> ssra}) / (${margZ} /. {pz -> pb, s20 -> sb, ssr -> ssrb})]]`
BF⁡a,b=p(𝐲∣𝐗,𝐳a)p(𝐲∣𝐗,𝐳b)=(1+g)12(−pa+pb)(σ0,a2σ0,b2)ν0/2(ν0σ0,a2+SSR⁡aν0σ0,b2+SSR⁡b)12(−n−ν0) \begin{aligned} &\operatorname{BF}_{a,b} = \frac{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_a)}{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_b)} \\ &= (1+g)^{\frac{1}{2} (-p_a +p_b)} \left(\frac{\sigma_{0,a}^2}{\sigma_{0,b}^2}\right)^{\nu_0 /2} \left(\frac{\nu_0 \sigma_{0,a}^2 +\operatorname{SSR}_a}{\nu_0 \sigma_{0,b}^2 +\operatorname{SSR}_b}\right)^{\frac{1}{2} (-n-\nu_0)} \end{aligned} Divide the marginal of a p1p_1-regressor model, SSR⁡g=gg+1r1+1g+1𝐲T𝐲\operatorname{SSR}_g = \tfrac{g}{g+1} r_1 + \tfrac{1}{g+1}\mathbf{y}^T\mathbf{y}, by that of the null model (pz=0p_z = 0, SSR⁡g=𝐲T𝐲\operatorname{SSR}_g = \mathbf{y}^T\mathbf{y}).
const bf = await show`FullSimplify[
  (${margZ} /. {pz -> p1, ssr -> (g/(g+1))*r1 + (1/(g+1))*yy}) / (${margZ} /. {pz -> 0, ssr -> yy})]`
BF⁡10(g)=p(𝐲∣𝐗,𝐳1)p(𝐲∣𝐗,𝐳0)=(1+g)−p1/2(ν0σ02+g r1+𝐲T𝐲1+gν0σ02+𝐲T𝐲)−n+ν02 \begin{aligned} &\operatorname{BF}_{10}(g) = \frac{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_1)}{p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}_0)} \\ &= (1+g)^{-p_1/2} \left(\frac{\nu_0 \sigma_0^2 + \frac{g\, r_1 + \mathbf{y}^T\mathbf{y}}{1+g}}{\nu_0 \sigma_0^2 + \mathbf{y}^T\mathbf{y}}\right)^{-\frac{n+\nu_0}{2}} \end{aligned} Take g→∞g \to \infty to prove Lindley’s paradox: diffuse gg-priors force BF⁡10→0\operatorname{BF}_{10} \to 0 in favor of the null.
await show`Assuming[p1 > 0 && nu0 > 0 && s20 > 0 && yy > 0 && r1 > 0 && n > 0, Limit[${bf}, g -> Infinity]]`
lim⁡g→∞BF⁡10=0 \lim_{g \to \infty} \operatorname{BF}_{10} = 0 Take r1→0r_1 \to 0 at σ02=0\sigma_0^2 = 0 to derive the information-paradox upper bound (1+g)(n+ν0−p1)/2(1 + g)^{(n + \nu_0 - p_1)/2}.
await show`Assuming[p1 > 0 && g > 0 && nu0 > 0 && yy > 0 && n > 0,
  FullSimplify[Limit[${bf} /. s20 -> 0, r1 -> 0]]]`
lim⁡r1→0BF⁡10∣σ02=0=(1+g)12(n+ν0−p1) \begin{aligned} &\lim_{r_1 \to 0}\left.\operatorname{BF}_{10}\right|_{\sigma_0^2=0} \\ &= (1+g)^{\frac{1}{2} (n+\nu_0 -p_1)} \end{aligned} Write the OLS estimate 𝜷^ols\hat{\boldsymbol{\beta}}_{\text{ols}} as a symbolic function of a design 𝐗z\mathbf{X}_z and the response 𝐲\mathbf{y}.
// Instantiate margZ directly on Hoff's Oxygen uptake Table 9.1 (g = n = 12, nu0 = 1, s20 = s_z^2)
const age = [23, 22, 22, 25, 27, 20, 31, 23, 27, 28, 22, 24]
const grp = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
const y = [-0.87, -10.74, -3.27, -1.97, 7.50, -7.25, 17.05, 4.96, 10.40, 11.05, 0.26, 2.51]
const Xfull = age.map((a, i) => [1, grp[i], a, grp[i] * a])
const olsB = await show`Function[{xm, yv}, LinearSolve[Transpose[xm].xm, Transpose[xm].yv]]`
𝜷^ols(𝐗z,𝐲)=(𝐗zT𝐗z)−1𝐗zT𝐲 \hat{\boldsymbol{\beta}}_{\text{ols}}(\mathbf{X}_z, \mathbf{y}) = (\mathbf{X}_z^T\mathbf{X}_z)^{-1}\mathbf{X}_z^T\mathbf{y} Write the residual variance sz2s_z^2 used as the prior guess σ02\sigma_0^2, with pzp_z the number of fitted coefficients.
const s2Fn = await show`Function[{xm, yv, bv}, (yv - xm.bv).(yv - xm.bv) / (Length[yv] - Length[bv])]`
sz2(𝐗z,𝐲,𝜷^)=(𝐲−𝐗z𝜷^)T(𝐲−𝐗z𝜷^)n−pz s_z^2(\mathbf{X}_z, \mathbf{y}, \hat{\boldsymbol{\beta}}) = \frac{(\mathbf{y} - \mathbf{X}_z\hat{\boldsymbol{\beta}})^T(\mathbf{y} - \mathbf{X}_z\hat{\boldsymbol{\beta}})}{n - p_z} Write the gg-prior residual sum of squares SSR⁡g\operatorname{SSR}_g in terms of the OLS fit.
const ssrFn = await show`Function[{xm, yv, bv, g}, yv.yv - (g/(g + 1))*(yv.xm.bv)]`
SSR⁡g(𝐗z,𝐲,𝜷^)=𝐲T𝐲−gg+1 𝐲T𝐗z𝜷^ \operatorname{SSR}_g(\mathbf{X}_z, \mathbf{y}, \hat{\boldsymbol{\beta}}) = \mathbf{y}^T\mathbf{y} - \frac{g}{g+1}\,\mathbf{y}^T\mathbf{X}_z\hat{\boldsymbol{\beta}} Instantiate log⁡p(𝐲∣𝐗,𝐳)\log p(\mathbf{y} \mid \mathbf{X}, \mathbf{z}) on all five models of Hoff’s Oxygen Uptake Table 9.1, plugging in sz2s_z^2 and SSR⁡g\operatorname{SSR}_g for each column subset.
const logMargs = await show`N[Table[With[{xz = ${Xfull}[[All, cols]]}, With[{bh = ${olsB}[xz, ${y}]},
  Log[${margZ} /. {n -> 12, g -> 12, nu0 -> 1, pz -> Length[cols],
    s20 -> ${s2Fn}[xz, ${y}, bh], ssr -> ${ssrFn}[xz, ${y}, bh, 12]}]]],
  {cols, {{1}, {1, 2}, {1, 3}, {1, 2, 3}, {1, 2, 3, 4}}}]]`
(log⁡p(𝐲∣𝐗,𝐳))Table 9.1={−44.3327,−42.3533,−37.6577,−36.4173,−37.5986} \begin{aligned} &\bigl(\log p(\mathbf{y} \mid \mathbf{X}, \mathbf{z})\bigr)_{\text{Table 9.1}} \\ &= \{-44.3327,-42.3533,-37.6577,-36.4173,-37.5986\} \end{aligned} Normalize the log marginal likelihoods via softmax to reproduce the exact posterior model probabilities.
await show`N[Exp[${logMargs} - Max[${logMargs}]] / Total[Exp[${logMargs} - Max[${logMargs}]]]]`
(p(𝐳∣𝐲,𝐗))Table 9.1={0.000228291,0.00165244,0.180895,0.625313,0.191911} \begin{aligned} &\bigl(p(\mathbf{z} \mid \mathbf{y}, \mathbf{X})\bigr)_{\text{Table 9.1}} \\ &= \{0.000228291,0.00165244,0.180895,0.625313,0.191911\} \end{aligned}

9.9Exercises

Exercise 9.8. Integrate σ2\sigma^2 out of (9.7) to show that any contrast 𝐜T𝜷\mathbf{c}^T\boldsymbol{\beta} has a Student-tt posterior with ν0+n\nu_0 + n degrees of freedom, location gg+1𝐜T𝜷^ols\frac{g}{g+1}\mathbf{c}^T\hat{\boldsymbol{\beta}}_{\text{ols}}, and squared scale gg+1 𝐜T(𝐗T𝐗)−1𝐜 (ν0σ02+SSR⁡g)/(ν0+n)\frac{g}{g+1}\,\mathbf{c}^T(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{c}\,(\nu_0\sigma_0^2 + \operatorname{SSR}_g)/(\nu_0 + n). Use it to reproduce the claim that the 95% interval for β2+β4 a\beta_2 + \beta_4\,a excludes zero at ages a=22,…,25a = 22, \dots, 25 only.

Symbolic Solution & InstantiationWrite the squared Student-tt marginal scale of a linear contrast cTβc^T \beta under the gg-prior.
// Ex 9.1: Exact Student-t marginal CI for oxygen contrast beta2 + beta4*age across ages 20..31
const age = [23, 22, 22, 25, 27, 20, 31, 23, 27, 28, 22, 24]
const grp = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
const y = [-0.87, -10.74, -3.27, -1.97, 7.50, -7.25, 17.05, 4.96, 10.40, 11.05, 0.26, 2.51]
const X = age.map((a, i) => [1, grp[i], a, grp[i] * a])
const scale2 = await show`(g/(g + 1))*cXtXinvC * (nu0*s20 + ssrG) / (nu0 + n)`
Scale⁡2 ⁣(c⊤β∣y)=(c⊤(X⊤X)−1c)g(ν0σ02+SSR⁡g)(1+g)(n+ν0) \begin{aligned} &\operatorname{Scale}^2\!\left(c^\top \beta \mid y\right) \\ &= \frac{(c^\top (X^\top X)^{-1} c) g (\nu_0 \sigma_0^2 +\operatorname{SSR}_g)}{(1+g) (n+\nu_0)} \end{aligned} Write the OLS estimate as a symbolic function of the design and response.
const olsB = await show`Function[{xm, yv}, LinearSolve[Transpose[xm].xm, Transpose[xm].yv]]`
β^ols(X,y)=(X⊤X)−1X⊤y \hat{\beta}_{\text{ols}}(X, y) = (X^\top X)^{-1} X^\top y Write the residual variance s2s^2, the prior guess σ02\sigma_0^2 (here n−p=8n - p = 8).
const s2Fn = await show`Function[{xm, yv, bv}, (yv - xm.bv).(yv - xm.bv) / (Length[yv] - Length[bv])]`
s2(X,y,β^)=(y−Xβ^)⊤(y−Xβ^)n−p s^2(X, y, \hat{\beta}) = \frac{(y - X\hat{\beta})^\top (y - X\hat{\beta})}{n - p} Write the gg-prior residual sum of squares SSR⁡g\operatorname{SSR}_g in terms of the OLS fit.
const ssrFn = await show`Function[{xm, yv, bv, g}, yv.yv - (g/(g + 1))*(yv.xm.bv)]`
SSR⁡g(X,y,β^)=y⊤y−gg+1 y⊤Xβ^ \operatorname{SSR}_g(X, y, \hat{\beta}) = y^\top y - \frac{g}{g+1}\, y^\top X \hat{\beta} Solve Pr⁡(∣T∣≤t)=0.95\Pr(\lvert T \rvert \le t) = 0.95 for T∼t13T \sim t_{13}, via the regularized incomplete beta function, to get the two-sided 95%95\% quantile.
const t13 = await show`t /. FindRoot[1 - Beta[13/(13 + t^2), 13/2, 1/2]/Beta[13/2, 1/2] == 95/100, {t, 2.16}]`
t13, 0.975=2.16037 t_{13,\,0.975} = 2.16037 Filter ages 20,…,3120, \dots, 31 where the exact 95%95\% t13t_{13} credible interval for Aerobics minus Running lies strictly above 00, plugging s2s^2, SSR⁡g\operatorname{SSR}_g and c⊤(X⊤X)−1cc^\top (X^\top X)^{-1} c into the scale.
await show`With[{bh = ${olsB}[${X}, ${y}]},
  Select[Range[20, 31], (12/13)*({0, 1, 0, #}.bh) - ${t13}*Sqrt[${scale2} /. {g -> 12, n -> 12, nu0 -> 1,
    cXtXinvC -> {0, 1, 0, #}.Inverse[Transpose[${X}].${X}].{0, 1, 0, #},
    s20 -> ${s2Fn}[${X}, ${y}, bh], ssrG -> ${ssrFn}[${X}, ${y}, bh, 12]}] > 0 &]]`
{age∈[20,31]:95% exact t13 CI>0}={22,23,24,25} \bigl\{\text{age} \in [20, 31] : 95\%\text{ exact } t_{13}\text{ CI} > 0\bigr\} = \{22,23,24,25\}

Exercise 9.9. The zero-centered gg-prior also shrinks the intercept. Show that the fitted values are gg+1\frac{g}{g+1} times the OLS fitted values, so that adding a constant to every yiy_i changes the posterior of the slopes’ variance (through 𝐲T𝐲\mathbf{y}^T\mathbf{y}) although it should be irrelevant. Propose a version with a flat prior on the intercept and centered regressors, and explain why a common improper prior on the intercept does not harm Bayes factors while one on the slopes would.

Symbolic Solution & InstantiationCompute SSR⁡g(y+c𝟏)−SSR⁡g(y)\operatorname{SSR}_g(y + c\mathbf{1}) - \operatorname{SSR}_g(y) symbolically to expose shift non-invariance under a zero-centered gg-prior.
// Ex 9.2: Shift non-invariance of zero-centered g-prior under y -> y + c*1
const shiftDiff = await show`FullSimplify[((g/(g + 1))*ssrOls + (1/(g + 1))*(yy + 2*c*n*yb + n*c^2)) - ((g/(g + 1))*ssrOls + (1/(g + 1))*yy)]`
SSR⁡g(y+c𝟏)−SSR⁡g(y)=cn(c+2y¯)1+g \operatorname{SSR}_g(y + c\mathbf{1}) - \operatorname{SSR}_g(y) = \frac{c n (c+2 \bar{y})}{1+g} Instantiate the shift inflation as an exact fraction and decimal at n=g=12,y¯=0,c=10n = g = 12, \bar{y} = 0, c = 10.
await show`{${shiftDiff}, N[${shiftDiff}]} /. {n -> 12, g -> 12, yb -> 0, c -> 10}`
(ΔSSR⁡g,exact,  ΔSSR⁡g,decimal)n=12,  g=12,  y¯=0,  c=10={120013,92.3077} \begin{aligned} &\bigl(\Delta\operatorname{SSR}_{g,\text{exact}},\; \Delta\operatorname{SSR}_{g,\text{decimal}}\bigr)_{n=12,\; g=12,\; \bar{y}=0,\; c=10} \\ &= \left\{\frac{1200}{13},92.3077\right\} \end{aligned}

Exercise 9.10. Verify both halves of Remark 9.5. Derive the bound (1+g)(ν0+n−pz)/2(1+g)^{(\nu_0+n-p_z)/2}, and plot the log Bayes factor of (1,1,1,0)(1,1,1,0) against (1,0,1,0)(1,0,1,0) for the oxygen data (common σ02=8.54\sigma_0^2 = 8.54) as gg ranges from 11 to 10610^6. At what gg does the preferred model switch?

Symbolic Solution & InstantiationWrite the Bayes factor BF⁡A,B(g)\operatorname{BF}_{A, B}(g) comparing (group + age) against (age only) as a function of gg.
// Ex 9.3: Exact switch point g* where Lindley's paradox flips A=(1,1,1,0) vs B=(1,0,1,0) on oxygen data (nu0 = 1)
const bfG = await show`(1 + g)^(-1/2) * ((s20 + yy - (g/(g + 1))*fB) / (s20 + yy - (g/(g + 1))*fA))^((n + 1)/2)`
BF⁡A,B(g)=(−FBg1+g+σ02+y⊤y−FAg1+g+σ02+y⊤y)1+n21+g \begin{aligned} &\operatorname{BF}_{A, B}(g) \\ &= \frac{\left(\frac{-\frac{F_B g}{1+g}+\sigma_0^2 +y^\top y}{-\frac{F_A g}{1+g}+\sigma_0^2 +y^\top y}\right)^{\frac{1+n}{2}}}{\sqrt{1+g}} \end{aligned} Write the fitted sum Fz=y⊤Xzβ^olsF_z = y^\top X_z \hat{\beta}_{\text{ols}} of a model as a symbolic function of its design XzX_z and the response.
const age = [23, 22, 22, 25, 27, 20, 31, 23, 27, 28, 22, 24]
const grp = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
const y = [-0.87, -10.74, -3.27, -1.97, 7.50, -7.25, 17.05, 4.96, 10.40, 11.05, 0.26, 2.51]
const Xfull = age.map((a, i) => [1, grp[i], a, grp[i] * a])
const fitSum = await show`Function[{xm, yv}, yv.xm.LinearSolve[Transpose[xm].xm, Transpose[xm].yv]]`
F(Xz,y)=y⊤Xz(Xz⊤Xz)−1Xz⊤y F(X_z, y) = y^\top X_z (X_z^\top X_z)^{-1} X_z^\top y Instantiate y⊤yy^\top y, FAF_A (columns 1,2,31, 2, 3) and FBF_B (columns 1,31, 3) on the oxygen data and solve BF⁡A,B(g∗)=1\operatorname{BF}_{A, B}(g^*) = 1 numerically to locate the exact Lindley-paradox crossover g∗g^*.
await show`g /. FindRoot[(${bfG} /. {s20 -> 8.54, yy -> N[${y}.${y}], n -> 12,
  fA -> N[${fitSum}[${Xfull}[[All, {1, 2, 3}]], ${y}]], fB -> N[${fitSum}[${Xfull}[[All, {1, 3}]], ${y}]]}) == 1, {g, 200}]`
g∗∣BF⁡A,B(g∗)=1=4423.36 g^*\big|_{\operatorname{BF}_{A,B}(g^*)=1} = 4423.36

Exercise 9.11. Simulate 𝐗\mathbf{X} with n=342n = 342, p=64p = 64 independent standard normal columns and an independent response 𝐲\mathbf{y}. Apply backward elimination with cutoff 1.651.65 and record the number of survivors; then run Algorithm 9.6 under both the uniform prior and the prior [(p+1)(ppz)]−1\bigl[(p+1)\binom{p}{p_z}\bigr]^{-1}. (For the latter, show that the prior odds in (9.10) equal (k+1)/(p−k)(k+1)/(p-k) when kk other regressors are included.) Compare the three answers.

Symbolic Solution & InstantiationSimplify the Beta–Binomial multiplicity-adjusting prior odds for adding one predictor to a model of size kk.
// Ex 9.4: Symbolic prior odds under Beta-Binomial multiplicity-adjusting prior
const odds = await show`FullSimplify[FunctionExpand[(1/((p + 1)*Binomial[p, k + 1])) / (1/((p + 1)*Binomial[p, k]))]]`
Pr⁡(zj=1∣z−j)Pr⁡(zj=0∣z−j)=1+k−k+p \frac{\Pr(z_j = 1 \mid z_{-j})}{\Pr(z_j = 0 \mid z_{-j})} = \frac{1+k}{-k+p} Evaluate the prior odds at p=64p = 64 for k=0k = 0 (1/641/64) and k=5k = 5 (6/596/59), contrasting with uniform odds 11.
await show`{${odds} /. {p -> 64, k -> 0}, ${odds} /. {p -> 64, k -> 5}}`
(oddsk=0,  oddsk=5)p=64={164,659} \begin{aligned} &\bigl(\text{odds}_{k=0},\; \text{odds}_{k=5}\bigr)_{p=64} \\ &= \left\{\frac{1}{64},\frac{6}{59}\right\} \end{aligned}