Contents
04Least variance
How reliable is \hat\beta? We measure it by its variance: how much the estimate changes from study to study, the spread of the dots in Figure 2b. Which estimate is the best? Best means two things: first unbiased, then, among unbiased estimators, the least variance. Two more assumptions give the variance a simple formula and make least squares the best.
4.1Two more assumptions and the variance
Definition 2 (Constant variance and independent errors). Assumption 3 (constant variance, homoscedasticity). \Var(\varepsilon_i\mid X)=\sigma^2 for every i: the same error spread at every value of the inputs.
Assumption 4 (independent errors). \Cov(\varepsilon_i,\varepsilon_j\mid X)=0 for i\neq j: one observation’s error says nothing about another’s.
Together they say \Var(\varepsilon\mid X)=\sigma^2I. Patients sampled independently satisfy Assumption 4; the end of this chapter returns to the cases that do not.
Proposition 2 (The variance of least squares, Hastie et al., 2009, §3.2). Under Assumptions 1–4,
From (8), \hat\beta-\beta=A\varepsilon with A=(X^\top X)^{-1}X^\top. For a fixed matrix A, \Var(A\varepsilon)=A\Var(\varepsilon)A^\top=\sigma^2AA^\top=\sigma^2(X^\top X)^{-1}X^\top X(X^\top X)^{-1}=\sigma^2(X^\top X)^{-1}. With one input, the slope’s entry of (X^\top X)^{-1} is 1/S_{xx}.
The one-input formula has a clear intuition: the slope is more precise when the noise \sigma is small, when there are many observations, and when the inputs are spread out, because a lever on a wide base is hard to tip. The error variance is unknown; it is estimated from the residuals as
dividing by n-p-1 rather than n because fitting p+1 coefficients uses up p+1 degrees of freedom; with this divisor \hat\sigma^2 is unbiased (Hastie et al., 2009, §3.2). The standard error of a coefficient is the square root of its estimated variance, \mathrm{SE}(\hat\beta_j)=\hat\sigma\sqrt{[(X^\top X)^{-1}]_{jj}}.
For our slope, \hat\sigma=62.5 points of progression and \mathrm{SE}(\hat\beta_1)=\hat\sigma/\sqrt{S_{xx}}=0.67: if Assumptions 1–4 hold, a typical study puts the slope within about 0.67 of the truth. We check Assumption 3 below.
4.2The Gauss–Markov theorem
Least squares is one linear unbiased estimator; there are many others, for example the line through the mean point of the lightest third of the patients and that of the heaviest third. Which is best? Gauss’s answer is one of the classic results of statistics.
Theorem 2 (Gauss–Markov, Hastie et al., 2009, §3.2.2). Under Assumptions 1–4 and full column rank, least squares is BLUE, the best linear unbiased estimator: for any other estimator \tilde\beta=Cy that is linear in y and unbiased, \Var(\tilde\beta\mid X)-\Var(\hat\beta\mid X) is positive semidefinite. In particular every coefficient, and every linear combination a^\top\beta, has a variance at least as large under \tilde\beta as under least squares.
Write C=(X^\top X)^{-1}X^\top+D for some matrix D. Then \E[\tilde\beta\mid X]=CX\beta=\beta+DX\beta, and unbiasedness for every \beta requires DX=0. Using \Var(\varepsilon\mid X)=\sigma^2I,
because DX=0. The extra term DD^\top is positive semidefinite, since a^\top DD^\top a=\|D^\top a\|^2\ge0.
In words, which is how the companion video puts it: any other linear unbiased estimator is least squares plus an extra part Dy; unbiasedness (DX=0) makes that part pure noise, Dy=D\varepsilon; the noise is uncorrelated with least squares, \mathrm{Cov}(\hat\beta,D\varepsilon\mid X)=\sigma^2(X^\top X)^{-1}(DX)^\top=0; and adding uncorrelated noise can only add variance.
Note what the theorem does not say. It needs no normal errors. And it compares least squares only with other linear unbiased estimators: a biased method such as ridge regression (Episode 4) can have a smaller mean squared error, trading a little bias for less variance (Episode 2).
4.3Checking constant variance: the funnel
The theorem is only as good as its assumptions, so we check. The standard picture is the residuals plotted against the fitted values (James et al., 2021, §3.3.3). If the variance is constant, the residuals form an even band around zero. If the spread grows with the fitted value, they form a funnel: heteroscedasticity.
Worked example 3 (A funnel in our data). Sort the patients by fitted value and split them into thirds (Figure 4). The residuals of the lowest third have a standard deviation of about 50 (points of progression); the middle and top thirds about 68. The spread grows with the prediction, so the variance is not constant: heteroscedasticity, and Assumption 3 fails. The Breusch–Pagan test, which regresses the squared residuals on the inputs and asks whether they can be predicted, agrees: p<0.001 (Breusch and Pagan, 1979).
What breaks? Not the slope: the proof of Theorem 1 used only Assumptions 1–2, so \hat\beta stays unbiased. But unequal noise affects two different things: how we fit, and how we report the uncertainty.
- How we fit: weighted least squares. Least squares is no longer the linear unbiased estimator with the least variance. It weighs every patient equally, but the patients with large predictions are noisier and deserve less weight. Weighted least squares gives observation i the weight 1/\sigma_i^2: \hat\beta_{\mathrm{WLS}}=(X^\top WX)^{-1}X^\top Wy with W=\mathrm{diag}(1/\sigma_i^2) (Murphy, 2022, eq. 11.24). It is a new fit, with new coefficients, and with the right weights it is again BLUE: its variance is smaller. Aitken (1936) proved the general version for any known error covariance (generalized least squares). In practice the \sigma_i^2 are unknown and must themselves be modeled.
How we report: robust standard errors (White, 1980). The standard error 0.67 above assumes one \sigma^2 for every patient, so with a funnel it is biased, and the confidence interval and t test of Chapter 5 are built on it. Keep the estimate, fix its variance. Repeating the proof of Proposition 2 without Assumption 3 gives \Var(\hat\beta\mid X)=(X^\top X)^{-1}X^\top\,\mathrm{diag}(\sigma_i^2)\,X(X^\top X)^{-1}, and White showed that replacing each unknown \sigma_i^2 by the patient’s own squared residual e_i^2 gives a consistent estimate:
\widehat{\Var}_{\mathrm{robust}}(\hat\beta)=(X^\top X)^{-1}\Bigl(\sum_{i=1}^{n}e_i^2\,x_ix_i^\top\Bigr)(X^\top X)^{-1}. \tag{14}It is called the sandwich estimator for its shape. For our slope it gives 0.61 instead of 0.67: the usual formula overstated the uncertainty by about 10\%.
- Transform the outcome. When the spread grows with the level, a concave transformation such as \log y or \sqrt y often evens it out (James et al., 2021, §3.3.3). Regressing \sqrt{y} on body mass index removes our funnel, at the price of a slope on a less natural scale.
4.4Measuring the spread directly: the bootstrap
We can also measure the spread from our real patients directly, with the bootstrap (Efron, 1979), a tool machine learning uses a lot. In Chapter 3, new studies drew new patients from a population we made up. With real data we cannot recruit new patients, so the bootstrap treats our 442 patients as the population: draw 442 of them at random, with replacement, so that some are drawn twice or more and some not at all, and refit.
Worked example 4 (The bootstrap on our patients). Repeat the draw 2{,}000 times: the slopes spread by 0.61 (their standard deviation), just like White’s robust standard error, not the classical 0.67. These resampled studies center on our own 10.2, not on the true slope, so they cannot check unbiasedness; but they do measure the spread.
4.5Correlated errors
Assumption 4 fails when observations come in sequences or groups: a time series, repeated measurements of the same person, patients from the same clinic. Correlated observations partly repeat each other, so they carry less information than independent ones. Least squares stays unbiased, but (11) counts every observation as new, so the standard errors are typically too small. Check by plotting the residuals in time order (long runs of the same sign are the warning) or with the Durbin–Watson statistic (Durbin and Watson, 1950); fix with standard errors that allow for the correlation (clustered or time-series versions of (14)) or generalized least squares. The bootstrap above also assumes independent patients; with groups, resample whole groups.
So under Assumptions 1–4 least squares is the best linear unbiased estimate, and now we know the center of \hat\beta, the true \beta, and its spread, the standard error. But how confident can we be about the true slope? For that we need the whole distribution of \hat\beta: its shape. Chapter 5 finds it.