← Contents Volume I · Lecture 1

Volume I · The Linear Story

1

The Line That Learns Itself

Fitting a line to data is usually handed over as a formula to memorise — invert this matrix, multiply by that vector, done. It is actually two different ideas wearing the same coat, and a third idea hiding underneath both that explains why anyone thought squared error was the right thing to minimise in the first place.

The mental model

There is an iterative way to fit a line and a closed-form way, and they agree — because both are secretly maximising the likelihood of the data under a single assumption about noise. See the assumption, and squared error stops being a convention and becomes a consequence.

The hypothesis and the cost

Start with the object being fit. Given an input x with n features, the hypothesis is a linear function of those features, weighted by a parameter vector θ: hθ(x) = θTx = Σj θjxj. The data supplying the fit is m training examples, written (x(i), y(i)) for i = 1, …, m — the superscript here is an index into the training set, not an exponent, a convention worth fixing now because it recurs for the rest of this course. x(i) is the i-th input, y(i) its target.

"Fitting" means choosing θ to make hθ(x(i)) close to y(i) across all m examples, and "close" is made precise by a cost function — half the sum of squared errors:

The cost function J(θ) = (1/2) Σi=1m ( hθ(x(i)) − y(i)

Everything in this lecture is one of two ways to make J(θ) small, plus a reason the squared term belongs there at all rather than, say, an absolute value or a fourth power.

LMS: descending the cost surface

The first way is to start θ somewhere — the zero vector will do — and repeatedly change it in the direction that decreases J(θ) fastest, which is the negative gradient. Working the derivative out for a single training example makes the update concrete. Differentiate (1/2)(hθ(x) − y)² with respect to θj: the chain rule pulls out a factor of (hθ(x) − y), and the inner derivative of θTx with respect to θj is just xj. So ∂J/∂θj = (hθ(x) − y)xj for one example, and stepping in the negative-gradient direction with a learning rate α gives the update rule:

LMS (Widrow-Hoff) update rule θj := θj + α ( y(i) − hθ(x(i)) ) xj(i)

Read the sign as a diagnosis: when the hypothesis under-predicts, y(i) − hθ(x(i)) is positive and every θj nudges up in proportion to how much xj(i) contributed; over-predict and the nudge reverses. Apply this update to one example at a time and it is stochastic gradient descent; sum the gradient over all m examples before taking a step and it is batch gradient descent. Either way, the rule is called least mean squares, or the Widrow-Hoff rule after the engineers who used it for adaptive filtering decades before it was applied to learning problems by this name.

FITTING BY GRADIENT DESCENT step 1 step 2 step 3 fitted
Figure 1a Three faint intermediate lines mark successive gradient steps, each rotating and shifting the fit to reduce J(θ); the solid line is the final position. Every one of these steps is the same local computation — how far off is the prediction at this point, and which way does nudging θ fix it.

The normal equations: skipping the walk

LMS reaches the minimum by taking many small steps. For linear regression specifically, that walk is optional: J(θ) is a convex quadratic function of θ — a bowl with a single lowest point and no other local minima to get stuck in — so the minimum can be located directly by setting its gradient to zero and solving. Collect the training inputs as rows of a design matrix X (m rows, one per example, n columns, one per feature) and the targets as a vector y. Solving ∇θJ(θ) = 0 in this matrix form yields the normal equations:

The normal equations, solved θ = (XTX)−1 XTy

One matrix inversion and two multiplications, and θ is exact — no learning rate to tune, no convergence to wait for. The reason gradient descent remains the default in practice despite this closed form existing is cost: inverting an n×n matrix takes roughly O(n³) time, which is fine at n in the dozens and prohibitive once n reaches the thousands or millions that real feature sets routinely hit. LMS trades an exact answer for one that scales.

Two roads, one destination

Because J(θ) is convex, both routes provably reach the same θ — gradient descent approaches it iteratively, the normal equations land on it in one step. Neither is more "correct"; they are a trade between exactness and cost that recurs throughout this course, most explicitly in Lecture 3's contrast between gradient ascent and Newton's method.

Why squared error at all

Both derivations above simply accepted J(θ) as the thing to minimise. The deeper question is why squared error is the "right" cost rather than an arbitrary choice, and the answer is a probabilistic argument worth deriving in full, because it is the payoff the rest of this lecture has been building towards.

Assume the target is the linear function of x plus noise: y(i) = θTx(i) + ε(i), where the error terms ε(i) are independent and identically distributed as ε(i) ~ 𝒩(0, σ²) — zero-mean Gaussian noise, of the kind that arises from unmodelled effects and measurement error. This assumption fixes the conditional distribution of y given x:

The noise assumption, restated as a distribution y(i) | x(i); θ ~ 𝒩( θTx(i), σ² )

Treating θ as fixed but unknown, the probability of the observed y(i) given x(i) and θ is the Gaussian density evaluated at y(i). Because the m training examples are independent, the likelihood of the whole dataset is their product:

The likelihood of θ L(θ) = Πi=1m p(y(i) | x(i); θ) = Πi=1m (1/√(2πσ²)) exp( − (y(i) − θTx(i))² / 2σ² )

Maximum likelihood says: choose θ to make the observed data as probable as possible under the model. Products of exponentials are unpleasant to differentiate, so take the log — a monotonic transform that preserves the location of the maximum — turning the product into a sum:

The log-likelihood ℓ(θ) = log L(θ) = m·log(1/√(2πσ²)) − (1/σ²) · (1/2) Σi=1m (y(i) − θTx(i)

Look at the second term. The first term and the 1/σ² factor do not depend on θ at all — they are constants as far as the maximisation is concerned. Maximising ℓ(θ) with respect to θ therefore reduces to minimising the one piece that does depend on θ: (1/2) Σi (y(i) − θTx(i))². That expression is J(θ), unchanged, term for term.

y | x; θ ~ 𝒩(θᵀx, σ²) x = x⁽¹⁾ x = x⁽²⁾ x = x⁽³⁾
Figure 1b A bell curve sketched at three points along the fitted line, each centred on the line itself. The model does not claim the data lies exactly on the line — it claims the true y at each x is a draw from a Gaussian whose mean is the line's value there. Squared error is what falls out of maximising the likelihood of that assumption.

That identity is the whole point: minimising J(θ) and maximising the log-likelihood under independent Gaussian noise are the same optimisation problem, algebraically, term by term. Squared error was never an arbitrary convenience — it is the exact loss that maximum likelihood hands you once you assume Gaussian noise, and no other loss is the maximum- likelihood answer under that assumption.

The throughline

This is the shape of most of what follows. Assume a probability model for how the data was generated, and maximum likelihood tells you exactly which loss to minimise — this lecture's route, and Volume II's route when the model is generative rather than discriminative. The alternative, taken in Volume III, is to state an optimisation problem directly and justify it on other grounds — a margin, a generalisation bound — without first naming a probability model at all. Two moves, and by the end of Volume III both will have been derived in full.

Read the primary source