Skip to content

Why Machine Learning Needs Mathematics: Rewriting Learning as Loss Minimisation

Prerequisite:Vector Spaces and Linear Maps: From the Eight Axioms to the Rank-Nullity TheoremLimits and Continuity: Reading ε-δ as a Contract on ErrorProbability Spaces and Kolmogorov's Axioms: Probability as a Measure of Total Mass One

Raw
  • Supervised learning, once the data, the hypothesis class and the loss function have been fixed, can be rewritten as a single optimisation problem: find the parameters that minimise the empirical risk. Regression and classification both fit inside this frame.
  • Arranging the data into a design matrix XX and a target vector y\boldsymbol{y} turns the minimisation of the squared loss into the normal equation XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y}, and geometrically into the orthogonal projection of y\boldsymbol{y} onto the column space (Theorem 3.2). This is the role of linear algebra.
  • The tool that carries out the minimisation is differentiation. For a convex function, “the gradient vanishes” and “the point is a global minimiser” are equivalent (Theorem 4.2), and both the range of learning rates for which gradient descent converges and the optimal learning rate are determined solely by the eigenvalues of 1nXTX\frac{1}{n}X^{\mathsf{T}}X (Theorem 4.4).
  • Probability answers the question “why the squared loss?”. Maximum likelihood estimation under Gaussian noise coincides exactly with least squares (Theorem 5.2), while assuming a Bernoulli distribution produces the cross-entropy (Example 5.3).
  • Probability also explains why we may use the error measured on our finitely many samples in place of the error on unseen data (Proposition 5.4). This guarantee, however, breaks the moment the model is chosen after looking at the data. That is what overfitting really is.

1. Motivation: what a single line of a learning library hides

Section titled “1. Motivation: what a single line of a learning library hides”

With a machine learning library, training a model takes one line. Hand over the data, call the fitting method, and a few seconds later you can make predictions. Seen from here, mathematics appears to have no part to play.

The trouble starts when that one line does not work. For instance, the following things happen.

  1. Changing the learning rate from 0.0010.001 to 0.010.01 makes the loss diverge instead of decrease.
  2. Changing the units of a feature from centimetres to metres cuts the number of iterations needed for convergence by a factor of several dozen.
  3. The error on the training data is almost zero, yet the model is hopeless on new data.
  4. Regression uses the squared error, while classification uses a different quantity called the cross-entropy.

Trial and error against these problems has no end, but rewritten in mathematical language each of them is explained in a single short sentence. In the second half of this article we answer all four.

Looking back at the history, the problem of “fitting a model to data” is some two hundred years older than machine learning. Legendre published the method of least squares in 1805, as an appendix to a work on determining the orbits of comets. In his Theoria Motus Corporum Coelestium of 1809, Gauss showed that if observation errors are assumed to follow a normal distribution, then least squares yields the most likely estimate. The idea of deriving the loss function from a probabilistic model, still used verbatim in modern deep learning, was already present at that point.

This article has two goals. First, to formulate regression and classification, two apparently separate tasks, as one and the same optimisation problem, the minimisation of the empirical risk. Second, to see where and why linear algebra, calculus and probability each become necessary in solving that problem, by carrying a single example, linear regression, all the way through the computation.

We first fix the vocabulary. From now on vectors are written in bold, x\boldsymbol{x}, and matrices as XX; the standard inner product on Rn\mathbb{R}^n is a,b=iaibi\langle\boldsymbol{a},\boldsymbol{b}\rangle=\sum_{i}a_ib_i and the associated norm is a=a,a\|\boldsymbol{a}\|=\sqrt{\langle\boldsymbol{a},\boldsymbol{a}\rangle}.

Definition 2.1The supervised learning setup

Let X\mathcal{X} be an input space and Y\mathcal{Y} an output space, both sets, and fix a probability distribution PP on X×Y\mathcal{X}\times\mathcal{Y}. The nn samples

D=((x1,y1),,(xn,yn))(X×Y)nD = \bigl((\boldsymbol{x}_1,y_1),\ldots,(\boldsymbol{x}_n,y_n)\bigr) \in (\mathcal{X}\times\mathcal{Y})^n

drawn independently from PP, all with the same distribution, are called the training data. Fix a space Y\mathcal{Y}' of predicted values; a map f:XYf:\mathcal{X}\to\mathcal{Y}' is called a predictor, and a set H\mathcal{H} of predictors fixed in advance is called a hypothesis class. Finally, a function :Y×Y[0,)\ell:\mathcal{Y}'\times\mathcal{Y}\to[0,\infty) is called a loss function, and (y^,y)\ell(\hat{y},y) expresses the penalty for predicting y^\hat{y} when the correct answer is yy.

The case Y=R\mathcal{Y}=\mathbb{R} is called regression, and the case where Y\mathcal{Y} is a finite set is called classification.

The reason for introducing a space Y\mathcal{Y}' of predicted values separate from Y\mathcal{Y} is classification. Even in binary classification, where the labels are Y={0,1}\mathcal{Y}=\{0,1\}, what the model outputs is normally the probability that the label is 11, that is, a value in Y=[0,1]\mathcal{Y}'=[0,1]. This distinction earns its keep in Example 5.3.

Definition 2.2Expected risk and empirical risk

In the setting of Definition 2.1, for a predictor fHf\in\mathcal{H} put

R(f):=E(X,Y)P[(f(X),Y)],R^n(f):=1ni=1n(f(xi),yi).R(f) := \mathbb{E}_{(\boldsymbol{X},Y)\sim P}\bigl[\ell(f(\boldsymbol{X}),Y)\bigr], \qquad \hat{R}_n(f) := \frac{1}{n}\sum_{i=1}^{n}\ell\bigl(f(\boldsymbol{x}_i),y_i\bigr) .

These are called the expected risk (generalisation error) and the empirical risk (training error) of ff. The learning principle of selecting the predictor that minimises R^n\hat{R}_n over H\mathcal{H} is called empirical risk minimisation.

The relation between these two quantities generates very nearly all of the difficulty in machine learning. What we truly want to make small is R(f)R(f), but the distribution PP is unknown, so R(f)R(f) cannot be computed. All we can compute is R^n(f)\hat{R}_n(f), measured on the data at hand. How far this substitution is justified is treated in Proposition 5.4.

Once the hypothesis class is described by finitely many parameters, learning becomes a finite-dimensional optimisation problem. Parametrising H={fwwRd}\mathcal{H}=\{f_{\boldsymbol{w}}\mid \boldsymbol{w}\in\mathbb{R}^d\} and setting

L(w):=R^n(fw),L(\boldsymbol{w}) := \hat{R}_n(f_{\boldsymbol{w}}),

learning is nothing other than the problem “find a minimiser of the function LL on Rd\mathbb{R}^d”. The overall flow is as follows.

flowchart TD
A["1. Data: collect n pairs of input and correct answer"] --> B["2. Representation: arrange them into a design matrix X and target vector y — linear algebra"]
B --> C["3. Hypothesis class: set up predictors f_w indexed by a parameter w"]
C --> D["4. Loss: quantify the misfit as the empirical risk L(w) — probability and statistics"]
D --> E["5. Optimisation: move w in a direction that decreases L — calculus"]
E --> F["6. Evaluation: estimate the error R on unseen data — probability and statistics"]
F --> C
The basic pipeline of supervised learning, with the mathematics used at each stage

We now look at stages 2, 5 and 6 in turn. The subject matter is the simplest possible one, linear regression, and yet all three areas of mathematics show their faces there.

3. Linear algebra: arranging the data into a matrix

Section titled “3. Linear algebra: arranging the data into a matrix”

Take the inputs to be points of X=Rd\mathcal{X}=\mathbb{R}^d, that is, tuples of dd real-valued features. Stacking the nn inputs x1,,xn\boldsymbol{x}_1,\ldots,\boldsymbol{x}_n vertically gives the matrix

X=(x1TxnT)Rn×d,y=(y1yn)RnX = \begin{pmatrix} \boldsymbol{x}_1^{\mathsf{T}} \\ \vdots \\ \boldsymbol{x}_n^{\mathsf{T}} \end{pmatrix} \in \mathbb{R}^{n\times d}, \qquad \boldsymbol{y} = \begin{pmatrix} y_1 \\ \vdots \\ y_n\end{pmatrix}\in\mathbb{R}^n

called the design matrix and the target vector. Rows correspond to data points and columns to features. When an intercept (bias) is needed, taking the first feature to be the constant xi1=1x_{i1}=1 absorbs it into w\boldsymbol{w}, so below we give the intercept no special treatment.

Taking the linear model fw(x)=w,xf_{\boldsymbol{w}}(\boldsymbol{x})=\langle\boldsymbol{w},\boldsymbol{x}\rangle, all nn predictions are written together as a single matrix-vector product XwX\boldsymbol{w}. This is the first benefit of linear algebra as a language of representation. A loop over individual data points is replaced by one matrix operation, which not only shortens the description but also lets an implementation hand the work over to highly optimised matrix-product routines or to a GPU.

With the loss (y^,y)=12(y^y)2\ell(\hat{y},y)=\tfrac{1}{2}(\hat{y}-y)^2 (the factor 12\tfrac12 is merely a convenience so that the 22 disappears on differentiation), the empirical risk becomes

L(w)=1ni=1n12(w,xiyi)2=12nXwy2.L(\boldsymbol{w}) = \frac{1}{n}\sum_{i=1}^{n}\frac{1}{2}\bigl(\langle\boldsymbol{w},\boldsymbol{x}_i\rangle-y_i\bigr)^2 = \frac{1}{2n}\|X\boldsymbol{w}-\boldsymbol{y}\|^2 .

Finding a minimiser of this function is linear regression. The key is the following completely elementary identity.

Lemma 3.1Quadratic expansion of the squared loss

Let XRn×dX\in\mathbb{R}^{n\times d} and yRn\boldsymbol{y}\in\mathbb{R}^n, and put L(w)=12nXwy2L(\boldsymbol{w})=\dfrac{1}{2n}\|X\boldsymbol{w}-\boldsymbol{y}\|^2. For all w,uRd\boldsymbol{w},\boldsymbol{u}\in\mathbb{R}^d,

L(w+u)=L(w)+1nu,XT(Xwy)+12nXu2.L(\boldsymbol{w}+\boldsymbol{u}) = L(\boldsymbol{w}) + \frac{1}{n}\bigl\langle \boldsymbol{u},\, X^{\mathsf{T}}(X\boldsymbol{w}-\boldsymbol{y})\bigr\rangle + \frac{1}{2n}\|X\boldsymbol{u}\|^2 .

In particular LL is totally differentiable on Rd\mathbb{R}^d, with gradient L(w)=1nXT(Xwy)\nabla L(\boldsymbol{w}) = \dfrac{1}{n}X^{\mathsf{T}}(X\boldsymbol{w}-\boldsymbol{y}).

Proof(Lemma 3.1)

Put r:=Xwy\boldsymbol{r}:=X\boldsymbol{w}-\boldsymbol{y}, so that X(w+u)y=r+XuX(\boldsymbol{w}+\boldsymbol{u})-\boldsymbol{y}=\boldsymbol{r}+X\boldsymbol{u}. Bilinearity and symmetry of the inner product give

r+Xu2=r+Xu,r+Xu=r2+2Xu,r+Xu2.\|\boldsymbol{r}+X\boldsymbol{u}\|^2 = \langle \boldsymbol{r}+X\boldsymbol{u},\,\boldsymbol{r}+X\boldsymbol{u}\rangle = \|\boldsymbol{r}\|^2 + 2\langle X\boldsymbol{u},\boldsymbol{r}\rangle + \|X\boldsymbol{u}\|^2 .

Applying the defining property of the transpose, Xu,r=u,XTr\langle X\boldsymbol{u},\boldsymbol{r}\rangle = \langle \boldsymbol{u}, X^{\mathsf{T}}\boldsymbol{r}\rangle, to the second term and dividing throughout by 2n2n yields the stated identity.

We check differentiability. The second term is a linear form in u\boldsymbol{u}. The third term is bounded, by the Cauchy–Schwarz inequality(Theorem 4.2)[内積空間とグラム・シュミット直交化], as

Xu2=i=1nxi,u2(i=1nxi2)u2,\|X\boldsymbol{u}\|^2 = \sum_{i=1}^n \langle \boldsymbol{x}_i,\boldsymbol{u}\rangle^2 \le \Bigl(\sum_{i=1}^n\|\boldsymbol{x}_i\|^2\Bigr)\|\boldsymbol{u}\|^2 ,

and C:=ixi2C:=\sum_i\|\boldsymbol{x}_i\|^2 is a constant independent of u\boldsymbol{u}, so that 12nXu2=O(u2)=o(u)\frac{1}{2n}\|X\boldsymbol{u}\|^2 = O(\|\boldsymbol{u}\|^2) = o(\|\boldsymbol{u}\|) as u0\|\boldsymbol{u}\|\to 0. Hence, by the very definition of total differentiability, LL is differentiable and its gradient is the coefficient vector of the linear form, namely 1nXT(Xwy)\frac{1}{n}X^{\mathsf{T}}(X\boldsymbol{w}-\boldsymbol{y}).

Theorem 3.2The normal equation

Let XRn×dX\in\mathbb{R}^{n\times d} and yRn\boldsymbol{y}\in\mathbb{R}^n, and put L(w)=12nXwy2L(\boldsymbol{w})=\dfrac{1}{2n}\|X\boldsymbol{w}-\boldsymbol{y}\|^2. A point wRd\boldsymbol{w}^{\star}\in\mathbb{R}^d is a global minimiser of LL if and only if

XTXw=XTy.X^{\mathsf{T}}X\boldsymbol{w}^{\star} = X^{\mathsf{T}}\boldsymbol{y} .

This equation is called the normal equation.

Proof(Theorem 3.2)

Put g:=XTXwXTy=XT(Xwy)\boldsymbol{g}:=X^{\mathsf{T}}X\boldsymbol{w}^{\star}-X^{\mathsf{T}}\boldsymbol{y} = X^{\mathsf{T}}(X\boldsymbol{w}^{\star}-\boldsymbol{y}). Applying Lemma 3.1 with w=w\boldsymbol{w}=\boldsymbol{w}^{\star}, we obtain, for every uRd\boldsymbol{u}\in\mathbb{R}^d,

L(w+u)L(w)=1nu,g+12nXu2.L(\boldsymbol{w}^{\star}+\boldsymbol{u}) - L(\boldsymbol{w}^{\star}) = \frac{1}{n}\langle\boldsymbol{u},\boldsymbol{g}\rangle + \frac{1}{2n}\|X\boldsymbol{u}\|^2 .

(Sufficiency.) If g=0\boldsymbol{g}=\boldsymbol{0}, the first term on the right vanishes and the remaining 12nXu2\frac{1}{2n}\|X\boldsymbol{u}\|^2 is nonnegative. Hence L(w+u)L(w)L(\boldsymbol{w}^{\star}+\boldsymbol{u})\ge L(\boldsymbol{w}^{\star}) for every u\boldsymbol{u}, that is, w\boldsymbol{w}^{\star} is a global minimiser.

(Necessity.) Suppose w\boldsymbol{w}^{\star} is a global minimiser. Fix u\boldsymbol{u} arbitrarily and replace u\boldsymbol{u} by tut\boldsymbol{u} (tRt\in\mathbb{R}) in the identity above; minimality gives

0tnu,g+t22nXu20 \le \frac{t}{n}\langle\boldsymbol{u},\boldsymbol{g}\rangle + \frac{t^2}{2n}\|X\boldsymbol{u}\|^2

for every tt. Multiply both sides by nn. For t>0t > 0, dividing by tt gives u,gt2Xu2\langle\boldsymbol{u},\boldsymbol{g}\rangle \ge -\frac{t}{2}\|X\boldsymbol{u}\|^2, and letting t0+t\to 0^{+} yields u,g0\langle\boldsymbol{u},\boldsymbol{g}\rangle\ge 0. For t<0t < 0, dividing by tt reverses the inequality, giving u,gt2Xu2\langle\boldsymbol{u},\boldsymbol{g}\rangle \le -\frac{t}{2}\|X\boldsymbol{u}\|^2, and letting t0t\to 0^{-} yields u,g0\langle\boldsymbol{u},\boldsymbol{g}\rangle\le 0. Combining the two, u,g=0\langle\boldsymbol{u},\boldsymbol{g}\rangle=0. Since u\boldsymbol{u} was arbitrary, taking u=g\boldsymbol{u}=\boldsymbol{g} gives g2=0\|\boldsymbol{g}\|^2=0, that is, g=0\boldsymbol{g}=\boldsymbol{0}.

The normal equation compresses nn equations (the number of data points) into dd equations (the number of parameters). The practical content of the theorem is that however many hundreds of millions of data points there are, the size of the linear system to be solved is determined by the number of features alone. We next investigate when that system has a unique solution.

Lemma 3.3The kernel of the Gram matrix

For every XRn×dX\in\mathbb{R}^{n\times d} we have ker(XTX)=kerX\ker(X^{\mathsf{T}}X)=\ker X. Consequently rank(XTX)=rank(X)\operatorname{rank}(X^{\mathsf{T}}X)=\operatorname{rank}(X), and the d×dd\times d matrix XTXX^{\mathsf{T}}X is invertible if and only if the dd column vectors of XX are linearly independent.

Proof(Lemma 3.3)

If Xu=0X\boldsymbol{u}=\boldsymbol{0}, multiplying on the left by XTX^{\mathsf{T}} gives XTXu=0X^{\mathsf{T}}X\boldsymbol{u}=\boldsymbol{0}, so kerXker(XTX)\ker X\subseteq\ker(X^{\mathsf{T}}X). Conversely, suppose XTXu=0X^{\mathsf{T}}X\boldsymbol{u}=\boldsymbol{0}. Taking the inner product of both sides with u\boldsymbol{u},

0=u,XTXu=Xu,Xu=Xu2,0 = \langle \boldsymbol{u}, X^{\mathsf{T}}X\boldsymbol{u}\rangle = \langle X\boldsymbol{u}, X\boldsymbol{u}\rangle = \|X\boldsymbol{u}\|^2 ,

and only the zero vector has norm 00, so Xu=0X\boldsymbol{u}=\boldsymbol{0}. Hence the two kernels coincide.

As for the ranks, applying the rank–nullity theorem(Theorem 7.3)[Vector Spaces and Linear Maps] to the two linear maps on Rd\mathbb{R}^d gives

rank(XTX)=ddimker(XTX)=ddimkerX=rank(X).\operatorname{rank}(X^{\mathsf{T}}X) = d - \dim\ker(X^{\mathsf{T}}X) = d - \dim\ker X = \operatorname{rank}(X) .

Finally, invertibility of XTXX^{\mathsf{T}}X is equivalent to ker(XTX)={0}\ker(X^{\mathsf{T}}X)=\{\boldsymbol{0}\}, which is equivalent to kerX={0}\ker X=\{\boldsymbol{0}\}; and kerX={0}\ker X=\{\boldsymbol{0}\} says that Xu=0X\boldsymbol{u}=\boldsymbol{0} only for u=0\boldsymbol{u}=\boldsymbol{0}, which is exactly linear independence of the columns of XX. For the relation between linear maps and kernels see Vector spaces and linear transformations.

Corollary 3.4Closed form for the least-squares solution

If the column vectors of XRn×dX\in\mathbb{R}^{n\times d} are linearly independent (that is, rankX=d\operatorname{rank}X=d, which in particular forces ndn\ge d), then L(w)=12nXwy2L(\boldsymbol{w})=\frac{1}{2n}\|X\boldsymbol{w}-\boldsymbol{y}\|^2 has exactly one minimiser, given by

w=(XTX)1XTy.\boldsymbol{w}^{\star} = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y} .
Proof(Corollary 3.4)

By Lemma 3.3, XTXX^{\mathsf{T}}X is invertible, so the normal equation XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w}=X^{\mathsf{T}}\boldsymbol{y} has the unique solution (XTX)1XTy(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y}. By Theorem 3.2 the set of solutions of the normal equation coincides with the set of minimisers of LL, so this single point is the only minimiser.

Remark 3.5When the columns are not linearly independent

Minimisers exist even when rankX<d\operatorname{rank}X < d. Indeed, from XTXu=XT(Xu)X^{\mathsf{T}}X\boldsymbol{u}=X^{\mathsf{T}}(X\boldsymbol{u}) we get Im(XTX)Im(XT)\operatorname{Im}(X^{\mathsf{T}}X)\subseteq\operatorname{Im}(X^{\mathsf{T}}), while Lemma 3.3 gives dimIm(XTX)=rankX\dim\operatorname{Im}(X^{\mathsf{T}}X)=\operatorname{rank}X, and equality of row rank and column rank gives dimIm(XT)=rank(XT)=rankX\dim\operatorname{Im}(X^{\mathsf{T}})=\operatorname{rank}(X^{\mathsf{T}})=\operatorname{rank}X. An inclusion between subspaces of equal dimension is an equality, so Im(XTX)=Im(XT)\operatorname{Im}(X^{\mathsf{T}}X)=\operatorname{Im}(X^{\mathsf{T}}), and the right-hand side XTyX^{\mathsf{T}}\boldsymbol{y} always lies in the left-hand side. That is, the normal equation always has a solution.

The solution is not unique, however: given one solution w\boldsymbol{w}^{\star}, the set of all solutions is the affine subspace w+kerX\boldsymbol{w}^{\star}+\ker X. In practice uniqueness is imposed by ridge regularisation (minimising L(w)+λw2L(\boldsymbol{w})+\lambda\|\boldsymbol{w}\|^2) or by the Moore–Penrose pseudoinverse(Definition 8.6)[Linear Regression and Least Squares].

The condition u,XT(Xwy)=0\langle\boldsymbol{u},X^{\mathsf{T}}(X\boldsymbol{w}^{\star}-\boldsymbol{y})\rangle=0 used in the proof of Theorem 3.2 reads XTr=0X^{\mathsf{T}}\boldsymbol{r}=\boldsymbol{0}, that is, the residual vector r=Xwy\boldsymbol{r}=X\boldsymbol{w}^{\star}-\boldsymbol{y} is orthogonal to every column of XX. This is the geometric meaning of least squares. Equivalently, XwX\boldsymbol{w}^{\star} is nothing other than the orthogonal projection of y\boldsymbol{y} onto the column space ImX\operatorname{Im}X, a viewpoint treated head-on in Theorem 4.2[Linear Regression and Least Squares].

Im Xall predictions the model can reach0y (observed target)Xw* (prediction)residual r = Xw* - y
The geometry of least squares. The prediction Xw* is the orthogonal projection of y onto the column space, and the residual is orthogonal to that space

Example 3.6Fitting a line to three points, carried through to the end

Take the data (xi,yi)=(1,2),(2,3),(3,5)(x_i,y_i)=(1,2),(2,3),(3,5) and the model y=w0+w1xy=w_0+w_1x. Placing the constant feature in the first column,

X=(111213),y=(235),XTX=(36614),XTy=(1023)X=\begin{pmatrix}1&1\\1&2\\1&3\end{pmatrix},\qquad \boldsymbol{y}=\begin{pmatrix}2\\3\\5\end{pmatrix},\qquad X^{\mathsf{T}}X=\begin{pmatrix}3&6\\6&14\end{pmatrix},\qquad X^{\mathsf{T}}\boldsymbol{y}=\begin{pmatrix}10\\23\end{pmatrix}

(here 3=1+1+13=1+1+1, 6=1+2+36=1+2+3, 14=1+4+914=1+4+9, 10=2+3+510=2+3+5 and 23=12+23+3523=1\cdot 2+2\cdot 3+3\cdot 5). Since det(XTX)=31466=60\det(X^{\mathsf{T}}X)=3\cdot 14-6\cdot 6=6\ne 0, Corollary 3.4 applies. In the normal equation

{3w0+6w1=106w0+14w1=23\begin{cases} 3w_0+6w_1=10\\ 6w_0+14w_1=23\end{cases}

multiplying the first equation by 22 gives 6w0+12w1=206w_0+12w_1=20; subtracting this from the second gives 2w1=32w_1=3, that is, w1=32w_1=\frac32. Substituting back into the first gives 3w0=109=13w_0=10-9=1, so w0=13w_0=\frac13. The fitted line is y=13+32xy=\frac13+\frac32x.

As a check, look at the residuals. The predicted values are 13+32=116\frac13+\frac32=\frac{11}{6}, 13+3=103\frac13+3=\frac{10}{3} and 13+92=296\frac13+\frac92=\frac{29}{6}, so

r=Xwy=(16, 13, 16)T.\boldsymbol{r}=X\boldsymbol{w}^{\star}-\boldsymbol{y}=\Bigl(-\tfrac16,\ \tfrac13,\ -\tfrac16\Bigr)^{\mathsf{T}} .

The inner product with the first column (all 11s) is 16+1316=0-\frac16+\frac13-\frac16=0, and with the second column (1,2,3)T(1,2,3)^{\mathsf{T}} it is 16+2312=0-\frac16+\frac23-\frac12=0. The residual is indeed orthogonal to both columns. The minimum value is L(w)=16(136+19+136)=1616=136L(\boldsymbol{w}^{\star})=\frac{1}{6}\bigl(\frac1{36}+\frac19+\frac1{36}\bigr)=\frac{1}{6}\cdot\frac16=\frac{1}{36}.

Example 3.7Duplicated features destroy uniqueness of the solution

For the same data, suppose we accidentally include both "xx" and "2x2x" as features (for instance, entering a height as two columns, one in centimetres and one in metres). The design matrix is

X=(112124136),X'=\begin{pmatrix}1&1&2\\1&2&4\\1&3&6\end{pmatrix} ,

and since the third column is twice the second, the columns are linearly dependent. Indeed Xu=0X'\boldsymbol{u}=\boldsymbol{0} for u=(0,2,1)T\boldsymbol{u}=(0,2,-1)^{\mathsf{T}}, so by Lemma 3.3 the matrix XTXX'^{\mathsf{T}}X' is not invertible.

The column space of XX' is the same as that of XX (the third column, being a constant multiple of the second, adds no new direction), so neither the minimum loss value nor the prediction vector differs from Example 3.6. What changes is the number of solutions. The vector w=(13,32,0)T\boldsymbol{w}=(\frac13,\frac32,0)^{\mathsf{T}} is one minimiser, but as stated in Remark 3.5, so is every

w(t)=(13, 32+2t, t)T,tR.\boldsymbol{w}(t)=\Bigl(\tfrac13,\ \tfrac32+2t,\ -t\Bigr)^{\mathsf{T}},\qquad t\in\mathbb{R} .

Indeed the prediction made by this w(t)\boldsymbol{w}(t) is 13+(32+2t)x+(t)(2x)=13+32x\frac13+(\frac32+2t)x+(-t)(2x)=\frac13+\frac32x, independent of tt. The problem of multicollinearity, that reading meaning into the coefficient values themselves is dangerous, is in the language of linear algebra nothing more than the statement that the kernel of the design matrix is nontrivial.

4. Calculus: translating minimisation into the language of gradients

Section titled “4. Calculus: translating minimisation into the language of gradients”

Theorem 3.2 relied heavily on the special form of the squared loss. Change the loss, or make the model nonlinear, and such closed forms are essentially never available. What is needed instead is a method that approaches a minimiser using only local information, namely derivatives. We first make clear what we should be aiming at.

Definition 4.1Convex function

A function f:RdRf:\mathbb{R}^d\to\mathbb{R} is convex if for all v,wRd\boldsymbol{v},\boldsymbol{w}\in\mathbb{R}^d and all t[0,1]t\in[0,1],

f((1t)w+tv)(1t)f(w)+tf(v).f\bigl((1-t)\boldsymbol{w}+t\boldsymbol{v}\bigr) \le (1-t)f(\boldsymbol{w}) + t f(\boldsymbol{v}) .

That is, the segment joining any two points on the graph never dips below the graph.

Theorem 4.2For convex functions, stationary points and global minimisers coincide

Let f:RdRf:\mathbb{R}^d\to\mathbb{R} be totally differentiable.

  1. If ff is convex, then f(v)f(w)+f(w),vwf(\boldsymbol{v}) \ge f(\boldsymbol{w}) + \langle \nabla f(\boldsymbol{w}),\,\boldsymbol{v}-\boldsymbol{w}\rangle for all v,wRd\boldsymbol{v},\boldsymbol{w}\in\mathbb{R}^d.
  2. If ff is convex, then w\boldsymbol{w}^{\star} is a global minimiser of ff if and only if f(w)=0\nabla f(\boldsymbol{w}^{\star})=\boldsymbol{0}. Moreover, the implication ”w\boldsymbol{w}^{\star} is a global minimiser     f(w)=0\implies \nabla f(\boldsymbol{w}^{\star})=\boldsymbol{0}” holds without assuming convexity.
Proof(Theorem 4.2)

(1) Put u:=vw\boldsymbol{u}:=\boldsymbol{v}-\boldsymbol{w} and take t(0,1]t\in(0,1]. Since (1t)w+tv=w+tu(1-t)\boldsymbol{w}+t\boldsymbol{v}=\boldsymbol{w}+t\boldsymbol{u}, the inequality of Definition 4.1 reads

f(w+tu)f(w)+t(f(v)f(w)).f(\boldsymbol{w}+t\boldsymbol{u}) \le f(\boldsymbol{w}) + t\bigl(f(\boldsymbol{v})-f(\boldsymbol{w})\bigr) .

Moving f(w)f(\boldsymbol{w}) to the left and dividing by t>0t>0,

f(w+tu)f(w)tf(v)f(w).\frac{f(\boldsymbol{w}+t\boldsymbol{u})-f(\boldsymbol{w})}{t} \le f(\boldsymbol{v})-f(\boldsymbol{w}) .

By total differentiability, f(w+tu)=f(w)+tf(w),u+o(t)f(\boldsymbol{w}+t\boldsymbol{u}) = f(\boldsymbol{w}) + t\langle\nabla f(\boldsymbol{w}),\boldsymbol{u}\rangle + o(t), so the left-hand side converges to f(w),u\langle\nabla f(\boldsymbol{w}),\boldsymbol{u}\rangle as t0+t\to 0^{+}. The right-hand side is a constant independent of tt, so passing to the limit gives f(w),vwf(v)f(w)\langle\nabla f(\boldsymbol{w}),\boldsymbol{v}-\boldsymbol{w}\rangle \le f(\boldsymbol{v})-f(\boldsymbol{w}).

(2) First assume f(w)=0\nabla f(\boldsymbol{w}^{\star})=\boldsymbol{0}. Taking w=w\boldsymbol{w}=\boldsymbol{w}^{\star} in (1) gives f(v)f(w)+0f(\boldsymbol{v})\ge f(\boldsymbol{w}^{\star})+0 for every v\boldsymbol{v}, so w\boldsymbol{w}^{\star} is a global minimiser.

Conversely, assume w\boldsymbol{w}^{\star} is a global minimiser. Take uRd\boldsymbol{u}\in\mathbb{R}^d arbitrarily and consider the one-variable function g(t):=f(w+tu)g(t):=f(\boldsymbol{w}^{\star}+t\boldsymbol{u}). By the chain rule gg is differentiable with g(0)=f(w),ug'(0)=\langle\nabla f(\boldsymbol{w}^{\star}),\boldsymbol{u}\rangle. Since gg attains its minimum at t=0t=0, the necessary condition for an extremum at an interior point (Fermat's lemma(Lemma 2.5)[Mean Value Theorems and Taylor's Theorem]) gives g(0)=0g'(0)=0, that is, f(w),u=0\langle\nabla f(\boldsymbol{w}^{\star}),\boldsymbol{u}\rangle=0. Taking u=f(w)\boldsymbol{u}=\nabla f(\boldsymbol{w}^{\star}) gives f(w)2=0\|\nabla f(\boldsymbol{w}^{\star})\|^2=0, hence f(w)=0\nabla f(\boldsymbol{w}^{\star})=\boldsymbol{0}. Convexity was never used in this direction.

The use of this theorem is plain. For a nonconvex function, “the gradient vanishes” is only a necessary condition, and one may have come to rest at a saddle point or a local minimiser. Convexity removes that worry: “making the gradient vanish” becomes exactly the same thing as “minimising”. The squared loss lies on the good side of this divide.

Remark 4.3The squared loss is convex

Put h(z):=12nz2h(\boldsymbol{z}):=\frac{1}{2n}\|\boldsymbol{z}\|^2. For t[0,1]t\in[0,1] the identity

(1t)a2+tb2(1t)a+tb2=t(1t)ab20(1-t)\|\boldsymbol{a}\|^2 + t\|\boldsymbol{b}\|^2 - \|(1-t)\boldsymbol{a}+t\boldsymbol{b}\|^2 = t(1-t)\|\boldsymbol{a}-\boldsymbol{b}\|^2 \ge 0

is verified by expansion alone (expanding the left-hand side gives [(1t)(1t)2]a2+(tt2)b22t(1t)a,b\bigl[(1-t)-(1-t)^2\bigr]\|\boldsymbol{a}\|^2+(t-t^2)\|\boldsymbol{b}\|^2-2t(1-t)\langle\boldsymbol{a},\boldsymbol{b}\rangle, and (1t)(1t)2=tt2=t(1t)(1-t)-(1-t)^2=t-t^2=t(1-t)), so hh is convex in the sense of Definition 4.1. Next, putting wt:=(1t)w+tv\boldsymbol{w}_t:=(1-t)\boldsymbol{w}+t\boldsymbol{v},

Xwty=(1t)Xw+tXvy=(1t)(Xwy)+t(Xvy)X\boldsymbol{w}_t-\boldsymbol{y} = (1-t)X\boldsymbol{w}+tX\boldsymbol{v}-\boldsymbol{y} = (1-t)(X\boldsymbol{w}-\boldsymbol{y})+t(X\boldsymbol{v}-\boldsymbol{y})

(using (1t)(y)+t(y)=y(1-t)(-\boldsymbol{y})+t(-\boldsymbol{y})=-\boldsymbol{y}). That is, an affine map sends convex combinations to convex combinations. Hence L(wt)=h(Xwty)(1t)h(Xwy)+th(Xvy)=(1t)L(w)+tL(v)L(\boldsymbol{w}_t)=h(X\boldsymbol{w}_t-\boldsymbol{y})\le(1-t)h(X\boldsymbol{w}-\boldsymbol{y})+th(X\boldsymbol{v}-\boldsymbol{y})=(1-t)L(\boldsymbol{w})+tL(\boldsymbol{v}), so LL too is convex.

Therefore, by part (2) of Theorem 4.2, solving the normal equation L(w)=0\nabla L(\boldsymbol{w})=\boldsymbol{0} and minimising LL are equivalent. We proved Theorem 3.2 without using differentiation at all precisely to show that this fact is visible with algebra alone. One and the same fact has three faces: algebraic (the normal equation), geometric (orthogonal projection) and analytic (stationary point).

4.1. Why an iterative method rather than a closed form

Section titled “4.1. Why an iterative method rather than a closed form”

Given the formula w=(XTX)1XTy\boldsymbol{w}^{\star}=(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y} of Corollary 3.4, it might seem that one need only evaluate it. In practice, iterative methods are used, for two reasons.

  • Cost. Forming XTXX^{\mathsf{T}}X takes O(nd2)O(nd^2) operations and solving the system takes O(d3)O(d^3). Once the number of features dd is of the order of 10510^5 or 10610^6, this is out of reach.
  • Scope. For models such as neural networks, where fwf_{\boldsymbol{w}} is nonlinear in w\boldsymbol{w}, the equation L(w)=0\nabla L(\boldsymbol{w})=\boldsymbol{0} is a nonlinear system and has in general no closed-form solution.

So we descend a little at a time, using only the gradient at the current point. With a constant learning rate η>0\eta>0, the rule

wk+1=wkηL(wk),k=0,1,2,\boldsymbol{w}_{k+1} = \boldsymbol{w}_{k} - \eta\,\nabla L(\boldsymbol{w}_{k}),\qquad k=0,1,2,\ldots

defines gradient descent. Part (1) of Theorem 4.2 can be read as saying that the gradient is the slope of a first-order approximation supporting the function from below, so moving in the opposite direction is a natural choice. But how large a step should we take? For the squared loss the answer can be written down completely.

Theorem 4.4Convergence of gradient descent for the least-squares loss

Let the column vectors of XRn×dX\in\mathbb{R}^{n\times d} be linearly independent, and put L(w)=12nXwy2L(\boldsymbol{w})=\frac{1}{2n}\|X\boldsymbol{w}-\boldsymbol{y}\|^2 and A:=1nXTXA:=\frac{1}{n}X^{\mathsf{T}}X. Then AA is symmetric positive definite; list its eigenvalues with multiplicity as 0<λ1λ2λd0<\lambda_1\le\lambda_2\le\cdots\le\lambda_d. Let w\boldsymbol{w}^{\star} be the unique minimiser of Corollary 3.4, let η>0\eta>0 be a constant, and define the sequence by wk+1=wkηL(wk)\boldsymbol{w}_{k+1}=\boldsymbol{w}_k-\eta\nabla L(\boldsymbol{w}_k).

  1. One has wkw\boldsymbol{w}_k\to\boldsymbol{w}^{\star} for every initial point w0Rd\boldsymbol{w}_0\in\mathbb{R}^d if and only if η<2/λd\eta < 2/\lambda_d.
  2. If η<2/λd\eta < 2/\lambda_d, then setting ρ(η):=max{1ηλ1,1ηλd}\rho(\eta):=\max\{|1-\eta\lambda_1|,\,|1-\eta\lambda_d|\} we have ρ(η)<1\rho(\eta)<1, and wkwρ(η)kw0w\|\boldsymbol{w}_k-\boldsymbol{w}^{\star}\| \le \rho(\eta)^k\,\|\boldsymbol{w}_0-\boldsymbol{w}^{\star}\| for every k0k\ge 0.
  3. The learning rate minimising ρ(η)\rho(\eta) is η=2λ1+λd\eta^{\star}=\dfrac{2}{\lambda_1+\lambda_d}, and then ρ(η)=κ1κ+1\rho(\eta^{\star})=\dfrac{\kappa-1}{\kappa+1}, where κ:=λd/λ1\kappa:=\lambda_d/\lambda_1 is the condition number of AA.
Proof(Theorem 4.4)

Step 1 (AA is symmetric positive definite). From (XTX)T=XT(XT)T=XTX(X^{\mathsf{T}}X)^{\mathsf{T}}=X^{\mathsf{T}}(X^{\mathsf{T}})^{\mathsf{T}}=X^{\mathsf{T}}X, the matrix AA is symmetric. Moreover, for u0\boldsymbol{u}\ne\boldsymbol{0} we have u,Au=1nXu2\langle\boldsymbol{u},A\boldsymbol{u}\rangle=\frac{1}{n}\|X\boldsymbol{u}\|^2, and linear independence of the columns together with Lemma 3.3 gives Xu0X\boldsymbol{u}\ne\boldsymbol{0}, so this is positive. The eigenvalues of a symmetric matrix are real, and positive definiteness makes them all positive.

Step 2 (recursion for the error). By Lemma 3.1, L(w)=1nXT(Xwy)=Aw1nXTy\nabla L(\boldsymbol{w})=\frac1n X^{\mathsf{T}}(X\boldsymbol{w}-\boldsymbol{y})=A\boldsymbol{w}-\frac1n X^{\mathsf{T}}\boldsymbol{y}. Since w\boldsymbol{w}^{\star} satisfies the normal equation (Theorem 3.2), we have 1nXTy=Aw\frac1n X^{\mathsf{T}}\boldsymbol{y}=A\boldsymbol{w}^{\star}, and therefore

L(w)=A(ww).\nabla L(\boldsymbol{w}) = A(\boldsymbol{w}-\boldsymbol{w}^{\star}) .

Writing the error as ek:=wkw\boldsymbol{e}_k:=\boldsymbol{w}_k-\boldsymbol{w}^{\star},

ek+1=wkηAekw=(IηA)ek,\boldsymbol{e}_{k+1} = \boldsymbol{w}_k - \eta A\boldsymbol{e}_k - \boldsymbol{w}^{\star} = (I-\eta A)\boldsymbol{e}_k,

hence ek=(IηA)ke0\boldsymbol{e}_k=(I-\eta A)^k\boldsymbol{e}_0.

Step 3 (spectral decomposition). Since AA is real symmetric, the spectral theorem provides an orthonormal basis q1,,qd\boldsymbol{q}_1,\ldots,\boldsymbol{q}_d of Rd\mathbb{R}^d with Aqj=λjqjA\boldsymbol{q}_j=\lambda_j\boldsymbol{q}_j (Corollary 4.3[スペクトル定理]; for the full statement see The spectral theorem). Expanding e0=j=1dcjqj\boldsymbol{e}_0=\sum_{j=1}^d c_j\boldsymbol{q}_j with cj=e0,qjc_j=\langle\boldsymbol{e}_0,\boldsymbol{q}_j\rangle and using (IηA)qj=(1ηλj)qj(I-\eta A)\boldsymbol{q}_j=(1-\eta\lambda_j)\boldsymbol{q}_j repeatedly gives

ek=j=1dcj(1ηλj)kqj,ek2=j=1dcj2(1ηλj)2k.\boldsymbol{e}_k = \sum_{j=1}^{d} c_j (1-\eta\lambda_j)^k \boldsymbol{q}_j, \qquad \|\boldsymbol{e}_k\|^2 = \sum_{j=1}^{d} c_j^2 (1-\eta\lambda_j)^{2k} .

The second identity uses orthonormality of the basis.

Step 4 (claim 1). If 1ηλj<1|1-\eta\lambda_j|<1 for every jj, then each term of the finite sum above tends to 00 as kk\to\infty, so ek0\|\boldsymbol{e}_k\|\to0. For η>0\eta>0 and λj>0\lambda_j>0,

1ηλj<1    1<1ηλj<1    0<ηλj<2    η<2/λj,|1-\eta\lambda_j| < 1 \iff -1 < 1-\eta\lambda_j < 1 \iff 0 < \eta\lambda_j < 2 \iff \eta < 2/\lambda_j ,

and requiring this for every jj is equivalent to the single condition η<2/λd\eta<2/\lambda_d on the largest eigenvalue. Conversely, if η2/λd\eta\ge 2/\lambda_d then 1ηλd1|1-\eta\lambda_d|\ge 1. Taking the initial point w0=w+qd\boldsymbol{w}_0=\boldsymbol{w}^{\star}+\boldsymbol{q}_d gives cd=1c_d=1 and cj=0c_j=0 otherwise, so ek=1ηλdk1\|\boldsymbol{e}_k\|=|1-\eta\lambda_d|^k\ge 1, and there exists an initial point from which the iteration fails to converge.

Step 5 (claim 2). Since η>0\eta>0, the quantity 1ηλj1-\eta\lambda_j is decreasing in λj\lambda_j, so 1ηλd1ηλj1ηλ11-\eta\lambda_d \le 1-\eta\lambda_j\le 1-\eta\lambda_1. If a real number xx lies in a closed interval [m,M][m,M] then xMMx\le M\le|M| and xmm-x\le -m\le |m|, so xmax{m,M}|x|\le\max\{|m|,|M|\}. Hence maxj1ηλj=ρ(η)\max_j|1-\eta\lambda_j|=\rho(\eta) (the endpoints being attained at j=1j=1 and j=dj=d), and the formula of Step 3 gives

ek2ρ(η)2kjcj2=ρ(η)2ke02.\|\boldsymbol{e}_k\|^2 \le \rho(\eta)^{2k}\sum_{j}c_j^2 = \rho(\eta)^{2k}\|\boldsymbol{e}_0\|^2 .

Taking square roots gives the claim. By Step 4, ρ(η)<1\rho(\eta)<1 when η<2/λd\eta<2/\lambda_d.

Step 6 (claim 3). We examine ρ(η)=max{1ηλ1,1ηλd}\rho(\eta)=\max\{|1-\eta\lambda_1|,|1-\eta\lambda_d|\} for η>0\eta>0. From λ1λd\lambda_1\le\lambda_d we get 1/λd1/λ11/\lambda_d\le 1/\lambda_1, and there are three cases.

  • For 0<η1/λd0<\eta\le 1/\lambda_d, both 1ηλ11-\eta\lambda_1 and 1ηλd1-\eta\lambda_d are nonnegative, so ρ(η)=1ηλ1\rho(\eta)=1-\eta\lambda_1, which is strictly decreasing in η\eta.
  • For 1/λdη1/λ11/\lambda_d\le\eta\le 1/\lambda_1, we have ρ(η)=max{1ηλ1, ηλd1}\rho(\eta)=\max\{1-\eta\lambda_1,\ \eta\lambda_d-1\}. The first expression decreases and the second increases, so the maximum is smallest where the two are equal. Solving 1ηλ1=ηλd11-\eta\lambda_1=\eta\lambda_d-1 gives η=2/(λ1+λd)\eta=2/(\lambda_1+\lambda_d), and from λ1λd\lambda_1\le\lambda_d this value lies in the interval [1/λd,1/λ1][1/\lambda_d,1/\lambda_1] (indeed 2/(λ1+λd)1/λd2/(\lambda_1+\lambda_d)\ge 1/\lambda_d is equivalent to 2λdλ1+λd2\lambda_d\ge\lambda_1+\lambda_d, and 2/(λ1+λd)1/λ12/(\lambda_1+\lambda_d)\le 1/\lambda_1 is equivalent to 2λ1λ1+λd2\lambda_1\le\lambda_1+\lambda_d, both of which hold).
  • For η1/λ1\eta\ge 1/\lambda_1, we have ρ(η)=max{ηλ11, ηλd1}=ηλd1\rho(\eta)=\max\{\eta\lambda_1-1,\ \eta\lambda_d-1\}=\eta\lambda_d-1, strictly increasing.

Hence ρ\rho attains its minimum at η=2/(λ1+λd)\eta^{\star}=2/(\lambda_1+\lambda_d), with value

ρ(η)=12λ1λ1+λd=λdλ1λd+λ1=κ1κ+1\rho(\eta^{\star}) = 1-\frac{2\lambda_1}{\lambda_1+\lambda_d} = \frac{\lambda_d-\lambda_1}{\lambda_d+\lambda_1} = \frac{\kappa-1}{\kappa+1}

(the last step divides numerator and denominator by λ1\lambda_1).

This theorem answers both questions 1 and 2 raised at the outset. Raising the learning rate too far makes the iteration diverge because it crosses into η2/λd\eta\ge 2/\lambda_d, and the speed of convergence is governed by the condition number κ\kappa alone. When κ\kappa is large, ρ=(κ1)/(κ+1)\rho=(\kappa-1)/(\kappa+1) approaches 11 and the number of iterations required grows roughly in proportion to κ\kappa. Let us see this in numbers.

Example 4.5The condition number decides the number of iterations

For the data of Example 3.6, A=13XTX=(12214/3)A=\frac13X^{\mathsf{T}}X=\begin{pmatrix}1&2\\2&14/3\end{pmatrix}. The characteristic polynomial is λ2173λ+23=0\lambda^2-\frac{17}{3}\lambda+\frac23=0 (the trace is 1+143=1731+\frac{14}{3}=\frac{17}{3} and the determinant is 1434=23\frac{14}{3}-4=\frac23), so

λ=17±2656,λ10.1202,λ25.5465.\lambda = \frac{17\pm\sqrt{265}}{6},\qquad \lambda_1\approx 0.1202,\quad \lambda_2\approx 5.5465 .

By Theorem 4.4, the convergence condition is η<2/5.54650.3606\eta < 2/5.5465\approx 0.3606 and the optimal learning rate is η=2/(17/3)=6/170.3529\eta^{\star}=2/(17/3)=6/17\approx 0.3529. The condition number is κ46.1\kappa\approx 46.1, so ρ45.1/47.10.9576\rho\approx 45.1/47.1\approx 0.9576. To shrink the error by a factor of 10310^{-3},

ρk103    k3ln10lnρ6.9080.0433159.4,\rho^k\le 10^{-3} \iff k \ge \frac{3\ln 10}{-\ln\rho} \approx \frac{6.908}{0.0433} \approx 159.4 ,

so 160160 iterations are needed. Moreover the gap between the upper limit 0.36060.3606 for convergence and the optimal value 0.35290.3529 is tiny, so a slightly greedy choice makes the iteration diverge.

Now centre the input, setting x~=x2\tilde{x}=x-2 (the mean of xx is 22). The second column of the design matrix becomes (1,0,1)T(-1,0,1)^{\mathsf{T}}, whose inner product with the first column is 00, so

X~TX~=(3002),A=diag(1, 23),κ=12/3=1.5.\tilde{X}^{\mathsf{T}}\tilde{X}=\begin{pmatrix}3&0\\0&2\end{pmatrix},\qquad A=\operatorname{diag}\Bigl(1,\ \tfrac23\Bigr),\qquad \kappa=\frac{1}{2/3}=1.5 .

Now η=2/(1+23)=65=1.2\eta^{\star}=2/(1+\frac23)=\frac65=1.2 and ρ=(1.51)/(1.5+1)=0.2\rho=(1.5-1)/(1.5+1)=0.2, so the number of iterations required is k3ln10/ln54.3k\ge 3\ln 10/\ln 5\approx 4.3, that is, 55.

without centringwith centring
eigenvalues0.120, 5.5470.120,\ 5.5470.667, 1.0000.667,\ 1.000
condition number κ\kappa46.146.11.51.5
learning rates that divergeη0.361\eta\ge 0.361η2\eta\ge 2
optimal learning rate η\eta^{\star}0.3530.3531.21.2
convergence rate ρ\rho0.9580.9580.20.2
iterations to shrink the error by 10310^{-3}16016055

The fitted line is still y=13+32xy=\frac13+\frac32x; all we changed was the choice of coordinates. That alone cut the number of iterations by a factor of 3232. This is the answer to question 2.

5. Probability and statistics: where the loss comes from and what it guarantees

Section titled “5. Probability and statistics: where the loss comes from and what it guarantees”

So far the loss function has been handed down from above. Why the square? Why not the absolute value? And why the cross-entropy for classification? Probability gives a principled answer to these questions.

Definition 5.1Maximum likelihood estimation

Given a probability model p(θ)p(\cdot\mid\boldsymbol{\theta}) with parameter θ\boldsymbol{\theta} and observed data y\boldsymbol{y}, the map θp(yθ)\boldsymbol{\theta}\mapsto p(\boldsymbol{y}\mid\boldsymbol{\theta}), regarded as a function of θ\boldsymbol{\theta}, is called the likelihood function. A maximiser θ^\hat{\boldsymbol{\theta}} of the likelihood is called a maximum likelihood estimator. Since the logarithm is strictly increasing, this is equivalent to maximising the log-likelihood logp(yθ)\log p(\boldsymbol{y}\mid\boldsymbol{\theta}), or to minimising the negative log-likelihood.

Theorem 5.2Under Gaussian noise, maximum likelihood coincides with least squares

Let x1,,xnRd\boldsymbol{x}_1,\ldots,\boldsymbol{x}_n\in\mathbb{R}^d be fixed (nonrandom) inputs, let σ>0\sigma>0 be a known constant, and suppose the observations are generated by

yi=w,xi+εi(i=1,,n),y_i = \langle\boldsymbol{w},\boldsymbol{x}_i\rangle + \varepsilon_i \qquad (i=1,\ldots,n),

where ε1,,εn\varepsilon_1,\ldots,\varepsilon_n are independent with the normal distribution N(0,σ2)\mathcal{N}(0,\sigma^2). Then the set of maximum likelihood estimators of w\boldsymbol{w} coincides with the set of minimisers of the squared loss L(w)=12nXwy2L(\boldsymbol{w})=\frac{1}{2n}\|X\boldsymbol{w}-\boldsymbol{y}\|^2.

Proof(Theorem 5.2)

Since εiN(0,σ2)\varepsilon_i\sim\mathcal{N}(0,\sigma^2), for fixed w\boldsymbol{w} the variable yiy_i is normally distributed with mean w,xi\langle\boldsymbol{w},\boldsymbol{x}_i\rangle and variance σ2\sigma^2. The εi\varepsilon_i are independent, so y1,,yny_1,\ldots,y_n are independent too and the joint density is a product.

p(yw)=i=1n12πσ2exp((yiw,xi)22σ2)p(\boldsymbol{y}\mid\boldsymbol{w}) = \prod_{i=1}^{n}\frac{1}{\sqrt{2\pi\sigma^2}}\exp\left(-\frac{\bigl(y_i-\langle\boldsymbol{w},\boldsymbol{x}_i\rangle\bigr)^2}{2\sigma^2}\right)

Taking logarithms turns the product into a sum:

logp(yw)=n2log(2πσ2)12σ2i=1n(yiw,xi)2=n2log(2πσ2)12σ2Xwy2.\log p(\boldsymbol{y}\mid\boldsymbol{w}) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^{n}\bigl(y_i-\langle\boldsymbol{w},\boldsymbol{x}_i\rangle\bigr)^2 = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\|X\boldsymbol{w}-\boldsymbol{y}\|^2 .

The first term is a constant independent of w\boldsymbol{w}, and the coefficient 12σ2\frac{1}{2\sigma^2} in the second is positive. Hence the set of w\boldsymbol{w} maximising logp\log p and the set of w\boldsymbol{w} minimising Xwy2\|X\boldsymbol{w}-\boldsymbol{y}\|^2 are exactly the same. Multiplying by the positive constant 12n\frac{1}{2n} does not change the minimisers, so this set coincides with the set of minimisers of LL.

In other words, the choice to “use the squared loss” is equivalent to the assumption that the errors are normally distributed, have the same spread at every data point, and are mutually independent. Choosing a loss function is implicitly choosing a probability model. If the assumptions do not match reality, the loss should be changed; for data contaminated by outliers, for instance, it is natural to assume a heavy-tailed distribution (Exercise 7.3).

Example 5.3The cross-entropy comes from the Bernoulli distribution

Consider binary classification. The labels are yi{0,1}y_i\in\{0,1\} and the model outputs the probability that the label is 11:

pi:=σ(zi),zi:=w,xi,σ(z)=11+ezp_i := \sigma(z_i),\qquad z_i := \langle\boldsymbol{w},\boldsymbol{x}_i\rangle,\qquad \sigma(z)=\frac{1}{1+e^{-z}}

(here σ\sigma is the sigmoid function, whose range is the open interval (0,1)(0,1)). Assuming the yiy_i are independent with Bernoulli distribution Be(pi)\mathrm{Be}(p_i), and using yi{0,1}y_i\in\{0,1\}, the probability of a single point can be packed into the single expression piyi(1pi)1yip_i^{y_i}(1-p_i)^{1-y_i} (which returns pip_i when yi=1y_i=1 and 1pi1-p_i when yi=0y_i=0). Hence

1nlogp(yw)=1ni=1n[yilogpi+(1yi)log(1pi)],-\frac{1}{n}\log p(\boldsymbol{y}\mid\boldsymbol{w}) = -\frac{1}{n}\sum_{i=1}^{n}\Bigl[y_i\log p_i + (1-y_i)\log(1-p_i)\Bigr] ,

and the right-hand side is precisely the cross-entropy loss. This is the answer to question 4. The difference between the squared loss and the cross-entropy is nothing more than a difference of probability model: whether the output is viewed as a real number or as a probability.

Let us also compute the gradient. From σ(z)=σ(z)(1σ(z))\sigma'(z)=\sigma(z)(1-\sigma(z)) we get ddzlogσ(z)=1σ(z)\frac{d}{dz}\log\sigma(z)=1-\sigma(z) and ddzlog(1σ(z))=σ(z)\frac{d}{dz}\log(1-\sigma(z))=-\sigma(z), so for the loss at a single point, i=[yilogpi+(1yi)log(1pi)]\ell_i=-[y_i\log p_i+(1-y_i)\log(1-p_i)],

izi=[yi(1pi)(1yi)pi]=[yiyipipi+yipi]=piyi.\frac{\partial \ell_i}{\partial z_i} = -\bigl[y_i(1-p_i) - (1-y_i)p_i\bigr] = -\bigl[y_i - y_ip_i - p_i + y_ip_i\bigr] = p_i - y_i .

By the chain rule, ziw=xi\frac{\partial z_i}{\partial\boldsymbol{w}}=\boldsymbol{x}_i, and therefore

R^n(w)=1ni=1n(piyi)xi=1nXT(py),p=(p1,,pn)T.\nabla \hat{R}_n(\boldsymbol{w}) = \frac{1}{n}\sum_{i=1}^n (p_i-y_i)\boldsymbol{x}_i = \frac{1}{n}X^{\mathsf{T}}(\boldsymbol{p}-\boldsymbol{y}),\qquad \boldsymbol{p}=(p_1,\ldots,p_n)^{\mathsf{T}} .

This has the same shape as L(w)=1nXT(Xwy)\nabla L(\boldsymbol{w})=\frac1n X^{\mathsf{T}}(X\boldsymbol{w}-\boldsymbol{y}) from Lemma 3.1. The structure “apply the transpose of the design matrix to the residual” is common to linear regression and logistic regression. This form of the gradient is established again in Theorem 5.1[Logistic Regression]; the details are treated in Logistic regression.

5.1. Why the training error may be trusted, and why it may not

Section titled “5.1. Why the training error may be trusted, and why it may not”

The other reason probability is needed is to justify the substitution of R^n\hat{R}_n for RR in Definition 2.2.

Proposition 5.4For a fixed predictor, the empirical risk concentrates around the expected risk

In the setting of Definition 2.1, let (X1,Y1),,(Xn,Yn)(\boldsymbol{X}_1,Y_1),\ldots,(\boldsymbol{X}_n,Y_n) be drawn independently from the distribution PP, all with the same distribution. Suppose the predictor ff is fixed in advance and does not depend on the training data, and that the random variables Zi:=(f(Xi),Yi)Z_i:=\ell(f(\boldsymbol{X}_i),Y_i) have finite expectation R(f)R(f) and finite variance σ2\sigma_{\ell}^2. Then the following hold.

  1. E[R^n(f)]=R(f)\mathbb{E}\bigl[\hat{R}_n(f)\bigr]=R(f) (unbiasedness).
  2. Var[R^n(f)]=σ2/n\operatorname{Var}\bigl[\hat{R}_n(f)\bigr]=\sigma_{\ell}^2/n.
  3. For every t>0t>0, Pr(R^n(f)R(f)t)σ2nt2\Pr\bigl(|\hat{R}_n(f)-R(f)|\ge t\bigr) \le \dfrac{\sigma_{\ell}^2}{n t^2}.
Proof(Proposition 5.4)

We have R^n(f)=1ni=1nZi\hat{R}_n(f)=\frac1n\sum_{i=1}^n Z_i. The Z1,,ZnZ_1,\ldots,Z_n are independent and identically distributed with E[Zi]=E[(f(Xi),Yi)]=R(f)\mathbb{E}[Z_i]=\mathbb{E}[\ell(f(\boldsymbol{X}_i),Y_i)]=R(f) (since ff does not depend on the data, the distribution of ZiZ_i is determined by PP and ff alone).

  1. By linearity of expectation, E[R^n(f)]=1niE[Zi]=1nnR(f)=R(f)\mathbb{E}[\hat{R}_n(f)]=\frac1n\sum_i\mathbb{E}[Z_i]=\frac1n\cdot nR(f)=R(f).
  2. The variance of a sum of independent random variables is the sum of the variances, so Var[iZi]=nσ2\operatorname{Var}[\sum_i Z_i]=n\sigma_{\ell}^2; and since Var[cW]=c2Var[W]\operatorname{Var}[cW]=c^2\operatorname{Var}[W], we get Var[R^n(f)]=1n2nσ2=σ2/n\operatorname{Var}[\hat{R}_n(f)]=\frac{1}{n^2}\cdot n\sigma_{\ell}^2=\sigma_{\ell}^2/n.
  3. Applying Chebyshev's inequality(Corollary 6.2)[Random Variables and Expectation] to the random variable R^n(f)\hat{R}_n(f), with mean R(f)R(f) and variance σ2/n\sigma_{\ell}^2/n, gives the bound immediately.

For linearity of expectation, additivity of variance for independent random variables, and Chebyshev’s inequality, see Random variables and expectation and The law of large numbers and the central limit theorem.

Claim 3 says that increasing nn brings the training error close to the generalisation error in the sense of probability. Since nn appears in the denominator, it also yields a quantitative guideline: to halve the accuracy tt, take four times as many samples. This is the most naive mathematical backing for the rule of thumb that more data is better.

Remark 5.5What breaks when the assumption that f is independent of the data is dropped

Given nn distinct points x1,,xnRx_1,\ldots,x_n\in\mathbb{R}, there always exists a polynomial of degree at most n1n-1 passing through all nn points (Lagrange interpolation; the coefficient matrix of the linear system determining the coefficients is a Vandermonde matrix, whose determinant is i<j(xjxi)0\prod_{i<j}(x_j-x_i)\ne0 when the points are distinct, so the matrix is invertible). Choosing this polynomial makes the empirical risk exactly 00.

The expected risk of this predictor, however, is in general enormous, and it oscillates wildly between the training points. This looks like a contradiction with Proposition 5.4, but it is not. The polynomial was determined after looking at the training data, so the hypothesis of the proposition, that ff does not depend on the data, fails. When ff is chosen as a function of the data, neither the independence of the ZiZ_i nor the identity E[Zi]=R(f)\mathbb{E}[Z_i]=R(f) holds.

This is what overfitting really is, and it is the answer to question 3. As long as we select from within a hypothesis class H\mathcal{H}, what we actually need is not a bound for each individual ff but a uniform bound on supfHR^n(f)R(f)\sup_{f\in\mathcal{H}}|\hat{R}_n(f)-R(f)|. Controlling this quantity by the “size” of the hypothesis class is the subject of statistical learning theory (VC dimension, Rademacher complexity, and so on).

6. The division of labour among the three areas, and what comes next

Section titled “6. The division of labour among the three areas, and what comes next”

All four questions raised at the outset now have answers.

  1. Raising the learning rate makes the iteration diverge. Because at η2/λd\eta\ge 2/\lambda_d the error along the direction of the largest eigenvalue is amplified (Theorem 4.4).
  2. Changing the units speeds up convergence. Because the condition number κ\kappa changes and the convergence rate (κ1)/(κ+1)(\kappa-1)/(\kappa+1) improves (Example 4.5).
  3. The training error is 00 yet the model misses. Because unbiasedness of the empirical risk is lost the moment the model is chosen after looking at the data (Remark 5.5).
  4. Regression and classification use different losses. Because the probability model placed on the output is normal in one case and Bernoulli in the other (Theorem 5.2, Example 5.3).

The division of labour among the three areas of mathematics can be summarised as follows.

Stage of learningMathematics chiefly usedConcrete instance seen hereRead on
Representing data and modelslinear algebra (matrices, subspaces, orthogonal projection)design matrix, normal equation, orthogonality of the residualLinear regression and least squares
Compressing and diagnosing representationslinear algebra (eigenvalues, symmetric matrices)the condition number κ\kappa decides the iteration countPrincipal component analysis
Minimising the losscalculus (gradients, convexity, limits)the bound 2/λd2/\lambda_d on the learning rate and its optimal valueGradient descent
Computing gradients in deep modelscalculus (chain rule)the derivative pyp-y through the sigmoidNeural networks and backpropagation
Designing the lossprobability and statistics (likelihood)squared loss from Gaussian noise, cross-entropy from BernoulliLogistic regression
Assessing uncertaintyprobability and statistics (expectation, law of large numbers)unbiasedness of the empirical risk and its variance σ2/n\sigma_{\ell}^2/nProbability and Bayesian statistics

The prerequisites are, for linear algebra, Vector spaces and linear transformations, Inner product spaces and Gram–Schmidt orthogonalisation and Eigenvalues and eigenvectors; for calculus, Differentiation of multivariable functions and partial derivatives and The mean value theorem and Taylor’s theorem; and for probability, Probability spaces and Kolmogorov’s axioms and Random variables and expectation. No deep excursion is required. All this article actually used was the relation between the inner product and the transpose, the rank–nullity theorem, the spectral theorem for symmetric matrices, the definition of total differentiability, linearity of expectation, and Chebyshev’s inequality.

Exercise 7.1Easy

For the data (xi,yi)=(0,1),(1,1),(2,4),(3,4)(x_i,y_i)=(0,1),(1,1),(2,4),(3,4), find the least-squares solution for the model y=w0+w1xy=w_0+w_1x from the normal equation. Then verify that the resulting residual vector is orthogonal to both columns of the design matrix.

Solution

The design matrix and target vector are

X=(10111213),y=(1144).X=\begin{pmatrix}1&0\\1&1\\1&2\\1&3\end{pmatrix},\qquad \boldsymbol{y}=\begin{pmatrix}1\\1\\4\\4\end{pmatrix} .

Then XTX=(46614)X^{\mathsf{T}}X=\begin{pmatrix}4&6\\6&14\end{pmatrix} (the 44 is the number of rows, 6=0+1+2+36=0+1+2+3, 14=0+1+4+914=0+1+4+9) and XTy=(1021)X^{\mathsf{T}}\boldsymbol{y}=\begin{pmatrix}10\\21\end{pmatrix} (10=1+1+4+410=1+1+4+4, 21=01+11+24+3421=0\cdot1+1\cdot1+2\cdot4+3\cdot4). Since det(XTX)=5636=200\det(X^{\mathsf{T}}X)=56-36=20\ne0, the solution is unique by Corollary 3.4. The normal equation is

{4w0+6w1=106w0+14w1=21\begin{cases}4w_0+6w_1=10\\ 6w_0+14w_1=21\end{cases}

Multiplying the first equation by 32\frac32 gives 6w0+9w1=156w_0+9w_1=15; subtracting from the second gives 5w1=65w_1=6, so w1=65w_1=\frac65. The first equation then gives 4w0=10665=10365=1454w_0=10-6\cdot\frac65=10-\frac{36}{5}=\frac{14}{5}, so w0=710w_0=\frac{7}{10}. The line is y=0.7+1.2xy=0.7+1.2x.

The predicted values are 0.7, 1.9, 3.1, 4.30.7,\ 1.9,\ 3.1,\ 4.3, so the residual is r=Xwy=(0.3, 0.9, 0.9, 0.3)T\boldsymbol{r}=X\boldsymbol{w}^{\star}-\boldsymbol{y}=(-0.3,\ 0.9,\ -0.9,\ 0.3)^{\mathsf{T}}. Its inner product with the first column (1,1,1,1)T(1,1,1,1)^{\mathsf{T}} is 0.3+0.90.9+0.3=0-0.3+0.9-0.9+0.3=0, and with the second column (0,1,2,3)T(0,1,2,3)^{\mathsf{T}} it is 0+0.91.8+0.9=00+0.9-1.8+0.9=0. This is nothing but the componentwise form of XT(Xwy)=0X^{\mathsf{T}}(X\boldsymbol{w}^{\star}-\boldsymbol{y})=\boldsymbol{0} from Theorem 3.2.

Exercise 7.2Standard

Let L(w)=12(w12+100w22)L(\boldsymbol{w})=\frac12\bigl(w_1^2+100w_2^2\bigr). This is L(w)=12w,AwL(\boldsymbol{w})=\frac12\langle\boldsymbol{w},A\boldsymbol{w}\rangle for A=diag(1,100)A=\operatorname{diag}(1,100), and the minimiser is the origin.

  1. Find the range of learning rates η\eta for which gradient descent converges from every initial point.
  2. Find the η\eta giving fastest convergence, and the corresponding convergence rate ρ\rho.
  3. Using the η\eta from part 2, find a number of iterations sufficient to guarantee wk103w0\|\boldsymbol{w}_k\|\le 10^{-3}\|\boldsymbol{w}_0\|.
Solution

We have L(w)=(w1,100w2)T=Aw\nabla L(\boldsymbol{w})=(w_1,100w_2)^{\mathsf{T}}=A\boldsymbol{w}, and since AA is diagonal its eigenvalues are simply λ1=1\lambda_1=1 and λ2=100\lambda_2=100, with the standard basis as eigenvectors. Theorem 4.4 applies directly (with w=0\boldsymbol{w}^{\star}=\boldsymbol{0}).

  1. By claim 1, 0<η<2/λ2=2/100=0.020<\eta<2/\lambda_2=2/100=0.02.
  2. By claim 3, η=21+100=21010.0198\eta^{\star}=\dfrac{2}{1+100}=\dfrac{2}{101}\approx 0.0198, and since κ=100\kappa=100 we get ρ=1001100+1=991010.9802\rho=\dfrac{100-1}{100+1}=\dfrac{99}{101}\approx 0.9802.
  3. By the bound in claim 2 it suffices that ρk103\rho^k\le 10^{-3}. Taking logarithms gives klnρ3ln10k\ln\rho\le -3\ln 10, and since lnρ<0\ln\rho<0,
k3ln10ln(101/99)6.90780.0200345.4,k \ge \frac{3\ln 10}{\ln(101/99)} \approx \frac{6.9078}{0.0200} \approx 345.4 ,

so 346346 iterations suffice. A condition number of 100100, which is hardly extreme, already forces several hundred iterations. The value of putting features on a common scale shows itself here.

Exercise 7.3Standard

In the same setting as Theorem 5.2, suppose the noise εi\varepsilon_i is independent with the Laplace density p(ε)=12bexp(ε/b)p(\varepsilon)=\dfrac{1}{2b}\exp\bigl(-|\varepsilon|/b\bigr), where b>0b>0 is known.

  1. Derive which function maximum likelihood estimation of w\boldsymbol{w} amounts to minimising.
  2. Explain, in terms of how the loss grows with the residual, why the resulting loss is less sensitive to outliers than the squared loss.
Solution
  1. By independence the joint density is a product:
p(yw)=i=1n12bexp(yiw,xib).p(\boldsymbol{y}\mid\boldsymbol{w}) = \prod_{i=1}^{n}\frac{1}{2b}\exp\left(-\frac{\bigl|y_i-\langle\boldsymbol{w},\boldsymbol{x}_i\rangle\bigr|}{b}\right) .

Taking the negative logarithm,

logp(yw)=nlog(2b)+1bi=1nyiw,xi.-\log p(\boldsymbol{y}\mid\boldsymbol{w}) = n\log(2b) + \frac{1}{b}\sum_{i=1}^{n}\bigl|y_i-\langle\boldsymbol{w},\boldsymbol{x}_i\rangle\bigr| .

The first term is a constant independent of w\boldsymbol{w} and the coefficient 1/b1/b of the second is positive, so by Definition 5.1 maximum likelihood is equivalent to minimising iyiw,xi\sum_i|y_i-\langle\boldsymbol{w},\boldsymbol{x}_i\rangle|, that is, to the absolute-value loss (least absolute deviations regression).

  1. For the residual ri=w,xiyir_i=\langle\boldsymbol{w},\boldsymbol{x}_i\rangle-y_i, the contribution of the squared loss is 12ri2\frac12 r_i^2, its derivative with respect to the residual is rir_i, and its contribution to the gradient is rixir_i\boldsymbol{x}_i. Moving one observation far away makes the contribution grow without bound in proportion to ri|r_i|, so that single point drags the solution around. The contribution of the absolute-value loss, by contrast, is ri|r_i|, its derivative at ri0r_i\ne0 is sign(ri)=±1\operatorname{sign}(r_i)=\pm1, and its contribution to the gradient is ±xi\pm\boldsymbol{x}_i, which is bounded. However far out a point lies, the size of its influence is capped.

In the language of probability models: the normal density decays rapidly as er2/2σ2e^{-r^2/2\sigma^2}, so it judges large residuals to be all but impossible and works hard to exclude them, whereas the Laplace distribution has heavy tails, er/be^{-|r|/b}, and regards large residuals as reasonably possible. Note, though, that the absolute-value loss is not differentiable at ri=0r_i=0, so optimisation requires subgradient methods or a reformulation as a linear program.

  • M. P. Deisenroth, A. A. Faisal, C. S. Ong, Mathematics for Machine Learning, Cambridge University Press, 2020 — Chapter 2 (linear algebra), Chapter 5 (vector calculus), Chapter 9 (linear regression). A full version released by the authors is available at mml-book.github.io.
  • C. M. Bishop, Pattern Recognition and Machine Learning, Springer, 2006 — Chapter 1 (decision theory and loss functions), Chapter 3 (linear regression and maximum likelihood), Chapter 4 (classification and logistic regression).
  • I. Goodfellow, Y. Bengio, A. Courville, Deep Learning, MIT Press, 2016 — Part I (Chapter 2 linear algebra, Chapter 3 probability and information theory, Chapter 4 numerical computation, Chapter 5 machine learning basics). Available at deeplearningbook.org.
  • S. Boyd, L. Vandenberghe, Convex Optimization, Cambridge University Press, 2004 — Chapter 3 (convex functions and the first-order condition), Chapter 9 (unconstrained minimisation and the convergence analysis of gradient descent). Available at web.stanford.edu/~boyd/cvxbook/.
  • S. Shalev-Shwartz, S. Ben-David, Understanding Machine Learning: From Theory to Algorithms, Cambridge University Press, 2014 — Chapter 2 (empirical risk minimisation and overfitting), Chapter 4 (uniform convergence).
  • S. M. Stigler, The History of Statistics: The Measurement of Uncertainty before 1900, Harvard University Press, 1986 — Part I treats the story of Legendre and Gauss around the method of least squares.

Report an error in this article ・Operated by: Mugen Giken LLCPricingTermsLegal notice

© 2026 夢現技研合同会社 ・Feeding the text to an LLM is welcome. Code samples are MIT licensed.