MoonSay /Tutorials/Machine Learning Interview #14
Contents
  1. 01Introduction
  2. 02Least squares
  3. 03Unbiasedness
  4. 04Least variance
  5. 05Distribution and confidence
  6. 06Interview follow-ups
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References

02Least squares

We start with the method itself, before any assumption. The question is simple: given a cloud of points, which line describes it best?

2.1The best line

We start with one input, body mass index, so that the data can be drawn on a two-dimensional plane (body mass index across, progression up); more inputs change nothing but the number of columns of X. Figure 1a plots disease progression against body mass index, one dot per patient. Heavier patients tend to progress further, and we want to summarize that with a line \hat y=\beta_0+\beta_1x. For any candidate line, the vertical miss of patient i is the residual e_i=y_i-\beta_0-\beta_1x_i (the red segments). A good line makes the misses small overall. Least squares measures “small overall” by the sum of the squared misses,

\mathrm{RSS}(\beta)=\sum_{i=1}^{n}\bigl(y_i-x_i^\top\beta\bigr)^2=\|y-X\beta\|^2 , \tag{2}

and picks the \beta that makes it smallest. Squaring makes every miss count positively, punishes big misses more than small ones, and, as we will see, gives a closed-form answer. Figure 1b turns a line about the mean point and plots RSS against its slope: a parabola with a single bottom. Least squares picks the bottom.

(a) Disease progression one year after baseline against body mass index for the 442 patients of Efron et al. (2004), the least-squares line, and the residuals of a few patients; the vertical axis is drawn at body mass index 0, where the line meets it at the intercept. (b) The residual sum of squares of a line through the mean point (\bar x,\bar y) as its slope turns; the flat line (slope 0) has \mathrm{RSS}=\mathrm{TSS}=\sum_i(y_i-\bar y)^2. Computed for this tutorial, with the same data as the companion video.
Figure 1. (a) Disease progression one year after baseline against body mass index for the 442 patients of Efron et al. (2004), the least-squares line, and the residuals of a few patients; the vertical axis is drawn at body mass index 0, where the line meets it at the intercept. (b) The residual sum of squares of a line through the mean point (\bar x,\bar y) as its slope turns; the flat line (slope 0) has \mathrm{RSS}=\mathrm{TSS}=\sum_i(y_i-\bar y)^2. Computed for this tutorial, with the same data as the companion video.
Try itTurn the line, watch the squares

The 442 patients of Figure 1. Move the slope of a line through the mean point; every red segment is a residual, and the right panel adds up their squares, equation (2). Least squares picks the bottom of that curve.

Try itStep through the derivation

Where is the bottom of the bowl in Figure 1b? Press Next step to derive the closed form one line at a time; each term keeps its colour from the expansion to its derivative. The same steps are written out in full below.

2.2The normal equations

RSS is a quadratic function of \beta, so its minimum is where the gradient vanishes. Expand (2):

\mathrm{RSS}(\beta)=y^\top y-2\beta^\top X^\top y+\beta^\top X^\top X\beta . \tag{3}

Using \nabla_\beta(\beta^\top a)=a and \nabla_\beta(\beta^\top A\beta)=2A\beta for a symmetric matrix A,

\nabla_\beta\,\mathrm{RSS}=-2X^\top y+2X^\top X\beta=0\qquad\Longrightarrow\qquad X^\top X\hat\beta=X^\top y . \tag{4}

These are the normal equations (Murphy, 2022, eq. 11.8). If the columns of X are linearly independent (X has full column rank: no column is an exact linear combination of the others; this needs at least as many observations as columns, that is, as coefficients), X^\top X is invertible and

\hat\beta=(X^\top X)^{-1}X^\top y . \tag{5}

The minimum is unique because the Hessian, 2X^\top X, is positive definite: v^\top X^\top Xv=\|Xv\|^2>0 for every v\neq0 when the columns are independent. With one input, solving the two normal equations by hand gives the familiar formulas

\hat\beta_1=\frac{\sum_i(x_i-\bar x)(y_i-\bar y)}{\sum_i(x_i-\bar x)^2}=\frac{\Cov(x,y)}{\Var(x)},\qquad \hat\beta_0=\bar y-\hat\beta_1\bar x : \tag{6}

the slope is the covariance of x and y divided by the variance of x, and the line passes through the mean point.

Worked example 1 (Body mass index and progression). For our 442 patients, (6) gives \hat y=-117.8+10.2\,x. Each extra point of body mass index goes with about 10 more points of progression a year later. The intercept, -117.8, is the prediction at body mass index 0: the point where the line meets the vertical axis of Figure 1a, drawn at 0 for that reason. No patient has a body mass index anywhere near 0, so the intercept places the line; it is not meant to be read on its own. The line explains R^2=34\% of the variance of progression: \mathrm{RSS} is 66\% of the flat line’s \mathrm{TSS} in Figure 1b.

2.3Geometry: a projection

The normal equations have a picture (Murphy, 2022, §11.2.2.2). Write them as X^\top(y-X\hat\beta)=X^\top e=0: the residual vector is perpendicular (normal, hence the name) to every column of X. Think of y as a point in n-dimensional space and of all vectors X\beta as a flat subspace through the origin. The fitted vector \hat y=X\hat\beta=X(X^\top X)^{-1}X^\top y is the foot of the perpendicular from y to that subspace, its orthogonal projection, and the residual e is the perpendicular itself.

Pause and think. Why do the residuals of a least-squares fit with an intercept always add up to zero?

Show the answer

The first column of X is all ones, and X^\top e=0 says the residuals are perpendicular to it: \sum_ie_i=0. The same equations make the residuals uncorrelated with every input, whatever the truth is, a fact that matters in Chapter 3.

So far the derivation used no assumption at all: least squares gives a line for our data, whatever the data look like. So why does linear regression need assumptions? To judge the estimate \hat\beta. Is it unbiased, that is, right on average (Chapter 3)? Does it have the least variance (Chapter 4)? How confident can we be about the true slope (Chapter 5)? Each answer needs assumptions, and each chapter adds the ones it needs.