Simple Algorithm
Machine Learning · Convergence · 28 min

Effective Complexity & Degrees of Freedom

Measuring model complexity: covariance penalties, SURE and the lasso.

How complex is a model? Counting parameters fails in both directions: a single parameter can fit arbitrarily many labelings, and a ridge fit with a thousand coefficients can behave like one with ten. This lesson measures complexity by what a fit does, how strongly each fitted value follows its own label, and derives the tools built on that idea: Mallows' CpC_p, AIC, Stein's unbiased risk estimate, and the surprising degrees of freedom of the lasso.

What you’ll learn

  • Prove the optimism identity E[Errin−err‾]=2n∑iCov⁡(y^i,yi)\E[\mathrm{Err}_{\text{in}}-\overline{\mathrm{err}}]=\frac2n\sum_i\Cov(\hat y_i,y_i), define degrees of freedom, and compute it for linear smoothers, ridge and kk-NN.
  • Prove Stein's lemma and SURE, and use SURE to show that the James–Stein estimator beats the sample mean for every mean vector in dimension 3 or more.
  • Compute the degrees of freedom of soft and hard thresholding, state the lasso results, and explain the hidden cost of searching over subsets.
  • Explain what CpC_p, AIC and cross-validation each estimate, and how they relate.

Parameter counts mislead

Four examples show why the number of parameters is a poor measure of complexity:

  • One parameter, unlimited capacity. The classifiers x↦1{sin⁡(θx)>0}x\mapsto\mathbf 1\{\sin(\theta x)>0\} have one parameter, yet for every mm some mm points can be labeled in all 2m2^m ways (VC dimension ∞\infty; shown in Learning Theory: How Many Examples Are Enough?).
  • Many parameters, little capacity. Ridge regression with a large penalty keeps all pp coefficients but lets them move very little.
  • No parameters at all. kk-nearest neighbors fits nothing, yet with k=1k=1 it reproduces every training label.
  • Search costs complexity. Choosing the best 3 of 100 candidate predictors, then fitting 3 coefficients, adapts to the data far more than a model whose 3 predictors were fixed in advance.

A useful definition must look at how the fit responds to the data.

Optimism and covariance penalties

Work with a fixed design: inputs x1,…,xn\mathbf x_1,\dots,\mathbf x_n are held fixed and y=μ+ε\mathbf y=\boldsymbol\mu+\boldsymbol\varepsilon with Eε=0\E\boldsymbol\varepsilon=\mathbf 0 and Cov⁡(ε)=σ2In\Cov(\boldsymbol\varepsilon)=\sigma^2I_n. A fitting rule produces y^=m(y)\hat{\mathbf y}=m(\mathbf y). The training error is err‾=1n∥y−y^∥2\overline{\mathrm{err}}=\frac1n\norm{\mathbf y-\hat{\mathbf y}}^2, and the in-sample error Errin=1nEy0∥y0−y^∥2\mathrm{Err}_{\text{in}}=\frac1n\E_{\mathbf y^0}\norm{\mathbf y^0-\hat{\mathbf y}}^2 uses fresh labels y0\mathbf y^0, an independent copy of y\mathbf y at the same inputs.

DerivationProof
  1. E(yi0−y^i)2=σ2+E(y^i−μi)2\E(y_i^0-\hat y_i)^2=\sigma^2+\E(\hat y_i-\mu_i)^2

    The fresh label yi0y_i^0 is independent of y^i\hat y_i, so only the noise variance and the estimation error remain:

  2. E(yi−y^i)2=E(εi−(y^i−μi))2=σ2−2Cov⁡(yi,y^i)+E(y^i−μi)2\E(y_i-\hat y_i)^2=\E\big(\varepsilon_i-(\hat y_i-\mu_i)\big)^2=\sigma^2-2\Cov(y_i,\hat y_i)+\E(\hat y_i-\mu_i)^2

    For the training label, the noise εi\varepsilon_i and the fit are correlated, and E[εi(y^i−μi)]=E[εiy^i]=Cov⁡(yi,y^i)\E[\varepsilon_i(\hat y_i-\mu_i)]=\E[\varepsilon_i\hat y_i]=\Cov(y_i,\hat y_i):

  3. E[Errin−err‾]=2n∑i=1nCov⁡(y^i,yi)\E\big[\mathrm{Err}_{\text{in}}-\overline{\mathrm{err}}\big]=\frac2n\sum_{i=1}^n\Cov(\hat y_i,y_i)

    Subtract and average over ii. Nothing about the fitting rule was used: it may be nonlinear, adaptive, or chosen by search.

For a linear smoother y^=Sy\hat{\mathbf y}=S\mathbf y, with SS depending on the inputs but not on y\mathbf y, Cov⁡(y^,y)=σ2S\Cov(\hat{\mathbf y},\mathbf y)=\sigma^2S, so df=tr⁡(S)\mathrm{df}=\operatorname{tr}(S):

  • least squares with pp linearly independent columns: tr⁡(H)=p\operatorname{tr}(H)=p;
  • ridge: ∑jdj2/(dj2+λ)\sum_jd_j^2/(d_j^2+\lambda) in terms of the singular values of XX, falling continuously from rank⁡(X)\rank(X) to 00 (Ridge Regression in Depth);
  • kk-NN regression, where each training point counts itself among its kk neighbors (no ties): Sii=1/kS_{ii}=1/k, so df=n/k\mathrm{df}=n/k;
  • fitting a constant: S=1n11⊤S=\frac1n\mathbf 1\mathbf 1^\top and df=1\mathrm{df}=1.

Plugging df into the optimism gives Mallows' CpC_p (1973), Cp=err‾+2 df σ^2/nC_p=\overline{\mathrm{err}}+2\,\mathrm{df}\,\hat\sigma^2/n, an unbiased estimate of E[Errin]\E[\mathrm{Err}_{\text{in}}] when df is known exactly and σ^2\hat\sigma^2 is unbiased. AIC (Akaike, 1974), −2log⁡L^+2d-2\log\hat L+2d for a model with dd parameters, is an asymptotically unbiased estimate of the expected out-of-sample deviance for correctly specified regular models; for Gaussian regression with known σ2\sigma^2 it is CpC_p up to scaling and constants. Its small-sample correction AICc (Hurvich & Tsai, 1989) adds 2d(d+1)/(n−d−1)2d(d+1)/(n-d-1).

Predict before you look

A kk-nearest-neighbor regression with k=10k=10 is fitted to n=200n=200 points (each point counts itself as a neighbor, no ties). How many degrees of freedom does it use?

Show the answer

20

Each fitted value gives weight 1/k1/k to its own label, so Sii=1/10S_{ii}=1/10 and df=tr⁡(S)=200/10=20\mathrm{df}=\operatorname{tr}(S)=200/10=20: as many as a least-squares fit with 20 coefficients, from a method with no fitted parameters at all. With k=1k=1, df=n\mathrm{df}=n and the training error is zero.

Stein's lemma and SURE

DerivationProof
  1. E[(Z−μ) g(Z)]=−σ2 ⁣∫g(z) ϕσ′(z−μ) dz=σ2 ⁣∫g′(z) ϕσ(z−μ) dz=σ2 E[g′(Z)]\E\big[(Z-\mu)\,g(Z)\big]=-\sigma^2\!\int g(z)\,\phi_\sigma'(z-\mu)\,dz=\sigma^2\!\int g'(z)\,\phi_\sigma(z-\mu)\,dz=\sigma^2\,\E\big[g'(Z)\big]

    In one dimension, (z−μ)ϕσ(z−μ)=−σ2ϕσ′(z−μ)(z-\mu)\phi_\sigma(z-\mu)=-\sigma^2\phi_\sigma'(z-\mu) for the N(0,σ2)\mathcal N(0,\sigma^2) density ϕσ\phi_\sigma. Integrate by parts (the boundary terms vanish under the integrability condition):

  2. E∥m−μ∥2=E∥m−y∥2+2 E⟨m−y, y−μ⟩+E∥y−μ∥2\E\norm{m-\boldsymbol\mu}^2=\E\norm{m-\mathbf y}^2+2\,\E\langle m-\mathbf y,\ \mathbf y-\boldsymbol\mu\rangle+\E\norm{\mathbf y-\boldsymbol\mu}^2

    Apply this to coordinate ii, conditioning on the others (independent under isotropic noise), to get Cov⁡(mi,yi)=E[(yi−μi)mi]=σ2E[∂imi]\Cov(m_i,y_i)=\E[(y_i-\mu_i)m_i]=\sigma^2\E[\partial_im_i]. Then expand the risk around y\mathbf y:

  3. E∥m(y)−μ∥2=E[∥y−m(y)∥2+2σ2 ∇⋅m(y)−nσ2]\E\norm{m(\mathbf y)-\boldsymbol\mu}^2=\E\Big[\norm{\mathbf y-m(\mathbf y)}^2+2\sigma^2\,\nabla\cdot m(\mathbf y)-n\sigma^2\Big]

    The middle term is 2∑iCov⁡(mi,yi)−2nσ2=2σ2E[∇⋅m]−2nσ22\sum_i\Cov(m_i,y_i)-2n\sigma^2=2\sigma^2\E[\nabla\cdot m]-2n\sigma^2, and the last is nσ2n\sigma^2:

DerivationApplication: James–Stein beats the sample mean (n ≥ 3)
  1. ∇⋅m(y)=n−(n−2)2σ2∥y∥2,∥y−m(y)∥2=(n−2)2σ4∥y∥2\nabla\cdot m(\mathbf y)=n-\frac{(n-2)^2\sigma^2}{\norm{\mathbf y}^2},\qquad \norm{\mathbf y-m(\mathbf y)}^2=\frac{(n-2)^2\sigma^4}{\norm{\mathbf y}^2}

    Shrink toward zero by a data-dependent factor: m(y)=(1−(n−2)σ2/∥y∥2)ym(\mathbf y)=\big(1-(n-2)\sigma^2/\norm{\mathbf y}^2\big)\mathbf y. Since ∇⋅(y/∥y∥2)=(n−2)/∥y∥2\nabla\cdot\big(\mathbf y/\norm{\mathbf y}^2\big)=(n-2)/\norm{\mathbf y}^2,

  2. E∥m(y)−μ∥2=nσ2−(n−2)2σ4 E[1∥y∥2] < nσ2\E\norm{m(\mathbf y)-\boldsymbol\mu}^2=n\sigma^2-(n-2)^2\sigma^4\,\E\Big[\frac1{\norm{\mathbf y}^2}\Big]\ <\ n\sigma^2

    Substitute into SURE. The risk of the unbiased estimator m(y)=ym(\mathbf y)=\mathbf y is nσ2n\sigma^2 for every μ\boldsymbol\mu; James–Stein is strictly better for every μ\boldsymbol\mu, since E[1/∥y∥2]\E[1/\norm{\mathbf y}^2] is finite and positive when n≥3n\ge3:

Predict before you look

Take n=10n=10 and true mean μ=0\boldsymbol\mu=\mathbf 0. Then ∥y∥2/σ2∼χ102\norm{\mathbf y}^2/\sigma^2\sim\chi^2_{10}, and E[1/χn2]=1/(n−2)\E[1/\chi^2_n]=1/(n-2). What is the James–Stein risk as a percentage of the sample mean's risk nσ2n\sigma^2?

Show the answer

20 %

E[1/∥y∥2]=1/((n−2)σ2)\E\big[1/\norm{\mathbf y}^2\big]=1/\big((n-2)\sigma^2\big), so the risk is nσ2−(n−2)σ2=2σ2n\sigma^2-(n-2)\sigma^2=2\sigma^2, whatever nn is: 2/10=20%2/10=20\% of the sample mean's risk. Far from 0\mathbf 0 the gain shrinks toward zero, but it never turns into a loss.

Thresholding, the lasso and the cost of search

With an orthonormal design the lasso reduces to soft thresholding of each coordinate, ηλ(y)=sign⁡(y) (∣y∣−λ)+\eta_\lambda(y)=\sign(y)\,(\abs y-\lambda)_+, and best-subset selection to hard thresholding, Hλ(y)=y 1{∣y∣>λ}H_\lambda(y)=y\,\mathbf 1\{\abs y>\lambda\}. Both keep the coordinates with ∣yi∣>λ\abs{y_i}>\lambda; they differ in what they do to the survivors.

Soft thresholding is continuous and piecewise linear, with derivative 1{∣y∣>λ}\mathbf 1\{\abs y>\lambda\} almost everywhere. Stein's lemma applies, and df=E #{i:∣yi∣>λ}\mathrm{df}=\E\,\#\{i:\abs{y_i}>\lambda\}: the expected number of nonzero coefficients. Its SURE, ∑imin⁡(yi2,λ2)+2σ2#{i:∣yi∣>λ}−nσ2\sum_i\min(y_i^2,\lambda^2)+2\sigma^2\#\{i:\abs{y_i}>\lambda\}-n\sigma^2, is what Donoho & Johnstone (1995) minimized to choose λ\lambda (SureShrink).

This is remarkable: the lasso picks its active set from the data, yet pays only for the size of the set. Shrinking the survivors toward zero exactly offsets the optimism of selecting them. Hard thresholding has no such discount. It jumps at ±λ\pm\lambda, so Stein's lemma fails, and a direct computation for one coordinate with μ=0\mu=0, σ=1\sigma=1 gives

df(Hλ)=E[y2 1{∣y∣>λ}]=2(1−Φ(λ))⏟E #nonzero+2λ ϕ(λ)⏟search cost.\mathrm{df}(H_\lambda)=\E\big[y^2\,\mathbf 1\{\abs y>\lambda\}\big]=\underbrace{2\big(1-\Phi(\lambda)\big)}_{\E\,\#\text{nonzero}}+\underbrace{2\lambda\,\phi(\lambda)}_{\text{search cost}}.
Think first At λ=2\lambda=2 (with μ=0\mu=0, σ=1\sigma=1), the expected number of nonzeros is 2(1−Φ(2))≈0.0462(1-\Phi(2))\approx0.046. Estimate the degrees of freedom of hard thresholding before revealing.

The search term is 2⋅2⋅ϕ(2)≈4⋅0.054=0.2162\cdot2\cdot\phi(2)\approx4\cdot0.054=0.216, so df≈0.261\mathrm{df}\approx0.261: about 5.7 times the expected number of nonzeros. A coordinate survives only when its noise happens to be large, and then it is kept at full size, so the fit follows its noise strongly. Ye (1998) introduced generalized degrees of freedom to measure this effect in adaptive procedures such as variable selection and regression trees, and Janson, Fithian & Hastie (2015) showed that for nonconvex procedures df can be non-monotone in the size constraint, can exceed the dimension of the space, and for nonconvex projection methods can be unbounded.

ProcedureDegrees of freedomWhy
least squares, pp fixed predictorspptr⁡(H)\operatorname{tr}(H)
ridge∑jdj2/(dj2+λ)<p\sum_jd_j^2/(d_j^2+\lambda)<pshrinkage reduces sensitivity
kk-NN (point counts itself)n/kn/keach fit weighs its own label by 1/k1/k
lassoE #nonzero\E\,\#\text{nonzero} (full-rank XX), Erank⁡(XA)\E\rank(X_{\mathcal A}) in generalselection and shrinkage cancel
best subset of size kktypically more than kkselection without shrinkage
The first three are linear smoothers; the last two choose which predictors to use from the data.

What the criteria estimate

CpC_p, AIC and SURE estimate in-sample error: new labels at the same inputs. Cross-validation estimates out-of-sample error at new inputs, for a model trained on slightly fewer points. For large nn the two often agree: Stone (1977) proved that choosing a model by leave-one-out cross-validation and by AIC are asymptotically equivalent under regularity conditions. With few points per parameter, or high-leverage inputs, they can differ, because extrapolating to new inputs costs more than refitting at old ones.

Efron (2004) related the two families through a Rao–Blackwell-type argument, in which cross-validation acts like a randomized version of a covariance penalty, and concluded that model-based penalties are more accurate when the model is believable, while cross-validation needs fewer assumptions. For procedures without a formula for df, such as trees or boosting, the covariance can be estimated by a parametric bootstrap: perturb y\mathbf y, refit, and regress the fitted values on the perturbations.

Key takeaways

  • Training error is optimistic by exactly 2n∑iCov⁡(y^i,yi)\frac2n\sum_i\Cov(\hat y_i,y_i); degrees of freedom df=∑iCov⁡(y^i,yi)/σ2\mathrm{df}=\sum_i\Cov(\hat y_i,y_i)/\sigma^2 measure complexity by sensitivity to the labels, not by counting parameters.
  • Linear smoothers have df=tr⁡S\mathrm{df}=\operatorname{tr}S: pp for least squares, ∑jdj2/(dj2+λ)\sum_jd_j^2/(d_j^2+\lambda) for ridge, n/kn/k for kk-NN. CpC_p and AIC add 2 df σ2/n2\,\mathrm{df}\,\sigma^2/n to the training error.
  • Stein's lemma turns the covariance into an expected divergence, giving SURE, an unbiased risk estimate for Gaussian noise; applied to James–Stein it proves that shrinkage beats the sample mean for every mean in dimension ≥3\ge3.
  • The lasso's df equal its expected number of nonzeros (Erank⁡XA\E\rank X_{\mathcal A} in general): shrinkage pays for selection. Hard thresholding and best subset pay an extra search cost, and for nonconvex methods df can exceed the dimension.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A fitting rule sets y^i=yi\hat y_i=y_i for every ii (it interpolates its labels). What are its degrees of freedom?

  1. 00
  2. 11
  3. nn
  4. It depends on μ\boldsymbol\mu
Show the answer

C. nn

Cov⁡(y^i,yi)=Var⁡(yi)=σ2\Cov(\hat y_i,y_i)=\Var(y_i)=\sigma^2 for every ii, so df=n\mathrm{df}=n; equivalently S=IS=I and tr⁡S=n\operatorname{tr}S=n. The training error is zero and the optimism is 2σ22\sigma^2: the fit copies every bit of the noise.

Question 2

Which assumptions does Stein's unbiased risk estimate need?

  1. A linear estimator and any noise distribution
  2. Gaussian noise with known variance and a weakly differentiable estimator
  3. A correctly specified model and a large sample
  4. An estimator with a closed-form formula for its bias
Show the answer

B. Gaussian noise with known variance and a weakly differentiable estimator

Stein's lemma is an integration by parts against the Gaussian density, so the noise must be Gaussian (with known σ2\sigma^2 to evaluate SURE), and mm must be weakly differentiable. The estimator can be nonlinear and the model wrong: SURE is unbiased for the risk regardless.

Question 3

With an orthonormal design, the lasso and best-subset selection keep the same coordinates at matching thresholds. How do their degrees of freedom compare?

  1. Equal, because they select the same variables
  2. The lasso's equal its expected number of nonzeros; best subset's exceed it, because it keeps the selected coefficients at full size
  3. The lasso's are larger, because it uses an ℓ1\ell_1 penalty
  4. Neither has well-defined degrees of freedom
Show the answer

B. The lasso's equal its expected number of nonzeros; best subset's exceed it, because it keeps the selected coefficients at full size

Soft thresholding is continuous, and Stein's lemma gives df=E #nonzero\mathrm{df}=\E\,\#\text{nonzero}. Hard thresholding jumps at the threshold and adds a search cost 2λϕ(λ)2\lambda\phi(\lambda) per null coordinate (with σ=1\sigma=1), so at λ=2\lambda=2 its degrees of freedom are about 5.7 times its expected number of nonzeros.

Question 4

Mallows' CpC_p is an unbiased estimate of which quantity?

  1. The error on new labels at the same inputs (in-sample error)
  2. The error at new inputs drawn from the population
  3. The training error
  4. The Bayes error
Show the answer

A. The error on new labels at the same inputs (in-sample error)

The optimism identity compares training error with fresh labels at the same xi\mathbf x_i, so Cp=err‾+2 df σ^2/nC_p=\overline{\mathrm{err}}+2\,\mathrm{df}\,\hat\sigma^2/n targets E[Errin]\E[\mathrm{Err}_{\text{in}}]. Error at new inputs adds the cost of extrapolation, which cross-validation captures and CpC_p does not.

Further reading

  • B. Efron, "The estimation of prediction error: covariance penalties and cross-validation," Journal of the American Statistical Association 99(467), 619–632 (2004).
  • C. M. Stein, "Estimation of the mean of a multivariate normal distribution," Annals of Statistics 9(6), 1135–1151 (1981).
  • H. Zou, T. Hastie & R. Tibshirani, "On the 'degrees of freedom' of the lasso," Annals of Statistics 35(5), 2173–2192 (2007).
  • R. J. Tibshirani & J. Taylor, "Degrees of freedom in lasso problems," Annals of Statistics 40(2), 1198–1232 (2012).
  • L. Janson, W. Fithian & T. J. Hastie, "Effective degrees of freedom: a flawed metaphor," Biometrika 102(2), 479–485 (2015).