Foundations to Frontier
Tests
A. Mathematical Foundations › A5 Statistics › Bayesian statistics

Hierarchical Models

Many related units (robots in a fleet, cells in a factory, schools, tasks, sensors) each have their own parameter, drawn from a shared population. A hierarchical model estimates the units and the population together, so each unit borrows strength from the others: noisy units are shrunk toward the population, well-measured ones keep their own value. You will build normal–normal and gamma–Poisson hierarchies, estimate the between-unit spread by empirical Bayes, see partial pooling beat both 'no pooling' and 'complete pooling', meet the classic eight-schools problem, and estimate fault rates for a robot fleet with wildly different hours in service.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Judging a new robot from 3 tests

Your fleet’s robots succeed about 90% of the time. A brand-new robot succeeds in its first 3 tries. Is it perfect? Of course not: 3 tries tell you little, and you know robots of this model are usually around 90%. A sensible estimate sits between “3 out of 3” and “the fleet average”, much closer to the fleet average. After 300 tries, you’d trust its own record.

That’s a hierarchical model: each robot has its own rate, the rates come from a fleet-wide distribution, and each estimate leans on the fleet in proportion to how little data it has.

Borrowing strength: shrink noisy groups toward the population
2 runs3 runs4 runs6 runs8 runs12 runs16 runs25 runs40 runs60 runs
empirical-Bayes τ̂ = 0.80 · MSE vs truth: no pooling 1.451 · complete pooling 1.637 · partial pooling 1.227
Each row is a robot cell with a given number of runs. Grey dot: its own average (with 95% bar). Orange dot: the partially pooled estimate (orange line = shrinkage). Green triangle: the true mean. Blue dashed: the complete-pooling mean. Cells with few runs are pulled strongly toward the population, while well-measured cells keep their own value. When τ is small the estimates collapse together. When τ is large they barely move.
Why engineers care

Fleets of robots or satellites, batches of components, many tasks in a benchmark, many users of a product: all are groups of similar units with uneven data. Hierarchical models give better estimates for every unit, and they stop you over-reacting to a lucky or unlucky start.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A new robot succeeds 3/3 times; the fleet averages 90%. A sensible estimate is…
  2. 2. With lots of data, a unit's estimate should…
  3. 3. Hierarchical models let units…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Three ways to estimate group means

For groups j=1,…,Jj = 1, \dots, J, the observed means are yj∼N(θj,sj2)y_j\sim\mathcal N(\theta_j, s_j^2).

  1. No pooling: θ^j=yj\hat\theta_j = y_j. Unbiased but noisy for small groups.
  2. Complete pooling: θ^j=yˉ\hat\theta_j = \bar y for all jj. Precise, but wrong if the groups really differ.
  3. Partial pooling with a population model θj∼N(μ,τ2)\theta_j\sim\mathcal N(\mu, \tau^2):
E[θj∣y]=(1−Bj)yj+Bjμ,Bj=sj2sj2+τ2\mathbb E[\theta_j\mid y] = (1 - B_j)y_j + B_j\mu, \qquad B_j = \frac{s_j^2}{s_j^2 + \tau^2}

The shrinkage factor BjB_j is large for noisy groups (big sjs_j) and when the groups are similar (small τ\tau). The hyperparameters μ,τ\mu, \tau are learned from all the groups together.

Counts and proportions

  • Fault counts kjk_j over exposures tjt_j, with λj∼Gamma(a,b)\lambda_j\sim\text{Gamma}(a, b): the posterior mean rate is (a+kj)/(b+tj)(a + k_j)/(b + t_j).
  • Success rates with pj∼Beta(a,b)p_j\sim\text{Beta}(a, b): (a+kj)/(a+b+nj)(a + k_j)/(a + b + n_j) (A4.2-5).
📝 Quick check (Level 1)
0/3 answered
  1. 1. B_j = s²/(s² + τ²) with s² = τ² equals…
  2. 2. If τ = 0 (all groups identical), partial pooling becomes…
  3. 3. If τ → ∞, partial pooling becomes…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Estimating the population: empirical Bayes vs full Bayes

Marginally, yj∼N(μ,sj2+τ2)y_j\sim\mathcal N(\mu, s_j^2 + \tau^2). Empirical Bayes maximizes this marginal likelihood over (μ,τ)(\mu, \tau), then plugs them in. The moment version is τ^2=max⁡(0,Var⁡(y)−s2‾)\hat\tau^2 = \max(0, \operatorname{Var}(y) - \overline{s^2}). Full Bayes puts a prior on τ\tau (for example half-normal or half-Cauchy) and integrates over it, usually with MCMC (A5.2-7).

That matters when JJ is small. In the classic eight schools data (Rubin, 1981), the marginal likelihood peaks at τ^=0\hat\tau = 0, so empirical Bayes pools completely. Full Bayes keeps meaningful uncertainty about τ\tau, and with it the possibility that schools differ (Gelman, 2006).

Why it beats both extremes

Partial pooling is the James–Stein idea (A5.1-3) applied to groups: shrinking noisy estimates toward a learned common value reduces total MSE whenever there are several groups. In Exercise 1 with 12 cells, partial pooling cuts the MSE by about 70% compared with no pooling, and it also beats complete pooling.

Model structure

yij∼p(⋅∣θj)y_{ij}\sim p(\cdot\mid\theta_j), θj∼p(⋅∣ϕ)\theta_j\sim p(\cdot\mid\phi), ϕ∼p(ϕ)\phi\sim p(\phi). Levels can nest further (measurements within runs within robots within sites) or cross (robot × task). Mixed-effects (random-effects) models in statistics are the same thing.

🐍 Exercise 1: partial pooling with empirical Bayes
Fit the normal hierarchical model by maximizing the marginal likelihood over τ (vectorized grid), compute partially pooled estimates, and compare MSE against no pooling and complete pooling over 400 simulated fleets of 12 robot cells. Then fit the eight-schools data.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In the normal hierarchical model, y_j is marginally…
  2. 2. For eight schools, empirical Bayes gives τ̂ =
  3. 3. Moment estimate of τ² =

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Deriving the shrinkage formula

Given μ,τ\mu, \tau, the posterior of one θj\theta_j combines the likelihood N(yj;θj,sj2)\mathcal N(y_j; \theta_j, s_j^2) with the prior N(θj;μ,τ2)\mathcal N(\theta_j; \mu, \tau^2). Precisions add, and the mean is precision-weighted:

θj∣yj∼N(yj/sj2+μ/τ21/sj2+1/τ2, 11/sj2+1/τ2)\theta_j\mid y_j\sim\mathcal N\Big(\frac{y_j/s_j^2 + \mu/\tau^2}{1/s_j^2 + 1/\tau^2},\ \frac1{1/s_j^2 + 1/\tau^2}\Big)

and 1/sj21/sj2+1/τ2=τ2sj2+τ2=1−Bj\frac{1/s_j^2}{1/s_j^2 + 1/\tau^2} = \frac{\tau^2}{s_j^2 + \tau^2} = 1 - B_j. ∎

Exchangeability and de Finetti

Why model units as draws from a common distribution? If, before seeing data, you’d treat the units symmetrically (their labels carry no information), the parameters are exchangeable. De Finetti’s theorem says an infinite exchangeable sequence is a mixture of i.i.d. sequences. That’s exactly the hierarchical form ∫∏jp(θj∣ϕ)p(ϕ)dϕ\int\prod_jp(\theta_j\mid\phi)p(\phi)d\phi. If units differ in known ways (age, site), put those covariates into the population model.

Funnels and parameterization

The joint posterior of (θ,τ)(\theta, \tau) has a “funnel”: when τ\tau is small, the θj\theta_j must be close together. MCMC samplers struggle there. The non-centred parameterization θj=μ+τηj\theta_j = \mu + \tau\eta_j, ηj∼N(0,1)\eta_j\sim\mathcal N(0,1) fixes it (Papaspiliopoulos, Roberts and Sköld, 2007; Betancourt and Girolami, 2015).

Exercise 3.1: Compute the eight-schools estimate for school A with μ = 8, τ = 5

B=225/(225+25)=0.9B = 225/(225 + 25) = 0.9, so 0.1⋅28+0.9⋅8=100.1\cdot28 + 0.9\cdot8 = 10. A 28-point effect with SE 15 is mostly noise.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The weight on a group's own data is…
  2. 2. Hierarchical models are justified by…
  3. 3. The non-centred parameterization writes θ_j as…

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

Fault rates across a robot fleet

A fleet of 40 robots has logged anywhere from 50 to 2000 hours each. Raw rates kj/tjk_j/t_j are wild for new units: one fault in 50 hours looks like a terrible robot. With a gamma population prior estimated from the whole fleet (empirical Bayes), each rate becomes (a+kj)/(b+tj)(a + k_j)/(b + t_j). New units are shrunk to the fleet rate, veterans keep their own. In this simulation the rate MSE drops about 7-fold. The same machinery ranks component suppliers, flags genuinely bad units, and sets per-unit maintenance intervals (Hamada et al., 2008). It’s also behind multi-task learning and meta-learning in ML.

🐍 Exercise 2 (research): empirical-Bayes fault rates for a fleet
Simulate 60 fleets of 40 robots with gamma-distributed fault rates and very unequal exposures. Fit the gamma population prior by method-of-moments empirical Bayes, compute shrunk rates (a + k)/(b + t), compare MSE with raw rates, and check the negative-binomial marginal likelihood.

Hierarchical models in AI, robotics and aerospace

  • Multi-task and meta-learning. Task-specific parameters drawn from a shared prior. MAML has a hierarchical-Bayes interpretation (Grant et al., 2018).
  • Fleet learning. Per-vehicle models with shared population structure for autonomous fleets and satellite constellations, such as battery degradation models per cell or pack.
  • Reliability across lots and suppliers. Random-effects models for component strength or lifetime by batch (Hamada et al., 2008).
  • Benchmark aggregation. Hierarchical models of algorithm performance across tasks and seeds separate task difficulty from algorithm skill.
  • Small-area estimation and A/B tests across many segments use partial pooling to avoid over-reacting to tiny segments.

References

  1. A. Gelman, J. Hill (2007). Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambridge University Press.
  2. D. Rubin (1981). “Estimation in parallel randomized experiments.” Journal of Educational Statistics, 6(4).
  3. A. Gelman (2006). “Prior distributions for variance parameters in hierarchical models.” Bayesian Analysis, 1(3).
  4. B. Efron, C. Morris (1975). “Data analysis using Stein’s estimator and its generalizations.” JASA, 70(350).
  5. B. de Finetti (1937). “La prévision: ses lois logiques, ses sources subjectives.” Annales de l’IHP, 7(1).
  6. O. Papaspiliopoulos, G. Roberts, M. Sköld (2007). “A general framework for the parametrization of hierarchical models.” Statistical Science, 22(1).
  7. M. Betancourt, M. Girolami (2015). “Hamiltonian Monte Carlo for hierarchical models.” In Current Trends in Bayesian Methodology with Applications. CRC Press.
  8. R. DerSimonian, N. Laird (1986). “Meta-analysis in clinical trials.” Controlled Clinical Trials, 7(3).
  9. E. Grant, C. Finn, S. Levine, T. Darrell, T. Griffiths (2018). “Recasting gradient-based meta-learning as hierarchical Bayes.” ICLR.
  10. M. Hamada, A. Wilson, C. S. Reese, H. Martz (2008). Bayesian Reliability. Springer.