ML Foundations Lecture 10 of 14
Part III — Linear Algebra · Lecture 10

Least Squares and the Normal Equations

Real data gives you more equations than unknowns, and no exact solution. Least squares is the principled answer: project the target onto what your model can actually reach. This is linear regression, derived twice — geometrically and by calculus — and shown to be the same theorem.

By the end you can

  • Explain why \(\mX\vw=\vy\) is usually unsolvable and what we should ask instead
  • Derive the normal equations \(\mX^\top\mX\vw = \mX^\top\vy\) from orthogonality
  • Derive the same equations by minimising \(\|\mX\vw-\vy\|^2\)
  • State when the solution is unique, and what ridge regularisation fixes
  • Fit a regression in NumPy correctly — and know why lstsq beats inv

Assumed

  • Lecture 8: projection, orthogonality, norms
  • Lecture 9: matrices as maps, column space, rank
  • Lecture 7: convexity and the second-derivative test

1An unsolvable system

We have \(n\) data points and \(d\) features, and we want weights \(\vw\) such that predictions match targets exactly:

\[\mX\vw = \vy, \qquad \mX\in\RR^{n\times d},\ \vw\in\RR^{d},\ \vy\in\RR^{n}.\]

In any realistic setting \(n \gg d\): thousands of houses, a handful of features. That is thousands of equations in a handful of unknowns — an overdetermined system. By Lecture 9's column picture, \(\mX\vw\) ranges over the column space \(\mathcal{C}(\mX)\), a subspace of \(\RR^n\) of dimension at most \(d\). Meanwhile \(\vy\) is an arbitrary point of \(\RR^n\). A \(d\)-dimensional subspace inside an \(n\)-dimensional space is a vanishingly thin sliver, so \(\vy\) is essentially never in it.

Intuition

Picture \(n=3\), \(d=2\): the reachable predictions form a plane through the origin in three-dimensional space, and \(\vy\) is a point floating somewhere off that plane. No \(\vw\) can hit it. So we stop asking for an exact hit and ask a better question: which point of the plane is closest to \(\vy\)? That is the projection question from Lecture 8, now with a subspace instead of a line.

The least-squares problem

\[ \hat{\vw} \;=\; \argmin_{\vw\in\RR^d}\ \|\mX\vw-\vy\|^2 \;=\; \argmin_{\vw}\ \sum_{i=1}^{n}\big(\vx_i^\top\vw - y_i\big)^2. \]

The vector \(\vr = \vy-\mX\hat{\vw}\) is the residual, and \(\hat{\vy}=\mX\hat{\vw}\) is the vector of fitted values.

y lives in ℝ³; the model can only reach a plane. Least squares drops a perpendicular.Interactive
column space of X (what the model can reach)target yfitted ŷ = Xŵresidual r = y − ŷ
35
1.60
1.00
0.30
Grey mesh: every prediction the model can produce. Purple: the target. Green: the least-squares fit — the foot of the perpendicular. Blue: whatever your sliders produce. The readout compares your squared error to the optimum; you cannot beat it, and the reason is that the optimal residual is perpendicular to the plane, so any move within the plane only adds length by Pythagoras.

2Derivation 1: orthogonality

This is Lecture 8's projection argument with a subspace in place of a line. The logic is identical; only the bookkeeping grows.

The normal equations from perpendicularity0 / 6
\[\hat{\vy} = \mX\hat{\vw} \in \mathcal{C}(\mX)\]
Parameterise the reachable set. Whatever the answer is, it is some combination of the columns of \(\mX\) — that is the entire content of the column picture. So we search over \(\vw\).
\[\vr = \vy - \mX\hat{\vw} \quad \perp \quad \mathcal{C}(\mX)\]
Impose orthogonality. Same intuition as before: if the residual had any component lying inside the reachable set, we could add that component to our prediction and shrink the error. At the optimum, nothing reachable is left in the error.
\[\vr \perp \mathcal{C}(\mX) \iff \vr \perp \text{every column of } \mX \iff \mX^\top\vr = \vzero\]
Turn 'perpendicular to a subspace' into an equation. Being perpendicular to a span means being perpendicular to each spanning vector (if \(\vr\cdot\va_j=0\) for every column then \(\vr\cdot\sum\alpha_j\va_j=0\) too, by linearity). And stacking those \(d\) dot products is exactly what \(\mX^\top\vr\) computes — row \(j\) of \(\mX^\top\) is column \(j\) of \(\mX\). This step is why transposes appear everywhere in statistics.
\[\mX^\top\big(\vy-\mX\hat{\vw}\big) = \vzero\]
Substitute the residual. One equation, and it contains everything.
\[\boxed{\;\mX^\top\mX\,\hat{\vw} = \mX^\top\vy\;} \qquad \textbf{(the normal equations)}\]
Expand and rearrange. \(\mX^\top\mX\) is \(d\times d\) — small, square, and symmetric. \(\mX^\top\vy\) is \(d\times 1\). We have converted \(n\) unsolvable equations into \(d\) solvable ones. Compare with Lecture 8's scalar version \(\vu\cdot\vv - k(\vu\cdot\vu)=0\): identical structure, with \(\mX^\top\mX\) playing the role of \(\vu\cdot\vu\).
\[\hat{\vw} = \big(\mX^\top\mX\big)^{-1}\mX^\top\vy \qquad \text{if } \mX^\top\mX \text{ is invertible}\]
Solve — with a condition. The inverse exists exactly when \(\rank(\mX)=d\), i.e. the columns are independent. The matrix \(\mX^{+}=(\mX^\top\mX)^{-1}\mX^\top\) is the pseudoinverse, and \(\mP = \mX\mX^{+}\) is the projection matrix onto \(\mathcal{C}(\mX)\). Never actually compute this inverse in code — see the numerical section below.

3Derivation 2: calculus

Now the same result by minimising, which is how you would get there without the geometry — and which reassures us that "closest point" and "minimum squared error" really are the same problem.

Minimising ‖Xw − y‖² directly0 / 6
\[J(\vw) = \|\mX\vw-\vy\|^2 = (\mX\vw-\vy)^\top(\mX\vw-\vy)\]
Write the norm as a dot product. \(\|\vv\|^2=\vv^\top\vv\) — Lecture 8's definition.
\[= \vw^\top\mX^\top\mX\vw - \vw^\top\mX^\top\vy - \vy^\top\mX\vw + \vy^\top\vy\]
Expand. Using \((\mA\vb)^\top = \vb^\top\mA^\top\). Four terms, as with expanding \((a-b)^2\).
\[= \vw^\top\mX^\top\mX\vw - 2\,\vw^\top\mX^\top\vy + \|\vy\|^2\]
Combine the middle terms. \(\vw^\top\mX^\top\vy\) is a \(1\times1\) matrix — a scalar — and a scalar equals its own transpose, so \(\vy^\top\mX\vw = (\vy^\top\mX\vw)^\top = \vw^\top\mX^\top\vy\). This little transpose trick recurs constantly.
\[\grad_{\vw} J = 2\,\mX^\top\mX\vw - 2\,\mX^\top\vy\]
Differentiate. Using two matrix-calculus rules we will prove in Lecture 12: \(\grad_{\vw}(\vw^\top\mA\vw)=2\mA\vw\) for symmetric \(\mA\) (and \(\mX^\top\mX\) is symmetric, since \((\mX^\top\mX)^\top = \mX^\top\mX\)), and \(\grad_{\vw}(\vw^\top\vb)=\vb\). These are the exact analogues of \(\frac{d}{dw}aw^2=2aw\) and \(\frac{d}{dw}bw=b\).
\[\grad_{\vw}J = \vzero \quad\Longrightarrow\quad \mX^\top\mX\vw = \mX^\top\vy\]
Same normal equations. The two derivations agree, exactly as they did in the one-dimensional case in Lecture 8. Geometry and calculus are describing one object.
\[\mH = \grad^2_{\vw}J = 2\,\mX^\top\mX \succeq 0\]
Confirm it is a minimum. For any \(\vv\), \(\vv^\top(\mX^\top\mX)\vv = (\mX\vv)^\top(\mX\vv) = \|\mX\vv\|^2 \ge 0\) — a sum of squares. So the Hessian is positive semi-definite, \(J\) is convex, and by Lecture 7 every critical point is a global minimum. If \(\rank(\mX)=d\) then \(\|\mX\vv\|^2>0\) for \(\vv\neq\vzero\), the Hessian is positive definite, \(J\) is strictly convex, and the minimiser is unique.
Check yourself
What goes wrong if two columns of \(\mX\) are identical?
Correct. Correct, and worth unpacking. If columns \(j\) and \(k\) are equal then \(\mX\vv=\vzero\) for \(\vv=\ve_j-\ve_k\), so the null space is nontrivial and \(\rank(\mX)<d\). Then \((\mX^\top\mX)\vv=\vzero\) too, so \(\mX^\top\mX\) is singular.

But notice what is not broken: the fitted values \(\hat{\vy}\) are still unique, because the projection onto a subspace is unique regardless of how you coordinatise that subspace. Predictions are fine; the interpretation of individual coefficients is meaningless. That distinction matters — a collinear model can predict perfectly well while its coefficients are pure noise, which is why you should never read feature importance off a regression without checking conditioning.
Not quite. They do not. The projection \(\hat{\vy}\) onto \(\mathcal{C}(\mX)\) is unique — duplicating a column does not change the column space, only the number of ways to name a point in it. The fit is exactly as good as it would have been with the duplicate removed.
Not quite. It does not handle it silently. np.linalg.inv(X.T @ X) will either raise LinAlgError or, worse, return an enormous garbage matrix built from round-off. Only routines built on the SVD — np.linalg.lstsq, pinv — degrade gracefully, and they do so by silently choosing the minimum-norm solution among the infinitely many. That is a reasonable choice, but it is a choice, and you should know it is being made for you.

4Ridge regression: making it always solvable

Add a penalty on the size of the weights:

\[J_\lambda(\vw) = \|\mX\vw-\vy\|^2 + \lambda\|\vw\|^2, \qquad \lambda>0.\]

Differentiating gives \(2\mX^\top\mX\vw - 2\mX^\top\vy + 2\lambda\vw = \vzero\), so

\[\boxed{\;\hat{\vw}_{\text{ridge}} = \big(\mX^\top\mX + \lambda\mI\big)^{-1}\mX^\top\vy\;}\]

The matrix \(\mX^\top\mX+\lambda\mI\) is invertible for every \(\lambda>0\), even when \(\mX^\top\mX\) is singular. Here is the one-line reason, which we will make fully rigorous in Lecture 11: for any \(\vv\neq\vzero\),

\[\vv^\top\big(\mX^\top\mX+\lambda\mI\big)\vv = \|\mX\vv\|^2 + \lambda\|\vv\|^2 \ \ge\ \lambda\|\vv\|^2 \ > \ 0,\]

so no nonzero vector is annihilated, so the null space is trivial, so the matrix is invertible. The \(\lambda\mI\) term puts a floor under every eigenvalue.

Ridge on near-collinear features: coefficients stabilise as λ growsInteractive
ŵ₁ŵ₂training error‖ŵ‖
0.9950
-6.00
Two features that are almost the same. With λ ≈ 0 the fitted coefficients are enormous and of opposite sign — the model is straining to cancel a near-null direction, and those numbers mean nothing. Slide λ up and they collapse to something sane, at the cost of a small rise in training error. That trade is the entire content of regularisation, and here you can watch it happen.
Where this shows up in ML

Three things this figure is showing you at once, all of which recur throughout Phase B:

5Doing it correctly in NumPy

Four ways to fit, three of which you should usePython · NumPy
Practical engineering note

Why is inv worse? Two reasons. First, forming \(\mX^\top\mX\) squares the condition number: if \(\mX\) has condition number \(10^{8}\) — quite ordinary — then \(\mX^\top\mX\) has \(10^{16}\), which exhausts float64 entirely. lstsq works on \(\mX\) directly via the SVD and never forms that product. Second, \(\mathbf{A}^{-1}\vb\) computed as "invert, then multiply" does more arithmetic and accumulates more error than solving the system directly. The rule never invert, always solve is close to universal in numerical linear algebra.

6Exercises

★

Fit \(y = w_1 x + w_0\) by least squares to the three points \((1,2),(2,2),(3,4)\), by hand.

  1. Write down \(\mX\) and \(\vy\).
  2. Compute \(\mX^\top\mX\) and \(\mX^\top\vy\).
  3. Solve the \(2\times2\) system.
  4. Verify \(\mX^\top\vr=\vzero\).

1. With the bias column first, \(\mX=\begin{bmatrix}1&1\\1&2\\1&3\end{bmatrix}\), \(\vy=\begin{bmatrix}2\\2\\4\end{bmatrix}\), \(\vw=\begin{bmatrix}w_0\\w_1\end{bmatrix}\).

2. \(\mX^\top\mX=\begin{bmatrix}3&6\\6&14\end{bmatrix}\) (entries: \(n=3\), \(\sum x=6\), \(\sum x^2=1+4+9=14\)). \(\mX^\top\vy=\begin{bmatrix}8\\ 2+4+12\end{bmatrix}=\begin{bmatrix}8\\18\end{bmatrix}\).

3. \(3w_0+6w_1=8\) and \(6w_0+14w_1=18\). Multiply the first by 2 and subtract: \(2w_1=2\), so \(w_1=1\). Then \(3w_0=8-6=2\), so \(w_0=\tfrac23\).

Cross-check with Lecture 7's formula: \(\bar x=2\), \(\bar y=\tfrac83\); \(\sum(x-\bar x)(y-\bar y) = (-1)(-\tfrac23)+(0)(-\tfrac23)+(1)(\tfrac43)=2\); \(\sum(x-\bar x)^2=2\); so \(\hat w_1=1\) ✓ and \(\hat w_0=\tfrac83-2=\tfrac23\) ✓.

4. Fitted values: \(\tfrac23+1,\ \tfrac23+2,\ \tfrac23+3 = \tfrac53,\tfrac83,\tfrac{11}3\). Residuals: \(2-\tfrac53=\tfrac13\), \(2-\tfrac83=-\tfrac23\), \(4-\tfrac{11}3=\tfrac13\).
\(\sum r_i = \tfrac13-\tfrac23+\tfrac13=0\) ✓ (orthogonality to the bias column).
\(\sum x_ir_i = \tfrac13-\tfrac43+1=0\) ✓ (orthogonality to the \(x\) column).

Note the first check is where "residuals sum to zero" comes from — it holds precisely because there is a column of ones. Drop the intercept and it stops being true.

★★

Define \(\mP=\mX(\mX^\top\mX)^{-1}\mX^\top\), the projection matrix (or "hat matrix", since \(\hat{\vy}=\mP\vy\)). Prove:

  1. \(\mP^2=\mP\) (idempotent). Interpret this geometrically.
  2. \(\mP^\top=\mP\) (symmetric).
  3. \((\mI-\mP)\mX = \mathbf{0}\).
  4. \(\tr(\mP)=d\), given \(\rank(\mX)=d\).

1. Write \(\mS=(\mX^\top\mX)^{-1}\).

\[\mP^2 = \mX\mS\mX^\top\,\mX\mS\mX^\top = \mX\mS\,\underbrace{(\mX^\top\mX)\mS}_{=\,\mI}\,\mX^\top = \mX\mS\mX^\top = \mP.\]

Geometrically: projecting a vector onto a subspace lands it in the subspace; projecting again moves nothing, because it is already there. Idempotence is "projection" in algebraic form.

2. \(\mP^\top = (\mX\mS\mX^\top)^\top = \mX\mS^\top\mX^\top\). And \(\mS^\top = ((\mX^\top\mX)^{-1})^\top = ((\mX^\top\mX)^\top)^{-1} = (\mX^\top\mX)^{-1}=\mS\), using that transpose and inverse commute and that \(\mX^\top\mX\) is symmetric. So \(\mP^\top=\mP\). ∎

(Symmetry is what makes it an orthogonal projection rather than an oblique one.)

3. \((\mI-\mP)\mX = \mX - \mX\mS\mX^\top\mX = \mX - \mX\mS(\mX^\top\mX) = \mX-\mX\mI = \mathbf{0}\). ∎
Meaning: \(\mI-\mP\) is the projection onto the orthogonal complement, and it annihilates everything in the column space. The residual is \(\vr=(\mI-\mP)\vy\), which is why \(\mX^\top\vr=\vzero\).

4. Use the cyclic property of the trace, \(\tr(\mA\mB)=\tr(\mB\mA)\):

\[\tr(\mP) = \tr\big(\mX\mS\mX^\top\big) = \tr\big(\mS\mX^\top\mX\big) = \tr\big((\mX^\top\mX)^{-1}(\mX^\top\mX)\big) = \tr(\mI_d) = d. \qquad\blacksquare\]

Why anyone cares: \(\tr(\mP)\) counts the effective degrees of freedom used by the fit. For ordinary least squares it is exactly \(d\), one per parameter. For ridge it is \(\tr(\mX(\mX^\top\mX+\lambda\mI)^{-1}\mX^\top) = \sum_j \frac{\sigma_j^2}{\sigma_j^2+\lambda} < d\) — the penalty buys you a model that behaves as if it had fewer parameters, continuously tunable by \(\lambda\). This is the precise sense in which regularisation reduces model complexity, and it is the basis of AIC, generalised cross-validation, and the whole effective-capacity literature.

★★

Show that least squares with squared error is exactly maximum likelihood under the model

\[ y_i = \vx_i^\top\vw + \epsilon_i, \qquad \epsilon_i \sim \mathcal{N}(0,\sigma^2) \text{ independent}. \]

You may use that the Gaussian density is \(p(\epsilon)=\frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\big(-\frac{\epsilon^2}{2\sigma^2}\big)\).

What does this tell you about when squared error is the wrong loss?

The likelihood of the whole dataset, using independence:

\[ L(\vw) = \prod_{i=1}^n \frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\left(-\frac{(y_i-\vx_i^\top\vw)^2}{2\sigma^2}\right).\]

Take logs — legal and argmax-preserving, by Lecture 2's theorem:

\[ \log L(\vw) = -\frac{n}{2}\log(2\pi\sigma^2) \;-\; \frac{1}{2\sigma^2}\sum_{i=1}^n (y_i-\vx_i^\top\vw)^2.\]

The first term does not involve \(\vw\). Maximising over \(\vw\) therefore means maximising \(-\frac{1}{2\sigma^2}\|\vy-\mX\vw\|^2\), i.e. minimising \(\|\vy-\mX\vw\|^2\) — the least-squares objective, with \(\frac{1}{2\sigma^2}\) an irrelevant positive constant. ∎

When squared error is wrong. The derivation used three assumptions, and squared error is the wrong loss exactly when one of them fails:

  • Gaussian noise. The exponent's \(\epsilon^2\) is where the squaring comes from. Laplace noise \(\propto e^{-|\epsilon|/b}\) gives \(\sum|r_i|\) instead — least absolute deviations, which is robust to outliers precisely because it does not square them. If your data has heavy tails, squared error is actively harmful: one outlier at distance 10 counts as much as a hundred at distance 1.
  • Constant \(\sigma^2\) (homoscedasticity). If noise varies by observation, the \(\frac{1}{2\sigma_i^2}\) does not factor out and you get weighted least squares, each residual divided by its own variance.
  • Independence. Correlated errors give \(\|\cdot\|^2\) replaced by a Mahalanobis norm \(\vr^\top\mS^{-1}\vr\) — generalised least squares.

And for classification, \(y\in\{0,1\}\) is not Gaussian at all. The corresponding derivation with a Bernoulli likelihood produces cross-entropy, exactly as in Lecture 2. Every standard loss function is a negative log-likelihood for some noise model; choosing a loss is choosing a noise model, whether or not you realise it.

★★★

Coding. Investigate the bias–variance effect of ridge empirically.

  1. Generate 200 independent datasets of \(n=30\) points from \(y=\mX\vw_{\text{true}}+\epsilon\) with \(d=20\) features and \(\sigma=1\).
  2. For each \(\lambda\) on a log grid, fit ridge on each dataset and record the estimated \(\hat{\vw}\).
  3. Compute, across the 200 fits: squared bias \(\|\E[\hat{\vw}]-\vw_{\text{true}}\|^2\), total variance \(\sum_j\Var(\hat w_j)\), and mean squared error on a held-out test set.
  4. Plot all three against \(\lambda\). Where is test error minimised, and does it coincide with minimum bias or minimum variance?
import numpy as np
rng = np.random.default_rng(0)
n, d, sigma, reps = 30, 20, 1.0, 200
w_true = rng.normal(size=d)
X  = rng.normal(size=(n, d))                  # fixed design
Xt = rng.normal(size=(2000, d))               # test design
yt = Xt @ w_true                              # noiseless test target

lams = np.logspace(-3, 3, 40)
rows = []
for lam in lams:
    W = np.empty((reps, d))
    for r in range(reps):
        y = X @ w_true + sigma*rng.normal(size=n)
        W[r] = np.linalg.solve(X.T@X + lam*np.eye(d), X.T@y)
    bias2 = np.sum((W.mean(0) - w_true)**2)
    var   = np.sum(W.var(0))
    test  = np.mean([(np.mean((Xt@w - yt)**2)) for w in W])
    rows.append((lam, bias2, var, test))
    print(f"lam={lam:9.4f}  bias^2={bias2:9.4f}  var={var:9.4f}  test MSE={test:9.4f}")

best = min(rows, key=lambda r: r[3])
print("\\nbest lambda:", best[0], " test MSE:", best[3])

What you will see. With \(n=30\) and \(d=20\), the design is badly under-determined and the unregularised estimator is terrible.

  • Squared bias starts near zero at \(\lambda\to0\) (OLS is unbiased) and rises monotonically — ridge shrinks towards \(\vzero\), and \(\vw_{\text{true}}\neq\vzero\).
  • Variance starts enormous (hundreds, driven by the small eigenvalues of \(\mX^\top\mX\)) and falls monotonically towards zero.
  • Test error is roughly bias² + variance + irreducible noise, so it is U-shaped, with a minimum somewhere in the middle — typically \(\lambda\) of order 1 to 10 for these settings.

The answer to part 4: the optimum coincides with neither. Minimum bias is at \(\lambda=0\); minimum variance is at \(\lambda=\infty\) (where the estimator is the constant \(\vzero\), with zero variance and maximal bias). The best model deliberately accepts some bias to buy a larger reduction in variance. That trade is the single most important idea in statistical learning, and it is why an unbiased estimator is not automatically a good one.

Try re-running with \(n=500\): the variance term shrinks by roughly \(n\), the U-shape flattens, and the optimal \(\lambda\) moves towards zero. More data reduces the need for regularisation — which is the same statement as "regularisation substitutes for data you do not have".


Next: we have been saying "\(\mX^\top\mX\) is positive semi-definite" and "\(\lambda\mI\) puts a floor under the eigenvalues" on credit. Lecture 11 introduces eigenvalues properly, and with them the spectral theorem, quadratic forms, condition numbers, and the multi-dimensional learning-rate bound.