AI / ML Theory
Chapter 1 · 24 min

1.1 — Linear regression as MLE under Gaussian noise

Goal: Show that least squares is exactly maximum likelihood under a Gaussian-noise model, and derive the normal equations as a stationarity condition.

1. Why this session exists

Linear regression is the simplest non-trivial model in this course, and the one we can take apart most rigorously. Three independent stories converge on it:

  • Optimisation. Minimise ∥y−Xw∥2\lVert y - X w \rVert^2 (least squares).
  • Probability. Maximise the likelihood of data under a Gaussian-noise observation model.
  • Geometry. Orthogonally project yy onto the column space of XX.

This session shows that the first two are the same problem, with the same minimiser given in closed form by the normal equations. The geometric picture comes for free.


2. The probabilistic model

Fix a design matrix X∈Rm×nX \in \mathbb{R}^{m \times n} (rows are data points xi⊤x_i^\top) and a target vector y∈Rmy \in \mathbb{R}^m. Assume

y=Xw∗+ε,ε∼N(0, σ2Im),y = X w^* + \varepsilon, \qquad \varepsilon \sim \mathcal{N}(0,\, \sigma^2 I_m),

with unknown true parameter w∗∈Rnw^* \in \mathbb{R}^n and known noise variance σ2>0\sigma^2 > 0. Conditional on XX and w∗w^*,

y∣X,w∗  ∼  N(Xw∗, σ2Im).y \mid X, w^* \;\sim\; \mathcal{N}(X w^*,\, \sigma^2 I_m).

The likelihood of yy under parameter ww is the Gaussian density evaluated at the observed yy:

p(y∣X,w)=1(2πσ2)m/2exp⁡ ⁣(−12σ2∥y−Xw∥2).p(y \mid X, w) = \frac{1}{(2 \pi \sigma^2)^{m/2}} \exp\!\left(-\frac{1}{2\sigma^2} \lVert y - X w \rVert^2\right).

(The covariance is σ2Im\sigma^2 I_m, which gives the diagonal Gaussian; the quadratic form in the exponent is the squared Euclidean norm.)


3. From MLE to least squares

The MLE estimator is wMLE=arg⁡max⁡wp(y∣X,w)w_{\text{MLE}} = \arg\max_w p(y \mid X, w). Logarithm is monotone, so

wMLE=arg⁡max⁡wlog⁡p(y∣X,w).w_{\text{MLE}} = \arg\max_w \log p(y \mid X, w).

Taking the log,

log⁡p(y∣X,w)=−m2log⁡(2πσ2)−12σ2∥y−Xw∥2.\log p(y \mid X, w) = -\frac{m}{2} \log(2 \pi \sigma^2) - \frac{1}{2 \sigma^2} \lVert y - X w \rVert^2.

The first term is constant in ww and drops out. The factor 1/(2σ2)1 / (2 \sigma^2) is positive, so maximising the log-likelihood is equivalent to minimising the residual sum of squares:

 wMLE  =  arg⁡min⁡w ∥y−Xw∥2. \boxed{\ w_{\text{MLE}} \;=\; \arg\min_w\, \lVert y - X w \rVert^2.\ }

Least squares is MLE under Gaussian noise. This is the prototype: every standard loss we'll meet (BCE, multi-class CE, Huber, …) is the negative log-likelihood of some observation model.


4. The normal equations

Let L(w)=12∥y−Xw∥2L(w) = \tfrac{1}{2} \lVert y - X w \rVert^2. The factor of 12\tfrac{1}{2} is purely cosmetic — it makes the gradient clean.

Expand:

L(w)=12(y−Xw)⊤(y−Xw)=12y⊤y−y⊤Xw+12w⊤X⊤Xw.L(w) = \tfrac{1}{2} (y - X w)^\top (y - X w) = \tfrac{1}{2} y^\top y - y^\top X w + \tfrac{1}{2} w^\top X^\top X w.

Apply the matrix-calculus identities from 0.2 (numerator convention):

∂∂w[y⊤Xw]=y⊤X,∂∂w[w⊤X⊤Xw]=2w⊤X⊤X.\frac{\partial}{\partial w}\bigl[y^\top X w\bigr] = y^\top X, \qquad \frac{\partial}{\partial w}\bigl[w^\top X^\top X w\bigr] = 2 w^\top X^\top X.

(The second uses ∂(w⊤Aw)/∂w=w⊤(A+A⊤)=2w⊤A\partial (w^\top A w) / \partial w = w^\top (A + A^\top) = 2 w^\top A when A=X⊤XA = X^\top X is symmetric.)

So

∂L∂w=−y⊤X+w⊤X⊤X.\frac{\partial L}{\partial w} = -y^\top X + w^\top X^\top X.

Setting this row vector to zero and transposing,

 X⊤X w  =  X⊤y. \boxed{\ X^\top X\, w \;=\; X^\top y.\ }

These are the normal equations: a linear system of nn equations in nn unknowns.

Existence and uniqueness. From 0.8 §4 we know ∇2L=X⊤X⪰0\nabla^2 L = X^\top X \succeq 0, so LL is convex; any stationary point is a global minimum. If XX has full column rank, X⊤X≻0X^\top X \succ 0 is invertible and

w∗=(X⊤X)−1X⊤y.w^* = (X^\top X)^{-1} X^\top y.

If XX is rank-deficient, the normal equations have a whole affine subspace of solutions; the minimum-norm solution is given by the Moore–Penrose pseudoinverse X+yX^+ y.


5. Geometric interpretation: projection

Let y^:=Xw∗\hat y := X w^*. The first normal equation says

X⊤(y−y^)=0,X^\top (y - \hat y) = 0,

i.e. the residual r:=y−y^r := y - \hat y is orthogonal to every column of XX. So y^\hat y is the orthogonal projection of yy onto col⁡(X)⊂Rm\operatorname{col}(X) \subset \mathbb{R}^m, with projector matrix

PX:=X(X⊤X)−1X⊤.P_X := X (X^\top X)^{-1} X^\top.

Two sanity checks: PX2=PXP_X^2 = P_X (idempotent) and PX⊤=PXP_X^\top = P_X (symmetric) — the defining properties of an orthogonal projector.


6. Quick computational note

Direct inversion of X⊤XX^\top X is numerically poor when XX is ill-conditioned (e.g. nearly-collinear features). In practice we either:

  1. Factorise X⊤X=R⊤RX^\top X = R^\top R via Cholesky and back-substitute.
  2. Compute a QR decomposition X=QRX = QR; then w∗=R−1Q⊤yw^* = R^{-1} Q^\top y — much better conditioned.
  3. Use the singular value decomposition X=UΣV⊤X = U \Sigma V^\top; then w∗=VΣ+U⊤yw^* = V \Sigma^+ U^\top y even when XX is rank-deficient.

Numerical linear algebra is its own course; we'll mention these techniques again when Hessian-based optimisers come up in Chapter 3.


7. Takeaways

  • The Gaussian-noise model turns MLE into least squares: wMLE=arg⁡min⁡∥y−Xw∥2w_{\text{MLE}} = \arg\min \lVert y - X w \rVert^2.
  • The minimiser is unique when XX has full column rank: w∗=(X⊤X)−1X⊤yw^* = (X^\top X)^{-1} X^\top y.
  • Fitted values y^=PXy\hat y = P_X y are the orthogonal projection of the targets onto the column space.
  • This is the template: each loss function in the rest of the course will be the negative log-likelihood of some observation model.

Self-test

(a) Derive the normal equations from scratch starting from L(w)=12∥y−Xw∥2L(w) = \tfrac{1}{2} \lVert y - X w \rVert^2. Show every step of the matrix differentiation.

(b) Prove that PX=X(X⊤X)−1X⊤P_X = X (X^\top X)^{-1} X^\top satisfies PX2=PXP_X^2 = P_X and PX=PX⊤P_X = P_X^\top. What is the trace of PXP_X? (Hint: it equals rank⁡(X)\operatorname{rank}(X).)

(c) Suppose XX has linearly dependent columns. Show that L(w)L(w) still has a global minimum (in fact, infinitely many), and that the residual r=y−PXyr = y - P_X y is unique even though w∗w^* is not.

(d) Bayesian twist. With a Gaussian prior w∼N(0,τ2In)w \sim \mathcal{N}(0, \tau^2 I_n) instead of plain MLE, show that the MAP estimate is the ridge regression solution wMAP=(X⊤X+λI)−1X⊤yw_{\text{MAP}} = (X^\top X + \lambda I)^{-1} X^\top y with λ=σ2/τ2\lambda = \sigma^2 / \tau^2. The L2L^2 regulariser is a Gaussian prior in disguise.


References

  1. [1]

    Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. — Linear regression (1.1), logistic regression (1.2–1.3), softmax (1.7), MAP / Gaussian priors.

    ↩