Simple Algorithm
Machine Learning · Convergence · 28 min

Inference for Linear Models

Gauss–Markov, standard errors, confidence intervals and their assumptions.

A regression coefficient is an estimate, and an estimate without an error bar is half an answer. Under two moment assumptions least squares is the best linear unbiased estimator; add Gaussian noise and every interval and test becomes exact. This lesson proves both, then shows what breaks when the noise variance changes, when there are many features, and when the model was chosen by looking at the data.

What you’ll learn

  • State and prove the Gauss–Markov theorem, and explain what "best linear unbiased" does and does not rule out.
  • Derive the distributions of w^\hat{\mathbf w} and σ^2\hat\sigma^2 under Gaussian noise, and build tt-tests, confidence intervals, prediction intervals and FF-tests.
  • Compute heteroscedasticity-consistent (sandwich) standard errors and explain when even they fail.
  • Explain why classical inference after data-driven model selection is invalid, and describe valid alternatives.

Assumptions and the Gauss–Markov theorem

Treat the design X∈Rn×dX \in \R^{n\times d} as fixed (or condition on it), with full column rank, and assume y=Xw+ε\mathbf y = X\mathbf w + \boldsymbol\varepsilon with

  • (A1) Eε=0\E\boldsymbol\varepsilon = \mathbf 0: the linear model for the mean is correct;
  • (A2) Cov⁡ε=σ2I\Cov\boldsymbol\varepsilon = \sigma^2 I: the errors are uncorrelated and have a common variance (homoscedastic).

No distribution is assumed. Since w^=(X⊤X)−1X⊤y=w+(X⊤X)−1X⊤ε\hat{\mathbf w} = (X^\top X)^{-1}X^\top\mathbf y = \mathbf w + (X^\top X)^{-1}X^\top\boldsymbol\varepsilon, (A1) makes least squares unbiased and (A2) gives Cov⁡w^=σ2(X⊤X)−1\Cov\hat{\mathbf w} = \sigma^2(X^\top X)^{-1}.

DerivationProof
  1. C=(X⊤X)−1X⊤+D,CX=I+DX=I ⟹ DX=0C = (X^\top X)^{-1}X^\top + D, \qquad CX = I + DX = I \ \Longrightarrow\ DX = 0

    Unbiasedness for every w\mathbf w means E[Cy]=CXw=w\E[C\mathbf y] = CX\mathbf w = \mathbf w for all w\mathbf w, that is CX=ICX = I. Write CC as least squares plus a correction DD; then DX=0DX = 0:

  2. Cov⁡w~=σ2CC⊤=σ2[(X⊤X)−1+(X⊤X)−1(DX)⊤+DX(X⊤X)−1+DD⊤]=σ2(X⊤X)−1+σ2DD⊤\Cov\tilde{\mathbf w} = \sigma^2CC^\top = \sigma^2\big[(X^\top X)^{-1} + (X^\top X)^{-1}(DX)^\top + DX(X^\top X)^{-1} + DD^\top\big] = \sigma^2(X^\top X)^{-1} + \sigma^2DD^\top

    Compute the covariance with (A2). The cross terms contain DXDX or its transpose, so they vanish:

  3. Var⁡(a⊤w~)−Var⁡(a⊤w^)=σ2∥D⊤a∥2 ≥ 0\Var(\mathbf a^\top\tilde{\mathbf w}) - \Var(\mathbf a^\top\hat{\mathbf w}) = \sigma^2\norm{D^\top\mathbf a}^2 \ \ge\ 0

    DD⊤DD^\top is positive semidefinite, with a⊤DD⊤a=∥D⊤a∥2\mathbf a^\top DD^\top\mathbf a = \norm{D^\top\mathbf a}^2, which vanishes for every a\mathbf a only when D=0D = 0.

Gauss proved the theorem in the 1820s; it also carries Andrey Markov's name. Read its fine print:

  • Biased estimators can win. Ridge regression is biased and has smaller mean squared error than least squares for a suitable λ>0\lambda > 0, often by a lot when features are collinear (Convergence lesson Ridge Regression in Depth).
  • Nonlinear estimators can win. With heavy-tailed noise, Huber's or absolute-loss estimators can be far more precise than least squares, and hence than every linear unbiased estimator (Convergence lesson Robust Regression).
  • Correlated or unequal noise. If Cov⁡ε=σ2Σ\Cov\boldsymbol\varepsilon = \sigma^2\Sigma with known Σ\Sigma, least squares is still unbiased but no longer best; the BLUE is generalized least squares, w^GLS=(X⊤Σ−1X)−1X⊤Σ−1y\hat{\mathbf w}_{\mathrm{GLS}} = (X^\top\Sigma^{-1}X)^{-1}X^\top\Sigma^{-1}\mathbf y (Aitken, 1935).
  • Gaussian noise removes the word "linear". If ε∼N(0,σ2I)\boldsymbol\varepsilon \sim \mathcal N(\mathbf 0, \sigma^2I), the Fisher information for w\mathbf w is X⊤X/σ2X^\top X/\sigma^2, and Cov⁡w^=σ2(X⊤X)−1\Cov\hat{\mathbf w} = \sigma^2(X^\top X)^{-1} attains the Cramér–Rao bound: least squares is then the best of all unbiased estimators.

Gaussian noise: exact distributions

DerivationProof
  1. w^−w=(X⊤X)−1X⊤ε∼N(0, σ2(X⊤X)−1)\hat{\mathbf w} - \mathbf w = (X^\top X)^{-1}X^\top\boldsymbol\varepsilon \sim \mathcal N\big(\mathbf 0,\ \sigma^2(X^\top X)^{-1}\big)

    (a) w^=w+(X⊤X)−1X⊤ε\hat{\mathbf w} = \mathbf w + (X^\top X)^{-1}X^\top\boldsymbol\varepsilon is an affine function of a Gaussian vector, hence Gaussian, with the mean and covariance computed earlier.

  2. E[RSS]=σ2Tr⁡(I−H)=σ2(n−d)\E[\mathrm{RSS}] = \sigma^2\Tr(I - H) = \sigma^2(n - d)

    The mean of RSS needs only (A1) and (A2), not normality. Write RSS=ε⊤(I−H)ε\mathrm{RSS} = \boldsymbol\varepsilon^\top(I - H)\boldsymbol\varepsilon and use E[ε⊤Aε]=Tr⁡(ACov⁡ε)\E[\boldsymbol\varepsilon^\top A\boldsymbol\varepsilon] = \Tr(A\Cov\boldsymbol\varepsilon): the nn residuals live in an (n−d)(n-d)-dimensional space, and dividing by n−dn - d corrects for it.

  3. RSSσ2=z⊤Λz=∑k=1n−dzk2∼χn−d2\frac{\mathrm{RSS}}{\sigma^2} = \mathbf z^\top\Lambda\mathbf z = \sum_{k=1}^{n-d} z_k^2 \sim \chi^2_{n-d}

    (b) Diagonalize the projection I−H=QΛQ⊤I - H = Q\Lambda Q^\top, with n−dn - d eigenvalues equal to 1 and the rest 0. The vector z=Q⊤ε/σ\mathbf z = Q^\top\boldsymbol\varepsilon/\sigma is standard Gaussian, so RSS is a sum of n−dn - d independent squared standard normals:

  4. Cov⁡(Hε, (I−H)ε)=σ2H(I−H)=0\Cov\big(H\boldsymbol\varepsilon,\ (I - H)\boldsymbol\varepsilon\big) = \sigma^2H(I - H) = 0

    (c) w^−w=(X⊤X)−1X⊤Hε\hat{\mathbf w} - \mathbf w = (X^\top X)^{-1}X^\top H\boldsymbol\varepsilon depends on ε\boldsymbol\varepsilon only through HεH\boldsymbol\varepsilon, and RSS only through (I−H)ε(I - H)\boldsymbol\varepsilon. These are jointly Gaussian and uncorrelated, hence independent. A standard normal divided by an independent χn−d2/(n−d)\sqrt{\chi^2_{n-d}/(n-d)} is tn−dt_{n-d}.

QuestionInterval or test (level 1−α1-\alpha, t=tn−d, 1−α/2t = t_{n-d,\,1-\alpha/2})
Where is wjw_j?Confidence interval w^j±t⋅se(w^j)\hat w_j \pm t\cdot\mathrm{se}(\hat w_j); test wj=0w_j = 0 with w^j/se(w^j)\hat w_j/\mathrm{se}(\hat w_j)
Where is the mean response x0⊤w\mathbf x_0^\top\mathbf w?x0⊤w^±t σ^x0⊤(X⊤X)−1x0\mathbf x_0^\top\hat{\mathbf w} \pm t\,\hat\sigma\sqrt{\mathbf x_0^\top(X^\top X)^{-1}\mathbf x_0}
Where will a new y0y_0 fall?Prediction interval x0⊤w^±t σ^1+x0⊤(X⊤X)−1x0\mathbf x_0^\top\hat{\mathbf w} \pm t\,\hat\sigma\sqrt{1 + \mathbf x_0^\top(X^\top X)^{-1}\mathbf x_0}
Do qq linear restrictions hold (a smaller nested model)?F=(RSS0−RSS1)/qRSS1/(n−d)∼Fq, n−dF = \dfrac{(\mathrm{RSS}_0 - \mathrm{RSS}_1)/q}{\mathrm{RSS}_1/(n-d)} \sim F_{q,\,n-d} under the restrictions; for one coefficient F=t2F = t^2
The "1 +" in the prediction interval is the new observation's own noise. As nn grows the confidence interval shrinks to a point, but the prediction interval keeps a width of about 2×1.96 σ2\times1.96\,\sigma at 95%.
Predict before you look

For the four points (0,1)(0,1), (1,3)(1,3), (2,4)(2,4), (3,8)(3,8): slope w^1=2.2\hat w_1 = 2.2, RSS=1.8\mathrm{RSS} = 1.8 and Sxx=5S_{xx} = 5, so [(X⊤X)−1]slope=1/Sxx[(X^\top X)^{-1}]_{\text{slope}} = 1/S_{xx}. What is the tt-statistic for testing a zero slope?

Show the answer

5.19

σ^2=1.8/(4−2)=0.9\hat\sigma^2 = 1.8/(4 - 2) = 0.9 and se=0.9/5≈0.424\mathrm{se} = \sqrt{0.9/5} \approx 0.424, so t=2.2/0.424≈5.19t = 2.2/0.424 \approx 5.19. With only 2 degrees of freedom the 97.5% quantile is 4.304.30 (not 1.96), giving the 95% interval 2.2±1.83=(0.37,4.03)2.2 \pm 1.83 = (0.37, 4.03) and a two-sided pp-value of about 0.0350.035 (for 2 degrees of freedom the tt tail has the closed form p=1−t/2+t2p = 1 - t/\sqrt{2 + t^2}).

Unequal variances and sandwich standard errors

Suppose (A2) fails: the errors are still uncorrelated, but Var⁡εi=σi2\Var\varepsilon_i = \sigma_i^2 varies, Cov⁡ε=Ω=diag⁡(σ12,…,σn2)\Cov\boldsymbol\varepsilon = \Omega = \diag(\sigma_1^2, \dots, \sigma_n^2). Least squares stays unbiased, but its covariance becomes a sandwich:

Cov⁡w^=(X⊤X)−1 X⊤ΩX (X⊤X)−1,\Cov\hat{\mathbf w} = (X^\top X)^{-1}\,X^\top\Omega X\,(X^\top X)^{-1},

and the classical σ^2(X⊤X)−1\hat\sigma^2(X^\top X)^{-1} can be too small or too large, depending on whether the large variances sit at high-leverage points. White (1980) showed that the "meat" can be estimated without modeling Ω\Omega at all, by plugging in squared residuals: X⊤Ω^X=∑iω^i xixi⊤X^\top\hat\Omega X = \sum_i \hat\omega_i\,\mathbf x_i\mathbf x_i^\top. The resulting estimator is consistent as n→∞n \to \infty with dd fixed.

Estimatorω^i\hat\omega_iIdea
HC0 (White, 1980)ei2e_i^2consistent, but too small in small samples, because Eei2=σ2(1−hii)\E e_i^2 = \sigma^2(1 - h_{ii}) under homoscedasticity
HC1nn−d ei2\frac{n}{n-d}\,e_i^2a global degrees-of-freedom correction
HC2ei2/(1−hii)e_i^2/(1 - h_{ii})exactly unbiased when the errors are in fact homoscedastic
HC3ei2/(1−hii)2e_i^2/(1 - h_{ii})^2close to the jackknife estimator, which MacKinnon & White (1985) found the most reliable in small samples; more conservative
The leverage corrections matter most when a few points have high leverage. Cluster-robust versions sum the products xiei\mathbf x_ie_i within groups of correlated observations before forming the meat.

Two warnings. First, these are large-sample results with dd fixed: when the number of covariates grows in proportion to nn, the usual Eicker–White estimators become inconsistent, and Cattaneo, Jansson & Newey (2018) derive a corrected formula. Second, the sandwich is more than a patch for heteroscedasticity. If the mean is not linear at all, least squares with random design estimates the best linear approximation w⋆=E[xx⊤]−1E[xy]\mathbf w^\star = \E[\mathbf x\mathbf x^\top]^{-1}\E[\mathbf x y], and the sandwich is the correct asymptotic covariance for that target, while the classical formula is not (Convergence lesson When the Model Is Wrong).

Check your understanding

A residual plot shows a funnel: the spread grows with xx. Which statement about ordinary least squares is correct?

  1. The coefficients are biased, so the fit must be discarded
  2. The coefficients are still unbiased, but the classical standard errors are unreliable; sandwich standard errors (or weighted least squares) fix the inference
  3. Nothing changes, because Gauss–Markov needs only Eε=0\E\boldsymbol\varepsilon = \mathbf 0
  4. The R2R^2 is invalid, but the standard errors are fine
Show the answer

B. The coefficients are still unbiased, but the classical standard errors are unreliable; sandwich standard errors (or weighted least squares) fix the inference

Unbiasedness needs only (A1). Heteroscedasticity breaks (A2), so σ2(X⊤X)−1\sigma^2(X^\top X)^{-1} is the wrong covariance and least squares is no longer BLUE; weighted least squares with weights 1/σi21/\sigma_i^2 would be, if the variances were known. Sandwich standard errors repair the inference without modeling them.

Inference after selection

Every interval above assumes the model was fixed before looking at the data. In practice analysts screen variables, run stepwise searches, try transformations, and keep what looks significant. Selection favors variables whose estimates came out large by chance, so the reported intervals undercover and the p-values are too small. Even consistent selection does not rescue classical inference uniformly over the parameter space; Leeb & Pötscher (2005) explain why, and discuss an impossibility result: the finite-sample distribution of a post-selection estimator cannot even be estimated uniformly consistently.

Predict before you look

You test 50 features that have no effect at all, each at level 0.050.05, and keep the most significant one. If the 50 tests were independent, what is the chance that at least one comes out "significant"?

Show the answer

92 %

1−0.9550≈0.9231 - 0.95^{50} \approx 0.923. Reporting the winner's pp-value as if it had been chosen in advance is the classic multiple-comparisons trap: a model built this way almost always "finds" something.

RemedyHow it worksCost
Sample splittingselect on one half of the data, do classical inference on the otherless data for each task; results depend on the split
Simultaneous intervals (PoSI; Berk, Brown, Buja, Zhang & Zhao, 2013)widen the intervals until they hold simultaneously for every submodel, so any selection rule, even an informal one, is coveredconservative, though less so than Scheffé's bound
Conditional (selective) inferencecompute pp-values conditional on the selection event, for example on which variables the lasso pickedrequires a precisely specified selection procedure
All three target valid inference for the selected model's coefficients; none of them makes a wrongly selected model right.

Key takeaways

  • Gauss–Markov: with mean-zero, uncorrelated, equal-variance errors, least squares has the smallest covariance among linear unbiased estimators. Biased (ridge) or nonlinear (robust) estimators can still beat it, and with Gaussian errors it is the best of all unbiased estimators.
  • Under Gaussian noise, w^∼N(w,σ2(X⊤X)−1)\hat{\mathbf w} \sim \mathcal N(\mathbf w, \sigma^2(X^\top X)^{-1}), RSS/σ2∼χn−d2\mathrm{RSS}/\sigma^2 \sim \chi^2_{n-d} independently, and tt-intervals, prediction intervals and FF-tests are exact. σ^2=RSS/(n−d)\hat\sigma^2 = \mathrm{RSS}/(n-d) is unbiased even without normality.
  • With unequal variances least squares stays unbiased but needs the sandwich covariance; HC0–HC3 estimate it from squared residuals, and they too fail when d/nd/n does not vanish.
  • Selecting a model with the data and then reporting classical intervals is invalid. Sample splitting, simultaneous (PoSI) intervals and conditional inference are valid alternatives.

Check yourself

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

Question 1

Which assumption is not needed for the Gauss–Markov theorem?

  1. The errors have mean zero
  2. The errors are normally distributed
  3. The errors are uncorrelated
  4. The errors have a common variance
Show the answer

B. The errors are normally distributed

Gauss–Markov uses only the first two moments: Eε=0\E\boldsymbol\varepsilon = \mathbf 0 and Cov⁡ε=σ2I\Cov\boldsymbol\varepsilon = \sigma^2I (uncorrelated, equal variances). Normality is needed for exact tt and FF distributions, and it upgrades "best linear unbiased" to "best unbiased".

Question 2

As the sample size nn grows (with a well-behaved design), what happens to a 95% confidence interval for the mean response at x0\mathbf x_0 and to a 95% prediction interval for a new y0y_0?

  1. Both shrink to zero width
  2. Both stay the same width
  3. The prediction interval shrinks to zero; the confidence interval does not
  4. The confidence interval shrinks to zero; the prediction interval approaches about ±1.96 σ\pm1.96\,\sigma
Show the answer

D. The confidence interval shrinks to zero; the prediction interval approaches about ±1.96 σ\pm1.96\,\sigma

x0⊤(X⊤X)−1x0→0\mathbf x_0^\top(X^\top X)^{-1}\mathbf x_0 \to 0 as data accumulate, so the uncertainty about the mean vanishes. A new observation still carries its own noise, the "1 +" under the square root, which no amount of data removes.

Question 3

Why is σ2\sigma^2 estimated by RSS/(n−d)\mathrm{RSS}/(n - d) rather than RSS/n\mathrm{RSS}/n?

  1. Because E[RSS]=σ2(n−d)\E[\mathrm{RSS}] = \sigma^2(n - d): the residuals are confined to an (n−d)(n-d)-dimensional subspace
  2. Because dividing by nn would make the estimate too large
  3. Because it is required for the residuals to sum to zero
  4. It is a convention with no effect on bias
Show the answer

A. Because E[RSS]=σ2(n−d)\E[\mathrm{RSS}] = \sigma^2(n - d): the residuals are confined to an (n−d)(n-d)-dimensional subspace

RSS=ε⊤(I−H)ε\mathrm{RSS} = \boldsymbol\varepsilon^\top(I-H)\boldsymbol\varepsilon has mean σ2Tr⁡(I−H)=σ2(n−d)\sigma^2\Tr(I - H) = \sigma^2(n - d). Fitting dd coefficients absorbs dd dimensions of noise, so RSS/nn underestimates σ2\sigma^2, badly when dd is a sizable fraction of nn.

Question 4

An analyst picks 5 of 50 candidate predictors by stepwise selection, refits by least squares, and reports the usual 95% intervals for the 5 coefficients. What is wrong?

  1. Nothing, provided the residuals look normal
  2. The intervals are too wide, because 45 predictors were dropped
  3. The intervals ignore the selection step and typically cover the targets less than 95% of the time
  4. Least squares is biased once any predictor is dropped
Show the answer

C. The intervals ignore the selection step and typically cover the targets less than 95% of the time

Variables are kept because their estimates looked large in this sample, so the usual intervals are centered on optimistically selected values and undercover. Split the sample, use simultaneous (PoSI) intervals, or condition on the selection event.

Further reading

  • G. Casella & R. L. Berger, Statistical Inference, 2nd ed. (Duxbury, 2002), chapter 11.
  • H. White, "A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity," Econometrica 48(4), 817–838 (1980).
  • J. G. MacKinnon & H. White, "Some heteroskedasticity-consistent covariance matrix estimators with improved finite sample properties," Journal of Econometrics 29(3), 305–325 (1985).
  • R. Berk, L. Brown, A. Buja, K. Zhang & L. Zhao, "Valid post-selection inference," Annals of Statistics 41(2), 802–837 (2013).
  • H. Leeb & B. M. Pötscher, "Model selection and inference: facts and fiction," Econometric Theory 21(1), 21–59 (2005).