Skip to content

Linear Regression and Least Squares: Reading the Normal Equations as an Orthogonal Projection

Prerequisite:Why Machine Learning Needs Mathematics: Rewriting Learning as Loss Minimisation

Raw
  • Training a linear regression model means searching for a w\boldsymbol{w} that satisfies yXw\boldsymbol{y} \approx X\boldsymbol{w} as well as possible for an observed vector y\boldsymbol{y}. The intercept bb can be absorbed into the weights by appending a column of all ones to the design matrix XX.
  • Minimising the sum of squared errors L(w)=yXw2L(\boldsymbol{w}) = \lVert \boldsymbol{y} - X\boldsymbol{w}\rVert^2 is exactly equivalent to solving the normal equations XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y}, and no differentiation is needed to see this (Theorem 3.3). That learning collapses into a system of linear equations is the whole point of the model.
  • The prediction Xw^X\hat{\boldsymbol{w}} produced by a solution w^\hat{\boldsymbol{w}} of the normal equations is nothing other than the orthogonal projection of y\boldsymbol{y} onto the column space ImX\operatorname{Im} X (Theorem 4.2). The residual is orthogonal to every explanatory variable.
  • The normal equations always have a solution, whatever XX and y\boldsymbol{y} may be. The solution is unique precisely when rankX=p\operatorname{rank} X = p (the columns are linearly independent), and then w^=(XTX)1XTy\hat{\boldsymbol{w}} = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y}. Even when the rank drops, the prediction Xw^X\hat{\boldsymbol{w}} remains uniquely determined.
  • In an implementation, never form (XTX)1(X^{\mathsf{T}}X)^{-1} explicitly. Since κ2(XTX)=κ2(X)2\kappa_2(X^{\mathsf{T}}X) = \kappa_2(X)^2 squares the condition number, going through a QR decomposition or a singular value decomposition is safer.

1.1. The problem of having too much information

Section titled “1.1. The problem of having too much information”

As one learns in school, there is exactly one line through two distinct points in the plane. So which line “passes through” 100 points contaminated by measurement error? The answer is: none. There are only two unknowns, the slope and the intercept, but there are 100 equations. Such an overdetermined system — a system of linear equations with more equations than unknowns — has no solution in the ordinary sense.

This situation was entirely routine in the astronomy and geodesy of the early nineteenth century. An orbit is described by six elements, yet observations are made dozens of times. Practitioners of the day resorted to ad hoc procedures: selecting a few observations and writing down equations, or grouping observations and averaging them. What was wanted was a procedure that uses all the observations impartially and still returns a unique answer.

That procedure is the method of least squares. On 1 January 1801 Giuseppe Piazzi discovered the asteroid Ceres, but after 41 days of observation it moved towards the Sun and was lost. Gauss, then 24, computed an orbit from the scanty data and predicted where it would reappear; at the end of the same year astronomers recovered Ceres exactly where he had said it would be. The episode made the power of least squares widely known. The method itself was published in 1805 by Legendre, as “méthode des moindres carrés” in an appendix to his Nouvelles méthodes pour la détermination des orbites des comètes, whereupon Gauss claimed in his Theoria Motus of 1809 that he had been using it since 1795 — the origin of a famous priority dispute.

The sum of squares is not the only way to measure error. The sum of absolute values iyiy^i\sum_i |y_i - \hat{y}_i| or the maximum error maxiyiy^i\max_i |y_i - \hat{y}_i| would do just as well. There are nevertheless clear reasons why the sum of squares became standard.

  1. It is differentiable. The absolute value is not differentiable at the origin, whereas the square is smooth everywhere and, moreover, a quadratic in w\boldsymbol{w}. The condition for a stationary point of a quadratic is a linear equation.
  2. It connects directly with geometry. The sum of squares is the square of the Euclidean norm, that is, the distance coming from an inner product. Hence “minimise the error” means the same as “drop a perpendicular onto a subspace”, and the Pythagorean theorem applies verbatim (Theorem 4.2).
  3. It has a probabilistic meaning. If the observation noise is assumed to be independent and identically normally distributed, maximising the log-likelihood coincides exactly with minimising the sum of squared errors. This was Gauss’s own justification; it is treated in The Role of Probability Theory and Bayesian Statistics at maximum likelihood under Gaussian noise is least squares(Proposition 3.3)[確率論とベイズ統計].

There is a weakness on the other side. Squaring emphasises large errors still further, so a single outlier can move the solution a long way. Remedies for this weakness (robust regression, regularisation) are touched on in the later sections and in the exercises.

As we saw in Why Does Machine Learning Need Mathematics?, machine learning algorithms become suddenly transparent once written in the language of mathematics. Among supervised learning models, linear regression is essentially the only one whose learning problem admits a closed-form solution. For logistic regression or neural networks the loss ceases to be a quadratic in w\boldsymbol{w}, the closed form disappears, and one falls back on iterative methods such as Gradient Descent. It is precisely because a closed form exists here that it is worth understanding completely what is going on. The triple that appears here — orthogonal projection, rank condition, condition number — carries over unchanged to Principal Component Analysis and to the iteratively reweighted least squares of Logistic Regression.

2. Preliminaries: arranging the data in a matrix

Section titled “2. Preliminaries: arranging the data in a matrix”

The data consist of nn observations (xi,yi)(\boldsymbol{x}_i, y_i) for i=1,,ni = 1, \dots, n. Here xiRd\boldsymbol{x}_i \in \mathbb{R}^d is the vector of explanatory variables (features) and yiRy_i \in \mathbb{R} is the response. The relationship we wish to predict has the form

y^=w1x1+w2x2++wdxd+b\hat{y} = w_1 x_1 + w_2 x_2 + \cdots + w_d x_d + b

where bb is the intercept (bias term). To handle these d+1d+1 unknowns together we set up the following notation.

Definition 2.1Design matrix and the linear regression model

Given nn observations (xi,yi)(\boldsymbol{x}_i, y_i) with xi=(xi1,,xid)TRd\boldsymbol{x}_i = (x_{i1}, \dots, x_{id})^{\mathsf{T}} \in \mathbb{R}^d and yiRy_i \in \mathbb{R}, put p=d+1p = d + 1 and set

X=(1x11x1d1x21x2d1xn1xnd)Rn×p,y=(y1y2yn)Rn.X = \begin{pmatrix} 1 & x_{11} & \cdots & x_{1d} \\ 1 & x_{21} & \cdots & x_{2d} \\ \vdots & \vdots & & \vdots \\ 1 & x_{n1} & \cdots & x_{nd} \end{pmatrix} \in \mathbb{R}^{n \times p}, \qquad \boldsymbol{y} = \begin{pmatrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{pmatrix} \in \mathbb{R}^{n}.

The matrix XX is called the design matrix. Its first column is the vector 1\boldsymbol{1} all of whose entries are 11. Writing the parameters as w=(b,w1,,wd)TRp\boldsymbol{w} = (b, w_1, \dots, w_d)^{\mathsf{T}} \in \mathbb{R}^{p}, the nn predicted values are collected into XwRnX\boldsymbol{w} \in \mathbb{R}^n. This is the linear regression model.

By placing 1\boldsymbol{1} in the first column, the intercept becomes “the weight attached to the constant feature 11”. From now on the intercept needs no special treatment. The ii-th row of XX is the ii-th data point, and the jj-th column is the jj-th feature recorded across all data. We shall use the correspondence rows are data, columns are features throughout.

Let us make the meaning of “linear” precise. The “linear” in linear regression means linear in the parameters w\boldsymbol{w}, not in the explanatory variables. This distinction widens the range of applications enormously.

Definition 2.2Extension by a feature map

Fix an arbitrary map φ ⁣:RdRp\varphi \colon \mathbb{R}^d \to \mathbb{R}^{p} and let ΦRn×p\Phi \in \mathbb{R}^{n \times p} be the matrix obtained by replacing the ii-th row of the design matrix with φ(xi)T\varphi(\boldsymbol{x}_i)^{\mathsf{T}}. The model y^=wTφ(x)\hat{y} = \boldsymbol{w}^{\mathsf{T}}\varphi(\boldsymbol{x}) is called the linear model with feature map φ\varphi. However nonlinear φ\varphi may be, the model is linear in w\boldsymbol{w}.

For instance, taking d=1d = 1 and φ(x)=(1,x,x2,x3)T\varphi(x) = (1, x, x^2, x^3)^{\mathsf{T}} gives a cubic polynomial fit, yet the model is still linear in the unknown w\boldsymbol{w}. Everything we build below therefore applies directly to polynomial regression, trigonometric expansions and radial basis function models. A concrete numerical instance appears in Example 6.2.

3. The least squares problem and the normal equations

Section titled “3. The least squares problem and the normal equations”

When n>pn > p, there is in general no w\boldsymbol{w} satisfying Xw=yX\boldsymbol{w} = \boldsymbol{y} exactly. We therefore give up on exactness and choose the w\boldsymbol{w} that makes the discrepancy as small as possible.

Definition 3.1The least squares problem

For XRn×pX \in \mathbb{R}^{n \times p} and yRn\boldsymbol{y} \in \mathbb{R}^n, define the residual vector r(w)=yXw\boldsymbol{r}(\boldsymbol{w}) = \boldsymbol{y} - X\boldsymbol{w} and the sum of squared errors (residual sum of squares)

L(w)=yXw2=i=1n(yi(Xw)i)2.L(\boldsymbol{w}) = \lVert \boldsymbol{y} - X\boldsymbol{w}\rVert^2 = \sum_{i=1}^{n} \bigl(y_i - (X\boldsymbol{w})_i\bigr)^2 .

A vector w^\hat{\boldsymbol{w}} minimising LL over all of Rp\mathbb{R}^p, that is, one satisfying

w^arg minwRpyXw2,\hat{\boldsymbol{w}} \in \operatorname*{arg\,min}_{\boldsymbol{w} \in \mathbb{R}^{p}} \lVert \boldsymbol{y} - X\boldsymbol{w}\rVert^2 ,

is called a least squares solution.

LL is a quadratic in each component of w\boldsymbol{w} and is bounded below (always L0L \ge 0). As we shall see, the minimum is always attained. The minimiser need not be unique, however. Capturing this “exists but need not be unique” structure precisely is the goal of this section and the next.

We first prove, independently, the differentiation formula we shall need. The partial derivatives here are those of multivariable calculus; if necessary, review the definition of a partial derivative(Definition 3.1)[多変数関数の微分と偏微分] in Differentiation of Multivariable Functions and Partial Derivatives.

Lemma 3.2Gradient of a quadratic form

Let ARp×pA \in \mathbb{R}^{p \times p} be symmetric (AT=AA^{\mathsf{T}} = A), let bRp\boldsymbol{b} \in \mathbb{R}^p and cRc \in \mathbb{R}, and define f ⁣:RpRf \colon \mathbb{R}^p \to \mathbb{R} by

f(w)=wTAw2bTw+c.f(\boldsymbol{w}) = \boldsymbol{w}^{\mathsf{T}} A \boldsymbol{w} - 2\boldsymbol{b}^{\mathsf{T}}\boldsymbol{w} + c .

Then ff is of class CC^{\infty}, and its gradient and Hessian are

f(w)=2Aw2b,2f(w)=2A.\nabla f(\boldsymbol{w}) = 2A\boldsymbol{w} - 2\boldsymbol{b}, \qquad \nabla^2 f(\boldsymbol{w}) = 2A .
Proof(Lemma 3.2)

In components, f(w)=i=1pj=1paijwiwj2i=1pbiwi+cf(\boldsymbol{w}) = \sum_{i=1}^{p}\sum_{j=1}^{p} a_{ij} w_i w_j - 2\sum_{i=1}^{p} b_i w_i + c, a polynomial in w1,,wpw_1, \dots, w_p, hence of class CC^{\infty}. Differentiate with respect to the kk-th component. The product wiwjw_i w_j involves wkw_k when i=ki = k and when j=kj = k; the term akkwk2a_{kk}w_k^2 with i=j=ki = j = k differentiates to 2akkwk2a_{kk}w_k, which agrees with the sum of the two counts. Therefore

fwk(w)=j=1pakjwj+i=1paikwi2bk=(Aw)k+(ATw)k2bk.\frac{\partial f}{\partial w_k}(\boldsymbol{w}) = \sum_{j=1}^{p} a_{kj} w_j + \sum_{i=1}^{p} a_{ik} w_i - 2 b_k = (A\boldsymbol{w})_k + (A^{\mathsf{T}}\boldsymbol{w})_k - 2b_k .

By the hypothesis AT=AA^{\mathsf{T}} = A the right-hand side equals 2(Aw)k2bk2(A\boldsymbol{w})_k - 2b_k, which gives the first formula. Differentiating once more with respect to wlw_l gives 2f/wlwk=2akl\partial^2 f / \partial w_l \partial w_k = 2a_{kl}, so the Hessian is 2A2A.

We come to the central theorem. Note that it can be proved without any differentiation at all. Because the proof needs nothing but an algebraic identity, it simultaneously establishes that we have a minimum, and not merely a stationary point.

Theorem 3.3The normal equations

Let XRn×pX \in \mathbb{R}^{n\times p} and yRn\boldsymbol{y} \in \mathbb{R}^n, and set L(w)=yXw2L(\boldsymbol{w}) = \lVert \boldsymbol{y} - X\boldsymbol{w}\rVert^2. For w^Rp\hat{\boldsymbol{w}} \in \mathbb{R}^p the following two conditions are equivalent.

  1. w^\hat{\boldsymbol{w}} minimises LL on Rp\mathbb{R}^p, that is, L(w^)L(w)L(\hat{\boldsymbol{w}}) \le L(\boldsymbol{w}) for every wRp\boldsymbol{w} \in \mathbb{R}^p.
  2. w^\hat{\boldsymbol{w}} satisfies the normal equations XTXw^=XTyX^{\mathsf{T}}X\hat{\boldsymbol{w}} = X^{\mathsf{T}}\boldsymbol{y}.

Moreover, in that case the identity

L(w)=L(w^)+X(ww^)2L(\boldsymbol{w}) = L(\hat{\boldsymbol{w}}) + \lVert X(\boldsymbol{w} - \hat{\boldsymbol{w}})\rVert^2

holds for every wRp\boldsymbol{w} \in \mathbb{R}^p, and the set of all least squares solutions coincides with w^+KerX={w^+v:Xv=0}\hat{\boldsymbol{w}} + \operatorname{Ker} X = \{\hat{\boldsymbol{w}} + \boldsymbol{v} : X\boldsymbol{v} = \boldsymbol{0}\}.

Proof(Theorem 3.3)

For arbitrary w^,vRp\hat{\boldsymbol{w}}, \boldsymbol{v} \in \mathbb{R}^p, put r^=yXw^\hat{\boldsymbol{r}} = \boldsymbol{y} - X\hat{\boldsymbol{w}} and expand L(w^+v)L(\hat{\boldsymbol{w}} + \boldsymbol{v}). Since the squared norm is an inner product,

L(w^+v)=r^Xv2=r^Xv, r^Xv=r^22r^, Xv+Xv2.\begin{aligned} L(\hat{\boldsymbol{w}} + \boldsymbol{v}) &= \lVert \hat{\boldsymbol{r}} - X\boldsymbol{v}\rVert^2 \\ &= \langle \hat{\boldsymbol{r}} - X\boldsymbol{v},\ \hat{\boldsymbol{r}} - X\boldsymbol{v}\rangle \\ &= \lVert \hat{\boldsymbol{r}}\rVert^2 - 2\langle \hat{\boldsymbol{r}},\ X\boldsymbol{v}\rangle + \lVert X\boldsymbol{v}\rVert^2 . \end{aligned}

By the transpose rule u,Av=ATu,v\langle \boldsymbol{u}, A\boldsymbol{v}\rangle = \langle A^{\mathsf{T}}\boldsymbol{u}, \boldsymbol{v}\rangle, the middle term can be written

r^, Xv=XTr^, v=vT(XTyXTXw^).\langle \hat{\boldsymbol{r}},\ X\boldsymbol{v}\rangle = \langle X^{\mathsf{T}}\hat{\boldsymbol{r}},\ \boldsymbol{v}\rangle = \boldsymbol{v}^{\mathsf{T}}\bigl(X^{\mathsf{T}}\boldsymbol{y} - X^{\mathsf{T}}X\hat{\boldsymbol{w}}\bigr) .

So, setting g=XTyXTXw^=XTr^\boldsymbol{g} = X^{\mathsf{T}}\boldsymbol{y} - X^{\mathsf{T}}X\hat{\boldsymbol{w}} = X^{\mathsf{T}}\hat{\boldsymbol{r}}, we obtain for every v\boldsymbol{v}

L(w^+v)=L(w^)2vTg+Xv2.L(\hat{\boldsymbol{w}} + \boldsymbol{v}) = L(\hat{\boldsymbol{w}}) - 2\boldsymbol{v}^{\mathsf{T}}\boldsymbol{g} + \lVert X\boldsymbol{v}\rVert^2 .

Both implications follow from this single identity.

(2) \Rightarrow (1). If the normal equations hold then g=0\boldsymbol{g} = \boldsymbol{0}, so the identity becomes L(w^+v)=L(w^)+Xv2L(\hat{\boldsymbol{w}} + \boldsymbol{v}) = L(\hat{\boldsymbol{w}}) + \lVert X\boldsymbol{v}\rVert^2. Since Xv20\lVert X\boldsymbol{v}\rVert^2 \ge 0, we get L(w^+v)L(w^)L(\hat{\boldsymbol{w}} + \boldsymbol{v}) \ge L(\hat{\boldsymbol{w}}) for every v\boldsymbol{v}; that is, w^\hat{\boldsymbol{w}} is a minimiser. Substituting w=w^+v\boldsymbol{w} = \hat{\boldsymbol{w}} + \boldsymbol{v} gives exactly the asserted identity. Equality holds if and only if Xv=0\lVert X\boldsymbol{v}\rVert = 0, that is, Xv=0X\boldsymbol{v} = \boldsymbol{0}, so the set of minimisers is w^+KerX\hat{\boldsymbol{w}} + \operatorname{Ker} X.

(1) \Rightarrow (2). We prove the contrapositive. Suppose g0\boldsymbol{g} \ne \boldsymbol{0} and substitute v=tg\boldsymbol{v} = t\boldsymbol{g} with t>0t > 0:

L(w^+tg)L(w^)=2tg2+t2Xg2=t(tXg22g2).L(\hat{\boldsymbol{w}} + t\boldsymbol{g}) - L(\hat{\boldsymbol{w}}) = -2t\lVert \boldsymbol{g}\rVert^2 + t^2 \lVert X\boldsymbol{g}\rVert^2 = t\bigl(t\lVert X\boldsymbol{g}\rVert^2 - 2\lVert \boldsymbol{g}\rVert^2\bigr) .

If Xg=0X\boldsymbol{g} = \boldsymbol{0} the right-hand side equals 2tg2<0-2t\lVert\boldsymbol{g}\rVert^2 < 0 for every t>0t > 0. If Xg0X\boldsymbol{g} \ne \boldsymbol{0}, taking for example t=g2/Xg2>0t = \lVert \boldsymbol{g}\rVert^2 / \lVert X\boldsymbol{g}\rVert^2 > 0 makes the right-hand side g4/Xg2<0-\lVert\boldsymbol{g}\rVert^4/\lVert X\boldsymbol{g}\rVert^2 < 0. In either case L(w^+tg)<L(w^)L(\hat{\boldsymbol{w}} + t\boldsymbol{g}) < L(\hat{\boldsymbol{w}}), so w^\hat{\boldsymbol{w}} is not a minimiser. Hence a minimiser must satisfy g=0\boldsymbol{g} = \boldsymbol{0}, that is, the normal equations.

The normal equations XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y} form a system of pp linear equations. However large nn may be — even hundreds of millions of data points — the size of the system to be solved is governed solely by the number of features pp. What actually happens behind the word “learning” is the solution of a p×pp \times p linear system.

Remark 3.4Derivation by partial differentiation

Let us also record the derivation using calculus. Expanding,

L(w)=y22yTXw+wTXTXw,L(\boldsymbol{w}) = \lVert \boldsymbol{y}\rVert^2 - 2\boldsymbol{y}^{\mathsf{T}}X\boldsymbol{w} + \boldsymbol{w}^{\mathsf{T}}X^{\mathsf{T}}X\boldsymbol{w} ,

so applying Lemma 3.2 with A=XTXA = X^{\mathsf{T}}X (symmetric, since (XTX)T=XTX(X^{\mathsf{T}}X)^{\mathsf{T}} = X^{\mathsf{T}}X), b=XTy\boldsymbol{b} = X^{\mathsf{T}}\boldsymbol{y} and c=y2c = \lVert\boldsymbol{y}\rVert^2 gives

L(w)=2XTXw2XTy.\nabla L(\boldsymbol{w}) = 2X^{\mathsf{T}}X\boldsymbol{w} - 2X^{\mathsf{T}}\boldsymbol{y} .

The condition L(w^)=0\nabla L(\hat{\boldsymbol{w}}) = \boldsymbol{0} is the normal equations. Being a stationary point does not in general imply being a minimum. Here, however, the Hessian is 2XTX2X^{\mathsf{T}}X, and vT(XTX)v=Xv20\boldsymbol{v}^{\mathsf{T}}(X^{\mathsf{T}}X)\boldsymbol{v} = \lVert X\boldsymbol{v}\rVert^2 \ge 0 for every v\boldsymbol{v}, so it is positive semidefinite; LL is therefore convex and a stationary point is a global minimum. The proof of Theorem 3.3 can be viewed as replacing this convexity argument by a single identity.

4. Geometry: the least squares solution is an orthogonal projection

Section titled “4. Geometry: the least squares solution is an orthogonal projection”

4.1. The residual is orthogonal to every feature

Section titled “4.1. The residual is orthogonal to every feature”

Rewrite the normal equations as XT(yXw^)=0X^{\mathsf{T}}(\boldsymbol{y} - X\hat{\boldsymbol{w}}) = \boldsymbol{0}, that is, XTr^=0X^{\mathsf{T}}\hat{\boldsymbol{r}} = \boldsymbol{0}. The jj-th component of XTr^X^{\mathsf{T}}\hat{\boldsymbol{r}} is the inner product of the jj-th column of XX with r^\hat{\boldsymbol{r}}, so the equation reads:

the residual vector is orthogonal to every column of XX, that is, to every feature.

In statistical terms, “the residual contains nothing further that could be explained by a linear combination of the available features”. Least squares is a procedure that stops exactly when everything the explanatory variables can account for has been used up. Let us turn this reading into a theorem.

Lemma 4.1Orthogonal complement of the column space

For any XRn×pX \in \mathbb{R}^{n\times p} we have (ImX)=KerXT(\operatorname{Im} X)^{\perp} = \operatorname{Ker} X^{\mathsf{T}}.

Proof(Lemma 4.1)

That u(ImX)\boldsymbol{u} \in (\operatorname{Im}X)^{\perp} means u,Xv=0\langle \boldsymbol{u}, X\boldsymbol{v}\rangle = 0 for every vRp\boldsymbol{v}\in\mathbb{R}^p. By the transpose rule u,Xv=XTu,v\langle \boldsymbol{u}, X\boldsymbol{v}\rangle = \langle X^{\mathsf{T}}\boldsymbol{u}, \boldsymbol{v}\rangle, this is equivalent to XTu,v=0\langle X^{\mathsf{T}}\boldsymbol{u}, \boldsymbol{v}\rangle = 0 for every v\boldsymbol{v}. Taking in particular v=XTu\boldsymbol{v} = X^{\mathsf{T}}\boldsymbol{u} gives XTu2=0\lVert X^{\mathsf{T}}\boldsymbol{u}\rVert^2 = 0, hence XTu=0X^{\mathsf{T}}\boldsymbol{u} = \boldsymbol{0}. Conversely, if XTu=0X^{\mathsf{T}}\boldsymbol{u} = \boldsymbol{0} then the inner product vanishes for every v\boldsymbol{v}. The two conditions are therefore equivalent, and the sets coincide.

Theorem 4.2Existence of least squares solutions and their projection form

Let XRn×pX \in \mathbb{R}^{n\times p} and yRn\boldsymbol{y}\in\mathbb{R}^n be arbitrary. Then the following hold.

  1. The normal equations XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y} have at least one solution. In particular a least squares solution always exists.
  2. If w^\hat{\boldsymbol{w}} is any least squares solution, then Xw^X\hat{\boldsymbol{w}} equals the orthogonal projection of y\boldsymbol{y} onto the subspace ImX\operatorname{Im}X. In particular Xw^X\hat{\boldsymbol{w}} is uniquely determined, independently of which least squares solution is chosen.
  3. y^=Xw^\hat{\boldsymbol{y}} = X\hat{\boldsymbol{w}} is the unique point of ImX\operatorname{Im}X closest to y\boldsymbol{y}: if zImX\boldsymbol{z} \in \operatorname{Im}X and zy^\boldsymbol{z} \ne \hat{\boldsymbol{y}}, then yz>yy^\lVert \boldsymbol{y}-\boldsymbol{z}\rVert > \lVert \boldsymbol{y}-\hat{\boldsymbol{y}}\rVert.
Proof(Theorem 4.2)

(1). Since W=ImXW = \operatorname{Im}X is a subspace of Rn\mathbb{R}^n, the orthogonal decomposition Rn=WW\mathbb{R}^n = W \oplus W^{\perp} gives a unique decomposition y=y^+s\boldsymbol{y} = \hat{\boldsymbol{y}} + \boldsymbol{s} with y^W\hat{\boldsymbol{y}} \in W and sW\boldsymbol{s}\in W^{\perp}. As y^ImX\hat{\boldsymbol{y}} \in \operatorname{Im}X, there is a w^\hat{\boldsymbol{w}} with y^=Xw^\hat{\boldsymbol{y}} = X\hat{\boldsymbol{w}}. By Lemma 4.1, sW=KerXT\boldsymbol{s} \in W^{\perp} = \operatorname{Ker}X^{\mathsf{T}}, that is, XTs=0X^{\mathsf{T}}\boldsymbol{s} = \boldsymbol{0}. Hence

XTXw^=XTy^=XT(ys)=XTy,X^{\mathsf{T}}X\hat{\boldsymbol{w}} = X^{\mathsf{T}}\hat{\boldsymbol{y}} = X^{\mathsf{T}}(\boldsymbol{y} - \boldsymbol{s}) = X^{\mathsf{T}}\boldsymbol{y} ,

so this w^\hat{\boldsymbol{w}} solves the normal equations. By Theorem 3.3 it is a least squares solution.

(2). Let w^\hat{\boldsymbol{w}} be any least squares solution. By Theorem 3.3, XT(yXw^)=0X^{\mathsf{T}}(\boldsymbol{y} - X\hat{\boldsymbol{w}}) = \boldsymbol{0}, hence by Lemma 4.1 we have yXw^W\boldsymbol{y} - X\hat{\boldsymbol{w}} \in W^{\perp}. On the other hand Xw^WX\hat{\boldsymbol{w}} \in W, so y=Xw^+(yXw^)\boldsymbol{y} = X\hat{\boldsymbol{w}} + (\boldsymbol{y}-X\hat{\boldsymbol{w}}) is a decomposition along WWW \oplus W^{\perp}. Such a decomposition is unique, so Xw^X\hat{\boldsymbol{w}} equals the y^\hat{\boldsymbol{y}} constructed in (1), namely the orthogonal projection of y\boldsymbol{y} onto WW, and does not depend on the choice of least squares solution.

(3). For zW\boldsymbol{z}\in W we have y^zW\hat{\boldsymbol{y}} - \boldsymbol{z} \in W, while yy^W\boldsymbol{y}-\hat{\boldsymbol{y}} \in W^{\perp}, so the two are orthogonal. Hence by the Pythagorean theorem(Remark 4.4)[内積空間とグラム・シュミット直交化]

yz2=(yy^)+(y^z)2=yy^2+y^z2.\lVert \boldsymbol{y}-\boldsymbol{z}\rVert^2 = \lVert (\boldsymbol{y}-\hat{\boldsymbol{y}}) + (\hat{\boldsymbol{y}}-\boldsymbol{z})\rVert^2 = \lVert \boldsymbol{y}-\hat{\boldsymbol{y}}\rVert^2 + \lVert \hat{\boldsymbol{y}}-\boldsymbol{z}\rVert^2 .

If zy^\boldsymbol{z}\ne\hat{\boldsymbol{y}} the second term is positive, giving the strict inequality.

Oy (observed)ŷ = Xŵ (fitted)r = y − Xŵ (residual)1st column of X2nd column of XIm X (column space)
The geometry of least squares. The observed vector y generally lies outside the column space Im X; the fitted vector is the foot of the perpendicular dropped from y onto Im X (the orthogonal projection). The residual r is orthogonal to the whole column space.

4.2. The hat matrix and the coefficient of determination

Section titled “4.2. The hat matrix and the coefficient of determination”

When rankX=p\operatorname{rank}X = p the projection can be written explicitly as a matrix.

Proposition 4.3The hat matrix

Suppose XRn×pX \in \mathbb{R}^{n\times p} satisfies rankX=p\operatorname{rank}X = p (so that XTXX^{\mathsf{T}}X is invertible by Corollary 5.2). Put

P=X(XTX)1XTRn×n.P = X(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}} \in \mathbb{R}^{n\times n} .

Then the following hold.

  1. PT=PP^{\mathsf{T}} = P and P2=PP^2 = P (symmetric idempotent).
  2. For every yRn\boldsymbol{y}\in\mathbb{R}^n, PyP\boldsymbol{y} is the orthogonal projection of y\boldsymbol{y} onto ImX\operatorname{Im}X, and Py=Xw^P\boldsymbol{y} = X\hat{\boldsymbol{w}}.
  3. InPI_n - P is the orthogonal projection onto (ImX)=KerXT(\operatorname{Im}X)^{\perp} = \operatorname{Ker}X^{\mathsf{T}}, and the residual is r^=(InP)y\hat{\boldsymbol{r}} = (I_n - P)\boldsymbol{y}.
  4. trP=p\operatorname{tr} P = p.
Proof(Proposition 4.3)

1. (XTX)1(X^{\mathsf{T}}X)^{-1} is the inverse of a symmetric matrix, hence symmetric (substitute AT=AA^{\mathsf{T}}=A into (A1)T=(AT)1(A^{-1})^{\mathsf{T}} = (A^{\mathsf{T}})^{-1}). Therefore PT=X((XTX)1)TXT=PP^{\mathsf{T}} = X\bigl((X^{\mathsf{T}}X)^{-1}\bigr)^{\mathsf{T}}X^{\mathsf{T}} = P. Also

P2=X(XTX)1XTX(XTX)1=IpXT=X(XTX)1XT=P.P^2 = X(X^{\mathsf{T}}X)^{-1}\underbrace{X^{\mathsf{T}}X(X^{\mathsf{T}}X)^{-1}}_{= I_p}X^{\mathsf{T}} = X(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}} = P .

2. w^=(XTX)1XTy\hat{\boldsymbol{w}} = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y} solves the normal equations (multiply both sides by XTXX^{\mathsf{T}}X to check), so Py=Xw^P\boldsymbol{y} = X\hat{\boldsymbol{w}}, and by part (2) of Theorem 4.2 this is the orthogonal projection.

3. We have r^=yPy=(InP)y\hat{\boldsymbol{r}} = \boldsymbol{y}-P\boldsymbol{y} = (I_n-P)\boldsymbol{y}, and the proof of Theorem 4.2 shows r^(ImX)\hat{\boldsymbol{r}} \in (\operatorname{Im}X)^{\perp}. Since (InP)T=InP(I_n-P)^{\mathsf{T}} = I_n - P and (InP)2=In2P+P2=InP(I_n-P)^2 = I_n - 2P + P^2 = I_n - P, this matrix is also symmetric idempotent; and for u(ImX)\boldsymbol{u}\in(\operatorname{Im}X)^{\perp} we have XTu=0X^{\mathsf{T}}\boldsymbol{u} = \boldsymbol{0} by Lemma 4.1, hence Pu=0P\boldsymbol{u} = \boldsymbol{0} and (InP)u=u(I_n-P)\boldsymbol{u} = \boldsymbol{u}, so InPI_n-P is the identity on (ImX)(\operatorname{Im}X)^{\perp}.

4. Using the cyclic property of the trace, tr(AB)=tr(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA), with A=XA = X and B=(XTX)1XTB = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}},

trP=tr((XTX)1XTX)=trIp=p.\operatorname{tr}P = \operatorname{tr}\bigl((X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}X\bigr) = \operatorname{tr}I_p = p .

Because y^=Py\hat{\boldsymbol{y}} = P\boldsymbol{y} “puts a hat on y\boldsymbol{y}”, PP is called the hat matrix. In statistics the diagonal entries PiiP_{ii} measure “how strongly the ii-th observation pulls its own fitted value” (the leverage), and trP=p\operatorname{tr}P = p can be read as ”pp degrees of freedom have been spent on nn observations”.

Corollary 4.4Decomposition of the sum of squares and the coefficient of determination

Assume that the columns of XX include the all-ones vector 1\boldsymbol{1}. Put y^=Xw^\hat{\boldsymbol{y}} = X\hat{\boldsymbol{w}}, r^=yy^\hat{\boldsymbol{r}} = \boldsymbol{y}-\hat{\boldsymbol{y}} and yˉ=1niyi\bar{y} = \frac{1}{n}\sum_i y_i. Then the following hold.

  1. i=1nr^i=0\sum_{i=1}^n \hat{r}_i = 0, and consequently 1niy^i=yˉ\frac{1}{n}\sum_i \hat{y}_i = \bar{y}.
  2. The decomposition of the sum of squares
i=1n(yiyˉ)2total Stot=i=1n(y^iyˉ)2explained+i=1nr^i2residual Sres\underbrace{\sum_{i=1}^n (y_i-\bar{y})^2}_{\text{total } S_{\mathrm{tot}}} = \underbrace{\sum_{i=1}^n (\hat{y}_i-\bar{y})^2}_{\text{explained}} + \underbrace{\sum_{i=1}^n \hat{r}_i^{\,2}}_{\text{residual } S_{\mathrm{res}}}

holds. In particular, when Stot0S_{\mathrm{tot}} \ne 0 the coefficient of determination R2=1Sres/StotR^2 = 1 - S_{\mathrm{res}}/S_{\mathrm{tot}} satisfies 0R210 \le R^2 \le 1.

Proof(Corollary 4.4)

1. By Theorem 3.3 we have XTr^=0X^{\mathsf{T}}\hat{\boldsymbol{r}} = \boldsymbol{0}, and the component corresponding to 1\boldsymbol{1} is 1,r^=ir^i=0\langle \boldsymbol{1}, \hat{\boldsymbol{r}}\rangle = \sum_i \hat{r}_i = 0. Dividing by nn gives yˉ1niy^i=0\bar{y} - \frac{1}{n}\sum_i\hat{y}_i = 0.

2. Decompose yyˉ1=(y^yˉ1)+r^\boldsymbol{y}-\bar{y}\boldsymbol{1} = (\hat{\boldsymbol{y}}-\bar{y}\boldsymbol{1}) + \hat{\boldsymbol{r}}. Since 1ImX\boldsymbol{1} \in \operatorname{Im}X and y^ImX\hat{\boldsymbol{y}}\in\operatorname{Im}X, we have y^yˉ1ImX\hat{\boldsymbol{y}}-\bar{y}\boldsymbol{1}\in\operatorname{Im}X, while r^(ImX)\hat{\boldsymbol{r}} \in (\operatorname{Im}X)^{\perp} by Theorem 4.2; the two are therefore orthogonal. The asserted identity follows from the Pythagorean theorem. Finally R2=(explained)/StotR^2 = (\text{explained})/S_{\mathrm{tot}}, and both terms are non-negative with sum StotS_{\mathrm{tot}}, so 0R210\le R^2\le 1.

Example 4.5Fitting a line to three points, seen as a projection

Take n=3n = 3, d=1d = 1 and the data (xi,yi)=(0,1),(1,1),(2,4)(x_i, y_i) = (0,1), (1,1), (2,4). The design matrix and observation vector are

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

Here ImX\operatorname{Im}X is a two-dimensional plane in R3\mathbb{R}^3, and y\boldsymbol{y} does not lie on it (the three points are not collinear). Computing the ingredients of the normal equations,

XTX=(3335),XTy=(1+1+401+11+24)=(69).X^{\mathsf{T}}X = \begin{pmatrix} 3 & 3 \\ 3 & 5\end{pmatrix},\qquad X^{\mathsf{T}}\boldsymbol{y} = \begin{pmatrix} 1+1+4 \\ 0\cdot 1 + 1\cdot 1 + 2\cdot 4\end{pmatrix} = \begin{pmatrix} 6 \\ 9\end{pmatrix} .

Since det(XTX)=159=60\det(X^{\mathsf{T}}X) = 15-9 = 6 \ne 0, the inverse exists and

w^=16(5333)(69)=16(302718+27)=(0.51.5),\hat{\boldsymbol{w}} = \frac{1}{6}\begin{pmatrix} 5 & -3 \\ -3 & 3\end{pmatrix}\begin{pmatrix} 6 \\ 9\end{pmatrix} = \frac{1}{6}\begin{pmatrix} 30-27 \\ -18+27\end{pmatrix} = \begin{pmatrix} 0.5 \\ 1.5\end{pmatrix} ,

that is, y^=0.5+1.5x\hat{y} = 0.5 + 1.5x. The fitted values and residuals are

y^=Xw^=(0.5, 2.0, 3.5)T,r^=yy^=(0.5, 1.0, 0.5)T.\hat{\boldsymbol{y}} = X\hat{\boldsymbol{w}} = (0.5,\ 2.0,\ 3.5)^{\mathsf{T}},\qquad \hat{\boldsymbol{r}} = \boldsymbol{y}-\hat{\boldsymbol{y}} = (0.5,\ -1.0,\ 0.5)^{\mathsf{T}} .

Let us verify orthogonality: 1,r^=0.51.0+0.5=0\langle \boldsymbol{1}, \hat{\boldsymbol{r}}\rangle = 0.5-1.0+0.5 = 0 and (0,1,2)T,r^=00.5+1(1.0)+20.5=0\langle (0,1,2)^{\mathsf{T}}, \hat{\boldsymbol{r}}\rangle = 0\cdot 0.5 + 1\cdot(-1.0) + 2\cdot 0.5 = 0, so the residual is indeed orthogonal to both columns of XX. The Pythagorean theorem also checks out:

y2=1+1+16=18,y^2=0.25+4+12.25=16.5,r^2=0.25+1+0.25=1.5\lVert\boldsymbol{y}\rVert^2 = 1+1+16 = 18,\quad \lVert\hat{\boldsymbol{y}}\rVert^2 = 0.25+4+12.25 = 16.5,\quad \lVert\hat{\boldsymbol{r}}\rVert^2 = 0.25+1+0.25 = 1.5

gives 18=16.5+1.518 = 16.5 + 1.5. The hat matrix can be written down explicitly:

P=X(XTX)1XT=16(521222125).P = X(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}} = \frac{1}{6}\begin{pmatrix} 5 & 2 & -1 \\ 2 & 2 & 2 \\ -1 & 2 & 5\end{pmatrix} .

Indeed Py=16(5+24, 2+2+8, 1+2+20)T=(0.5,2.0,3.5)T=y^P\boldsymbol{y} = \frac{1}{6}(5+2-4,\ 2+2+8,\ -1+2+20)^{\mathsf{T}} = (0.5, 2.0, 3.5)^{\mathsf{T}} = \hat{\boldsymbol{y}}. Symmetry is visible at a glance, and trP=(5+2+5)/6=2=p\operatorname{tr}P = (5+2+5)/6 = 2 = p agrees with part 4 of Proposition 4.3.

5. When is the solution unique? The rank condition

Section titled “5. When is the solution unique? The rank condition”

By Theorem 4.2 the prediction y^=Xw^\hat{\boldsymbol{y}} = X\hat{\boldsymbol{w}} is always unique. Whether the coefficient vector w^\hat{\boldsymbol{w}} itself is unique is another matter. The last assertion of Theorem 3.3 says the solution set is w^+KerX\hat{\boldsymbol{w}} + \operatorname{Ker}X, so uniqueness is equivalent to KerX={0}\operatorname{Ker}X = \{\boldsymbol{0}\}. Let us translate this into the language of XTXX^{\mathsf{T}}X.

Lemma 5.1Kernel of the Gram matrix

For any XRn×pX\in\mathbb{R}^{n\times p},

Ker(XTX)=KerX,rank(XTX)=rankX.\operatorname{Ker}(X^{\mathsf{T}}X) = \operatorname{Ker}X, \qquad \operatorname{rank}(X^{\mathsf{T}}X) = \operatorname{rank}X .
Proof(Lemma 5.1)

If Xv=0X\boldsymbol{v} = \boldsymbol{0}, multiplying on the left by XTX^{\mathsf{T}} gives XTXv=0X^{\mathsf{T}}X\boldsymbol{v} = \boldsymbol{0}, so KerXKer(XTX)\operatorname{Ker}X \subseteq \operatorname{Ker}(X^{\mathsf{T}}X). Conversely, suppose XTXv=0X^{\mathsf{T}}X\boldsymbol{v} = \boldsymbol{0} and multiply on the left by vT\boldsymbol{v}^{\mathsf{T}}:

0=vTXTXv=(Xv)T(Xv)=Xv2.0 = \boldsymbol{v}^{\mathsf{T}}X^{\mathsf{T}}X\boldsymbol{v} = (X\boldsymbol{v})^{\mathsf{T}}(X\boldsymbol{v}) = \lVert X\boldsymbol{v}\rVert^2 .

Only the zero vector has zero norm, so Xv=0X\boldsymbol{v} = \boldsymbol{0}, giving Ker(XTX)KerX\operatorname{Ker}(X^{\mathsf{T}}X)\subseteq\operatorname{Ker}X. The two kernels therefore coincide. As for the ranks, both XX and XTXX^{\mathsf{T}}X have pp columns, so applying the rank–nullity theorem

rankA=pdimKerA\operatorname{rank}A = p - \dim\operatorname{Ker}A

to each and using the equality of the kernel dimensions gives the equality of the ranks.

Corollary 5.2Uniqueness of the least squares solution

Let XRn×pX\in\mathbb{R}^{n\times p} and yRn\boldsymbol{y}\in\mathbb{R}^n. The following three conditions are equivalent.

  1. The pp columns of XX are linearly independent, that is, rankX=p\operatorname{rank}X = p.
  2. XTXX^{\mathsf{T}}X is invertible.
  3. There is exactly one least squares solution.

In that case the least squares solution is

w^=(XTX)1XTy.\hat{\boldsymbol{w}} = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y} .

Furthermore, npn \ge p is necessary for these conditions to hold.

Proof(Corollary 5.2)

1 \Leftrightarrow 2. XTXX^{\mathsf{T}}X is a p×pp\times p square matrix, so invertibility is equivalent to rank(XTX)=p\operatorname{rank}(X^{\mathsf{T}}X) = p. By Lemma 5.1 this is equivalent to rankX=p\operatorname{rank}X = p, which is exactly linear independence of the columns.

1 \Leftrightarrow 3. By Theorem 4.2 a least squares solution always exists, and by Theorem 3.3 the solution set is w^+KerX\hat{\boldsymbol{w}} + \operatorname{Ker}X. Hence there is exactly one solution if and only if KerX={0}\operatorname{Ker}X = \{\boldsymbol{0}\}, which by the rank–nullity theorem is equivalent to rankX=p\operatorname{rank}X = p.

The formula. Under condition 2, setting w=(XTX)1XTy\boldsymbol{w} = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}\boldsymbol{y} gives XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y}, so this solves the normal equations and is therefore a least squares solution by Theorem 3.3. Uniqueness has just been shown.

Necessity of npn\ge p. Since rankXmin(n,p)\operatorname{rank}X \le \min(n,p), the equality rankX=p\operatorname{rank}X = p forces pnp \le n.

When there are fewer data points than features (n<pn < p), the solution is in this sense never unique. The situation of “more parameters than data”, an everyday occurrence in deep learning, already shows its face at the level of linear regression.

Example 5.3When features are duplicated (multicollinearity)

Take the same data (xi,yi)=(0,1),(1,1),(2,4)(x_i, y_i) = (0,1), (1,1), (2,4) as in Example 4.5 and add the pointless feature x=2xx' = 2x, “twice xx”. The design matrix becomes

X=(100112124),X = \begin{pmatrix} 1 & 0 & 0 \\ 1 & 1 & 2 \\ 1 & 2 & 4\end{pmatrix} ,

whose third column is exactly twice the second, so rankX=2<3=p\operatorname{rank}X = 2 < 3 = p. The kernel is

KerX={t(0,2,1)T:tR}\operatorname{Ker}X = \{\, t(0, 2, -1)^{\mathsf{T}} : t\in\mathbb{R}\,\}

(indeed 01+2x1(2x)=00\cdot\boldsymbol{1} + 2\boldsymbol{x} - 1\cdot(2\boldsymbol{x}) = \boldsymbol{0}). Appending a third coordinate 00 to the (0.5,1.5)(0.5, 1.5) found in Example 4.5 gives the least squares solution w^0=(0.5,1.5,0)T\hat{\boldsymbol{w}}_0 = (0.5, 1.5, 0)^{\mathsf{T}}, so by Theorem 3.3 the solution set is the whole line

{(0.5, 1.52t, t)T:tR}.\{\,(0.5,\ 1.5 - 2t,\ t)^{\mathsf{T}} : t\in\mathbb{R}\,\} .

For example t=0.75t = 0.75 gives (0.5,0,0.75)(0.5, 0, 0.75) and t=1t = -1 gives (0.5,3.5,1)(0.5, 3.5, -1); the coefficients look completely different. Whether “the effect of xx is 1.51.5” or “the effect of xx is 00 and that of xx' is 0.750.75” cannot be decided from the data. The prediction, however, is

X(0.5, 1.52t, t)T=(0.5, 0.5+(1.52t)+2t, 0.5+2(1.52t)+4t)T=(0.5, 2, 3.5)T,X(0.5,\ 1.5-2t,\ t)^{\mathsf{T}} = (0.5,\ 0.5 + (1.5-2t) + 2t,\ 0.5 + 2(1.5-2t)+4t)^{\mathsf{T}} = (0.5,\ 2,\ 3.5)^{\mathsf{T}} ,

constant in tt, exactly as part (2) of Theorem 4.2 predicts.

6.1. The closed formula for simple regression

Section titled “6.1. The closed formula for simple regression”

Let us write out the case d=1d = 1 (a single explanatory variable) in full generality. Here p=2p = 2 and

XTX=(nixiixiixi2),XTy=(iyiixiyi).X^{\mathsf{T}}X = \begin{pmatrix} n & \sum_i x_i \\ \sum_i x_i & \sum_i x_i^2\end{pmatrix}, \qquad X^{\mathsf{T}}\boldsymbol{y} = \begin{pmatrix} \sum_i y_i \\ \sum_i x_i y_i\end{pmatrix} .

Put xˉ=1nixi\bar{x} = \frac1n\sum_i x_i, yˉ=1niyi\bar{y} = \frac1n\sum_i y_i, Sxx=i(xixˉ)2S_{xx} = \sum_i (x_i-\bar{x})^2 and Sxy=i(xixˉ)(yiyˉ)S_{xy} = \sum_i (x_i-\bar{x})(y_i-\bar{y}). Expanding,

det(XTX)=nixi2(ixi)2=n(ixi2nxˉ2)=nSxx,\det(X^{\mathsf{T}}X) = n\sum_i x_i^2 - \Bigl(\sum_i x_i\Bigr)^2 = n\Bigl(\sum_i x_i^2 - n\bar{x}^2\Bigr) = nS_{xx} ,

so if Sxx0S_{xx}\ne 0 (the xix_i are not all equal) then Corollary 5.2 applies and

w^=1nSxx(ixi2ixiixin)(nyˉixiyi).\hat{\boldsymbol{w}} = \frac{1}{nS_{xx}}\begin{pmatrix} \sum_i x_i^2 & -\sum_i x_i \\ -\sum_i x_i & n\end{pmatrix}\begin{pmatrix} n\bar{y} \\ \sum_i x_i y_i\end{pmatrix} .

The second component is (nixiyinyˉixi)/(nSxx)=(ixiyinxˉyˉ)/Sxx=Sxy/Sxx\bigl(n\sum_i x_iy_i - n\bar{y}\sum_i x_i\bigr)/(nS_{xx}) = (\sum_i x_iy_i - n\bar{x}\bar{y})/S_{xx} = S_{xy}/S_{xx}. Computing the first component in the same way gives (yˉixi2xˉixiyi)/Sxx(\bar{y}\sum_i x_i^2 - \bar{x}\sum_i x_iy_i)/S_{xx}, which agrees with yˉSxxxˉSxy=yˉixi2nxˉ2yˉxˉixiyi+nxˉ2yˉ=yˉixi2xˉixiyi\bar{y}S_{xx} - \bar{x}S_{xy} = \bar{y}\sum_i x_i^2 - n\bar{x}^2\bar{y} - \bar{x}\sum_i x_iy_i + n\bar{x}^2\bar{y} = \bar{y}\sum_i x_i^2 - \bar{x}\sum_i x_iy_i. In summary we recover the formula familiar from school:

w^1=SxySxx,b^=yˉw^1xˉ.\hat{w}_1 = \frac{S_{xy}}{S_{xx}}, \qquad \hat{b} = \bar{y} - \hat{w}_1\bar{x} .

The second equation says that the regression line always passes through the centroid (xˉ,yˉ)(\bar{x},\bar{y}). It is also a restatement of part 1 of Corollary 4.4 (that the residuals sum to 00).

Example 6.1Carrying a five-point simple regression through to the end

Take the data (xi,yi)=(1,2),(2,3),(3,5),(4,4),(5,6)(x_i,y_i) = (1,2), (2,3), (3,5), (4,4), (5,6). With n=5n = 5,

ixi=15,iyi=20,ixi2=55,ixiyi=2+6+15+16+30=69,\sum_i x_i = 15,\quad \sum_i y_i = 20,\quad \sum_i x_i^2 = 55,\quad \sum_i x_iy_i = 2+6+15+16+30 = 69 ,

so xˉ=3\bar{x} = 3 and yˉ=4\bar{y} = 4. The normal equations read

(5151555)(bw1)=(2069).\begin{pmatrix} 5 & 15 \\ 15 & 55\end{pmatrix}\begin{pmatrix} b \\ w_1\end{pmatrix} = \begin{pmatrix} 20 \\ 69\end{pmatrix} .

Since det=555152=275225=500\det = 5\cdot 55 - 15^2 = 275-225 = 50 \ne 0,

(b^w^1)=150(5515155)(2069)=150(11001035300+345)=150(6545)=(1.30.9),\begin{pmatrix} \hat{b} \\ \hat{w}_1\end{pmatrix} = \frac{1}{50}\begin{pmatrix} 55 & -15 \\ -15 & 5\end{pmatrix}\begin{pmatrix} 20 \\ 69\end{pmatrix} = \frac{1}{50}\begin{pmatrix} 1100 - 1035 \\ -300 + 345\end{pmatrix} = \frac{1}{50}\begin{pmatrix} 65 \\ 45\end{pmatrix} = \begin{pmatrix} 1.3 \\ 0.9\end{pmatrix} ,

that is, y^=1.3+0.9x\hat{y} = 1.3 + 0.9x. Let us check with the formula as well: Sxx=4+1+0+1+4=10S_{xx} = 4+1+0+1+4 = 10 and Sxy=(2)(2)+(1)(1)+01+10+22=9S_{xy} = (-2)(-2)+(-1)(-1)+0\cdot 1+1\cdot 0+2\cdot 2 = 9, so w^1=9/10=0.9\hat{w}_1 = 9/10 = 0.9 and b^=40.93=1.3\hat{b} = 4 - 0.9\cdot 3 = 1.3, in agreement.

The fitted values are y^=(2.2, 3.1, 4.0, 4.9, 5.8)T\hat{\boldsymbol{y}} = (2.2,\ 3.1,\ 4.0,\ 4.9,\ 5.8)^{\mathsf{T}} and the residuals are

r^=(0.2, 0.1, 1.0, 0.9, 0.2)T.\hat{\boldsymbol{r}} = (-0.2,\ -0.1,\ 1.0,\ -0.9,\ 0.2)^{\mathsf{T}} .

Verifying the orthogonality conditions, ir^i=0.20.1+1.00.9+0.2=0\sum_i \hat{r}_i = -0.2-0.1+1.0-0.9+0.2 = 0 and ixir^i=0.20.2+3.03.6+1.0=0\sum_i x_i\hat{r}_i = -0.2-0.2+3.0-3.6+1.0 = 0, both zero. The sums of squares are Sres=0.04+0.01+1.00+0.81+0.04=1.90S_{\mathrm{res}} = 0.04+0.01+1.00+0.81+0.04 = 1.90 and Stot=4+1+1+0+4=10S_{\mathrm{tot}} = 4+1+1+0+4 = 10, so by Corollary 4.4 the explained variation is 101.9=8.110-1.9 = 8.1, which matches Sxy2/Sxx=81/10=8.1S_{xy}^2/S_{xx} = 81/10 = 8.1. The coefficient of determination is R2=11.9/10=0.81R^2 = 1 - 1.9/10 = 0.81.

The same computation in NumPy looks as follows. Note that it solves a linear system rather than forming an inverse.

import numpy as np
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.0, 3.0, 5.0, 4.0, 6.0])
X = np.column_stack([np.ones_like(x), x]) # design matrix (5, 2)
# solve the normal equations X^T X w = X^T y directly
w_ne = np.linalg.solve(X.T @ X, X.T @ y) # [1.3, 0.9]
# numerically preferable (uses an SVD internally)
w_ls, *_ = np.linalg.lstsq(X, y, rcond=None) # [1.3, 0.9]
r = y - X @ w_ls
print(X.T @ r) # very close to [0. 0.] (up to rounding error)
print(1 - r @ r / ((y - y.mean()) ** 2).sum()) # 0.81

As Definition 2.2 says, merely swapping the feature map turns the procedure into curve fitting. None of the machinery changes.

Example 6.2Fitting a quadratic polynomial

Take the data (xi,yi)=(2,6),(1,2),(0,2),(1,2),(2,5)(x_i, y_i) = (-2,6), (-1,2), (0,2), (1,2), (2,5) and φ(x)=(1,x,x2)T\varphi(x) = (1, x, x^2)^{\mathsf{T}}, that is, fit y^=w0+w1x+w2x2\hat{y} = w_0 + w_1x + w_2x^2. Here p=3p = 3, and the required sums are

ixi=0,ixi2=10,ixi3=0,ixi4=34\sum_i x_i = 0,\quad \sum_i x_i^2 = 10,\quad \sum_i x_i^3 = 0,\quad \sum_i x_i^4 = 34

(the odd powers vanish because the xix_i are symmetric about 00), together with

iyi=17,ixiyi=122+0+2+10=2,ixi2yi=24+2+0+2+20=48.\sum_i y_i = 17,\quad \sum_i x_iy_i = -12-2+0+2+10 = -2,\quad \sum_i x_i^2y_i = 24+2+0+2+20 = 48 .

The normal equations are therefore

(5010010010034)(w0w1w2)=(17248).\begin{pmatrix} 5 & 0 & 10 \\ 0 & 10 & 0 \\ 10 & 0 & 34 \end{pmatrix} \begin{pmatrix} w_0 \\ w_1 \\ w_2\end{pmatrix} = \begin{pmatrix} 17 \\ -2 \\ 48\end{pmatrix} .

The second row immediately gives w^1=0.2\hat{w}_1 = -0.2. The first and third rows form a system in w0w_0 and w2w_2 alone; doubling the first row and subtracting it from the third gives

(3420)w2=4834,that is14w2=14,(34 - 20)w_2 = 48 - 34, \qquad \text{that is}\quad 14w_2 = 14 ,

so w^2=1\hat{w}_2 = 1, and then the first row gives w^0=(1710)/5=1.4\hat{w}_0 = (17-10)/5 = 1.4. The fitted curve is

y^=1.40.2x+x2.\hat{y} = 1.4 - 0.2x + x^2 .

The fitted values are y^=(5.8, 2.6, 1.4, 2.2, 5.0)T\hat{\boldsymbol{y}} = (5.8,\ 2.6,\ 1.4,\ 2.2,\ 5.0)^{\mathsf{T}} and the residuals are r^=(0.2, 0.6, 0.6, 0.2, 0)T\hat{\boldsymbol{r}} = (0.2,\ -0.6,\ 0.6,\ -0.2,\ 0)^{\mathsf{T}}. Checking the orthogonality conditions again, all three hold:

ir^i=0.20.6+0.60.2+0=0,ixir^i=0.4+0.6+00.2+0=0,ixi2r^i=0.80.6+00.2+0=0.\begin{aligned} \sum_i \hat{r}_i &= 0.2-0.6+0.6-0.2+0 = 0,\\ \sum_i x_i\hat{r}_i &= -0.4+0.6+0-0.2+0 = 0,\\ \sum_i x_i^2\hat{r}_i &= 0.8-0.6+0-0.2+0 = 0. \end{aligned}

The residual sum of squares is Sres=0.04+0.36+0.36+0.04+0=0.8S_{\mathrm{res}} = 0.04+0.36+0.36+0.04+0 = 0.8, and since yˉ=3.4\bar{y} = 3.4 the total is Stot=6.76+1.96+1.96+1.96+2.56=15.2S_{\mathrm{tot}} = 6.76+1.96+1.96+1.96+2.56 = 15.2, so R2=10.8/15.20.947R^2 = 1-0.8/15.2 \approx 0.947. We fitted a curve, yet all we solved was a 3×33\times 3 linear system. That is the power of being linear in the parameters.

Let us gather the results so far onto a single page.

flowchart TD
A["data X, y"] --> B["minimise the sum of squared errors L"]
B --> C["normal equations X^T X w = X^T y"]
C --> D&#123;"is rank X = p?"&#125;
D -- "yes" --> E["unique solution; implement via Cholesky or QR"]
D -- "no" --> F["solution set is w0 + Ker X; choose the minimum-norm one"]
E --> G["the prediction Xw is the orthogonal projection of y onto Im X"]
F --> G
From the least squares problem to the solution. The branch is the rank condition, and the vector of predictions is uniquely determined along either branch.

When rankX=p\operatorname{rank}X = p, the matrix XTXX^{\mathsf{T}}X is symmetric and positive definite. Indeed, by Lemma 5.1, if v0\boldsymbol{v}\ne\boldsymbol{0} then Xv0X\boldsymbol{v}\ne\boldsymbol{0}, so vTXTXv=Xv2>0\boldsymbol{v}^{\mathsf{T}}X^{\mathsf{T}}X\boldsymbol{v} = \lVert X\boldsymbol{v}\rVert^2 > 0. A positive definite symmetric matrix admits a Cholesky decomposition XTX=LLTX^{\mathsf{T}}X = LL^{\mathsf{T}} with LL lower triangular, so the implementation reduces to: form XTXX^{\mathsf{T}}X and XTyX^{\mathsf{T}}\boldsymbol{y}, compute the Cholesky factor, and solve two triangular systems.

The normal equations, clean as they are in theory, have a numerical drawback. Measuring the condition number of a matrix AA by κ2(A)=σmax(A)/σmin(A)\kappa_2(A) = \sigma_{\max}(A)/\sigma_{\min}(A) (the ratio of singular values), one has, for XX of full rank,

κ2(XTX)=κ2(X)2.\kappa_2(X^{\mathsf{T}}X) = \kappa_2(X)^2 .

The reason is that with the singular value decomposition X=UΣVTX = U\Sigma V^{\mathsf{T}} we get XTX=VΣTΣVTX^{\mathsf{T}}X = V\Sigma^{\mathsf{T}}\Sigma V^{\mathsf{T}}, so the eigenvalues of XTXX^{\mathsf{T}}X are the squares of the singular values of XX (for the diagonalisation of symmetric matrices see orthogonal diagonalisation of real symmetric matrices(Corollary 4.3)[スペクトル定理] in The Spectral Theorem). The condition number indicates by what factor a relative error in the input is amplified in the solution, so the moment one forms XTXX^{\mathsf{T}}X the amplification factor is squared.

Example 7.1Centring alone improves the condition number

For the data of Example 6.1 we had XTX=(5151555)X^{\mathsf{T}}X = \begin{pmatrix} 5 & 15 \\ 15 & 55\end{pmatrix}. Being a symmetric 2×22\times 2 matrix, its eigenvalues come from the characteristic equation λ260λ+50=0\lambda^2 - 60\lambda + 50 = 0 (trace 6060, determinant 5050), giving

λ=30±90050=30±850,\lambda = 30 \pm \sqrt{900-50} = 30 \pm \sqrt{850} ,

that is, λmax59.155\lambda_{\max} \approx 59.155 and λmin0.845\lambda_{\min} \approx 0.845. Hence κ2(XTX)70.0\kappa_2(X^{\mathsf{T}}X) \approx 70.0 and κ2(X)708.4\kappa_2(X) \approx \sqrt{70} \approx 8.4.

Now centre xx, replacing the second column by xixˉ=2,1,0,1,2x_i - \bar{x} = -2,-1,0,1,2. Its inner product with the first column becomes i(xixˉ)=0\sum_i (x_i-\bar{x}) = 0, so

XcTXc=(50010)X_{\mathrm{c}}^{\mathsf{T}}X_{\mathrm{c}} = \begin{pmatrix} 5 & 0 \\ 0 & 10\end{pmatrix}

is diagonal and κ2(XcTXc)=10/5=2\kappa_2(X_{\mathrm{c}}^{\mathsf{T}}X_{\mathrm{c}}) = 10/5 = 2. The data are the same; only the choice of coordinates changed. This is one of the reasons why centring and scaling the features is recommended as preprocessing. (Centring does not change the fitted line itself; see exercise Exercise 8.2.)

Remark 7.2Solving via the QR decomposition

When rankX=p\operatorname{rank}X = p, one can factor X=QRX = QR with QRn×pQ\in\mathbb{R}^{n\times p} satisfying QTQ=IpQ^{\mathsf{T}}Q = I_p and RRp×pR\in\mathbb{R}^{p\times p} upper triangular with positive diagonal entries. This is precisely Gram–Schmidt orthogonalisation(Theorem 6.1)[内積空間とグラム・シュミット直交化]. Substituting into the normal equations gives

XTX=RTQTQR=RTR,XTy=RTQTy,X^{\mathsf{T}}X = R^{\mathsf{T}}Q^{\mathsf{T}}QR = R^{\mathsf{T}}R, \qquad X^{\mathsf{T}}\boldsymbol{y} = R^{\mathsf{T}}Q^{\mathsf{T}}\boldsymbol{y} ,

so the normal equations become RTRw=RTQTyR^{\mathsf{T}}R\boldsymbol{w} = R^{\mathsf{T}}Q^{\mathsf{T}}\boldsymbol{y}. Since RR is invertible (positive diagonal), so is RTR^{\mathsf{T}}, and multiplying both sides by (RT)1(R^{\mathsf{T}})^{-1} turns the system into the upper triangular system

Rw^=QTy,R\hat{\boldsymbol{w}} = Q^{\mathsf{T}}\boldsymbol{y} ,

which is solved by back substitution. Because XTXX^{\mathsf{T}}X is never formed, the condition number stays at κ2(X)\kappa_2(X). The hat matrix also simplifies to P=QQTP = QQ^{\mathsf{T}}. When rank deficiency is possible, the still more robust approach via the singular value decomposition (the Appendix at the end of this article) is used.

Here are the typical situations in which the closed form ceases to be usable, together with the tools that take over.

SituationWhat happensRemedy
pp in the tens of thousands or morefactoring a p×pp\times p matrix costs O(p3)O(p^3)Gradient Descent, conjugate gradients
rankX<p\operatorname{rank}X < p, or nearly dependent columnsindeterminate solution, wild coefficientsridge regression (exercise Exercise 8.3), pseudoinverse (Appendix)
the response is 00 or 11squared error is unnatural and does not give probabilitiesLogistic Regression
one wants to construct the features themselveschoosing φ\varphi becomes the problemPrincipal Component Analysis, Neural Networks and Backpropagation
one wants the uncertainty of the coefficientsa point estimate is not enoughThe Role of Probability Theory and Bayesian Statistics

From the statistical side, one can show that under the assumption that the errors εi\varepsilon_i have mean 00, variance σ2\sigma^2 and are uncorrelated, the least squares estimator has the smallest variance among unbiased linear estimators (the Gauss–Markov theorem). We do not treat it here, since no probabilistic assumptions have been made in this article, but the proof needs nothing beyond the hat matrix and the orthogonality established above.

Exercise 8.1Easy

Show that if the columns of the design matrix XX include 1=(1,,1)T\boldsymbol{1} = (1,\dots,1)^{\mathsf{T}}, then the residual of a least squares solution satisfies ir^i=0\sum_i \hat{r}_i = 0. Then verify with explicit data that this fails for the model without intercept y^=wx\hat{y} = w x (whose design matrix is the single column x\boldsymbol{x}).

Solution

First part. By Theorem 3.3, a least squares solution satisfies XT(yXw^)=XTr^=0X^{\mathsf{T}}(\boldsymbol{y}-X\hat{\boldsymbol{w}}) = X^{\mathsf{T}}\hat{\boldsymbol{r}} = \boldsymbol{0}. The jj-th component of XTr^X^{\mathsf{T}}\hat{\boldsymbol{r}} is the inner product of the jj-th column of XX with r^\hat{\boldsymbol{r}}. If the jj-th column is 1\boldsymbol{1}, that component is 1,r^=ir^i\langle\boldsymbol{1},\hat{\boldsymbol{r}}\rangle = \sum_i \hat{r}_i, which therefore vanishes.

Second part. Take the data (x1,y1)=(1,1)(x_1,y_1) = (1,1) and (x2,y2)=(2,0)(x_2,y_2) = (2,0). The design matrix is X=(1,2)TX = (1, 2)^{\mathsf{T}} with n=2n=2, p=1p=1, and XTX=12+22=5X^{\mathsf{T}}X = 1^2+2^2 = 5, XTy=11+20=1X^{\mathsf{T}}\boldsymbol{y} = 1\cdot 1 + 2\cdot 0 = 1, so w^=1/5=0.2\hat{w} = 1/5 = 0.2. The fitted values are y^=(0.2,0.4)T\hat{\boldsymbol{y}} = (0.2, 0.4)^{\mathsf{T}} and the residuals r^=(0.8,0.4)T\hat{\boldsymbol{r}} = (0.8, -0.4)^{\mathsf{T}}, whence ir^i=0.40\sum_i \hat{r}_i = 0.4 \ne 0. Of course x,r^=10.8+2(0.4)=0\langle \boldsymbol{x}, \hat{\boldsymbol{r}}\rangle = 1\cdot 0.8 + 2\cdot(-0.4) = 0 still holds. Orthogonality is to the columns of XX, and 1\boldsymbol{1}, not being in the model, need not be orthogonal to the residual.

Exercise 8.2Standard

Split X=(1  X~)X = (\boldsymbol{1} \ \ \tilde{X}) with X~Rn×d\tilde{X}\in\mathbb{R}^{n\times d}, and split the parameters correspondingly as w=(b,u)\boldsymbol{w} = (b, \boldsymbol{u}) with uRd\boldsymbol{u}\in\mathbb{R}^d. Write the column means as xˉ=1nX~T1Rd\bar{\boldsymbol{x}} = \frac1n\tilde{X}^{\mathsf{T}}\boldsymbol{1}\in\mathbb{R}^d and the centred matrix and vector as Xc=X~1xˉTX_{\mathrm{c}} = \tilde{X}-\boldsymbol{1}\bar{\boldsymbol{x}}^{\mathsf{T}} and yc=yyˉ1\boldsymbol{y}_{\mathrm{c}} = \boldsymbol{y}-\bar{y}\boldsymbol{1}. Show that the normal equations are equivalent to

b^=yˉxˉTu^,XcTXcu^=XcTyc.\hat{b} = \bar{y}-\bar{\boldsymbol{x}}^{\mathsf{T}}\hat{\boldsymbol{u}}, \qquad X_{\mathrm{c}}^{\mathsf{T}}X_{\mathrm{c}}\,\hat{\boldsymbol{u}} = X_{\mathrm{c}}^{\mathsf{T}}\boldsymbol{y}_{\mathrm{c}} .
Solution

Write the normal equations XTXw=XTyX^{\mathsf{T}}X\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y} in block form. Since 1T1=n\boldsymbol{1}^{\mathsf{T}}\boldsymbol{1} = n, 1TX~=nxˉT\boldsymbol{1}^{\mathsf{T}}\tilde{X} = n\bar{\boldsymbol{x}}^{\mathsf{T}} and 1Ty=nyˉ\boldsymbol{1}^{\mathsf{T}}\boldsymbol{y} = n\bar{y},

(nnxˉTnxˉX~TX~)(bu)=(nyˉX~Ty).\begin{pmatrix} n & n\bar{\boldsymbol{x}}^{\mathsf{T}} \\ n\bar{\boldsymbol{x}} & \tilde{X}^{\mathsf{T}}\tilde{X}\end{pmatrix} \begin{pmatrix} b \\ \boldsymbol{u}\end{pmatrix} = \begin{pmatrix} n\bar{y} \\ \tilde{X}^{\mathsf{T}}\boldsymbol{y}\end{pmatrix} .

The first block row reads nb+nxˉTu=nyˉnb + n\bar{\boldsymbol{x}}^{\mathsf{T}}\boldsymbol{u} = n\bar{y}, that is, b=yˉxˉTub = \bar{y}-\bar{\boldsymbol{x}}^{\mathsf{T}}\boldsymbol{u}. Substituting this into the second block row nxˉb+X~TX~u=X~Tyn\bar{\boldsymbol{x}}b + \tilde{X}^{\mathsf{T}}\tilde{X}\boldsymbol{u} = \tilde{X}^{\mathsf{T}}\boldsymbol{y} gives

(X~TX~nxˉxˉT)u=X~Tynxˉyˉ.\bigl(\tilde{X}^{\mathsf{T}}\tilde{X} - n\bar{\boldsymbol{x}}\bar{\boldsymbol{x}}^{\mathsf{T}}\bigr)\boldsymbol{u} = \tilde{X}^{\mathsf{T}}\boldsymbol{y} - n\bar{\boldsymbol{x}}\bar{y} .

It remains to check that the coefficients on the left and right are the centred quantities. From 1T1=n\boldsymbol{1}^{\mathsf{T}}\boldsymbol{1} = n and 1TX~=nxˉT\boldsymbol{1}^{\mathsf{T}}\tilde{X} = n\bar{\boldsymbol{x}}^{\mathsf{T}},

XcTXc=(X~1xˉT)T(X~1xˉT)=X~TX~X~T1xˉTxˉ1TX~+xˉ(1T1)xˉT=X~TX~nxˉxˉTnxˉxˉT+nxˉxˉT=X~TX~nxˉxˉT\begin{aligned} X_{\mathrm{c}}^{\mathsf{T}}X_{\mathrm{c}} &= (\tilde{X}-\boldsymbol{1}\bar{\boldsymbol{x}}^{\mathsf{T}})^{\mathsf{T}}(\tilde{X}-\boldsymbol{1}\bar{\boldsymbol{x}}^{\mathsf{T}}) \\ &= \tilde{X}^{\mathsf{T}}\tilde{X} - \tilde{X}^{\mathsf{T}}\boldsymbol{1}\bar{\boldsymbol{x}}^{\mathsf{T}} - \bar{\boldsymbol{x}}\boldsymbol{1}^{\mathsf{T}}\tilde{X} + \bar{\boldsymbol{x}}(\boldsymbol{1}^{\mathsf{T}}\boldsymbol{1})\bar{\boldsymbol{x}}^{\mathsf{T}} \\ &= \tilde{X}^{\mathsf{T}}\tilde{X} - n\bar{\boldsymbol{x}}\bar{\boldsymbol{x}}^{\mathsf{T}} - n\bar{\boldsymbol{x}}\bar{\boldsymbol{x}}^{\mathsf{T}} + n\bar{\boldsymbol{x}}\bar{\boldsymbol{x}}^{\mathsf{T}} = \tilde{X}^{\mathsf{T}}\tilde{X} - n\bar{\boldsymbol{x}}\bar{\boldsymbol{x}}^{\mathsf{T}} \end{aligned}

and similarly

XcTyc=X~TyyˉX~T1xˉ1Ty+yˉxˉ1T1=X~Tynyˉxˉ.X_{\mathrm{c}}^{\mathsf{T}}\boldsymbol{y}_{\mathrm{c}} = \tilde{X}^{\mathsf{T}}\boldsymbol{y} - \bar{y}\tilde{X}^{\mathsf{T}}\boldsymbol{1} - \bar{\boldsymbol{x}}\boldsymbol{1}^{\mathsf{T}}\boldsymbol{y} + \bar{y}\bar{\boldsymbol{x}}\boldsymbol{1}^{\mathsf{T}}\boldsymbol{1} = \tilde{X}^{\mathsf{T}}\boldsymbol{y} - n\bar{y}\bar{\boldsymbol{x}} .

This gives the two asserted equations, and conversely the original block system can be recovered from them, so they are equivalent. In other words, one may first centre the data, solve a system in dd unknowns, and finally recover the intercept as b^=yˉxˉTu^\hat{b} = \bar{y}-\bar{\boldsymbol{x}}^{\mathsf{T}}\hat{\boldsymbol{u}}. For d=1d = 1 this is exactly the formula of Example 6.1.

Exercise 8.3Standard

Let λ>0\lambda > 0 and consider the ridge regression objective

Lλ(w)=yXw2+λw2.L_{\lambda}(\boldsymbol{w}) = \lVert \boldsymbol{y}-X\boldsymbol{w}\rVert^2 + \lambda\lVert\boldsymbol{w}\rVert^2 .

(1) Show that XTX+λIpX^{\mathsf{T}}X+\lambda I_p is invertible regardless of the rank of XX. (2) Show that LλL_{\lambda} has exactly one minimiser, given by w^λ=(XTX+λIp)1XTy\hat{\boldsymbol{w}}_{\lambda} = (X^{\mathsf{T}}X+\lambda I_p)^{-1}X^{\mathsf{T}}\boldsymbol{y}.

Solution

(1). For v0\boldsymbol{v}\ne\boldsymbol{0},

vT(XTX+λIp)v=Xv2+λv2λv2>0.\boldsymbol{v}^{\mathsf{T}}(X^{\mathsf{T}}X+\lambda I_p)\boldsymbol{v} = \lVert X\boldsymbol{v}\rVert^2 + \lambda\lVert\boldsymbol{v}\rVert^2 \ge \lambda\lVert\boldsymbol{v}\rVert^2 > 0 .

If there were a v0\boldsymbol{v}\ne\boldsymbol{0} with (XTX+λIp)v=0(X^{\mathsf{T}}X+\lambda I_p)\boldsymbol{v} = \boldsymbol{0}, the left-hand side would be 00, a contradiction. Hence the kernel is {0}\{\boldsymbol{0}\} and the matrix is invertible.

(2). Consider the augmented design matrix and observation vector

Xλ=(XλIp)R(n+p)×p,yλ=(y0)Rn+p.X_{\lambda} = \begin{pmatrix} X \\ \sqrt{\lambda}\,I_p\end{pmatrix}\in\mathbb{R}^{(n+p)\times p}, \qquad \boldsymbol{y}_{\lambda} = \begin{pmatrix} \boldsymbol{y} \\ \boldsymbol{0}\end{pmatrix}\in\mathbb{R}^{n+p} .

Computing the norm block by block,

yλXλw2=yXw2+λw2=Lλ(w),\lVert \boldsymbol{y}_{\lambda}-X_{\lambda}\boldsymbol{w}\rVert^2 = \lVert\boldsymbol{y}-X\boldsymbol{w}\rVert^2 + \lVert\sqrt{\lambda}\boldsymbol{w}\rVert^2 = L_{\lambda}(\boldsymbol{w}) ,

so ridge regression is an ordinary least squares problem for the augmented data. Since XλTXλ=XTX+λIpX_{\lambda}^{\mathsf{T}}X_{\lambda} = X^{\mathsf{T}}X+\lambda I_p and XλTyλ=XTyX_{\lambda}^{\mathsf{T}}\boldsymbol{y}_{\lambda} = X^{\mathsf{T}}\boldsymbol{y}, by Theorem 3.3 its normal equations are (XTX+λIp)w=XTy(X^{\mathsf{T}}X+\lambda I_p)\boldsymbol{w} = X^{\mathsf{T}}\boldsymbol{y}. By (1) and Corollary 5.2 there is exactly one solution, given by the stated formula. As long as λ>0\lambda > 0, the answer is uniquely determined even for rank-deficient data such as that of Example 5.3; this is the benefit of regularisation.

Exercise 8.4Hard

Let PRn×nP\in\mathbb{R}^{n\times n} be symmetric and idempotent (PT=PP^{\mathsf{T}} = P, P2=PP^2 = P). (1) Show that the eigenvalues of PP are 00 and 11 only. (2) Show that trP=rankP\operatorname{tr}P = \operatorname{rank}P. (3) Verify that this is consistent with trP=p\operatorname{tr}P = p for the hat matrix of Proposition 4.3.

Solution

(1). Suppose Pv=μvP\boldsymbol{v} = \mu\boldsymbol{v} with v0\boldsymbol{v}\ne\boldsymbol{0}. Multiplying both sides by PP gives P2v=μPv=μ2vP^2\boldsymbol{v} = \mu P\boldsymbol{v} = \mu^2\boldsymbol{v}, while P2=PP^2 = P makes the left-hand side Pv=μvP\boldsymbol{v} = \mu\boldsymbol{v}. Hence (μ2μ)v=0(\mu^2-\mu)\boldsymbol{v} = \boldsymbol{0}, and since v0\boldsymbol{v}\ne\boldsymbol{0} we get μ2=μ\mu^2 = \mu, that is, μ{0,1}\mu\in\{0,1\}.

(2). As PP is real symmetric, the spectral theorem gives an orthogonal matrix UU with P=UΛUTP = U\Lambda U^{\mathsf{T}} and Λ=diag(μ1,,μn)\Lambda = \operatorname{diag}(\mu_1,\dots,\mu_n). By (1) each μi\mu_i is 00 or 11. By the cyclic property of the trace, trP=tr(ΛUTU)=trΛ=#{i:μi=1}\operatorname{tr}P = \operatorname{tr}(\Lambda U^{\mathsf{T}}U) = \operatorname{tr}\Lambda = \#\{i : \mu_i = 1\}. On the other hand UU is invertible, so rankP=rankΛ\operatorname{rank}P = \operatorname{rank}\Lambda, which is also the number of ones. The two are therefore equal.

(3). The matrix P=X(XTX)1XTP = X(X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}} of Proposition 4.3 is symmetric idempotent by part 1 of that proposition, and its image is ImX\operatorname{Im}X by part 2, so rankP=dimImX=rankX=p\operatorname{rank}P = \dim\operatorname{Im}X = \operatorname{rank}X = p. Combined with (2) this gives trP=p\operatorname{tr}P = p, agreeing with the result obtained directly from cyclicity in part 4 of the proposition. In Example 4.5 we had n=3n = 3, p=2p = 2, eigenvalues 1,1,01, 1, 0 and trace 22.

Textbooks (linear algebra and geometry). G. Strang, Introduction to Linear Algebra, 5th ed., Wellesley–Cambridge Press, 2016 — Chapter 4 (orthogonality, projections, least squares). The standard geometric account of projection matrices and the normal equations. Kenichi Kanatani, Kore nara Wakaru Saitekika Sugaku: Kiso Genri kara Keisan Shuho made, Kyoritsu Shuppan, 2005 (in Japanese) — the chapters on least squares and the singular value decomposition; a well-organised introduction for Japanese readers.

Numerical computation. G. H. Golub and C. F. Van Loan, Matrix Computations, 4th ed., Johns Hopkins University Press, 2013 — Chapter 5 (orthogonalisation and least squares problems). The standard source for QR-based solutions and the discussion of condition numbers. Å. Björck, Numerical Methods for Least Squares Problems, SIAM, 1996 — a monograph covering numerical methods for least squares problems comprehensively.

Connections to statistics and machine learning. T. Hastie, R. Tibshirani, and J. Friedman, The Elements of Statistical Learning, 2nd ed., Springer, 2009 — Chapter 3 (linear methods for regression). Ridge regression, multicollinearity and variable selection are explained as extensions of the framework of this article. A PDF is made available by the authors. C. M. Bishop, Pattern Recognition and Machine Learning, Springer, 2006 — Chapter 3 (linear models for regression); a careful treatment of least squares reread as maximum likelihood.

History. S. M. Stigler, The History of Statistics: The Measurement of Uncertainty before 1900, Harvard University Press, 1986 — Part I. The Legendre–Gauss priority dispute and the process by which least squares came to be accepted are traced carefully from the sources.

Appendix: The pseudoinverse and the minimum-norm solution

Section titled “Appendix: The pseudoinverse and the minimum-norm solution”

Pinning down one answer even when the rank drops. As we saw in Example 5.3, when rankX<p\operatorname{rank}X < p there are infinitely many least squares solutions. A numerical library must return one of them. The standard choice is the one of smallest norm.

Proposition 8.5The minimum-norm least squares solution

Let XRn×pX\in\mathbb{R}^{n\times p} and yRn\boldsymbol{y}\in\mathbb{R}^n, and let SS be the set of least squares solutions. Then SIm(XT)S \cap \operatorname{Im}(X^{\mathsf{T}}) consists of a single point, and that element w^+\hat{\boldsymbol{w}}^{+} is the unique element of SS of smallest norm.

Proof(Proposition 8.5)

By Theorem 4.2 we have SS\ne\varnothing, and by Theorem 3.3 S=w0+KerXS = \boldsymbol{w}_0+\operatorname{Ker}X for any chosen w0S\boldsymbol{w}_0\in S. Applying Lemma 4.1 to XTX^{\mathsf{T}} gives (ImXT)=KerX(\operatorname{Im}X^{\mathsf{T}})^{\perp} = \operatorname{Ker}X, so we have the orthogonal decomposition

Rp=Im(XT)KerX.\mathbb{R}^p = \operatorname{Im}(X^{\mathsf{T}}) \oplus \operatorname{Ker}X .

Decomposing w0=u+z\boldsymbol{w}_0 = \boldsymbol{u}+\boldsymbol{z} accordingly, with uImXT\boldsymbol{u}\in\operatorname{Im}X^{\mathsf{T}} and zKerX\boldsymbol{z}\in\operatorname{Ker}X, we get u=w0zS\boldsymbol{u} = \boldsymbol{w}_0-\boldsymbol{z} \in S and uImXT\boldsymbol{u}\in\operatorname{Im}X^{\mathsf{T}}, so SIm(XT)S\cap\operatorname{Im}(X^{\mathsf{T}})\ne\varnothing. If two elements u,u\boldsymbol{u},\boldsymbol{u}' belong to this intersection, then uuKerX\boldsymbol{u}-\boldsymbol{u}'\in\operatorname{Ker}X (from the form of SS) and uuImXT\boldsymbol{u}-\boldsymbol{u}'\in\operatorname{Im}X^{\mathsf{T}}; the two summands of an orthogonal decomposition intersect in {0}\{\boldsymbol{0}\}, so u=u\boldsymbol{u} = \boldsymbol{u}' and the intersection is a single point, which we denote w^+\hat{\boldsymbol{w}}^{+}.

For minimality, every wS\boldsymbol{w}\in S can be written w=w^++v\boldsymbol{w} = \hat{\boldsymbol{w}}^{+}+\boldsymbol{v} with vKerX\boldsymbol{v}\in\operatorname{Ker}X, and w^+v\hat{\boldsymbol{w}}^{+}\perp\boldsymbol{v}, so the Pythagorean theorem gives

w2=w^+2+v2w^+2,\lVert\boldsymbol{w}\rVert^2 = \lVert\hat{\boldsymbol{w}}^{+}\rVert^2 + \lVert\boldsymbol{v}\rVert^2 \ge \lVert\hat{\boldsymbol{w}}^{+}\rVert^2 ,

with equality only when v=0\boldsymbol{v} = \boldsymbol{0}, that is, w=w^+\boldsymbol{w} = \hat{\boldsymbol{w}}^{+}.

The pseudoinverse. This w^+\hat{\boldsymbol{w}}^{+} can be written using the Moore–Penrose pseudoinverse built from the singular value decomposition X=UΣVTX = U\Sigma V^{\mathsf{T}}.

Definition 8.6The Moore–Penrose pseudoinverse

Let X=UΣVTX = U\Sigma V^{\mathsf{T}} be a singular value decomposition of XRn×pX\in\mathbb{R}^{n\times p}, where UU and VV are orthogonal and the diagonal entries of Σ\Sigma are the singular values σ1σr>0\sigma_1\ge\cdots\ge\sigma_r > 0 with all other entries 00. Let Σ+\Sigma^{+} be the matrix obtained by replacing the non-zero diagonal entries of Σ\Sigma by their reciprocals and transposing. Then

X+=VΣ+UTRp×nX^{+} = V\Sigma^{+}U^{\mathsf{T}} \in \mathbb{R}^{p\times n}

is called the pseudoinverse of XX. When rankX=p\operatorname{rank}X = p it agrees with X+=(XTX)1XTX^{+} = (X^{\mathsf{T}}X)^{-1}X^{\mathsf{T}}.

One has w^+=X+y\hat{\boldsymbol{w}}^{+} = X^{+}\boldsymbol{y}; this is checked by writing out, in terms of UU, Σ\Sigma and VV, that X+yX^{+}\boldsymbol{y} satisfies the normal equations and lies in ImXT\operatorname{Im}X^{\mathsf{T}} (see Chapter 5 of Golub–Van Loan for details). This is the solution returned by NumPy’s np.linalg.lstsq and np.linalg.pinv. For the solution set (0.5,1.52t,t)(0.5, 1.5-2t, t) of Example 5.3, the squared norm

0.25+(1.52t)2+t2=5t26t+2.50.25 + (1.5-2t)^2 + t^2 = 5t^2 - 6t + 2.5

is minimised at t=3/5=0.6t = 3/5 = 0.6, giving w^+=(0.5, 0.3, 0.6)T\hat{\boldsymbol{w}}^{+} = (0.5,\ 0.3,\ 0.6)^{\mathsf{T}}. Indeed this solution is orthogonal to the generator (0,2,1)T(0,2,-1)^{\mathsf{T}} of the kernel (20.30.6=02\cdot 0.3 - 0.6 = 0). Its squared norm is 0.70.7, smaller than the 2.52.5 of the solution (0.5,1.5,0)(0.5, 1.5, 0) at t=0t = 0.

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.