Chapter 5

The Normal Model

The normal distribution is the workhorse of continuous-data modeling—not because real-world populations are literally Gaussian, but because the Central Limit Theorem pushes averages toward normality and because a normal model depends only on first and second moments.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 Chapter 5 marks a decisive step up in complexity: for the first time we have two unknown parameters, the population mean θ∈ℝ\theta \in \mathbb{R} and variance σ2>0\sigma^2 > 0, forcing us to confront joint priors, marginalization, and the exact trade-off between bias and variance.

5.1Properties and Joint Sufficient Statistics

A random variable Y∼normal⁡(θ,σ2)Y \sim \operatorname{normal}(\theta, \sigma^2) has density

p(y∣θ,σ2)=12πσ2exp⁡{−(y−θ)22σ2},y∈ℝ. p(y \mid \theta, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left\{-\frac{(y - \theta)^2}{2\sigma^2}\right\}, \qquad y \in \mathbb{R}. (5.1)

For an i.i.d. sample Y1,…,Yn∣θ,σ2∼i.i.d.normal⁡(θ,σ2)Y_1, \dots, Y_n \mid \theta, \sigma^2 \stackrel{\text{i.i.d.}}{\sim} \operatorname{normal}(\theta, \sigma^2), expanding the sum of squared deviations around the sample mean y¯=1n∑i=1nyi\bar{y} = \frac{1}{n}\sum_{i=1}^n y_i yields the algebraic workhorse identity that powers Chapters 5 through 9:

∑i=1n(yi−θ)2=∑i=1n(yi−y¯+y¯−θ)2=(n−1)s2+n(y¯−θ)2, \sum_{i=1}^n (y_i - \theta)^2 = \sum_{i=1}^n (y_i - \bar{y} + \bar{y} - \theta)^2 = (n - 1)s^2 + n(\bar{y} - \theta)^2, (5.2)

where s2=1n−1∑i=1n(yi−y¯)2s^2 = \frac{1}{n-1}\sum_{i=1}^n (y_i - \bar{y})^2 is the unbiased sample variance (the cross term 2(y¯−θ)∑i=1n(yi−y¯)2(\bar{y} - \theta)\sum_{i=1}^n (y_i - \bar{y}) vanishes identically). Substituting (5.2) into the joint likelihood gives

p(y1,…,yn∣θ,σ2)=(2πσ2)−n/2exp⁡{−12σ2[(n−1)s2+n(y¯−θ)2]}. p(y_1, \dots, y_n \mid \theta, \sigma^2) = (2\pi\sigma^2)^{-n/2} \exp\left\{-\frac{1}{2\sigma^2}\bigl[(n - 1)s^2 + n(\bar{y} - \theta)^2\bigr]\right\}. (5.3)

By the Fisher–Neyman factorization theorem, (y¯,s2)(\bar{y}, s^2) is a jointly sufficient statistic for (θ,σ2)(\theta, \sigma^2): the entire dataset of size nn compresses without loss into two numbers.

5.2Conditional Inference for the Mean (σ2\sigma^2 Known)

Temporarily suppose σ2\sigma^2 is known. Because the likelihood (5.3) is exponential-quadratic in θ\theta, a normal prior θ∼normal⁡(μ0,τ02)\theta \sim \operatorname{normal}(\mu_0, \tau_0^2) is conjugate. Writing precisions as σ~2=1/σ2\tilde{\sigma}^2 = 1/\sigma^2 and τ~02=1/τ02\tilde{\tau}_0^2 = 1/\tau_0^2, the log-posterior collects into a quadratic in θ\theta:

log⁡p(θ∣y1:n,σ2)=−12[τ~02(θ−μ0)2+nσ~2(θ−y¯)2]+const=−12τ~n2(θ−μn)2+const. \log p(\theta \mid y_{1:n}, \sigma^2) = -\frac{1}{2}\Bigl[\tilde{\tau}_0^2(\theta - \mu_0)^2 + n\tilde{\sigma}^2(\theta - \bar{y})^2\Bigr] + \text{const} = -\frac{1}{2}\tilde{\tau}_n^2(\theta - \mu_n)^2 + \text{const}. (5.4)

Matching the coefficients of θ2\theta^2 and θ\theta (completing the square; see Appendix B) yields the additive precision rule:

Proposition 5.1 (Normal–Normal Conditional Update). If θ∼normal⁡(μ0,τ02)\theta \sim \operatorname{normal}(\mu_0, \tau_0^2) and Y1,…,Yn∣θ,σ2∼i.i.d.normal⁡(θ,σ2)Y_1, \dots, Y_n \mid \theta, \sigma^2 \stackrel{\text{i.i.d.}}{\sim} \operatorname{normal}(\theta, \sigma^2) with σ2\sigma^2 known, then θ∣y1:n,σ2∼normal⁡(μn,τn2)\theta \mid y_{1:n}, \sigma^2 \sim \operatorname{normal}(\mu_n, \tau_n^2), where

τ~n2=1τn2=1τ02+nσ2,μn=1τ02μ0+nσ2y¯1τ02+nσ2. \tilde{\tau}_n^2 = \frac{1}{\tau_n^2} = \frac{1}{\tau_0^2} + \frac{n}{\sigma^2}, \qquad \mu_n = \frac{\frac{1}{\tau_0^2}\mu_0 + \frac{n}{\sigma^2}\bar{y}}{\frac{1}{\tau_0^2} + \frac{n}{\sigma^2}}. (5.5)

Moreover, a future observation Y~\tilde{Y} has posterior predictive distribution Y~∣y1:n,σ2∼normal⁡(μn, σ2+τn2)\tilde{Y} \mid y_{1:n}, \sigma^2 \sim \operatorname{normal}(\mu_n,\, \sigma^2 + \tau_n^2).

Look closely at (5.5):

  • Precisions add: posterior precision equals prior precision (1/τ021/\tau_0^2) plus data precision (n/σ2n/\sigma^2).

  • Means average by precision: μn\mu_n is a convex combination of μ0\mu_0 and y¯\bar{y}, weighted by their respective precisions. Setting κ0=σ2/τ02\kappa_0 = \sigma^2 / \tau_0^2 expresses the prior precision as κ0\kappa_0 equivalent observations, giving

    μn=κ0κ0+nμ0+nκ0+ny¯. \mu_n = \frac{\kappa_0}{\kappa_0 + n}\mu_0 + \frac{n}{\kappa_0 + n}\bar{y}.
  • Predictive variances add: writing Y~=θ+ϵ~\tilde{Y} = \theta + \tilde{\epsilon} with ϵ~∼normal⁡(0,σ2)\tilde{\epsilon} \sim \operatorname{normal}(0, \sigma^2) independent of θ∣y1:n\theta \mid y_{1:n}, we immediately get Var⁡(Y~∣y1:n,σ2)=τn2+σ2\operatorname{Var}(\tilde{Y} \mid y_{1:n}, \sigma^2) = \tau_n^2 + \sigma^2—again separating parameter uncertainty from irreducible sampling variability.

5.3Joint Inference for Mean and Variance: The Conjugate Normal–Inverse-Gamma Model

When both θ\theta and σ2\sigma^2 are unknown, any joint prior factors via the chain rule as p(θ,σ2)=p(θ∣σ2) p(σ2)p(\theta, \sigma^2) = p(\theta \mid \sigma^2)\,p(\sigma^2). To keep the joint posterior in closed form, we tie the prior variance of θ\theta proportionally to σ2\sigma^2 through a prior sample size κ0>0\kappa_0 > 0:

θ∣σ2∼normal⁡ ⁣(μ0, σ2κ0),1σ2∼gamma⁡ ⁣(ν02, ν0σ022), \theta \mid \sigma^2 \sim \operatorname{normal}\!\left(\mu_0,\, \frac{\sigma^2}{\kappa_0}\right), \qquad \frac{1}{\sigma^2} \sim \operatorname{gamma}\!\left(\frac{\nu_0}{2},\, \frac{\nu_0 \sigma_0^2}{2}\right), (5.6)

equivalently σ2∼inverse-gamma⁡(ν0/2, ν0σ02/2)\sigma^2 \sim \operatorname{inverse-gamma}(\nu_0/2,\, \nu_0\sigma_0^2/2), where σ02\sigma_0^2 is a prior guess for σ2\sigma^2 and ν0>0\nu_0 > 0 is its prior degrees of freedom (effective sample size).

Theorem 5.2 (Joint Conjugate Normal–Inverse-Gamma Posterior and Student-t Marginal). Under the conjugate prior (5.6) and i.i.d. normal sampling model (5.3), define κn=κ0+n\kappa_n = \kappa_0 + n, νn=ν0+n\nu_n = \nu_0 + n, and

μn=κ0μ0+ny¯κn,νnσn2=ν0σ02+(n−1)s2+κ0nκ0+n(y¯−μ0)2. \mu_n = \frac{\kappa_0 \mu_0 + n \bar{y}}{\kappa_n}, \qquad \nu_n \sigma_n^2 = \nu_0 \sigma_0^2 + (n - 1)s^2 + \frac{\kappa_0 n}{\kappa_0 + n}(\bar{y} - \mu_0)^2. (5.7)

Then the joint posterior p(θ,σ2∣y1:n)=p(θ∣σ2,y1:n) p(σ2∣y1:n)p(\theta, \sigma^2 \mid y_{1:n}) = p(\theta \mid \sigma^2, y_{1:n})\,p(\sigma^2 \mid y_{1:n}) is again Normal–Inverse-Gamma:

θ∣σ2,y1:n∼normal⁡ ⁣(μn, σ2κn),1σ2∣y1:n∼gamma⁡ ⁣(νn2, νnσn22). \theta \mid \sigma^2, y_{1:n} \sim \operatorname{normal}\!\left(\mu_n,\, \frac{\sigma^2}{\kappa_n}\right), \qquad \frac{1}{\sigma^2} \mid y_{1:n} \sim \operatorname{gamma}\!\left(\frac{\nu_n}{2},\, \frac{\nu_n \sigma_n^2}{2}\right). (5.8)

Integrating out σ2\sigma^2 yields an exact location–scale Student-tt marginal posterior for θ\theta:

θ−μnσn/κn  ∣  y1:n  ∼  tνn, \frac{\theta - \mu_n}{\sigma_n / \sqrt{\kappa_n}} \;\Bigm|\; y_{1:n} \;\sim\; t_{\nu_n}, (5.9)

and the posterior predictive distribution for a future observation Y~\tilde{Y} is (Y~−μn)/(σn1+1/κn)∣y1:n∼tνn(\tilde{Y} - \mu_n) / \bigl(\sigma_n\sqrt{1 + 1/\kappa_n}\bigr) \mid y_{1:n} \sim t_{\nu_n}.

Proof. First, substitute τ02=σ2/κ0\tau_0^2 = \sigma^2/\kappa_0 into Proposition 5.1 to obtain θ∣σ2,y1:n∼normal⁡(μn,σ2/κn)\theta \mid \sigma^2, y_{1:n} \sim \operatorname{normal}(\mu_n, \sigma^2/\kappa_n) immediately. Next, to find p(σ2∣y1:n)p(\sigma^2 \mid y_{1:n}), combine the prior quadratic κ0(θ−μ0)2\kappa_0(\theta - \mu_0)^2 and data quadratic n(θ−y¯)2n(\theta - \bar{y})^2 using the algebraic identity

κ0(θ−μ0)2+n(θ−y¯)2=κn(θ−μn)2+κ0nκ0+n(y¯−μ0)2. \kappa_0(\theta - \mu_0)^2 + n(\theta - \bar{y})^2 = \kappa_n(\theta - \mu_n)^2 + \frac{\kappa_0 n}{\kappa_0 + n}(\bar{y} - \mu_0)^2.

Now multiply p(θ,σ2)p(\theta, \sigma^2) by p(y1:n∣θ,σ2)p(y_{1:n} \mid \theta, \sigma^2) and integrate over θ∈ℝ\theta \in \mathbb{R}. The θ\theta-integral

∫exp⁡{−κn2σ2(θ−μn)2} dθ=2πσ2/κn \int \exp\bigl\{-\frac{\kappa_n}{2\sigma^2}(\theta - \mu_n)^2\bigr\}\,d\theta = \sqrt{2\pi\sigma^2/\kappa_n}

cancels the (σ2)−1/2(\sigma^2)^{-1/2} factor from p(θ∣σ2)p(\theta \mid \sigma^2), leaving

p(σ2∣y1:n)∝(σ2)−ν0+n2−1exp⁡{−ν0σ02+(n−1)s2+κ0nκ0+n(y¯−μ0)22σ2}, p(\sigma^2 \mid y_{1:n}) \propto (\sigma^2)^{-\frac{\nu_0 + n}{2} - 1} \exp\left\{-\frac{\nu_0 \sigma_0^2 + (n - 1)s^2 + \frac{\kappa_0 n}{\kappa_0 + n}(\bar{y} - \mu_0)^2}{2\sigma^2}\right\},

which is the exact kernel of inverse-gamma⁡(νn/2, νnσn2/2)\operatorname{inverse-gamma}(\nu_n/2,\, \nu_n\sigma_n^2/2). Finally, to obtain p(θ∣y1:n)p(\theta \mid y_{1:n}), integrate the joint posterior over precision σ~2=1/σ2∈(0,∞)\tilde{\sigma}^2 = 1/\sigma^2 \in (0, \infty):

p(θ∣y1:n)∝∫0∞(σ~2)1/2e−κn(θ−μn)22σ~2(σ~2)νn2−1e−νnσn22σ~2 dσ~2=∫0∞(σ~2)νn+12−1exp⁡{−νnσn2+κn(θ−μn)22 σ~2}dσ~2∝[1+1νn(θ−μnσn/κn)2]−νn+12, \begin{aligned} p(\theta \mid y_{1:n}) &\propto \int_0^\infty (\tilde{\sigma}^2)^{1/2} e^{-\frac{\kappa_n(\theta - \mu_n)^2}{2}\tilde{\sigma}^2} (\tilde{\sigma}^2)^{\frac{\nu_n}{2} - 1} e^{-\frac{\nu_n \sigma_n^2}{2}\tilde{\sigma}^2}\,d\tilde{\sigma}^2 \\ &= \int_0^\infty (\tilde{\sigma}^2)^{\frac{\nu_n + 1}{2} - 1} \exp\left\{-\frac{\nu_n \sigma_n^2 + \kappa_n(\theta - \mu_n)^2}{2}\,\tilde{\sigma}^2\right\} d\tilde{\sigma}^2 \propto \left[1 + \frac{1}{\nu_n}\left(\frac{\theta - \mu_n}{\sigma_n / \sqrt{\kappa_n}}\right)^2\right]^{-\frac{\nu_n + 1}{2}}, \end{aligned}

which is precisely the density kernel of a Student-tt distribution with νn\nu_n degrees of freedom, location μn\mu_n, and scale σn/κn\sigma_n/\sqrt{\kappa_n}. □

Look at the third term inside νnσn2\nu_n \sigma_n^2 in (5.7): κ0nκ0+n(y¯−μ0)2\frac{\kappa_0 n}{\kappa_0 + n}(\bar{y} - \mu_0)^2. When the observed mean y¯\bar{y} disagrees sharply with the prior mean μ0\mu_0, the posterior variance σn2\sigma_n^2 automatically inflates to reconcile the discrepancy! Notice also the subtle improper reference limit: letting κ0,ν0→0+\kappa_0, \nu_0 \to 0^+ corresponds to p(θ,σ2)∝(σ2)−3/2p(\theta, \sigma^2) \propto (\sigma^2)^{-3/2} and gives a tnt_n posterior with σn2=(n−1)s2/n\sigma_n^2 = (n - 1)s^2/n. The standard reference prior p(θ,σ2)∝1/σ2p(\theta, \sigma^2) \propto 1/\sigma^2 corresponds formally to κ0→0\kappa_0 \to 0, ν0=−1\nu_0 = -1, and ν0σ02→0\nu_0\sigma_0^2 \to 0, yielding νn=n−1\nu_n = n - 1 and σn2=s2\sigma_n^2 = s^2, so (θ−y¯)/(s/n)∣y1:n∼tn−1(\theta - \bar{y})/(s/\sqrt{n}) \mid y_{1:n} \sim t_{n-1}—matching the classical Student-tt interval while endowing it with a genuine posterior probability interpretation!

5.3.1Example: Midge Wing Length

Grogan and Wirth measured wing lengths (in mm) of n=9n = 9 midges: (1.64,1.70,1.72,1.74,1.82,1.82,1.82,1.90,2.08)(1.64, 1.70, 1.72, 1.74, 1.82, 1.82, 1.82, 1.90, 2.08), giving y¯=1.804\bar{y} = 1.804 and s2=0.0169s^2 = 0.0169. Using weak prior knowledge from related species (μ0=1.9\mu_0 = 1.9, σ02=0.01\sigma_0^2 = 0.01, κ0=ν0=1\kappa_0 = \nu_0 = 1), Theorem 5.2 gives κn=10\kappa_n = 10, νn=10\nu_n = 10, μn=1.814\mu_n = 1.814, and σn2=0.0153\sigma_n^2 = 0.0153, with 95% Student-tt credible interval [1.727,1.901][1.727, 1.901] (Figure 5.1, left panel).

Three panels: contour plot of the Normal-Inverse-Gamma joint posterior for midge wing length; relative MSE curves of the Bayes shrinkage estimator versus the sample mean for kappa_0 equals 1, 2, 3 as a function of n; and sampling densities at n equals 10 illustrating the bias-variance trade-off
Figure 5.1: Left: conjugate Normal–Inverse-Gamma joint posterior contours for the n=9n = 9 midge wing lengths. Center: relative frequentist MSE of the Bayes mean θ^b\hat{\theta}_b vs. y¯\bar{y} when true IQ θ0=112\theta_0 = 112, σ2=169\sigma^2 = 169, and prior μ0=100\mu_0 = 100—even with a 12-point prior bias, κ0=1\kappa_0 = 1 beats y¯\bar{y} for every nn. Right: sampling densities at n=10n = 10 showing how mild shrinkage trades tiny bias for substantial variance reduction.

5.4Bias, Variance, and When the Bayes Mean Beats y¯\bar{y} in MSE

A natural objection to Bayesian shrinkage is: isn’t θ^b=E[θ∣y1:n]=wy¯+(1−w)μ0\hat{\theta}_b = E[\theta \mid y_{1:n}] = w\bar{y} + (1 - w)\mu_0 (with w=nn+κ0w = \frac{n}{n + \kappa_0}) biased whenever μ0≠θ0\mu_0 \neq \theta_0? Yes—its bias is (1−w)(μ0−θ0)(1 - w)(\mu_0 - \theta_0), whereas y¯\bar{y} is unbiased. Yet unbiasedness ignores variance! Measuring total accuracy by Mean Squared Error (MSE⁡=Var⁡+Bias⁡2\operatorname{MSE} = \operatorname{Var} + \operatorname{Bias}^2) reveals a striking frequentist justification for Bayesian shrinkage:

Theorem 5.3 (When the Bayes Mean Beats y-bar in MSE). Suppose Y1,…,Yn∼i.i.d.normal⁡(θ0,σ2)Y_1, \dots, Y_n \stackrel{\text{i.i.d.}}{\sim} \operatorname{normal}(\theta_0, \sigma^2) and

θ^b=nn+κ0y¯+κ0n+κ0μ0 \hat{\theta}_b = \frac{n}{n+\kappa_0}\bar{y} + \frac{\kappa_0}{n+\kappa_0}\mu_0

with κ0>0\kappa_0 > 0. Then

MSE⁡(θ^b)=(nn+κ0)2σ2n+(κ0n+κ0)2(μ0−θ0)2. \operatorname{MSE}(\hat{\theta}_b) = \left(\frac{n}{n + \kappa_0}\right)^2 \frac{\sigma^2}{n} + \left(\frac{\kappa_0}{n + \kappa_0}\right)^2 (\mu_0 - \theta_0)^2. (5.10)

Consequently, MSE⁡(θ^b)<MSE⁡(y¯)=σ2/n\operatorname{MSE}(\hat{\theta}_b) < \operatorname{MSE}(\bar{y}) = \sigma^2 / n if and only if

(μ0−θ0)2<σ2 ⁣(1n+2κ0). (\mu_0 - \theta_0)^2 < \sigma^2\!\left(\frac{1}{n} + \frac{2}{\kappa_0}\right). (5.11)

Proof. Write MSE⁡(θ^b)<σ2/n\operatorname{MSE}(\hat{\theta}_b) < \sigma^2/n using (5.10) and multiply both sides by (n+κ0)2(n + \kappa_0)^2:

nσ2+κ02(μ0−θ0)2<(n+κ0)2nσ2=(n+2κ0+κ02n)σ2. n\sigma^2 + \kappa_0^2 (\mu_0 - \theta_0)^2 < \frac{(n + \kappa_0)^2}{n}\sigma^2 = \left(n + 2\kappa_0 + \frac{\kappa_0^2}{n}\right)\sigma^2.

Subtracting nσ2n\sigma^2 and dividing by κ02>0\kappa_0^2 > 0 gives (μ0−θ0)2<σ2(2/κ0+1/n)(\mu_0 - \theta_0)^2 < \sigma^2(2/\kappa_0 + 1/n). □

Notice how generous (5.11) is! For Hoff’s town-IQ example (θ0=112\theta_0 = 112, σ=13\sigma = 13, prior μ0=100\mu_0 = 100, so (μ0−θ0)2=144(\mu_0 - \theta_0)^2 = 144), taking κ0=1\kappa_0 = 1 gives σ2(1/n+2)>338>144\sigma^2(1/n + 2) > 338 > 144 for all nn! Even though the prior guess is wrong by almost a full standard deviation, shrinking mildly toward it strictly lowers MSE at every sample size (Figure 5.1, center and right). Conversely, setting κ0\kappa_0 too large (κ0=3\kappa_0 = 3) makes bias dominate once nn grows.

5.5Expectation-Based Priors, Robustness, and the Limit of Conjugacy

Two deeper lessons close Chapter 5 and motivate the rest of Part II:

  1. Why normal sampling is surprisingly robust: The normal likelihood depends on the data only through (y¯,s2)(\bar{y}, s^2), and by the Central Limit Theorem y¯\bar{y} is approximately normal even when individual observations YiY_i are skewed. Still, Lukacs’ theoremEugene Lukacs. A Characterization of the Normal Distribution. The Annals of Mathematical Statistics, 13:91–93, 1942 proves that Y¯\bar{Y} and S2S^2 are independent if and only if the population is strictly normal; separately, heavy-tailed non-normal populations can distort variance inference and predictions.
  2. The awkwardness of joint conjugacy: The conjugate prior (5.6) forced Var⁡(θ∣σ2)=σ2/κ0\operatorname{Var}(\theta \mid \sigma^2) = \sigma^2/\kappa_0, coupling our uncertainty about the mean θ\theta to the unknown sampling variance σ2\sigma^2. In the midge example, we actually had an independent prior belief θ∼normal⁡(1.9,0.952)\theta \sim \operatorname{normal}(1.9, 0.95^2) based on positivity—independent of σ2\sigma^2! Separating the priors into p(θ,σ2)=p(θ) p(σ2)p(\theta, \sigma^2) = p(\theta)\,p(\sigma^2) breaks joint conjugacy, so p(θ,σ2∣y)p(\theta, \sigma^2 \mid y) no longer has a closed form. Solving that exact problem is why we turn next to the Gibbs sampler in Chapter 6.
import numpy as np
from scipy import stats

# Conjugate Normal-IG analysis of Hoff's midge wing length data (n=9)
y = np.array([1.64, 1.70, 1.72, 1.74, 1.82, 1.82, 1.82, 1.90, 2.08])
n, ybar, s2 = len(y), np.mean(y), np.var(y, ddof=1)
mu0, k0, s20, nu0 = 1.9, 1.0, 0.01, 1.0

kn, nun = k0 + n, nu0 + n
mun = (k0 * mu0 + n * ybar) / kn
s2n = (nu0 * s20 + (n - 1) * s2 + k0 * n * (ybar - mu0)**2 / kn) / nun

# Exact Student-t 95% credible interval for theta
ci_exact = stats.t.ppf([0.025, 0.975], df=nun, loc=mun, scale=np.sqrt(s2n / kn))
print(f"Exact mu_n = {mun:.4f}, s2_n = {s2n:.5f}, 95% CI = {np.round(ci_exact, 4)}")

# Exact ancestral Monte Carlo verification (draw 1/sigma^2, then theta | sigma^2)
rng = np.random.default_rng(42)
S = 20_000
s2_mc = 1.0 / rng.gamma(shape=nun / 2.0, scale=2.0 / (nun * s2n), size=S)
th_mc = rng.normal(mun, np.sqrt(s2_mc / kn))
print("MC 95% CI for theta:  ", np.round(np.quantile(th_mc, [0.025, 0.975]), 4))
print("MC 95% CI for sigma:  ", np.round(np.quantile(np.sqrt(s2_mc), [0.025, 0.975]), 4))

5.6Modern Developments: Learning the Prior and Surviving Outliers

5.6.1Tweedie’s Formula and Empirical Bayes Shrinkage

Tweedie’s formula computes a normal posterior mean from the marginal density of the data alone, with no explicit prior.Beyond Hoff (2009). This material is not in Hoff’s book; the identity itself is much older—Robbins reported it in 1956 and credited Maurice Tweedie—and Efron (2011) modernized it as an empirical Bayes correction for selection bias.Bradley Efron. Tweedie’s Formula and Selection Bias. Journal of the American Statistical Association, 106(496):1602–1614, 2011. doi: 10.1198/jasa.2011.tm11181 Take y¯∣θ∼normal⁡(θ,v)\bar{y} \mid \theta \sim \operatorname{normal}(\theta, v) with v=σ2/nv = \sigma^2/n known and any prior π(θ)\pi(\theta), and let m(y¯)=∫p(y¯∣θ) π(θ) dθm(\bar{y}) = \int p(\bar{y} \mid \theta)\,\pi(\theta)\,d\theta. Then

E[θ∣y¯]=y¯+v ddy¯log⁡m(y¯),Var⁡(θ∣y¯)=v[1+v d2dy¯2log⁡m(y¯)]. E[\theta \mid \bar{y}] = \bar{y} + v\,\frac{d}{d\bar{y}}\log m(\bar{y}), \qquad \operatorname{Var}(\theta \mid \bar{y}) = v\left[1 + v\,\frac{d^2}{d\bar{y}^2}\log m(\bar{y})\right]. (5.12)

Why? Since ∂y¯ p(y¯∣θ)=θ−y¯v p(y¯∣θ)\partial_{\bar{y}}\,p(\bar{y} \mid \theta) = \frac{\theta - \bar{y}}{v}\,p(\bar{y} \mid \theta), differentiating under the integral gives m′/m=(E[θ∣y¯]−y¯)/vm'/m = (E[\theta \mid \bar{y}] - \bar{y})/v; a second derivative gives the variance. With π=normal⁡(μ0,τ02)\pi = \operatorname{normal}(\mu_0, \tau_0^2), mm is normal⁡(μ0,τ02+v)\operatorname{normal}(\mu_0, \tau_0^2 + v) and (5.12) collapses to y¯−vτ02+v(y¯−μ0)\bar{y} - \frac{v}{\tau_0^2 + v}(\bar{y} - \mu_0), exactly μn\mu_n in (5.5).

The gain appears with many parallel problems (θi,y¯i)(\theta_i, \bar{y}_i) sharing vv: the observed y¯i\bar{y}_i are themselves a sample from mm, so a smooth estimate m^\hat{m} replaces the prior. The most extreme y¯i\bar{y}_i are then pulled back by v (log⁡m^)′v\,(\log \hat{m})', correcting the winner’s curse: a Bayes posterior given y¯i\bar{y}_i is unchanged by having selected case ii for its extremeness.

When to use it. With one mean and real prior knowledge, use Proposition 5.1. With hundreds of parallel means and no trusted prior family, use Tweedie—but not in the far tails, where m^\hat{m} has little data and its derivative is unstable.

5.6.2Whole Robustness with Log-Regularly Varying Tails

A sampling density with sufficiently heavy tails can make a conflicting outlier’s influence on the posterior of both θ\theta and σ\sigma vanish in the limit, a property called whole robustness.Beyond Hoff (2009). This material is not in Hoff’s book; Desgagné (2015) proves whole robustness for location–scale models, sharpening the older heavy-tail advice for outliers.Alain Desgagné. Robustness to outliers in location–scale parameter model using log-regularly varying distributions. The Annals of Statistics, 43(4):1568–1595, 2015. doi: 10.1214/15-AOS1316 The normal model fails badly: moving one observation yi→∞y_i \to \infty drags μn\mu_n along and inflates νnσn2\nu_n\sigma_n^2 in (5.7) without bound. Replace it by p(yi∣θ,σ)=1σf(yi−θσ)p(y_i \mid \theta, \sigma) = \frac{1}{\sigma}f\bigl(\frac{y_i - \theta}{\sigma}\bigr) and examine the outlier’s likelihood factor, normalized by the constant f(yi)f(y_i), as yi→∞y_i \to \infty:

1σ f ⁣(yi−θσ)f(yi)  ⟶  {σν,f=tν(partial robustness),1,zf(z) log-regularly varying(whole robustness). \frac{\frac{1}{\sigma}\,f\!\left(\frac{y_i - \theta}{\sigma}\right)}{f(y_i)} \;\longrightarrow\; \begin{cases} \sigma^{\nu}, & f = t_\nu \quad (\text{partial robustness}), \\ 1, & z f(z) \text{ log-regularly varying} \quad (\text{whole robustness}). \end{cases} (5.13)

For tνt_\nu, f(z)∼c ∣z∣−ν−1f(z) \sim c\,\lvert z \rvert^{-\nu-1}: θ\theta is protected, but the surviving factor σν\sigma^\nu still pushes the scale upward. If instead zf(z)=h(log⁡z)z f(z) = h(\log z) with hh regularly varying—say f(z)∝1/[z(log⁡z)b]f(z) \propto 1/[z(\log z)^{b}], b>1b > 1, spliced onto a normal core—then shifting and scaling alter log⁡z\log z only by lower-order terms, so the ratio tends to 11 and the outlier drops out. Passing from factors to the whole posterior requires regularity of ff (symmetry, continuity, monotone tails), a prior that is controlled near σ=0\sigma = 0, and sufficiently many non-outliers relative to outliers; otherwise the “outliers” could form the dominant cluster. The theorem in the paper gives the exact count.

When to use it. Keep Hoff’s conjugate model for clean data and closed forms; use a log-regularly varying ff, at the price of MCMC and of predictive distributions with no finite mean, when gross contamination must affect neither location nor scale.

5.6.3Computational & Symbolic Check

The script compares (5.12), computed from a numerically integrated marginal mm, with the conjugate update, and then evaluates the outlier factor (5.13) for t4t_4 and for the log-regularly varying tail f(z)∝1/[z(log⁡z)2]f(z) \propto 1/[z(\log z)^2].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 integrate, stats

# Tweedie from the integrated marginal m(y); log m is quadratic, so central differences are exact
mu0, t20, v, y, h = 1.0, 4.0, 1.0, 3.0, 0.1
def log_m(u):
    f = lambda th: stats.norm.pdf(u, th, np.sqrt(v)) * stats.norm.pdf(th, mu0, np.sqrt(t20))
    return np.log(integrate.quad(f, -20, 20, epsabs=0, epsrel=1e-12)[0])
lo, mid, hi = log_m(y - h), log_m(y), log_m(y + h)
print(f"Tweedie:   {y + v * (hi - lo) / (2 * h):.6f}, {v * (1 + v * (hi - 2 * mid + lo) / h**2):.6f}")
print(f"Conjugate: {(t20 * y + v * mu0) / (t20 + v):.6f}, {t20 * v / (t20 + v):.6f}")

# Outlier factor (1/s) f((z - th)/s) / f(z): limit s^nu = 16 for t_4, limit 1 for the LRV tail
th, s, nu, b = 1.0, 2.0, 4.0, 2.0
for z in (1e2, 1e8, 1e100):
    t_ratio = ((1 + ((z - th) / s)**2 / nu) / (1 + z**2 / nu))**(-(nu + 1) / 2) / s
    lrv_ratio = z / (z - th) * (np.log(z) / np.log((z - th) / s))**b
    print(f"z = {z:.0e}: Student-t {t_ratio:.4f}, log-regularly varying {lrv_ratio:.4f}")
Write the log of the Normal–Normal marginal m=𝒩(μ0,τ02+v)m = \mathcal{N}(\mu_0, \tau_0^2 + v) as a function of y¯\bar{y}.
// Chapter 5 (modern): Tweedie on the Normal-Normal marginal & outlier-factor limits
const logM = await show`-(yb - mu0)^2/(2*(t2 + v)) - Log[2*Pi*(t2 + v)]/2`
log⁡m(y¯)=−(y¯−μ0)22(τ02+v)−12log⁡(2π(τ02+v)) \log m(\bar{y}) = -\frac{(\bar{y}-\mu_0)^2}{2 (\tau_0^2+v)}-\frac{1}{2} \log \bigl(2 \pi (\tau_0^2+v)\bigr) Apply Tweedie’s mean formula y¯+v (log⁡m)′(y¯)\bar{y} + v\,(\log m)'(\bar{y}) to that marginal.
const twMean = await show`Factor[yb + v*D[${logM}, yb]]`
E[θ∣y¯]=y¯+v (log⁡m)′(y¯)=μ0v+τ02y¯τ02+v E[\theta \mid \bar{y}] = \bar{y} + v\,(\log m)'(\bar{y}) = \frac{\mu_0 v+\tau_0^2 \bar{y}}{\tau_0^2+v} Apply Tweedie’s variance formula to the same marginal.
const twVar = await show`Factor[v*(1 + v*D[${logM}, {yb, 2}])]`
Var⁡(θ∣y¯)=v[1+v (log⁡m)′′(y¯)]=τ02vτ02+v \operatorname{Var}(\theta \mid \bar{y}) = v\bigl[1 + v\,(\log m)''(\bar{y})\bigr] = \frac{\tau_0^2 v}{\tau_0^2+v} Subtract the precision-weighted conjugate mean and variance: both differences vanish identically.
await show`Simplify[{${twMean} - (mu0/t2 + yb/v)/(1/t2 + 1/v), ${twVar} - 1/(1/t2 + 1/v)}]`
(ETweedie−Econj,  Var⁡Tweedie−Var⁡conj)={0,0} \bigl(E_{\text{Tweedie}} - E_{\text{conj}},\; \operatorname{Var}_{\text{Tweedie}} - \operatorname{Var}_{\text{conj}}\bigr) = \{0,0\} Instantiate at the Python values (μ0,τ02,v,y¯)=(1,4,1,3)(\mu_0, \tau_0^2, v, \bar{y}) = (1, 4, 1, 3).
await show`{${twMean}, ${twVar}} /. {mu0 -> 1, t2 -> 4, v -> 1, yb -> 3}`
(E[θ∣y¯],  Var⁡(θ∣y¯))={135,45} \bigl(E[\theta \mid \bar{y}],\; \operatorname{Var}(\theta \mid \bar{y})\bigr) = \left\{\frac{13}{5},\frac{4}{5}\right\} Student-tt outlier factor: the ratio of the location–scale tνt_\nu density at an outlier zz to the standard tνt_\nu density there.
const tFac = await show`(1/s)*((1 + ((z - th)/s)^2/nu)/(1 + z^2/nu))^(-(nu + 1)/2)`
1σ tν ⁣(z−θσ)tν(z)=1σ(1+(z−θ)2νσ21+z2ν)−1+ν2 \frac{\frac{1}{\sigma}\,t_\nu\!\left(\frac{z - \theta}{\sigma}\right)}{t_\nu(z)} = \frac{1}{\sigma}\left(\frac{1+\frac{(z-\theta)^2}{\nu \sigma^2}}{1+\frac{z^2}{\nu}}\right)^{-\frac{1+\nu}{2}} Its limit as z→∞z \to \infty is σν\sigma^\nu, which still involves the scale (partial robustness).
await show`Assuming[nu > 0 && s > 0 && Element[th, Reals], FullSimplify[Limit[${tFac}, z -> Infinity]]]`
lim⁡z→∞1σ tν ⁣(z−θσ)tν(z)=σν \lim_{z \to \infty} \frac{\frac{1}{\sigma}\,t_\nu\!\left(\frac{z - \theta}{\sigma}\right)}{t_\nu(z)} = \sigma^\nu Log-regularly varying outlier factor for f(z)∝1/[z(log⁡z)2]f(z) \propto 1/[z(\log z)^2], i.e. b=2b = 2.
const lrvFac = await show`z/(z - th)*(Log[z]/Log[(z - th)/s])^2`
1σ f ⁣(z−θσ)f(z)∣f(z)∝1/[z(log⁡z)2]=zz−θ(log⁡zlog⁡z−θσ)2 \frac{\frac{1}{\sigma}\,f\!\left(\frac{z - \theta}{\sigma}\right)}{f(z)}\bigg|_{f(z) \propto 1/[z(\log z)^2]} = \frac{z}{z-\theta}\left(\frac{\log z}{\log \frac{z-\theta}{\sigma}}\right)^2 Its limit as z→∞z \to \infty is free of θ\theta and σ\sigma (whole robustness).
await show`Assuming[s > 0 && Element[th, Reals], Limit[${lrvFac}, z -> Infinity]]`
lim⁡z→∞1σ f ⁣(z−θσ)f(z)∣f(z)∝1/[z(log⁡z)2]=1 \lim_{z \to \infty} \frac{\frac{1}{\sigma}\,f\!\left(\frac{z - \theta}{\sigma}\right)}{f(z)}\bigg|_{f(z) \propto 1/[z(\log z)^2]} = 1 Tabulate both factors at z∈{102,108,10100}z \in \{10^2, 10^8, 10^{100}\} for (θ,σ,ν)=(1,2,4)(\theta, \sigma, \nu) = (1, 2, 4): convergence to 11 is only logarithmic.
await show`N[Table[{${tFac}, ${lrvFac}} /. {th -> 1, s -> 2, nu -> 4, z -> zz}, {zz, {10^2, 10^8, 10^100}}]]`
(t4 factor,  LRV factor)z∈{102, 108, 10100}=(16.77291.4069816.1.0797316.1.00605) \begin{aligned} &\bigl(t_4 \text{ factor},\; \text{LRV factor}\bigr)_{z \in \{10^2,\, 10^8,\, 10^{100}\}} \\ &= \left( \begin{array}{cc} 16.7729 & 1.40698 \\ 16. & 1.07973 \\ 16. & 1.00605 \\ \end{array} \right) \end{aligned}

5.7Symbolic Check in Wolfram Language

Integrates precision p=1/σ2p = 1/\sigma^2 out of the Normal–Inverse-Gamma joint posterior in wolfram-ws-bridge JavaScript mode, verifies that the ratio to the Student-tt kernel has zero derivative in θ\theta, derives the exact symbolic Student-tt posterior variance Var⁡[θ∣y]\operatorname{Var}[\theta \mid y], factors (n+κ0)2(MSE⁡(θ^b)−MSE⁡(y¯))(n + \kappa_0)^2(\operatorname{MSE}(\hat{\theta}_b) - \operatorname{MSE}(\bar{y})), and instantiates all symbolic formulas on Hoff’s midge wing lengths.

Integrate precision p=1/σ2p = 1/\sigma^2 out of the Normal–Gamma joint posterior to obtain the marginal kernel in θ\theta.
// Chapter 5: Symbolic Normal-IG -> Student-t marginal, variance & exact MSE inequality
const marg = await show`Assuming[nun > 0 && rate > 0,
  FullSimplify[Integrate[p^((nun + 1)/2 - 1)*Exp[-rate*p/2], {p, 0, Infinity}]]] /. rate -> nun*s2n + kn*(th - mun)^2`
∫0∞pνn+12−1e−p(νnσn2+κn(θ−μn)2)/2 dp=21+νn2(1νnσn2+κn(−μn+θ)2)1+νn2Γ(1+νn2) \begin{aligned} &\int_0^\infty p^{\frac{\nu_n+1}{2}-1} e^{-p(\nu_n\sigma_n^2 + \kappa_n(\theta-\mu_n)^2)/2}\,dp \\ &= 2^{\frac{1+\nu_n}{2}} \left(\frac{1}{\nu_n \sigma_n^2 +\kappa_n (-\mu_n +\theta)^2}\right)^{\frac{1+\nu_n}{2}} \Gamma \left(\frac{1+\nu_n}{2}\right) \end{aligned} Write the unnormalized tνn(μn,σn2/κn)t_{\nu_n}(\mu_n, \sigma_n^2/\kappa_n) kernel in θ\theta.
const tKer = await show`(1 + kn*(th - mun)^2 / (nun*s2n))^(-(nun + 1)/2)`
tνn(θ∣μn,σn2/κn)∝(1+κn(θ−μn)2νnσn2)−1+νn2 t_{\nu_n}(\theta \mid \mu_n, \sigma_n^2/\kappa_n) \propto \left(1+\frac{\kappa_n (\theta -\mu_n)^2}{\nu_n \sigma_n^2}\right)^{-\frac{1+\nu_n}{2}} Check that the log-ratio of the integrated kernel to the tνn(μn,σn2/κn)t_{\nu_n}(\mu_n, \sigma_n^2/\kappa_n) kernel has zero derivative in θ\theta.
await show`Assuming[kn > 0 && nun > 0 && s2n > 0 && Element[{th, mun}, Reals],
  FullSimplify[D[Log[${marg} / ${tKer}], th]]]`
ddθlog⁡ ⁣(p(θ∣y)tνn(θ∣μn,σn2/κn))=0 \begin{aligned} &\frac{d}{d\theta}\log\!\left(\frac{p(\theta \mid y)}{t_{\nu_n}(\theta \mid \mu_n, \sigma_n^2/\kappa_n)}\right) \\ &= 0 \end{aligned} Integrate u2u^2 against the Student-tt density to confirm Var⁡(θ∣y)=[νn/(νn−2)](σn2/κn)\operatorname{Var}(\theta \mid y) = [\nu_n/(\nu_n - 2)](\sigma_n^2/\kappa_n).
// Symbolic Student-t variance Var[theta | y] = (nun / (nun - 2)) * (s2n / kn) for nun > 2
const tVar = await show`Assuming[nun > 2 && kn > 0 && s2n > 0,
  FullSimplify[(s2n/kn) * Integrate[u^2*(1 + u^2/nun)^(-(nun + 1)/2) / (Sqrt[nun]*Beta[1/2, nun/2]), {u, -Infinity, Infinity}]]]`
Var⁡(θ∣y)=νnσn2κn(−2+νn) \operatorname{Var}(\theta \mid y) = \frac{\nu_n \sigma_n^2}{\kappa_n (-2+\nu_n)} Factor the scaled MSE difference (n+κ0)2[MSE⁡(θ^b)−MSE⁡(y¯)](n + \kappa_0)^2[\operatorname{MSE}(\hat{\theta}_b) - \operatorname{MSE}(\bar{y})] to locate the crossover.
// Scaled MSE difference (n + k0)^2 * (MSE(theta_b) - MSE(ybar))
const { Plus, Times, Power, Divide, Subtract, n, k0, s2, mu0, th0 } = W
const mseB = Plus(
  Times(Power(Divide(n, Plus(n, k0)), 2), Divide(s2, n)),
  Times(Power(Divide(k0, Plus(n, k0)), 2), Power(Subtract(mu0, th0), 2))
)
await show`Factor[Together[(${mseB} - s2/n) * (n + k0)^2]]`
(n+κ0)2(MSE⁡(θ^b)−MSE⁡(y¯))=κ0(κ0μ02n−κ0σ2−2nσ2−2κ0μ0nθ0+κ0nθ02)n \begin{aligned} &(n + \kappa_0)^2\bigl(\operatorname{MSE}(\hat{\theta}_b) - \operatorname{MSE}(\bar{y})\bigr) \\ &= \frac{\kappa_0 \left(\kappa_0 \mu_0^2 n-\kappa_0 \sigma^2 -2 n \sigma^2 -2 \kappa_0 \mu_0 n \theta_0 +\kappa_0 n \theta_0^2\right)}{n} \end{aligned} Posterior mean μn=(κ0μ0+ny¯)/κn\mu_n = (\kappa_0\mu_0 + n\bar{y})/\kappa_n on Hoff’s midge data, with (μ0,κ0)=(1.9,1)(\mu_0, \kappa_0) = (1.9, 1) and n=9n = 9.
// Instantiate symbolic formulas on Hoff's midge wing lengths (n = 9)
const y = [1.64, 1.70, 1.72, 1.74, 1.82, 1.82, 1.82, 1.90, 2.08]
const mun = await show`N[(19/10 + Total[${y}]) / 10]`
μn=κ0μ0+∑i=19yiκ0+n=1.9+16.2410=1.814 \mu_n = \frac{\kappa_0 \mu_0 + \sum_{i=1}^{9} y_i}{\kappa_0 + n} = \frac{1.9 + 16.24}{10} = 1.814 Posterior scale σn2=[ν0σ02+(n−1)s2+κ0nκn(y¯−μ0)2]/νn\sigma_n^2 = [\nu_0\sigma_0^2 + (n-1)s^2 + \frac{\kappa_0 n}{\kappa_n}(\bar{y} - \mu_0)^2]/\nu_n, with (ν0,σ02)=(1,0.01)(\nu_0, \sigma_0^2) = (1, 0.01).
const s2n = await show`N[(1/100 + 8*Variance[${y}] + (9/10)*(Mean[${y}] - 19/10)^2) / 10]`
σn2=ν0σ02+(n−1)s2+κ0nκn(y¯−μ0)2νn=0.015324 \sigma_n^2 = \frac{\nu_0\sigma_0^2 + (n-1)s^2 + \frac{\kappa_0 n}{\kappa_n}(\bar{y}-\mu_0)^2}{\nu_n} = 0.015324 Solve 1−I10/(10+t2)(5,12)=0.951 - I_{10/(10+t^2)}(5, \tfrac12) = 0.95 for the two-sided 97.5%97.5\% quantile of t10t_{10}.
const tCrit = await show`t /. FindRoot[1 - Beta[10/(10 + t^2), 5, 1/2]/Beta[5, 1/2] == 95/100, {t, 2.2}]`
t10, 0.975:  1−B(1010+t2; 5,12)B(5,12)=0.95  ⟹  t=2.22814 t_{10,\,0.975} : \; 1 - \frac{B\bigl(\tfrac{10}{10+t^2};\, 5, \tfrac12\bigr)}{B\bigl(5, \tfrac12\bigr)} = 0.95 \;\Longrightarrow\; t = 2.22814 Instantiate the exact posterior mean, variance, SD, and 95%95\% Student-tt credible interval on Hoff’s midge data.
await show`N[{${mun}, ${s2n}, Sqrt[${tVar} /. {nun -> 10, kn -> 10, s2n -> ${s2n}}], ${mun} - ${tCrit}*Sqrt[${s2n}/10], ${mun} + ${tCrit}*Sqrt[${s2n}/10]}]`
(μn,  σn2,  SD⁡(θ∣y),  CI0.025,  CI0.975)midge={1.814,0.015324,0.0437664,1.72678,1.90122} \begin{aligned} &\bigl(\mu_n,\; \sigma_n^2,\; \operatorname{SD}(\theta \mid y),\; \text{CI}_{0.025},\; \text{CI}_{0.975}\bigr)_{\text{midge}} \\ &= \{1.814,0.015324,0.0437664,1.72678,1.90122\} \end{aligned}

5.8Exercises

Exercise 5.4. Show that taking κ0,ν0→0+\kappa_0, \nu_0 \to 0^+ in Theorem 5.2 gives μn=y¯\mu_n = \bar{y} and νn=n\nu_n = n, whereas the reference prior p(θ,σ2)∝1/σ2p(\theta, \sigma^2) \propto 1/\sigma^2 gives n−1n - 1 degrees of freedom. Explain the discrepancy by tracking the factor (σ2)−1/2(\sigma^2)^{-1/2} in p(θ∣σ2)p(\theta \mid \sigma^2).

Symbolic Solution & InstantiationTake κ0→0+\kappa_0 \to 0^+ to verify that the posterior mean collapses to the sample mean y¯\bar{y}.
// Ex 5.1: Marginal posterior exponent of sigma^2 under kappa_0, nu_0 -> 0+ vs. reference prior 1/sigma^2
await show`Limit[(k0*mu0 + n*yb)/(k0 + n), k0 -> 0]`
lim⁡κ0→0+μn=y¯ \lim_{\kappa_0 \to 0^+} \mu_n = \bar{y} Evaluate the marginal posterior exponent of σ2\sigma^2 in the limit κ0,ν0→0+\kappa_0, \nu_0 \to 0^+.
const powLim = await show`Assuming[n > 0, FullSimplify[-(n + nu0)/2 - 1 /. nu0 -> 0]]`
Marginal exponent of σ2 as κ0,ν0→0+=−1−n2 \text{Marginal exponent of } \sigma^2 \text{ as } \kappa_0, \nu_0 \to 0^+ = -1-\frac{n}{2} Compute the corresponding marginal exponent of σ2\sigma^2 under the Jeffreys/reference prior p(θ,σ2)∝1/σ2p(\theta, \sigma^2) \propto 1/\sigma^2.
const powRef = await show`Assuming[n > 0, FullSimplify[-(n - 1)/2 - 1]]`
Marginal exponent of σ2 under p(θ,σ2)∝1/σ2=12(−1−n) \begin{aligned} &\text{Marginal exponent of } \sigma^2 \text{ under } p(\theta, \sigma^2) \propto 1/\sigma^2 \\ &= \frac{1}{2} (-1-n) \end{aligned} Instantiate the resulting degrees of freedom at n=9n = 9, showing the 11-df difference (99 versus 88).
await show`{-2*(${powLim} + 1), -2*(${powRef} + 1)} /. n -> 9`
(dfκ0,ν0→0+,  df1/σ2)n=9={9,8} \bigl(\text{df}_{\kappa_0,\nu_0\to 0^+},\; \text{df}_{1/\sigma^2}\bigr)_{n=9} = \{9,8\}

Exercise 5.5. Derive the exact posterior predictive distribution Y~∣y1:n\tilde{Y} \mid y_{1:n} in Theorem 5.2 by integrating Y~∣θ,σ2∼normal⁡(θ,σ2)\tilde{Y} \mid \theta, \sigma^2 \sim \operatorname{normal}(\theta, \sigma^2) first over θ∣σ2,y1:n\theta \mid \sigma^2, y_{1:n} and then over σ2∣y1:n\sigma^2 \mid y_{1:n}. Confirm that its variance is νnνn−2 σn2(1+1/κn)\frac{\nu_n}{\nu_n - 2}\,\sigma_n^2(1 + 1/\kappa_n) for νn>2\nu_n > 2.

Symbolic Solution & InstantiationSimplify the exact posterior predictive Student-tt variance Var⁡(Y~∣y)\operatorname{Var}(\tilde{Y} \mid y) for νn>2\nu_n > 2.
// Ex 5.2: Exact posterior predictive Student-t variance for Normal-IG model
const predVar = await show`Assuming[nun > 2 && kn > 0 && s2n > 0,
  FullSimplify[(nun/(nun - 2)) * s2n * (1 + 1/kn)]]`
Var⁡(Y~∣y)=(1+1κn)νnσn2−2+νn \begin{aligned} &\operatorname{Var}(\tilde{Y} \mid y) \\ &= \frac{\left(1+\frac{1}{\kappa_n}\right) \nu_n \sigma_n^2}{-2+\nu_n} \end{aligned} Instantiate the midge predictive SD alongside the much narrower posterior SD of θ\theta.
await show`N[{Sqrt[${predVar}], Sqrt[(nun/(nun - 2))*s2n/kn]} /. {nun -> 10, kn -> 10, s2n -> 0.01532}]`
(SD⁡(Y~∣y),  SD⁡(θ∣y))midge={0.145138,0.0437607} \begin{aligned} &\bigl(\operatorname{SD}(\tilde{Y} \mid y),\; \operatorname{SD}(\theta \mid y)\bigr)_{\text{midge}} \\ &= \{0.145138,0.0437607\} \end{aligned}

Exercise 5.6. In Theorem 5.3, suppose θ0=112\theta_0 = 112, σ2=169\sigma^2 = 169, μ0=100\mu_0 = 100, and κ0=3\kappa_0 = 3. Find the exact sample size n∗n^* above which y¯\bar{y} achieves lower MSE than θ^b\hat{\theta}_b, and verify your threshold against the orange curve in Figure 5.1.

Symbolic Solution & InstantiationSolve MSE⁡(θ^b)=MSE⁡(y¯)\operatorname{MSE}(\hat{\theta}_b) = \operatorname{MSE}(\bar{y}) symbolically for the exact crossover sample size n∗n^*.
// Ex 5.3: Exact sample-size crossover n* where ybar beats Bayes shrinkage in MSE (exists iff k0*(mu0 - th0)^2 > 2*s2)
const nStar = await show`n /. First[Solve[(mu0 - th0)^2 == s2*(1/n + 2/k0), n]]`
n∗=κ0σ2κ0μ02−2σ2−2κ0μ0θ0+κ0θ02 \begin{aligned} &n^* \\ &= \frac{\kappa_0 \sigma^2}{\kappa_0 \mu_0^2-2 \sigma^2 -2 \kappa_0 \mu_0 \theta_0 +\kappa_0 \theta_0^2} \end{aligned} Evaluate n∗n^* both as an exact fraction and numerically for the town-IQ parameters (θ0=112,μ0=100\theta_0=112, \mu_0=100).
await show`{${nStar}, N[${nStar}]} /. {th0 -> 112, mu0 -> 100, s2 -> 169, k0 -> 3}`
(nexact∗,  ndecimal∗)θ0=112,  μ0=100,  σ2=169,  κ0=3={50794,5.39362} \begin{aligned} &\bigl(n^*_{\text{exact}},\; n^*_{\text{decimal}}\bigr)_{\theta_0=112,\;\mu_0=100,\;\sigma^2=169,\;\kappa_0=3} \\ &= \left\{\frac{507}{94},5.39362\right\} \end{aligned}