ContentsChapter 4 · Regression1 of 4

4.1

Least squares

In this chapter
  1. 4.1Least squares
  2. 4.2Basis functions
  3. 4.3Overfitting and bias–variance
  4. 4.4Regularisation
Before you readIf no line goes through every point, what makes one line the best fit?
Watch: Linear Regression, Clearly Explained!!! · StatQuest with Josh Starmer

Fit a line y=w0+w1xy = w_0 + w_1 x with intercept w0w_0 and slope w1w_1 At each input xix_i the residual is the observed yiy_i minus the line’s prediction.

The design matrix XX has one row (1,xi)(1, x_i) per observation. The column of ones adds w0w_0 to every prediction. With w=(w0,w1)Tw = (w_0, w_1)^T and yy the vector of observed outputs, the predictions are XwXw Least squares minimises the sum of squared residuals:

∥y−Xw∥2=∑i(yi−(w0+w1xi))2.\|y - Xw\|^2 = \sum_i \big(y_i - (w_0 + w_1 x_i)\big)^2.

The best predictions XwXw are the projection of yy onto the column space of XX The residual is orthogonal to each column, so XT(y−Xw)=0X^T(y-Xw)=0 This gives the normal equations:

XTXw=XTy.X^T Xw = X^T y.

If the columns of XX are independent, XTXX^T X is invertible and w=(XTX)−1XTyw = (X^T X)^{-1}X^T y With independent Gaussian noise of mean zero and common variance, least squares is also the maximum-likelihood fit.

024680246810
Sum of squared errors = 14.84Minimum = 3.77
Move either end of the line up or down. Each vertical gap is a residual. The area of its square is that residual squared; the best fit makes the total area smallest.
Recall
Why does the design matrix have a column of ones?
Recall
What does least squares minimise?
Recall
Give the normal equations for least squares.
Exercise
Use the normal equations to fit a line to (0,2)(0,2) (1,1)(1,1) and (2,6)(2,6) Give w0w_0 and w1w_1 in that order.
Exercise
Find the xx that minimises g(x)=∑i(x−yi)2g(x) = \sum_i (x-y_i)^2 when the observed values are 11 44 77 and 1212