Lesson 3.13 · Stage 03: The maths behind machine learning

Maths chapter 11: Putting it together: linear regression

One small problem uses all four topics. Three students studied 1, 2 and 3 hours and scored 2, 3 and 5 marks. We want a line ŷ = wx + b that predicts marks from hours.

Published 27 November 2026

One small problem uses all four topics. Three students studied 1, 2 and 3 hours and scored 2, 3 and 5 marks. We want a line ŷ = wx + b that predicts marks from hours.

Student Hours x Marks y
A 1 2
B 2 3
C 3 5

11.1 The error to minimise (Chapters 1 and 6)

We measure how bad a line is with the mean squared error (MSE): square each miss, then average.

L(w,b)=1n∑i=1n(wxi+b−yi)2L(w, b) = \frac{1}{n} \sum_{i=1}^{n} \big( w x_i + b - y_i \big)^2

Squaring makes every miss positive and punishes big misses more. L is a bowl in w and b, so it is convex (Chapter 10): there is exactly one best line.

11.2 Solving it with matrices (Chapters 2–3)

Stack the data into a matrix X (a column of 1s for b, then the hours) and a vector y:

X=[111213],y=[235],XTX[bw]=XTyX = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}, \quad \mathbf{y} = \begin{bmatrix} 2 \\ 3 \\ 5 \end{bmatrix}, \quad X^{\mathsf{T}} X \begin{bmatrix} b \\ w \end{bmatrix} = X^{\mathsf{T}} \mathbf{y}

This last equation is called the normal equation.

  1. XᵀX = [[3, 6], [6, 14]] and Xᵀy = (10, 23).
  2. Determinant = 3 × 14 − 6 × 6 = 6. Inverse = (1/6) × [[14, −6], [−6, 3]].
  3. (b, w) = (1/6) × (14 × 10 − 6 × 23, −6 × 10 + 3 × 23) = (1/6) × (2, 9) = (0.33, 1.5).

Best line: ŷ = 1.5x + 0.33. Each extra hour of study adds about 1.5 marks.

11.3 The geometry (Chapter 4)

Predictions are 1.83, 3.33 and 4.83. Misses are 0.17, −0.33 and 0.17. MSE = (0.028 + 0.111 + 0.028) ÷ 3 ≈ 0.056.

The prediction vector is the projection (shadow) of y onto the space that X can reach. The miss vector is orthogonal to that space. That is exactly why the normal equation works.

11.4 Solving it with gradient descent (Chapters 7 and 10)

For big data, inverting XᵀX is too slow, so we walk downhill instead. The gradient of L is:

∂L∂w=2n∑(y^i−yi) xi,∂L∂b=2n∑(y^i−yi)\frac{\partial L}{\partial w} = \frac{2}{n} \sum (\hat{y}_i - y_i)\, x_i, \qquad \frac{\partial L}{\partial b} = \frac{2}{n} \sum (\hat{y}_i - y_i)

One step by hand. Start at w = 0, b = 0, so every prediction is 0 and the misses are −2, −3, −5.

  • ∂L/∂w = (2/3) × (−2 × 1 − 3 × 2 − 5 × 3) = (2/3) × (−23) ≈ −15.3
  • ∂L/∂b = (2/3) × (−10) ≈ −6.7
  • With η = 0.05: w = 0 + 0.05 × 15.3 ≈ 0.77 and b = 0 + 0.05 × 6.7 ≈ 0.33.

Repeat a few hundred times and you arrive at w = 1.5, b = 0.33, the same answer as the matrix method.

11.5 The probability view (Chapters 8–9)

Assume each real mark = the line’s prediction + a random error from a normal distribution with mean 0. Ask: which line makes the data we saw most probable? This is called maximum likelihood. Working it out gives exactly the MSE above. So “least squares” is not an arbitrary choice; it follows from assuming bell-curve errors.

11.6 Where each topic showed up

Topic Its job in linear regression
Vectors and matrices store the data; the normal equation
Analytic geometry prediction = projection; errors are orthogonal
Calculus gradient of the loss
Probability normal errors justify the squared loss
Optimization convex loss; gradient descent finds the minimum

This is Chapter 9 of Mathematics for Machine Learning. After this book, you are ready to read it.