Chapter 8

Group Comparisons and Hierarchical Modeling

Whenever data come in groups—students nested inside schools, patients nested inside hospitals, or baseball players across a league—two naive extremes tempt the analyst. No pooling fits each group completely separately, leaving small groups noisy and erratic. Complete pooling lumps all groups into one common mean, erasing genuine differences between groups. Chapter 8 resolves this tension cleanly through hierarchical modelingPeter 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; Dennis V. Lindley and Adrian F. M. Smith. Bayes Estimates for the Linear Model. Journal of the Royal Statistical Society, Series B, 34:1–41, 1972; and Andrew Gelman and Jennifer Hill. Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press, New York, 2007, where groups share information smoothly (partial pooling) in direct proportion to their sample sizes and between-group variance.

8.1Two-Group Comparisons: Continuous Pooling via (μ±δ)(\mu \pm \delta)

Start with m=2m = 2 urban public high schools from the 2002 Educational Longitudinal Study (ELS): n1=31n_1 = 31 tenth graders in School 1 (y¯1=50.81\bar{y}_1 = 50.81) and n2=28n_2 = 28 in School 2 (y¯2=46.15\bar{y}_2 = 46.15), a sample gap of y¯1−y¯2=4.66\bar{y}_1 - \bar{y}_2 = 4.66 points. Classical practice runs a two-sample tt-test: if p<0.05p < 0.05, report separate means y¯1,y¯2\bar{y}_1, \bar{y}_2; if p≥0.05p \ge 0.05, pool both schools into a single grand mean. That binary switch is discontinuous and wastes information when groups are similar yet non-identical.

Hoff replaces the binary switch with a reparameterization into an overall level μ\mu and a half-difference δ\delta:

θ1=μ+δ,θ2=μ−δ,μ∼normal⁡(μ0,γ02),δ∼normal⁡(δ0,τ02), \theta_1 = \mu + \delta, \qquad \theta_2 = \mu - \delta, \qquad \mu \sim \operatorname{normal}(\mu_0, \gamma_0^2), \qquad \delta \sim \operatorname{normal}(\delta_0, \tau_0^2), (8.1)

with a shared variance σ−2∼gamma⁡(ν0/2,ν0σ02/2)\sigma^{-2} \sim \operatorname{gamma}(\nu_0/2, \nu_0\sigma_0^2/2). With δ0=0\delta_0 = 0, the prior variance τ02\tau_0^2 dials continuously between the two extremes: as τ02→0\tau_0^2 \to 0, δ→0\delta \to 0 (complete pooling); as γ02,τ02→∞\gamma_0^2, \tau_0^2 \to \infty together with γ02=τ02\gamma_0^2 = \tau_0^2, θ1\theta_1 and θ2\theta_2 become prior-independent (no pooling); for finite τ02\tau_0^2, the data themselves determine how far apart the two school means should be pulled.

8.2Many Groups: Double Exchangeability and the Hierarchical Normal Model

Now suppose we observe m=100m = 100 public high schools from the 2002 ELS, with njn_j tenth-grade students in school jj (njn_j ranging from 44 to 3232). Two symmetries hold simultaneously:

  1. Within-group exchangeability: given school jj’s mean θj\theta_j and within-school variance σ2\sigma^2, student scores Y1,j,…,Ynj,jY_{1,j}, \dots, Y_{n_j,j} are exchangeable, so Yi,j∣θj,σ2∼i.i.d.normal⁡(θj,σ2)Y_{i,j} \mid \theta_j, \sigma^2 \stackrel{\text{i.i.d.}}{\sim} \operatorname{normal}(\theta_j, \sigma^2).
  2. Between-group exchangeability: the 100 schools are themselves a sample from the population of U.S. public high schools, so their means θ1,…,θm\theta_1, \dots, \theta_m are exchangeable across schools: θj∣μ,τ2∼i.i.d.normal⁡(μ,τ2)\theta_j \mid \mu, \tau^2 \stackrel{\text{i.i.d.}}{\sim} \operatorname{normal}(\mu, \tau^2).

Remark 8.1 (Sampling model vs. prior in hierarchical models). Hoff emphasizes a vital conceptual distinction: p(θ1,…,θm∣μ,τ2)p(\theta_1, \dots, \theta_m \mid \mu, \tau^2) is not merely a fixed prior—it is a between-group sampling model describing variation across schools, whose population parameters (μ,τ2)(\mu, \tau^2) are estimated from the data! Only the top-level distribution p(μ,τ2,σ2)p(\mu, \tau^2, \sigma^2) is a subjective prior.

Equipping (μ,τ2,σ2)(\mu, \tau^2, \sigma^2) with independent semiconjugate priors,

μ∼normal⁡(μ0,γ02),1τ2∼gamma⁡ ⁣(η02,η0τ022),1σ2∼gamma⁡ ⁣(ν02,ν0σ022), \mu \sim \operatorname{normal}(\mu_0, \gamma_0^2), \qquad \frac{1}{\tau^2} \sim \operatorname{gamma}\!\left(\frac{\eta_0}{2}, \frac{\eta_0 \tau_0^2}{2}\right), \qquad \frac{1}{\sigma^2} \sim \operatorname{gamma}\!\left(\frac{\nu_0}{2}, \frac{\nu_0 \sigma_0^2}{2}\right), (8.2)

yields the directed acyclic graph (DAG) in Figure 8.1 (left panel) and the joint posterior factorization

p(θ1,…,θm,μ,τ2,σ2∣𝐲1,…,𝐲m)∝p(μ) p(τ2) p(σ2)∏j=1mp(θj∣μ,τ2)∏i=1njp(yi,j∣θj,σ2). p(\theta_1, \dots, \theta_m, \mu, \tau^2, \sigma^2 \mid \mathbf{y}_1, \dots, \mathbf{y}_m) \propto p(\mu)\,p(\tau^2)\,p(\sigma^2) \prod_{j=1}^m p(\theta_j \mid \mu, \tau^2) \prod_{i=1}^{n_j} p(y_{i,j} \mid \theta_j, \sigma^2). (8.3)
Three panels: hierarchical normal DAG connecting hyperparameters (mu, tau^2) and sigma^2 to group means theta_j and observations y_j; scatter plot of sample means ybar_j versus shrunk hierarchical estimates theta_hat_j colored by group size; and scatter plot of shrinkage magnitude ybar_j minus theta_hat_j versus sample size n_j
Figure 8.1: Left: DAG of the hierarchical normal model. Center: hierarchical posterior means θ^j\hat{\theta}_j pull extreme group averages y¯j\bar{y}_j toward the grand mean μ^\hat{\mu}. Right: shrinkage magnitude ∣y¯j−θ^j∣\lvert \bar{y}_j - \hat{\theta}_j \rvert is largest for small groups (nj≈5n_j \approx 5) and vanishes for large groups.

8.2.1Full Conditionals and Modular Gibbs Sampling

Reading directly off the DAG factorization (8.3), each parameter depends only on its immediate Markov blanket (parents, children, and co-parents), yielding four standard full conditionals:

Proposition 8.2 (Full Conditionals of the Hierarchical Normal Model). Under (8.2)–(8.3), let N=∑j=1mnjN = \sum_{j=1}^m n_j and θ¯=1m∑j=1mθj\bar{\theta} = \frac{1}{m}\sum_{j=1}^m \theta_j. Then:

  1. Group means (conditionally independent across j=1,…,mj = 1, \dots, m):

    θj∣μ,τ2,σ2,𝐲j∼normal⁡ ⁣(njσ2y¯j+1τ2μnjσ2+1τ2,  1njσ2+1τ2). \theta_j \mid \mu, \tau^2, \sigma^2, \mathbf{y}_j \sim \operatorname{normal}\!\left( \frac{\frac{n_j}{\sigma^2}\bar{y}_j + \frac{1}{\tau^2}\mu}{\frac{n_j}{\sigma^2} + \frac{1}{\tau^2}},\; \frac{1}{\frac{n_j}{\sigma^2} + \frac{1}{\tau^2}} \right). (8.4)
  2. Population grand mean (depends on 𝐲\mathbf{y} only through θ1,…,θm\theta_1, \dots, \theta_m):

    μ∣𝜽,τ2∼normal⁡ ⁣(mτ2θ¯+1γ02μ0mτ2+1γ02,  1mτ2+1γ02). \mu \mid \boldsymbol{\theta}, \tau^2 \sim \operatorname{normal}\!\left( \frac{\frac{m}{\tau^2}\bar{\theta} + \frac{1}{\gamma_0^2}\mu_0}{\frac{m}{\tau^2} + \frac{1}{\gamma_0^2}},\; \frac{1}{\frac{m}{\tau^2} + \frac{1}{\gamma_0^2}} \right). (8.5)
  3. Between-group precision:

    1τ2∣𝜽,μ∼gamma⁡ ⁣(η0+m2,  η0τ02+∑j=1m(θj−μ)22). \frac{1}{\tau^2} \mid \boldsymbol{\theta}, \mu \sim \operatorname{gamma}\!\left( \frac{\eta_0 + m}{2},\; \frac{\eta_0 \tau_0^2 + \sum_{j=1}^m (\theta_j - \mu)^2}{2} \right). (8.6)
  4. Within-group precision:

    1σ2∣𝜽,𝐲∼gamma⁡ ⁣(ν0+N2,  ν0σ02+∑j=1m∑i=1nj(yi,j−θj)22). \frac{1}{\sigma^2} \mid \boldsymbol{\theta}, \mathbf{y} \sim \operatorname{gamma}\!\left( \frac{\nu_0 + N}{2},\; \frac{\nu_0 \sigma_0^2 + \sum_{j=1}^m \sum_{i=1}^{n_j} (y_{i,j} - \theta_j)^2}{2} \right). (8.7)

Notice how every single equation in Proposition 8.2 is just the Chapter 6 semiconjugate normal update applied at two levels: (μ,τ2)(\mu, \tau^2) treat θ1,…,θm\theta_1, \dots, \theta_m as their “data,” while each θj\theta_j treats (μ,τ2)(\mu, \tau^2) as its “prior.”Note a minor typographical slip on p. 135 of the 2009 printing of Hoff, which prints 1/τ21/\tau^2 instead of μ/τ2\mu/\tau^2 in the numerator of (8.4) (Hoff’s R code on p. 138 correctly writes mu/tau2); see Appendix C.

8.3Adaptive Shrinkage and Stein’s Paradox

Rewriting the conditional expectation in (8.4) exposes the exact shrinkage weight for group jj:

E[θj∣μ,τ2,σ2,𝐲j]=Bj y¯j+(1−Bj) μ,Bj=nj/σ2nj/σ2+1/τ2=njnj+σ2/τ2. E[\theta_j \mid \mu, \tau^2, \sigma^2, \mathbf{y}_j] = B_j\,\bar{y}_j + (1 - B_j)\,\mu, \qquad B_j = \frac{n_j / \sigma^2}{n_j / \sigma^2 + 1 / \tau^2} = \frac{n_j}{n_j + \sigma^2 / \tau^2}. (8.8)

Two forces control how aggressively group jj borrows strength from the grand mean μ\mu:

  • Sample size njn_j: small groups (small njn_j) receive smaller weight BjB_j and shrink strongly toward μ\mu; large groups stay near y¯j\bar{y}_j (Figure 8.1, right).
  • Variance ratio σ2/τ2\sigma^2/\tau^2: when schools are genuinely similar (τ2≪σ2\tau^2 \ll \sigma^2), σ2/τ2\sigma^2/\tau^2 is large and shrinkage is strong; when schools truly differ widely (τ2≫σ2\tau^2 \gg \sigma^2), Bj→1B_j \to 1 and the model lets each school stand on its own.

In the 2002 ELS math scores (m=100m = 100 schools, posterior μ^≈48.1\hat{\mu} \approx 48.1, τ^≈5.0\hat{\tau} \approx 5.0, σ^≈9.2\hat{\sigma} \approx 9.2, so σ^2/τ^2≈3.4\hat{\sigma}^2/\hat{\tau}^2 \approx 3.4), this produces a dramatic ranking reversal among low-scoring schools: School 82 has the lower raw sample mean (y¯82=38.76\bar{y}_{82} = 38.76) based on only n82=5n_{82} = 5 students, so its hierarchical estimate shrinks strongly upward toward μ^\hat{\mu} to 42.5342.53; School 46 has a slightly higher raw mean (y¯46=40.18\bar{y}_{46} = 40.18) based on n46=21n_{46} = 21 students, so it shrinks far less to 41.3141.31—reversing their order so that θ^82>θ^46\hat{\theta}_{82} > \hat{\theta}_{46}! This echoes Stein’s phenomenonCharles Stein. Inadmissibility of the Usual Estimator for the Mean of a Multivariate Normal Distribution. In Proceedings of the Third Berkeley Symposium on Mathematical Statistics and Probability, volume 1, pages 197–206, Berkeley, 1956. University of California Press; and Bradley Efron and Carl Morris. Stein’s Estimation Rule and Its Competitors—An Empirical Bayes Approach. Journal of the American Statistical Association, 68:117–130, 1973: with known equal variances, suitable shrinkage toward a fixed point (m≥3m \ge 3) or toward the estimated grand mean (m≥4m \ge 4) strictly dominates the unpooled sample means under total squared error ∑j=1m(θ^j−θj)2\sum_{j=1}^m (\hat{\theta}_j - \theta_j)^2.

Remark 8.3 (Two modern caveats: variance priors and non-centered parameterizations).

  1. Prior sensitivity for τ2\tau^2 when mm is small: With m=100m = 100 schools, the data pin down τ2\tau^2 easily. When mm is small (say m=5m = 5), however, standard inverse-gamma⁡(ϵ,ϵ)\operatorname{inverse-gamma}(\epsilon, \epsilon) priors on τ2\tau^2 can severely distort inference near τ=0\tau = 0; weakly informative half-tt or half-Cauchy priors on the standard deviation τ\tau are far saferAndrew Gelman. Prior Distributions for Variance Parameters in Hierarchical Models. Bayesian Analysis, 1(3):515–534, 2006.
  2. Centered vs. non-centered mixing: When τ2\tau^2 is small relative to σ2/nj\sigma^2/n_j, the centered parameterization θj∼𝒩(μ,τ2)\theta_j \sim \mathcal{N}(\mu, \tau^2) creates a narrow “funnel” geometry where Gibbs steps stall. Writing θj=μ+τ θ~j\theta_j = \mu + \tau\,\tilde{\theta}_j with θ~j∼i.i.d.𝒩(0,1)\tilde{\theta}_j \stackrel{\text{i.i.d.}}{\sim} \mathcal{N}(0, 1) (the non-centered parameterizationOmiros Papaspiliopoulos, Gareth O. Roberts, and Martin Sköld. A General Framework for the Parametrization of Hierarchical Models. Statistical Science, 22(1):59–73, 2007) decouples θ~j\tilde{\theta}_j from τ\tau and restores fast mixing.

8.4Heterogeneous Group Variances

What if different schools also have different within-school spreads σ12,…,σm2\sigma_1^2, \dots, \sigma_m^2? Adding a second hierarchy for the variances borrows strength across groups for variance estimation as well:

1σj2∣ν0,σ02∼i.i.d.gamma⁡ ⁣(ν02, ν0σ022),σ02∼gamma⁡(a,b),p(ν0)∝e−αν0(ν0∈{1,2,…,νmax⁡}). \frac{1}{\sigma_j^2} \mid \nu_0, \sigma_0^2 \stackrel{\text{i.i.d.}}{\sim} \operatorname{gamma}\!\left(\frac{\nu_0}{2},\, \frac{\nu_0 \sigma_0^2}{2}\right), \qquad \sigma_0^2 \sim \operatorname{gamma}(a, b), \qquad p(\nu_0) \propto e^{-\alpha \nu_0} \quad (\nu_0 \in \{1, 2, \dots, \nu_{\max}\}). (8.9)

Each σj−2\sigma_j^{-2} now has its own conjugate gamma full conditional gamma⁡(ν0+nj2, ν0σ02+∑i=1nj(yi,j−θj)22)\operatorname{gamma}\bigl(\frac{\nu_0 + n_j}{2},\, \frac{\nu_0 \sigma_0^2 + \sum_{i=1}^{n_j}(y_{i,j} - \theta_j)^2}{2}\bigr). Multiplying ∏j=1mp(σj−2∣ν0,σ02)\prod_{j=1}^m p(\sigma_j^{-2} \mid \nu_0, \sigma_0^2) by σ02(a−1)e−bσ02\sigma_0^{2(a - 1)} e^{-b\sigma_0^2} yields the exact conjugate full conditional for the shared variance hyperparameter σ02\sigma_0^2:

p(σ02∣ν0,σ12,…,σm2)∝(σ02)a−1e−bσ02∏j=1m[(σ02)ν0/2exp⁡ ⁣(−ν0σ022σj2)]=(σ02)a+mν02−1exp⁡ ⁣{−(b+ν02∑j=1m1σj2)σ02}, p(\sigma_0^2 \mid \nu_0, \sigma_1^2, \dots, \sigma_m^2) \propto (\sigma_0^2)^{a - 1} e^{-b\sigma_0^2} \prod_{j=1}^m \left[(\sigma_0^2)^{\nu_0/2} \exp\!\left(-\frac{\nu_0 \sigma_0^2}{2\sigma_j^2}\right)\right] = (\sigma_0^2)^{a + \frac{m\nu_0}{2} - 1} \exp\!\left\{-\left(b + \frac{\nu_0}{2}\sum_{j=1}^m \frac{1}{\sigma_j^2}\right)\sigma_0^2\right\}, (8.10)

which is gamma⁡ ⁣(a+mν02,  b+ν02∑j=1mσj−2)\operatorname{gamma}\!\bigl(a + \frac{m\nu_0}{2},\; b + \frac{\nu_0}{2}\sum_{j=1}^m \sigma_j^{-2}\bigr).Watch out for a missing factor of ν0\nu_0 in the rate parameter printed on p. 144 of Hoff (2009), which writes b+1/2∑σj−2b + 1/2\sum \sigma_j^{-2} instead of b+ν0/2∑σj−2b + \nu_0/2\sum \sigma_j^{-2}; see Appendix C. Because ν0\nu_0 enters inside the gamma function Γ(ν0/2)\Gamma(\nu_0/2), it has no standard conjugate density; restricting ν0\nu_0 to a discrete grid {1,2,…,νmax⁡}\{1, 2, \dots, \nu_{\max}\} lets us evaluate its exact log-full conditional (using the log-sum-exp trick for numerical stability) and draw ν0\nu_0 from a categorical distribution at every Gibbs step:

log⁡p(ν0∣σ02,σ12,…,σm2)=const+m ⁣[ν02log⁡ ⁣(ν0σ022)−log⁡Γ ⁣(ν02)]+(ν02−1)∑j=1mlog⁡ ⁣(1σj2)−ν0σ022∑j=1m1σj2−αν0. \begin{aligned} \log p(\nu_0 \mid \sigma_0^2, \sigma_1^2, \dots, \sigma_m^2) &= \text{const} + m\!\left[\frac{\nu_0}{2}\log\!\left(\frac{\nu_0 \sigma_0^2}{2}\right) - \log\Gamma\!\left(\frac{\nu_0}{2}\right)\right] \\ &\quad + \left(\frac{\nu_0}{2} - 1\right)\sum_{j=1}^m \log\!\left(\frac{1}{\sigma_j^2}\right) - \frac{\nu_0 \sigma_0^2}{2}\sum_{j=1}^m \frac{1}{\sigma_j^2} - \alpha \nu_0. \end{aligned} (8.11)
import numpy as np

# Hierarchical normal Gibbs sampler on synthetic 60-school ELS-like data
rng = np.random.default_rng(42)
m = 60
n_j = rng.integers(4, 25, size=m)
th_true = rng.normal(48.1, 4.5, size=m)
groups = [rng.normal(th, 9.2, size=nj) for th, nj in zip(th_true, n_j)]
ybar = np.array([g.mean() for g in groups])
ss_within = np.array([np.sum((g - g.mean())**2) for g in groups])
N = int(n_j.sum())

# Weakly informative hyperparameters (matching Hoff Ch. 8)
mu0, g20 = 50.0, 25.0
eta0, t20 = 1.0, 100.0
nu0, s20 = 1.0, 100.0

S = 4_000
mu_s, tau2_s, sig2_s = np.empty(S), np.empty(S), np.empty(S)
theta_s = np.empty((S, m))
th_cur, mu_cur, t2_cur, s2_cur = ybar.copy(), ybar.mean(), np.var(ybar, ddof=1), 85.0

for s in range(S):
    # 1. Vectorized full conditional for all m group means theta_j
    v_j = 1.0 / (n_j / s2_cur + 1.0 / t2_cur)
    m_j = v_j * (n_j * ybar / s2_cur + mu_cur / t2_cur)
    th_cur = rng.normal(m_j, np.sqrt(v_j))
    # 2. Grand mean mu | theta, tau^2
    v_mu = 1.0 / (m / t2_cur + 1.0 / g20)
    m_mu = v_mu * (m * th_cur.mean() / t2_cur + mu0 / g20)
    mu_cur = rng.normal(m_mu, np.sqrt(v_mu))
    # 3. Between-group variance tau^2 | theta, mu
    t2_cur = 1.0 / rng.gamma((eta0 + m) / 2.0,
                             2.0 / (eta0 * t20 + np.sum((th_cur - mu_cur)**2)))
    # 4. Within-group variance sigma^2 | theta, y (fast O(m) sum of squares)
    sse = np.sum(ss_within + n_j * (ybar - th_cur)**2)
    s2_cur = 1.0 / rng.gamma((nu0 + N) / 2.0, 2.0 / (nu0 * s20 + sse))
    theta_s[s], mu_s[s], tau2_s[s], sig2_s[s] = th_cur, mu_cur, t2_cur, s2_cur

th_hat = theta_s[500:].mean(axis=0)
print(f"Posterior means: mu={mu_s[500:].mean():.2f}, tau={np.sqrt(tau2_s[500:]).mean():.2f}, "
      f"sigma={np.sqrt(sig2_s[500:]).mean():.2f}")
print(f"Total MSE vs truth: unpooled={np.mean((ybar - th_true)**2):.2f}, "
      f"hierarchical={np.mean((th_hat - th_true)**2):.2f}")

8.5Modern Developments: Complexity Priors, Assisted Intervals, and Ranking

8.5.1Penalized Complexity Priors

A penalized complexity (PC) prior derives the prior on τ\tau from how far the hierarchical model departs from complete pooling.Beyond Hoff (2009). This material is not in Hoff’s book; Simpson et al. (2017) supply a general construction that complements the half-tt advice for τ\tau.Daniel Simpson, Håvard Rue, Andrea Riebler, Thiago G. Martins, and Sigrunn H. Sørbye. Penalising Model Component Complexity: A Principled, Practical Approach to Constructing Priors. Statistical Science, 32(1):1–28, 2017. doi: 10.1214/16-STS576 Take τ=0\tau = 0 as the simple base model, measure departure by d(τ)=2KL⁡d(\tau) = \sqrt{2\operatorname{KL}}, and put a constant-rate (exponential) prior on dd. Approaching the base through normal⁡(0,ϵ2)\operatorname{normal}(0, \epsilon^2) with ϵ→0\epsilon \to 0,

2KL⁡(𝒩(0,τ2) ∥ 𝒩(0,ϵ2))=τ2ϵ2−1−log⁡τ2ϵ2  ∼  τϵ  ⟹  τ∼exponential⁡(λ),λ=−log⁡αU, \sqrt{2\operatorname{KL}\bigl(\mathcal{N}(0, \tau^2)\,\|\,\mathcal{N}(0, \epsilon^2)\bigr)} = \sqrt{\frac{\tau^2}{\epsilon^2} - 1 - \log\frac{\tau^2}{\epsilon^2}} \;\sim\; \frac{\tau}{\epsilon} \;\Longrightarrow\; \tau \sim \operatorname{exponential}(\lambda), \quad \lambda = -\frac{\log\alpha}{U}, (8.12)

where (U,α)(U, \alpha) encodes one interpretable statement, Pr⁡(τ>U)=α\Pr(\tau > U) = \alpha. Distance is proportional to τ\tau, and the constant 1/ϵ1/\epsilon is absorbed into λ\lambda.

Compare (8.2) with η0=1\eta_0 = 1, τ02=100\tau_0^2 = 100, under which 100/τ2∼χ12100/\tau^2 \sim \chi^2_1 and Pr⁡(τ<3)≈0.0009\Pr(\tau < 3) \approx 0.0009: the prior virtually forbids strong pooling. Taking Pr⁡(τ>10)=0.05\Pr(\tau > 10) = 0.05 gives λ≈0.30\lambda \approx 0.30 and Pr⁡(τ<3)≈0.59\Pr(\tau < 3) \approx 0.59, with its density maximal, rather than vanishing, at τ=0\tau = 0.

When to use it. With m=100m = 100 schools the choice hardly matters. For small mm (see Remark 8.3), or several variance components needing comparable scaling, prefer the PC prior; the update (8.6) is then no longer conjugate, so draw τ\tau by a one-dimensional slice or Metropolis step.

8.5.2Frequentist-Assisted Bayes Confidence Intervals

FAB intervals borrow strength across groups, yet cover each θj\theta_j with exactly the nominal frequency.Beyond Hoff (2009). This material is not in Hoff’s book; Yu and Hoff himself (2018) develop it, extending an older idea of Pratt to adaptive multigroup intervals.C Yu and P D Hoff. Adaptive multigroup confidence intervals with constant coverage. Biometrika, 105(2):319–335, 2018. doi: 10.1093/biomet/asy009 The credible interval centered at (8.8) covers 95%95\% only on average over θj∼normal⁡(μ,τ2)\theta_j \sim \operatorname{normal}(\mu, \tau^2), undercovering schools far from μ\mu. With sj=σ/njs_j = \sigma/\sqrt{n_j} known, split the test error α\alpha unevenly as αw\alpha w below and α(1−w)\alpha(1 - w) above, with ww depending on the tested θ\theta:

C(y¯j)={θ:y¯j+sj Φ−1(α(1−w(θ)))≤θ≤y¯j+sj Φ−1(1−αw(θ))},Φ−1(αw)−Φ−1(α(1−w))=2sj(θ−μ)τ2. \begin{aligned} C(\bar{y}_j) &= \Bigl\{\theta : \bar{y}_j + s_j\,\Phi^{-1}\bigl(\alpha(1 - w(\theta))\bigr) \le \theta \le \bar{y}_j + s_j\,\Phi^{-1}\bigl(1 - \alpha w(\theta)\bigr)\Bigr\}, \\ &\qquad \Phi^{-1}(\alpha w) - \Phi^{-1}(\alpha(1 - w)) = \frac{2 s_j(\theta - \mu)}{\tau^2}. \end{aligned} (8.13)

Every test has level α\alpha for any w(θ)w(\theta), so coverage is exactly 1−α1 - \alpha at every θj\theta_j, even if the prior is wrong, provided (μ,τ2)(\mu, \tau^2) are estimated independently of y¯j\bar{y}_j—for instance, from the other schools. The stated ww minimizes expected length under the prior: equating the ratio of the normal⁡(θ,sj2)\operatorname{normal}(\theta, s_j^2) to the marginal normal⁡(μ,sj2+τ2)\operatorname{normal}(\mu, s_j^2 + \tau^2) density at both endpoints of the acceptance region puts its midpoint at θ+sj2(θ−μ)/τ2\theta + s_j^2(\theta - \mu)/\tau^2. As τ→∞\tau \to \infty, w→12w \to \frac12 recovers the usual zz-interval.

For School 82 (plug-in μ=48.1\mu = 48.1, τ=5\tau = 5, σ=9.2\sigma = 9.2) the zz-interval is [30.7,46.8][30.7, 46.8], FAB gives [32.0,47.4][32.0, 47.4], and the credible interval is [36.3,48.8][36.3, 48.8].

When to use it. Report Hoff’s credible intervals for prior-averaged accuracy; choose FAB when every group must be covered at the nominal rate. Unknown σ\sigma calls for the tt-version.

8.5.3Ranking as a Compound Decision and the NPMLE

Ranking schools is a decision problem with its own loss, and the posterior mean need not be the right ranking score.Beyond Hoff (2009). This material is not in Hoff’s book; Gu and Koenker (2023) treat ranking and selection as compound decisions, while the nonparametric MLE—an idea from the 1950s—became routinely computable through the convex optimization of Koenker and Mizera (2014).Jiaying Gu and Roger Koenker. Invidious Comparisons: Ranking and Selection as Compound Decisions. Econometrica, 91(1):1–41, 2023. doi: 10.3982/ECTA19304; and Roger Koenker and Ivan Mizera. Convex Optimization, Shape Constraints, Compound Decisions, and Empirical Bayes Rules. Journal of the American Statistical Association, 109(506):674–685, 2014. doi: 10.1080/01621459.2013.869224 Suppose we flag a fraction of schools as belonging to the lowest α\alpha of the population, with θj∼G\theta_j \sim G and θα\theta_\alpha the α\alpha-quantile of GG. Subject to a cap on the number flagged, the expected number of correct flags is maximized by thresholding the posterior tail probability

vα(y¯j,nj)=Pr⁡(θj≤θα∣y¯j,nj)=∫−∞θα𝒩(y¯j∣θ,σ2/nj) dG(θ)∫𝒩(y¯j∣θ,σ2/nj) dG(θ), v_\alpha(\bar{y}_j, n_j) = \Pr(\theta_j \le \theta_\alpha \mid \bar{y}_j, n_j) = \frac{\int_{-\infty}^{\theta_\alpha} \mathcal{N}(\bar{y}_j \mid \theta, \sigma^2/n_j)\,dG(\theta)}{\int \mathcal{N}(\bar{y}_j \mid \theta, \sigma^2/n_j)\,dG(\theta)}, (8.14)

because the expected number of correct flags is the sum of the flagged vαv_\alpha, so one should take the largest. With equal njn_j this agrees with ranking by (8.8); with unequal njn_j it need not.

Revisit the reversal of Schools 46 and 82, whose plug-in posteriors are normal⁡(41.28,1.862)\operatorname{normal}(41.28, 1.86^2) and normal⁡(42.53,3.182)\operatorname{normal}(42.53, 3.18^2). For the lowest decile (θ0.10=41.69\theta_{0.10} = 41.69), v=0.59v = 0.59 for School 46 and 0.400.40 for School 82, agreeing with the posterior means. For the lowest 2.5%2.5\% (θ0.025=38.30\theta_{0.025} = 38.30), v=0.055v = 0.055 against 0.0910.091: School 82’s wider posterior puts it first, reversing the reversal.

Normality of GG matters in such tails. The NPMLE G^=arg⁡max⁡G∑jlog⁡∫𝒩(y¯j∣θ,σ2/nj) dG(θ)\hat{G} = \arg\max_G \sum_j \log\int \mathcal{N}(\bar{y}_j \mid \theta, \sigma^2/n_j)\,dG(\theta) is a convex problem with a discrete solution, and replaces GG in (8.14).

When to use it. Hoff’s posterior means suit squared-error estimation. To select or rank, use vαv_\alpha at the cutoff that matters, with G^\hat{G} when mm is large and normality doubtful.

8.5.4Computational & Symbolic Check

The script checks the PC tail statement below (8.12), solves (8.13) for School 82, and simulates the coverage of FAB and credible intervals at fixed values of θ\theta.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 optimize, stats

U, al, rng, q = 10.0, 0.05, np.random.default_rng(8), stats.norm.ppf
lam = -np.log(al) / U
tau = rng.exponential(1 / lam, size=1_000_000)   # NumPy scale = 1/rate
print(f"PC: lambda = {lam:.4f}, Pr(tau > U) = {np.mean(tau > U):.4f}, Pr(tau < 3) = {np.mean(tau < 3):.4f}")

mu, t, s, yb = 48.1, 5.0, 9.2 / np.sqrt(5), 38.76
def w(th):   # optimal split of the test error alpha at the tested value th
    return optimize.brentq(lambda x: q(al * x) - q(al * (1 - x)) - 2 * s * (th - mu) / t**2, 1e-15, 1 - 1e-15)
lo = optimize.brentq(lambda th: yb + s * q(al * (1 - w(th))) - th, 30, 60)
hi = optimize.brentq(lambda th: yb - s * q(al * w(th)) - th, 30, 60)   # -q(p) = q(1 - p)
print(f"School 82 FAB interval: [{lo:.1f}, {hi:.1f}]")
B = t**2 / (t**2 + s**2)
for th in (35.0, 48.1, 60.0):
    Y, wt = rng.normal(th, s, size=400_000), w(th)
    fab = (Y + s * q(al * (1 - wt)) <= th) & (th <= Y - s * q(al * wt))
    cred = np.abs(B * Y + (1 - B) * mu - th) <= q(1 - al / 2) * np.sqrt(B) * s
    print(f"theta = {th}: coverage FAB {fab.mean():.4f}, credible {cred.mean():.4f}")
Integrate the exponential density beyond UU.
// Chapter 8 (modern): exact PC-prior tail probability & exact FAB coverage
const pcTail = await show`Assuming[lam > 0 && u > 0, Integrate[lam*Exp[-lam*x], {x, u, Infinity}]]`
Pr⁡(τ>U)=∫U∞λe−λτ dτ=e−λU \Pr(\tau > U) = \int_U^\infty \lambda e^{-\lambda\tau}\,d\tau = e^{-\lambda U} Substitute the PC rate λ=−log⁡(α)/U\lambda = -\log(\alpha)/U: the tail mass is exactly α\alpha.
await show`Simplify[${pcTail} /. lam -> -Log[al]/u]`
Pr⁡(τ>U) ∣λ=−log⁡(α)/U=α \Pr(\tau > U)\,\big|_{\lambda = -\log(\alpha)/U} = \alpha Instantiate λ\lambda and Pr⁡(τ<3)\Pr(\tau < 3) at (U,α)=(10,0.05)(U, \alpha) = (10, 0.05).
await show`N[{lam, 1 - (${pcTail} /. u -> 3)} /. lam -> -Log[5/100]/10]`
(λ,  Pr⁡(τ<3))U=10,  α=0.05={0.299573,0.592909} \bigl(\lambda,\; \Pr(\tau < 3)\bigr)_{U=10,\;\alpha=0.05} = \{0.299573,0.592909\} Lower normal quantile of the FAB interval, via Φ−1(p)=−2 erfc⁡−1(2p)\Phi^{-1}(p) = -\sqrt{2}\,\operatorname{erfc}^{-1}(2p).
// Normal quantile: Phi^(-1)(p) = -Sqrt[2]*InverseErfc[2*p]
const qLo = await show`-Sqrt[2]*InverseErfc[2*al*(1 - w)]`
Φ−1(α(1−w))=−2 erfc⁡−1(2α(1−w)) \Phi^{-1}\bigl(\alpha(1 - w)\bigr) = -\sqrt{2}\, \operatorname{erfc}^{-1}\bigl(2 \alpha (1-w)\bigr) Upper normal quantile of the FAB interval, by the same identity.
const qHi = await show`-Sqrt[2]*InverseErfc[2*(1 - al*w)]`
Φ−1(1−αw)=−2 erfc⁡−1(2(1−αw)) \Phi^{-1}(1 - \alpha w) = -\sqrt{2}\, \operatorname{erfc}^{-1}\bigl(2 (1-\alpha w)\bigr) Evaluate Pr⁡θ(θ∈C)=Φ(Φ−1(1−αw))−Φ(Φ−1(α(1−w)))\Pr_\theta(\theta \in C) = \Phi\bigl(\Phi^{-1}(1 - \alpha w)\bigr) - \Phi\bigl(\Phi^{-1}(\alpha(1 - w))\bigr), since (θ−Y¯)/s(\theta - \bar{Y})/s is standard normal at every θ\theta: the split ww cancels.
await show`FullSimplify[1/2*Erfc[-(${qHi})/Sqrt[2]] - 1/2*Erfc[-(${qLo})/Sqrt[2]]]`
Pr⁡θ(θ∈C(Y¯))=1−α \Pr_\theta\bigl(\theta \in C(\bar{Y})\bigr) = 1-\alpha Evaluate the left side of the ww-equation at w=12w = \frac12: it vanishes, so θ=μ\theta = \mu (or τ→∞\tau \to \infty) recovers the zz-interval.
await show`Simplify[(-Sqrt[2]*InverseErfc[2*al*w] + Sqrt[2]*InverseErfc[2*al*(1 - w)]) /. w -> 1/2]`
[Φ−1(αw)−Φ−1(α(1−w))]w=1/2=0 \Bigl[\Phi^{-1}(\alpha w) - \Phi^{-1}(\alpha(1 - w))\Bigr]_{w = 1/2} = 0

8.6Symbolic Check in Wolfram Language

Derives the two-group prior correlation from (θ1,θ2)=(μ+δ,μ−δ)(\theta_1, \theta_2) = (\mu + \delta, \mu - \delta), the shrinkage weight BjB_j, the symbolic shrunk-mean gap θ^2−θ^1\hat{\theta}_2 - \hat{\theta}_1 governing ranking reversals between two groups (n1,y¯1)(n_1, \bar{y}_1) and (n2,y¯2)(n_2, \bar{y}_2), and (non-circularly from the log-density derivative) both the shape and ν0/2\nu_0/2-scaled rate of σ02\sigma_0^2, then evaluates plug-in shrinkage on Schools 46 and 82 (compare Hoff’s full MCMC means 41.3141.31 and 42.5342.53).

Push diag⁡(γ02,τ02)\operatorname{diag}(\gamma_0^2, \tau_0^2) through (θ1,θ2)⊤=A(μ,δ)⊤(\theta_1, \theta_2)^\top = A(\mu, \delta)^\top to get the prior covariance of the two group means.
// Chapter 8: Symbolic two-group correlation, ranking-reversal condition & sigma_0^2 full conditional
const A = [[1, 1], [1, -1]]
const cov2 = await show`${A} . DiagonalMatrix[{g0^2, t0^2}] . Transpose[${A}]`
Cov⁡(θ1,θ2)=Adiag⁡(γ02,τ02)A⊤=(γ02+τ02γ02−τ02γ02−τ02γ02+τ02) \operatorname{Cov}(\theta_1, \theta_2) = A\operatorname{diag}(\gamma_0^2, \tau_0^2)A^\top = \left( \begin{array}{cc} \gamma_0^2+\tau_0^2 & \gamma_0^2-\tau_0^2 \\ \gamma_0^2-\tau_0^2 & \gamma_0^2+\tau_0^2 \\ \end{array} \right) Compute the prior correlation between two group means under the (μ,δ)(\mu, \delta) parameterization.
await show`Together[${cov2}[[1, 2]] / ${cov2}[[1, 1]]]`
(Adiag⁡(γ02,τ02)A⊤)1,2(Adiag⁡(γ02,τ02)A⊤)1,1=γ02−τ02γ02+τ02 \begin{aligned} &\frac{(A\operatorname{diag}(\gamma_0^2, \tau_0^2)A^\top)_{1,2}}{(A\operatorname{diag}(\gamma_0^2, \tau_0^2)A^\top)_{1,1}} \\ &= \frac{\gamma_0^2-\tau_0^2}{\gamma_0^2+\tau_0^2} \end{aligned} Simplify the hierarchical shrinkage weight Bj=(nj/σ2)/(nj/σ2+1/τ2)B_j = (n_j/\sigma^2)/(n_j/\sigma^2 + 1/\tau^2).
// Shrinkage weight B_j and symbolic shrunk-mean gap thetaHat_2 - thetaHat_1
const Bj = await show`Together[(nj / s2) / (nj / s2 + 1 / t2)]`
Bj=nj/σ2nj/σ2+1/τ2=njτ2σ2+njτ2 B_j = \frac{n_j/\sigma^2}{n_j/\sigma^2 + 1/\tau^2} = \frac{n_j \tau^2}{\sigma^2 +n_j \tau^2} Derive the closed-form difference θ^2−θ^1\hat{\theta}_2 - \hat{\theta}_1 between two shrunk group means.
const gapSym = await show`FullSimplify[((${Bj} /. nj -> n2)*yb2 + (1 - (${Bj} /. nj -> n2))*mu)
  - ((${Bj} /. nj -> n1)*yb1 + (1 - (${Bj} /. nj -> n1))*mu)]`
θ^2−θ^1=[B2y¯2+(1−B2)μ]−[B1y¯1+(1−B1)μ]=τ2(μ(n1−n2)σ2−n1(σ2+n2τ2)y¯1+n2(σ2+n1τ2)y¯2)(σ2+n1τ2)(σ2+n2τ2) \begin{aligned} &\hat{\theta}_2 - \hat{\theta}_1 = \bigl[B_2\bar{y}_2 + (1-B_2)\mu\bigr] - \bigl[B_1\bar{y}_1 + (1-B_1)\mu\bigr] \\ &= \frac{\tau^2 (\mu (n_1 -n_2) \sigma^2 -n_1 (\sigma^2 +n_2 \tau^2) \bar{y}_1 +n_2 (\sigma^2 +n_1 \tau^2) \bar{y}_2)}{(\sigma^2 +n_1 \tau^2) (\sigma^2 +n_2 \tau^2)} \end{aligned} Write log⁡p(σ02∣ν0,x1,x2,x3)\log p(\sigma_0^2 \mid \nu_0, x_1, x_2, x_3): a gamma⁡(a,b)\operatorname{gamma}(a, b) prior times three gamma⁡(ν0/2,ν0σ02/2)\operatorname{gamma}(\nu_0/2, \nu_0\sigma_0^2/2) likelihood terms (m=3m = 3).
// Non-circular derivation of sigma_0^2 full conditional shape & rate (m = 3)
const logp = await show`(a - 1)*Log[s0] - b*s0 + Sum[nu0/2*Log[nu0*s0/2] - LogGamma[nu0/2]
  + (nu0/2 - 1)*Log[x] - nu0*s0*x/2, {x, {x1, x2, x3}}]`
log⁡p(σ02∣ν0,x1,x2,x3)=(a−1)log⁡σ02−b σ02+3ν02log⁡ν0σ022−3log⁡Γ ⁣(ν02)+(ν02−1)(log⁡x1+log⁡x2+log⁡x3)−ν0σ022(x1+x2+x3) \begin{aligned} &\log p(\sigma_0^2 \mid \nu_0, x_1, x_2, x_3) = (a-1)\log\sigma_0^2 - b\,\sigma_0^2 + \frac{3\nu_0}{2}\log\frac{\nu_0\sigma_0^2}{2} - 3\log\Gamma\!\left(\frac{\nu_0}{2}\right) \\ &\quad + \left(\frac{\nu_0}{2}-1\right)(\log x_1+\log x_2+\log x_3) - \frac{\nu_0\sigma_0^2}{2}(x_1+x_2+x_3) \end{aligned} Differentiate in σ02\sigma_0^2 and expand into powers of σ02\sigma_0^2.
const dLogp = await show`Expand[D[${logp}, s0]]`
∂∂σ02log⁡p=−b−1σ02+aσ02+3ν02σ02−ν0x12−ν0x22−ν0x32 \frac{\partial}{\partial \sigma_0^2}\log p = -b-\frac{1}{\sigma_0^2}+\frac{a}{\sigma_0^2}+\frac{3 \nu_0}{2 \sigma_0^2}-\frac{\nu_0 x_1}{2}-\frac{\nu_0 x_2}{2}-\frac{\nu_0 x_3}{2} Extract the [(σ02)−1][(\sigma_0^2)^{-1}] coefficient of ∂σ02log⁡p\partial_{\sigma_0^2}\log p to verify the Gamma full-conditional shape (m=3m = 3).
await show`Simplify[1 + Coefficient[${dLogp}, s0, -1]]`
1+[(σ02)−1]∂∂σ02log⁡p(σ02∣ν0,x1,x2,x3)=a+3ν02 \begin{aligned} &1 + [(\sigma_0^2)^{-1}]\frac{\partial}{\partial \sigma_0^2}\log p(\sigma_0^2 \mid \nu_0, x_1, x_2, x_3) \\ &= a+\frac{3 \nu_0}{2} \end{aligned} Extract the constant term of −∂σ02log⁡p-\partial_{\sigma_0^2}\log p to verify the Gamma full-conditional rate.
await show`Simplify[-Coefficient[${dLogp}, s0, 0]]`
−[(σ02)0]∂∂σ02log⁡p(σ02∣ν0,x1,x2,x3)=b+12ν0(x1+x2+x3) \begin{aligned} &-[(\sigma_0^2)^0]\frac{\partial}{\partial \sigma_0^2}\log p(\sigma_0^2 \mid \nu_0, x_1, x_2, x_3) \\ &= b+\frac{1}{2} \nu_0 (x_1 +x_2 +x_3) \end{aligned} Instantiate shrinkage weights and posterior means for School 46 (n=21n=21) and School 82 (n=5n=5).
// Instantiate plug-in shrinkage on School 46 (n1=21, yb1=40.18) vs School 82 (n2=5, yb2=38.76)
await show`N[Table[{w, w*p[[2]] + (1 - w)*48.1} /. w -> (${Bj} /. {nj -> p[[1]], t2 -> 25, s2 -> 84.64}), {p, {{21, 40.18}, {5, 38.76}}}]]`
(Bj,  θ^j)School 46, School 82=(0.86116441.27960.5962642.5309) \begin{aligned} &\bigl(B_j,\; \hat{\theta}_j\bigr)_{\text{School 46, School 82}} \\ &= \left( \begin{array}{cc} 0.861164 & 41.2796 \\ 0.59626 & 42.5309 \\ \end{array} \right) \end{aligned} Evaluate the shrunk difference θ^82−θ^46\hat{\theta}_{82} - \hat{\theta}_{46}, confirming the ranking reversal over the raw gap.
await show`N[${gapSym} /. {n1 -> 21, yb1 -> 40.18, n2 -> 5, yb2 -> 38.76, mu -> 48.1, t2 -> 25, s2 -> 84.64}]`
(θ^82−θ^46)shrunk=1.25135 (\hat{\theta}_{82} - \hat{\theta}_{46})_{\text{shrunk}} = 1.25135

8.7Exercises

Exercise 8.4. In the Python script above, vary the true between-school standard deviation τ∈{1,3,5,15}\tau \in \{1, 3, 5, 15\} while holding σ=9.2\sigma = 9.2 fixed. Compare the total estimation MSE 1m∑j=1m(θ^j−θj)2\frac{1}{m}\sum_{j=1}^m (\hat{\theta}_j - \theta_j)^2 of the hierarchical posterior mean against both the unpooled sample means y¯j\bar{y}_j and the completely pooled grand mean y¯\bar{y}. When is the gain from partial pooling largest?

Symbolic Solution & InstantiationSimplify the oracle hierarchical Bayes risk (MSE) (σ2/n)τ2/(σ2/n+τ2)(\sigma^2/n)\tau^2 / (\sigma^2/n + \tau^2) for equal group sizes nn.
// Ex 8.1: Oracle Bayes risk (MSE, equal n) of unpooled, completely pooled, and hierarchical partial pooling
const rPart = await show`FullSimplify[(s2/n)*t2 / (s2/n + t2)]`
Risk⁡hier(σ2,τ2,n)=σ2τ2σ2+nτ2 \begin{aligned} &\operatorname{Risk}_{\text{hier}}(\sigma^2, \tau^2, n) \\ &= \frac{\sigma^2 \tau^2}{\sigma^2 +n \tau^2} \end{aligned} Express the risk ratio relative to unpooled y¯j\bar{y}_j as the shrinkage weight BjB_j.
await show`FullSimplify[${rPart} / (s2/n)]`
Bj=Risk⁡hierσ2/n=nτ2σ2+nτ2 B_j = \frac{\operatorname{Risk}_{\text{hier}}}{\sigma^2/n} = \frac{n \tau^2}{\sigma^2 +n \tau^2} Express the risk ratio relative to complete pooling as 1−Bj1 - B_j.
await show`FullSimplify[${rPart} / t2]`
1−Bj=Risk⁡hierτ2=σ2σ2+nτ2 1 - B_j = \frac{\operatorname{Risk}_{\text{hier}}}{\tau^2} = \frac{\sigma^2}{\sigma^2 +n \tau^2} Tabulate unpooled, pooled, and hierarchical MSE across τ∈{1,3,5,15}\tau \in \{1, 3, 5, 15\} at σ2=84.64,n=10\sigma^2 = 84.64, n = 10.
// Instantiate {unpooled, pooled, hierarchical} MSE at s2 = 84.64, n = 10 for tau in {1, 3, 5, 15}
await show`N[Table[{s2/n, tau^2, ${rPart} /. t2 -> tau^2} /. {s2 -> 84.64, n -> 10}, {tau, {1, 3, 5, 15}}]]`
(unpooled,  pooled,  hierarchical MSE)τ∈{1, 3, 5, 15}=(8.4641.0.8943368.4649.4.361898.46425.6.323218.464225.8.15715) \begin{aligned} &\bigl(\text{unpooled},\; \text{pooled},\; \text{hierarchical MSE}\bigr)_{\tau \in \{1,\,3,\,5,\,15\}} \\ &= \left( \begin{array}{ccc} 8.464 & 1. & 0.894336 \\ 8.464 & 9. & 4.36189 \\ 8.464 & 25. & 6.32321 \\ 8.464 & 225. & 8.15715 \\ \end{array} \right) \end{aligned}

Exercise 8.5. Extend the Python Gibbs sampler to allow heterogeneous school variances σ12,…,σm2\sigma_1^2, \dots, \sigma_m^2 using (8.10) and (8.11) over the grid ν0∈{1,2,…,50}\nu_0 \in \{1, 2, \dots, 50\} (remembering to subtract log_p.max() before exponentiating when normalizing ν0\nu_0). Verify that the factor ν0\nu_0 in the rate of (8.10) is required by checking whether your sampler recovers known synthetic values (ν0,σ02)=(10,80)(\nu_0, \sigma_0^2) = (10, 80).

Symbolic Solution & InstantiationCompute the posterior mean of σ02\sigma_0^2 under the correct full-conditional rate b+ν0/2∑jσj−2b + \nu_0/2\sum_j \sigma_j^{-2}.
// Ex 8.2: Bias of omitting nu0 in the full conditional rate of sigma_0^2 (plugging in sum sigma_j^{-2} = m / sigma_0^2)
const meanCorrect = await show`FullSimplify[(a + m*nu0/2) / (b + (nu0/2)*m/s0True)]`
𝔼[σ02∣correct rate,  ∑jσj−2=m/σ02]=(2a+mν0)σ02mν0+2bσ02 \begin{aligned} &\mathbb{E}[\sigma_0^2 \mid \text{correct rate},\; \sum_j \sigma_j^{-2} = m/\sigma_0^2] \\ &= \frac{(2 a+m \nu_0) \sigma_0^2}{m \nu_0 +2 b \sigma_0^2} \end{aligned} Compute the distorted posterior mean when the factor ν0\nu_0 is mistakenly omitted from the rate.
const meanBuggy = await show`FullSimplify[(a + m*nu0/2) / (b + (1/2)*m/s0True)]`
𝔼[σ02∣buggy rate,  ∑jσj−2=m/σ02]=(2a+mν0)σ02m+2bσ02 \begin{aligned} &\mathbb{E}[\sigma_0^2 \mid \text{buggy rate},\; \sum_j \sigma_j^{-2} = m/\sigma_0^2] \\ &= \frac{(2 a+m \nu_0) \sigma_0^2}{m+2 b \sigma_0^2} \end{aligned} Take m→∞m \to \infty to prove the bug inflates the posterior mean by an exact asymptotic factor of ν0\nu_0.
await show`Assuming[nu0 > 0 && s0True > 0, Limit[${meanBuggy} / ${meanCorrect}, m -> Infinity]]`
lim⁡m→∞𝔼[σ02∣buggy]𝔼[σ02∣correct]=ν0 \begin{aligned} &\lim_{m \to \infty} \frac{\mathbb{E}[\sigma_0^2 \mid \text{buggy}]}{\mathbb{E}[\sigma_0^2 \mid \text{correct}]} \\ &= \nu_0 \end{aligned} Instantiate correct mean, buggy mean, and their ratio at (ν0=10,σ02=80,m=100)(\nu_0 = 10, \sigma_0^2 = 80, m = 100).
await show`N[{${meanCorrect}, ${meanBuggy}, ${meanBuggy}/${meanCorrect}} /. {a -> 1, b -> 1/100, m -> 100, nu0 -> 10, s0True -> 80}]`
(𝔼correct,  𝔼buggy,  ratio)ν0=10,  σ02=80,  m=100={80.0319,788.976,9.85827} \begin{aligned} &\bigl(\mathbb{E}_{\text{correct}},\; \mathbb{E}_{\text{buggy}},\; \text{ratio}\bigr)_{\nu_0=10,\;\sigma_0^2=80,\;m=100} \\ &= \{80.0319,788.976,9.85827\} \end{aligned}

Exercise 8.6. Simulate m=8m = 8 groups with nj=5n_j = 5, σ=10\sigma = 10, and very small true between-group spread τ=0.5\tau = 0.5. Compare the lag-1 autocorrelation and SeffS_{\text{eff}} of τ\tau under (a) the centered Gibbs sampler Proposition 8.2 and (b) the non-centered reparameterization θj=μ+τθ~j\theta_j = \mu + \tau\tilde{\theta}_j (see Remark 8.3).

Symbolic Solution & InstantiationDerive the posterior correlation Corr⁡(θj,μ∣y,σ2,τ2)\operatorname{Corr}(\theta_j, \mu \mid y, \sigma^2, \tau^2) across mm groups under centered parameterization.
// Ex 8.3: Centered posterior coupling across m groups & centered mu-chain lag-1 ACF at fixed tau
const corrCent = await show`FullSimplify[Sqrt[(s2/nj) / (s2/nj + m*t2)]]`
Corr⁡(θj,μ∣y,σ2,τ2)=σ2σ2+mnjτ2 \begin{aligned} &\operatorname{Corr}(\theta_j, \mu \mid y, \sigma^2, \tau^2) \\ &= \sqrt{\frac{\sigma^2}{\sigma^2 +m n_j \tau^2}} \end{aligned} Derive the lag-1 autocorrelation of the centered μ\mu-chain at fixed τ\tau.
const acfMu = await show`FullSimplify[(s2/nj) / (s2/nj + t2)]`
ACF⁡1(μ(s)∣τ)=σ2σ2+njτ2 \operatorname{ACF}_1(\mu^{(s)} \mid \tau) = \frac{\sigma^2}{\sigma^2 +n_j \tau^2} Evaluate both coupling diagnostics at m=8,nj=5,σ=10,τ=0.5m = 8, n_j = 5, \sigma = 10, \tau = 0.5.
await show`N[{${corrCent}, ${acfMu}} /. {s2 -> 100, nj -> 5, m -> 8, t2 -> 1/4}]`
(Corr⁡(θj,μ∣y,τ),  ACF⁡1(μ∣τ))m=8,  nj=5,  σ=10,  τ=0.5={0.953463,0.987654} \begin{aligned} &\bigl(\operatorname{Corr}(\theta_j, \mu \mid y, \tau),\; \operatorname{ACF}_1(\mu \mid \tau)\bigr)_{m=8,\; n_j=5,\; \sigma=10,\; \tau=0.5} \\ &= \{0.953463,0.987654\} \end{aligned}