Least squares
Fitting a model to data that no model fits exactly — which turns out to be projection in disguise.
- Set up a regression as an overdetermined system Xβ = y
- Solve it with the normal equations and know why they work
- Compare models by their sum of squared errors
Eight data points and a straight line with two parameters: eight equations, two unknowns, and almost certainly no line passes through all eight points. The system \(X\boldsymbol\beta = \mathbf{y}\) has no exact solution.
Least squares asks for the next best thing — the \(\boldsymbol\beta\) that makes the total squared error as small as possible:
\[\min_{\boldsymbol\beta}\; \|\mathbf{y} - X\boldsymbol\beta\|^2\]“Squared” is literal. Each residual — the vertical gap between a point and the line — can be drawn as a square, and least squares minimises their total area.
The normal equations
This is the previous lesson again. \(X\boldsymbol\beta\) ranges over the column space of \(X\), and the closest point to \(\mathbf{y}\) in that space is the projection — where the error is perpendicular to every column:
\[X^{\mathsf{T}}(\mathbf{y} - X\boldsymbol\beta) = \mathbf{0} \quad\Longrightarrow\quad X^{\mathsf{T}}X\,\boldsymbol\beta = X^{\mathsf{T}}\mathbf{y}\]The notebook solves it three ways and gets the same answer:
beta1 = np.array(Matrix(np.concatenate([X.T@X, X.T@y], axis=1)).rref()[0][:, -1]) # row reduction
beta2 = np.linalg.inv(X.T @ X) @ (X.T @ y) # left inverse
beta3 = np.linalg.solve(X.T @ X, X.T @ y) # practical choice
It also tries applying RREF straight to \([X \mid \mathbf{y}]\) — and that doesn’t work, because an inconsistent system just produces a contradiction row. Multiplying by \(X^{\mathsf{T}}\) first is what turns an unsolvable system into a solvable one.
The design matrix is the model
The columns of \(X\) decide what shapes the fit can take. The notebook fits the same eight points three ways:
data = np.array([[-4, 0, -3, 1, 2, 8, 5, 8]]).T
N = len(data)
X = np.ones([N, 1]) # 1. a constant: the mean
X = np.array([np.arange(0, N)]).T # 2. a slope through the origin
X = np.concatenate([np.ones([N, 1]),
np.array([np.arange(0, N)]).T], axis=1) # 3. intercept + slope
b = np.linalg.solve(X.T @ X, X.T @ data)
The first one recovers a classic result: with a single column of ones, the least-squares answer is exactly the mean (2.125).
The intercept-plus-slope model wins here, at \(\hat{y} = -3.83 + 1.70\,x\). A column of ones is almost always worth including: without it the line is forced through the origin.
A word on numerics
\(X^{\mathsf{T}}X\) squares the condition number of \(X\), which can lose precision on badly scaled data. That is why production code reaches for np.linalg.lstsq (which uses the SVD) rather than forming the normal equations explicitly. For a well-behaved problem like this one, all methods agree.
Try it yourself. Add a third column \(x^2\) to the design matrix and refit. Does the error drop again? Now add columns up to \(x^7\): the error falls to zero, because eight parameters can pass through eight points exactly. Plot that curve and explain why a perfect fit is a bad model.