Skip to content
LeetQuant
E02 Bayesian Statistics

E02 · Bayesian Statistics · Lesson 2 of 4

Hierarchical Models and Shrinkage

Partial pooling across related units, the James–Stein estimator and why it beats the raw estimates, empirical Bayes estimation of the prior, and shrinking fund and strategy Sharpe ratios towards a group mean.

19 min

Objective

You have estimates for many related units (Sharpe ratios of 50 strategies, alphas of 200 funds, hit rates of 30 traders), each noisy. Where does the prior for each unit come from? A hierarchical model answers: from the other units. By the end of this lesson you should be able to write down a two-level normal model, estimate its prior from the cross-section (empirical Bayes), explain why the James–Stein estimator beats the raw estimates for every possible truth, and shrink a set of Sharpe ratios by hand.

Three ways to use a group

Take units j=1,…,Jj = 1, \dots, J with estimates yjy_j of true values θj\theta_j:

yj∣θj∼N(θj,σj2),θj∼N(μ,τ2).y_j \mid \theta_j \sim N(\theta_j, \sigma_j^2), \qquad \theta_j \sim N(\mu, \tau^2) .

The sampling variance σj2\sigma_j^2 is known from each unit's own data (for an annualised Sharpe ratio from TT years of iid returns, roughly (1+SR2/2)/T(1 + SR^2/2)/T, often rounded to 1/T1/T). The population parameters μ\mu and τ2\tau^2 describe how true values vary across units.

  • No pooling (τ=∞\tau = \infty): use yjy_j. Unbiased per unit, but the extremes are mostly noise.
  • Complete pooling (τ=0\tau = 0): give every unit the group mean. No noise, but ignores real differences.
  • Partial pooling: the posterior given μ,τ\mu, \tau is
E[θj∣yj]=(1−Bj) yj+Bj μ,Bj=σj2σj2+τ2,Var⁡(θj∣yj)=(1σj2+1τ2)−1.\mathbb{E}[\theta_j \mid y_j] = (1 - B_j)\, y_j + B_j\, \mu, \qquad B_j = \frac{\sigma_j^2}{\sigma_j^2 + \tau^2}, \qquad \operatorname{Var}(\theta_j \mid y_j) = \Big(\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}\Big)^{-1} .

This is the familiar normal shrinkage formula, but with a crucial difference: μ\mu and τ2\tau^2 are now estimated from the data. Units with noisier estimates (shorter track records) are shrunk more, and the amount of shrinkage is set by how dispersed the estimates are compared with their noise.

The data tell you the prior

Marginally, integrating out θj\theta_j,

yj∼N(μ,σj2+τ2).y_j \sim N(\mu, \sigma_j^2 + \tau^2) .

If every unit has the same σ2\sigma^2, the observed estimates scatter with variance σ2+τ2\sigma^2 + \tau^2, so the excess dispersion over pure noise measures real differences. With yˉ\bar y the group mean and S=∑j(yj−yˉ)2S = \sum_j (y_j - \bar y)^2:

E[S]=(J−1)(σ2+τ2)  ⇒  τ^2=max⁡ ⁣(0,SJ−1−σ2).\mathbb{E}[S] = (J - 1)(\sigma^2 + \tau^2) \;\Rightarrow\; \hat\tau^2 = \max\!\Big(0, \frac{S}{J - 1} - \sigma^2\Big) .

For the shrinkage factor itself there is an exactly unbiased estimate. Since S/(σ2+τ2)∼χJ−12S/(\sigma^2 + \tau^2) \sim \chi^2_{J-1} and E[1/χk2]=1/(k−2)\mathbb{E}[1/\chi^2_k] = 1/(k - 2),

B^=(J−3) σ2S,θ^j=yˉ+(1−B^)(yj−yˉ).\hat B = \frac{(J - 3)\,\sigma^2}{S}, \qquad \hat\theta_j = \bar y + (1 - \hat B)(y_j - \bar y) .

This is the Efron–Morris (James–Stein towards the mean) estimator, and plugging estimates of μ\mu and τ2\tau^2 into the Bayes formula is empirical Bayes. If B^>1\hat B > 1 (the estimates are less dispersed than noise alone predicts), use the positive-part version max⁡(0,1−B^)\max(0, 1 - \hat B), which pools completely.

With unequal σj\sigma_j, estimate μ\mu and τ2\tau^2 by maximising the marginal likelihood ∏jN(yj;μ,σj2+τ2)\prod_j N(y_j; \mu, \sigma_j^2 + \tau^2), or go fully Bayesian with a prior on τ\tau (a half-normal or half-Cauchy) and average over it. Full Bayes matters when JJ is small: plugging in a single τ^\hat\tau ignores uncertainty about it, so the empirical-Bayes posterior intervals are too narrow, especially when τ^\hat\tau is near zero.

The James–Stein estimator and why it dominates

Take y∼Np(θ,σ2I)y \sim N_p(\theta, \sigma^2 I) with σ2\sigma^2 known and no prior at all. The raw estimate θ^=y\hat\theta = y has total risk E∥y−θ∥2=pσ2\mathbb{E}\lVert y - \theta \rVert^2 = p\sigma^2 for every θ\theta. James and Stein (1961) showed that for p≥3p \ge 3

θ^JS=(1−(p−2) σ2∥y∥2) y\hat\theta_{\text{JS}} = \Big(1 - \frac{(p - 2)\,\sigma^2}{\lVert y \rVert^2}\Big)\, y

has strictly smaller total risk for every θ\theta. So yy is inadmissible: there is an estimator at least as good everywhere and better somewhere.

Intuition. E∥y∥2=∥θ∥2+pσ2\mathbb{E}\lVert y\rVert^2 = \lVert\theta\rVert^2 + p\sigma^2, so the raw vector is too long on average, and the longer it is relative to the noise, the less it needs shortening.

Proof via Stein's lemma. For y∼N(θ,σ2I)y \sim N(\theta, \sigma^2 I) and a weakly differentiable gg, E[(y−θ)⊤g(y)]=σ2 E[∇⋅g(y)]\mathbb{E}[(y - \theta)^\top g(y)] = \sigma^2\, \mathbb{E}[\nabla \cdot g(y)]. Write the estimator as y+g(y)y + g(y):

E∥y+g(y)−θ∥2=pσ2+E∥g(y)∥2+2σ2 E[∇⋅g(y)].\mathbb{E}\lVert y + g(y) - \theta \rVert^2 = p\sigma^2 + \mathbb{E}\lVert g(y) \rVert^2 + 2\sigma^2\, \mathbb{E}[\nabla \cdot g(y)] .

With g(y)=−cσ2y/∥y∥2g(y) = -c\sigma^2 y/\lVert y\rVert^2, we have ∥g∥2=c2σ4/∥y∥2\lVert g \rVert^2 = c^2\sigma^4/\lVert y\rVert^2 and ∇⋅g=−cσ2(p−2)/∥y∥2\nabla \cdot g = -c\sigma^2 (p - 2)/\lVert y\rVert^2, so

risk=pσ2−σ4(2c(p−2)−c2) E[1/∥y∥2].\text{risk} = p\sigma^2 - \sigma^4\big(2c(p - 2) - c^2\big)\, \mathbb{E}\big[1/\lVert y \rVert^2\big] .

The bracket is positive for 0<c<2(p−2)0 < c < 2(p - 2), which needs p≥3p \ge 3, and is largest at c=p−2c = p - 2, giving risk pσ2−(p−2)2σ4 E[1/∥y∥2]<pσ2p\sigma^2 - (p - 2)^2\sigma^4\, \mathbb{E}[1/\lVert y\rVert^2] < p\sigma^2. Shrinking towards the estimated grand mean uses one degree of freedom, so the constant becomes p−3p - 3 and you need p≥4p \ge 4.

What dominance does and does not say.

  • It is a statement about total squared error summed over units. An individual unit whose true value is far from the others can be made worse; the gains elsewhere outweigh it.
  • It holds for every θ\theta, with no prior assumed, but it does use the normal likelihood with known (or well-estimated) σ2\sigma^2.
  • The units need not be related for the maths to work, which is the "paradox". The gains are large only when the θj\theta_j really are close together, so in practice shrink within groups that are plausibly exchangeable: same asset class, same style, similar capacity.
  • The positive-part estimator dominates plain James–Stein, and is itself inadmissible, though only slightly.

The simulation below compares the raw Sharpe estimates of 8 funds with James–Stein and positive-part James–Stein towards the grand mean.

import random

random.seed(3)
p, sigma, R = 8, 0.5, 20_000            # 8 funds, s.e. 0.5 on each Sharpe estimate
mse = {"raw": 0.0, "JS": 0.0, "JS+": 0.0}
for _ in range(R):
    theta = [random.gauss(0.5, 0.3) for _ in range(p)]       # true Sharpes
    y = [t + random.gauss(0, sigma) for t in theta]           # estimates
    ybar = sum(y) / p
    S = sum((v - ybar) ** 2 for v in y)
    B = (p - 3) * sigma**2 / S                                 # estimated shrinkage
    js = [ybar + (1 - B) * (v - ybar) for v in y]
    jsp = [ybar + max(0.0, 1 - B) * (v - ybar) for v in y]    # positive part
    for name, est in (("raw", y), ("JS", js), ("JS+", jsp)):
        mse[name] += sum((e - t) ** 2 for e, t in zip(est, theta)) / p / R

for name, v in mse.items():
    print(f"{name:4s} mean squared error per fund: {v:.4f}")

Output:

raw  mean squared error per fund: 0.2500
JS   mean squared error per fund: 0.1347
JS+  mean squared error per fund: 0.1212

Theory gives σ2−(p−3)σ4/(p(σ2+τ2))=0.25−5×0.0625/(8×0.34)=0.135\sigma^2 - (p - 3)\sigma^4/\big(p(\sigma^2 + \tau^2)\big) = 0.25 - 5 \times 0.0625/(8 \times 0.34) = 0.135 for James–Stein, matching the simulation. An oracle who knew μ=0.5\mu = 0.5 and τ=0.3\tau = 0.3 would reach σ2τ2/(σ2+τ2)=0.066\sigma^2\tau^2/(\sigma^2 + \tau^2) = 0.066; the gap is the price of estimating the prior from only 8 funds.

Worked example

shrinking eight Sharpe ratios

Eight funds in the same style each have 4 years of history, so each annualised Sharpe estimate has sampling variance about σ2=1/4=0.25\sigma^2 = 1/4 = 0.25. The estimates are 1.6,1.1,0.9,0.7,0.5,0.3,0.1,−0.41.6, 1.1, 0.9, 0.7, 0.5, 0.3, 0.1, -0.4. Compute the Efron–Morris shrinkage, the shrunk Sharpe ratios, and the implied spread of true Sharpe ratios.

Show solution

Group mean. yˉ=4.8/8=0.6\bar y = 4.8/8 = 0.6. Deviations: 1.0,0.5,0.3,0.1,−0.1,−0.3,−0.5,−1.01.0, 0.5, 0.3, 0.1, -0.1, -0.3, -0.5, -1.0.

Dispersion. S=1+0.25+0.09+0.01+0.01+0.09+0.25+1=2.70S = 1 + 0.25 + 0.09 + 0.01 + 0.01 + 0.09 + 0.25 + 1 = 2.70, so the cross-sectional variance is 2.70/7=0.3862.70/7 = 0.386. Pure noise would give 0.250.25, so only about a third of the observed spread is real.

Shrinkage. B^=(J−3)σ2/S=5×0.25/2.70=0.463\hat B = (J - 3)\sigma^2/S = 5 \times 0.25/2.70 = 0.463, so each fund keeps 53.7%53.7\% of its deviation from the mean.

Raw1.61.10.90.70.50.30.1−0.4
Shrunk1.140.870.760.650.550.440.330.06

The top fund's 1.6 becomes 1.14: still the best, but the evidence supports a much less impressive number. The worst fund's −0.4-0.4 becomes slightly positive: four bad years in a style that typically earns 0.6 are weak evidence of a negative true Sharpe.

Implied prior. B=σ2/(σ2+τ2)B = \sigma^2/(\sigma^2 + \tau^2) gives τ^2=σ2(1−B^)/B^=0.25×0.537/0.463=0.29\hat\tau^2 = \sigma^2(1 - \hat B)/\hat B = 0.25 \times 0.537/0.463 = 0.29, so true Sharpe ratios in this style have SD about 0.540.54. The plug-in posterior SD of each fund is σ2(1−B^)=0.37\sqrt{\sigma^2(1 - \hat B)} = 0.37, which understates the truth a little because B^\hat B is itself uncertain with only eight funds.

Worked example

short track record against long track record

In a style where true Sharpe ratios are about N(0.4,0.32)N(0.4, 0.3^2), fund A shows a Sharpe of 1.5 over 2 years and fund B shows 1.0 over 9 years. Using sampling variance 1/T1/T, which fund has the higher posterior mean Sharpe, and how certain are the two estimates?

Show solution

Prior variance τ2=0.09\tau^2 = 0.09.

Fund A. σA2=1/2=0.5\sigma_A^2 = 1/2 = 0.5, so BA=0.5/0.59=0.847B_A = 0.5/0.59 = 0.847. Posterior mean 0.4+0.153×1.1=0.5680.4 + 0.153 \times 1.1 = 0.568. Posterior variance 1/(1/0.09+1/0.5)=1/13.11=0.07631/(1/0.09 + 1/0.5) = 1/13.11 = 0.0763, SD 0.2760.276.

Fund B. σB2=1/9=0.111\sigma_B^2 = 1/9 = 0.111, so BB=0.111/0.201=0.552B_B = 0.111/0.201 = 0.552. Posterior mean 0.4+0.448×0.6=0.6690.4 + 0.448 \times 0.6 = 0.669. Posterior variance 1/(11.11+9)=0.04971/(11.11 + 9) = 0.0497, SD 0.2230.223.

Ranking flips. On raw numbers A looks 50% better; after shrinkage B is ahead, 0.670.67 against 0.570.57, and B's estimate is also tighter. Two years of a Sharpe of 1.5 is roughly a 1.5/0.5=2.11.5/\sqrt{0.5} = 2.1 standard-error result, and a group in which true Sharpes above 1 are rare produces such results by luck quite often. Interview point: when someone ranks managers or signals on raw Sharpe ratios with different track lengths, the first fix is to put them on a common, shrunk scale.

Knowledge check

Ten strategies each have Sharpe estimates with sampling variance σ2=0.25\sigma^2 = 0.25. The group mean is 0.40.4 and S=∑j(yj−yˉ)2=5.0S = \sum_j (y_j - \bar y)^2 = 5.0. Using B^=(J−3)σ2/S\hat B = (J - 3)\sigma^2/S, what is the shrunk Sharpe of a strategy whose raw estimate is 1.41.4?

Knowledge check

Which statement about the James–Stein estimator (shrinking towards the grand mean, normal estimates with known noise variance, at least four units) is correct?

Knowledge check

You have 20 strategies, each with a Sharpe estimate from 4 years of data (sampling variance 0.25). The sample variance of the 20 estimates is 0.22. What does empirical Bayes conclude?

Key takeaways

  • Hierarchical model: yj∼N(θj,σj2)y_j \sim N(\theta_j, \sigma_j^2), θj∼N(μ,τ2)\theta_j \sim N(\mu, \tau^2). The posterior mean shrinks each yjy_j towards μ\mu by Bj=σj2/(σj2+τ2)B_j = \sigma_j^2/(\sigma_j^2 + \tau^2), more for noisier units.
  • The prior is estimated from the cross-section: the estimates scatter with variance σ2+τ2\sigma^2 + \tau^2, so excess dispersion over noise measures real differences. B^=(J−3)σ2/S\hat B = (J - 3)\sigma^2/S is unbiased for BB.
  • James–Stein: for p≥3p \ge 3 (or p≥4p \ge 4 shrinking to the mean) the raw estimate is inadmissible; shrinkage lowers total squared error for every truth, though not necessarily for every unit.
  • For Sharpe ratios with sampling variance about 1/T1/T, short track records shrink most; rankings on raw Sharpe ratios across different track lengths are unreliable.
  • Empirical Bayes understates uncertainty when there are few units; full Bayes puts a prior on τ\tau and averages over it.
  • Shrink within exchangeable groups, or towards a regression on known characteristics, and beware selected or survivor-only groups.

Practice problems

Finished reading?

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