Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

6.4. The Complete Solution to the Normal Equations

In Chapter 6.3, we explored the idea of projecting a vector onto the column space of a matrix. If X\color{#3d81f6} X has linearly independent columns, then the vector in colsp(X)\text{colsp}({\color{#3d81f6} X}) that is closest to yβƒ—\color{orange} \vec y is the vector Xwβƒ—βˆ—{\color{#3d81f6} X} \vec w^*, where wβƒ—βˆ—\vec w^* is the unique solution to the normal equation,

XTXwβƒ—βˆ—=XTyβƒ—{\color{#3d81f6} X^T} {\color{#3d81f6} X} \vec w^* = {\color{#3d81f6} X^T} {\color{orange} \vec y}

When X\color{#3d81f6} X has linearly independent columns, then XTX{\color{#3d81f6} X^T} {\color{#3d81f6} X} is invertible, so we can solve for wβƒ—βˆ—\vec w^* uniquely.

wβƒ—βˆ—=(XTX)βˆ’1XTyβƒ—\vec w^* = ({\color{#3d81f6} X^T} {\color{#3d81f6} X})^{-1} {\color{#3d81f6} X^T} {\color{orange} \vec y}

What if X\color{#3d81f6} X’s Columns are Linearly Dependent?ΒΆ

In the case where X\color{#3d81f6} X’s columns are linearly dependent, we can’t invert XTX\color{#3d81f6} X^T \color{#3d81f6} X to solve for wβƒ—βˆ—\vec w^*. This means that

XTXw⃗=XTy⃗{\color{#3d81f6} X^T} {\color{#3d81f6} X} \vec w = {\color{#3d81f6} X^T} {\color{orange} \vec y}

has infinitely many solutions. Let’s give more thought to what these solutions actually are.

First, note that all of these solutions for wβƒ—βˆ—\vec w^* correspond to the same projection, pβƒ—=Xwβƒ—βˆ—\vec p = X \vec w^*. The β€œbest approximation” of yβƒ—\color{orange} \vec y in colsp(X)\text{colsp}({\color{#3d81f6}X}) is always just one vector; if there are infinitely many wβƒ—βˆ—\vec w^*'s, that just means there are infinitely many ways of describing that one best approximation. Remember, if vectors are linearly independent, then any of their linear combinations can only be expressed in one way; if they are linearly dependent, then their linear combinations can be expressed in infinitely many ways.

In other words, if X\color{#3d81f6} X has linearly dependent columns, then there are infinitely many wβƒ—βˆ—\vec w^*'s that satisfy the normal equation, but they all correspond to the same projection pβƒ—=Xwβƒ—βˆ—\vec p = {\color{#3d81f6} X} \vec w^* in the figure below.

Loading...

Let me drive this point home further. Let’s suppose both wβƒ—1\vec w_1 and wβƒ—2\vec w_2 satisfy

XTXw⃗=XTy⃗{\color{#3d81f6} X^T} {\color{#3d81f6} X} \vec w = {\color{#3d81f6} X^T} {\color{orange} \vec y}

Then,

XTXwβƒ—1βˆ’XTXwβƒ—2=yβƒ—βˆ’yβƒ—=0βƒ—X^TX \vec w_1 - X^TX \vec w_2 = \vec y - \vec y = \vec 0

which means that

(XTX)(wβƒ—1βˆ’wβƒ—2)=0βƒ—(X^TX)(\vec w_1 - \vec w_2) = \vec 0

i.e. the difference between the two vectors, wβƒ—1βˆ’wβƒ—2\vec w_1 - \vec w_2, is in nullsp(XTX)\text{nullsp}(X^TX). But, back in Chapter 5.3, we proved that XTXX^TX and XX have the same null space, meaning any vector that gets sent to 0βƒ—\vec 0 by XX also gets sent to 0βƒ—\vec 0 by XTXX^TX, and vice versa.

So,

X(wβƒ—1βˆ’wβƒ—2)=0βƒ—X(\vec w_1 - \vec w_2) = \vec 0

too, but that just means

Xw⃗1=Xw⃗2X \vec w_1 = X \vec w_2

meaning that even though wβƒ—1\vec w_1 and wβƒ—2\vec w_2 are different-looking coefficient vectors, they both still correspond to the same linear combination of XX’s columns!

Let’s see how we can apply this to an example. Let X=[310621310]{\color{#3d81f6} X = \begin{bmatrix} 3 & 1 & 0 \\ 6 & 2 & 1 \\ 3 & 1 & 0 \end{bmatrix}} and yβƒ—=[213]{\color{orange} \vec y = \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix}}. This is an example of a matrix with linearly dependent columns, so there’s no unique wβƒ—βˆ—\vec w^* that satisfies the normal equations.

Finding One SolutionΒΆ

One way to find a possible vector wβƒ—βˆ—\vec w^* is to solve the normal equations. XTX\color{#3d81f6} X^T \color{#3d81f6} X is not invertible, so we can’t solve for wβƒ—βˆ—\vec w^* uniquely, but we can still try and find a solution.

Here’s one approach: let’s just toss out the linearly dependent columns of X\color{#3d81f6} X and solve for wβƒ—βˆ—\vec w^* using the remaining columns. Then, wβƒ—βˆ—\vec w^* for the full X\color{#3d81f6} X can use the same coefficients for the linearly independent columns, but 0s for the dependent ones. Removing the linearly dependent columns does not change colsp(X)\text{colsp}({\color{#3d81f6} X}) (i.e. the set of all linear combinations of X\color{#3d81f6} X’s columns), so the projection is the same.

The easy solution is to keep columns 2 and 3, since their numbers are smallest. So, for now, let’s say

X′=[102110],y⃗=[213]X' = \begin{bmatrix} 1 & 0 \\ 2 & 1 \\ 1 & 0 \end{bmatrix}, \quad \color{orange} \vec y = \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix}

Here, wβƒ—β€²=(Xβ€²TXβ€²)βˆ’1Xβ€²Tyβƒ—=[5/2βˆ’4]\vec w' = (X'^T X')^{-1} X'^T {\color{orange} \vec y} = \begin{bmatrix} 5/2 \\ -4 \end{bmatrix}. I won’t bore you with the calculations; you can verify them yourself.

Now, one possible wβƒ—βˆ—\vec w^* for the full X\color{#3d81f6} X is [05/2βˆ’4]\begin{bmatrix} 0 \\ 5/2 \\ -4\end{bmatrix}, which keeps the same coefficients on columns 2 and 3 as in wβƒ—β€²\vec w', but 0 for the column we didn’t use.

Finding All SolutionsΒΆ

As I mentioned above, if there are infinitely many solutions to the normal equation, then the difference between any two solutions is in nullsp(XTX)\text{nullsp}({\color{#3d81f6} X^TX}), which is also nullsp(X)\text{nullsp}({\color{#3d81f6} X}). Put another way, if wβƒ—s\vec w_s satisfies the normal equations, then so does wβƒ—s+nβƒ—\vec w_s + \vec n for any nβƒ—βˆˆnullsp(XTX)\vec n \in \text{nullsp}({\color{#3d81f6} X^TX}).

XTXwβƒ—s=XTyβƒ—XTX(wβƒ—s+nβƒ—)=XTXwβƒ—s+XTXnβƒ—βŸ0βƒ—,Β byΒ definitionΒ ofΒ nullΒ space=XTyβƒ—+0βƒ—\begin{align*} {\color{#3d81f6} X^T X} \vec w_s &= {\color{#3d81f6} X^T} \color{orange} \vec y \\ {\color{#3d81f6} X^T X} (\vec w_s + \vec n) &= {\color{#3d81f6} X^T X} \vec w_s + \underbrace{{\color{#3d81f6} X^T X} \vec n}_{\vec 0, \text{ by definition of null space}} &= {\color{#3d81f6} X^T} {\color{orange} \vec y} + \vec 0 \\ \end{align*}

So, once we have one wβƒ—βˆ—\vec w^*, to get the rest, just add any vector in nullsp(XTX)\text{nullsp}({\color{#3d81f6} X^TX}) or nullsp(X)\text{nullsp}({\color{#3d81f6} X}) (since those are the same subspaces).

What is nullsp(X)\text{nullsp}({\color{#3d81f6} X})? It’s the set of vectors vβƒ—\vec v such that Xvβƒ—=0βƒ—{\color{#3d81f6} X} \vec v = \vec 0.

In our particular example,

X=[310621310]{\color{#3d81f6} X = \begin{bmatrix} 3 & 1 & 0 \\ 6 & 2 & 1 \\ 3 & 1 & 0 \end{bmatrix}}

we see that rank(X)=2\text{rank}({\color{#3d81f6} X}) = 2, so nullsp(X)\text{nullsp}({\color{#3d81f6} X}) has a dimension of 3βˆ’2=13 - 2 = 1 (by the rank-nullity theorem), so it’s going to be the span of a single vector. All we need to do now is find one vector in nullsp(X)\text{nullsp}({\color{#3d81f6} X}), and we will know that the null space is the set of scalar multiples of that vector.

Since column 1 is three times column 2, the vector vβƒ—=[1βˆ’30]\vec v = \begin{bmatrix} 1 \\ -3 \\ 0 \end{bmatrix} must be in nullsp(X)\text{nullsp}({\color{#3d81f6} X}).

X[1βˆ’30]=[310621310][1βˆ’30]=[000]{\color{#3d81f6} X} \begin{bmatrix} 1 \\ -3 \\ 0 \end{bmatrix} = {\color{#3d81f6}\begin{bmatrix} 3 & 1 & 0 \\ 6 & 2 & 1 \\ 3 & 1 & 0 \end{bmatrix}}\begin{bmatrix} 1 \\ -3 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}

So, since nullsp(X)=nullsp(XTX)=span({[1βˆ’30]})\text{nullsp}({\color{#3d81f6} X}) = \text{nullsp}({\color{#3d81f6} X^TX}) = \text{span}\left(\left\{\begin{bmatrix} 1 \\ -3 \\ 0 \end{bmatrix}\right\}\right), we know that the set of all possible wβƒ—βˆ—\vec w^*'s is

[05/2βˆ’4]+t[1βˆ’30],t∈R⏟thereΒ areΒ infinitelyΒ manyΒ solutionsΒ toΒ theΒ normalΒ equations,Β butΒ they’reΒ allΒ ofΒ thisΒ form\underbrace{\begin{bmatrix} 0 \\ 5/2 \\ -4 \end{bmatrix} + t\begin{bmatrix} 1 \\ -3 \\ 0 \end{bmatrix}, t \in \mathbb{R}}_\text{there are infinitely many solutions to the normal equations, but they're all of this form}

This is not a subspace, since it doesn’t contain the zero vector.


There’s another way to arrive at this set of possible wβƒ—βˆ—\vec w^*'s: we can solve the normal equations directly. I wouldn’t recommend this second approach since it’s much longer, but I’ll add it here for completeness.

XTX=[363121010][310621310]=[541861862621]XTy⃗=[363121010][213]=[2171]\begin{align*} {\color{#3d81f6}X^TX}&={\color{#3d81f6}\begin{bmatrix} 3 & 6 & 3 \\ 1 & 2 & 1 \\ 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} 3 & 1 & 0 \\ 6 & 2 & 1 \\ 3 & 1 & 0 \end{bmatrix}}={\color{#3d81f6}\begin{bmatrix}54 & 18 & 6 \\ 18 & 6 & 2 \\ 6 & 2 & 1 \end{bmatrix}} \\ \\ {\color{#3d81f6}X^T}{\color{orange}\vec y}&={\color{#3d81f6}\begin{bmatrix} 3 & 6 & 3 \\ 1 & 2 & 1 \\ 0 & 1 & 0 \end{bmatrix}}{\color{orange}\begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix}}=\begin{bmatrix} 21 \\ 7 \\ 1 \end{bmatrix} \end{align*}

Then, the normal equations XTXwβƒ—βˆ—=XTyβƒ—{\color{#3d81f6}X^TX}\vec w^*={\color{#3d81f6}X^T}\color{orange}\vec y give us

54w1βˆ—+18w2βˆ—+6w3βˆ—=2118w1βˆ—+6w2βˆ—+2w3βˆ—=76w1βˆ—+2w2βˆ—+w3βˆ—=1\begin{align*} 54w^*_1 + 18w^*_2 + 6w^*_3 &= 21 \\18w^*_1 + 6w^*_2 + 2w^*_3&=7 \\6w^*_1 + 2w^*_2 + w^*_3 &= 1 \end{align*}

The first and second equations are just scalar multiples of each other, so we can disregard one of them, and solve for a form where we can use one unknown as a parameter for the other two. To illustrate, let’s pick w1βˆ—=tw_1^* = t.

18t+6w2βˆ—+2w3βˆ—=76t+2w2βˆ—+w3βˆ—=1\begin{align*} 18t + 6w^*_2 + 2w^*_3&=7 \tag{2} \\6t + 2w^*_2 + w^*_3 &= 1 \tag{3} \end{align*}

(2)βˆ’3β‹…(3)(2) - 3 \cdot (3) gives us w3βˆ—=βˆ’4w_3^* = -4. Plugging this into both equations gives us

18t+6w2βˆ—βˆ’8=7β€…β€ŠβŸΉβ€…β€Š18t+6w2βˆ—=156t+2w2βˆ—βˆ’4=1β€…β€ŠβŸΉβ€…β€Š6t+2w2βˆ—=5\begin{align*} 18t + 6w_2^* - 8 &= 7 \implies 18t + 6w_2^* &= 15 \\6t + 2w_2^* - 4 &= 1 \implies 6t + 2w_2^* &= 5 \end{align*}

These are now both the same equation; the first one is just 3 times the second. So, we can solve for w2βˆ—w_2^* in terms of tt:

w2βˆ—=5βˆ’6t2w_2^* = \frac{5-6t}{2}

which gives us the complete solution

wβƒ—βˆ—=[t5βˆ’6t2βˆ’4],t∈R\vec w^* = \begin{bmatrix} t \\ \frac{5-6t}{2} \\ -4 \end{bmatrix}, t \in \mathbb{R}

This is the exact same line as using the null space approach! Plug in t=0t = 0 to get [05/2βˆ’4]\begin{bmatrix} 0 \\ 5/2 \\ -4 \end{bmatrix}, for example. The set of all possible wβƒ—βˆ—\vec w^*'s is not a subspace, since it doesn’t contain the zero vector.


The Projection MatrixΒΆ

So far, we’ve established that the vector in colsp(X)\text{colsp}({\color{#3d81f6} X}) that is closest to yβƒ—\color{orange} \vec y is the vector Xwβƒ—βˆ—{\color{#3d81f6} X} \vec w^*, where wβƒ—βˆ—\vec w^* is the solution to the normal equations,

XTXwβƒ—βˆ—=XTyβƒ—{\color{#3d81f6} X^T X} \vec w^* = {\color{#3d81f6} X^T} \color{orange} \vec y

If XTXX^TX is invertible, then wβƒ—βˆ—\vec w^* is the unique vector

wβƒ—βˆ—=(XTX)βˆ’1XTyβƒ—\vec w^* = ({\color{#3d81f6} X^T X})^{-1} \color{#3d81f6} X^T \color{orange} \vec y

meaning that the vector in colsp(X)\text{colsp}({\color{#3d81f6} X}) that is closest to y⃗\color{orange} \vec y is

pβƒ—=Xwβƒ—βˆ—=X(XTX)βˆ’1XTyβƒ—\vec p = {\color{#3d81f6} X} \vec w^* = {\color{#3d81f6} X} ({\color{#3d81f6} X^T X})^{-1} \color{#3d81f6} X^T \color{orange} \vec y

You’ll notice that the above expression also looks like a linear transformation applied to yβƒ—\color{orange} \vec y, where yβƒ—\color{orange} \vec y is being multiplied by the matrix

P=X(XTX)βˆ’1XTP = {\color{#3d81f6} X} ({\color{#3d81f6} X^T X})^{-1} \color{#3d81f6} X^T

The matrix PP is called the projection matrix. In other classes, it is called the β€œhat matrix”, because they might use w^\hat{\mathbf{w}} instead of wβƒ—βˆ—\vec w^* and y^\hat{\mathbf{y}} instead of pβƒ—\vec p, and in that notation, y^=Py\hat{\mathbf{y}} = P \mathbf{y}, so PP puts a β€œhat” on y\mathbf{y}. (I don’t use hat notation in this class because drawing a hat on top of a vector is awkward. Doesn’t wβƒ—^\hat{\vec w} look strange?)

So,

pβƒ—=Xwβƒ—βˆ—=Pyβƒ—\vec p = {\color{#3d81f6} X} \vec w^* = P \color{orange} \vec y

shows us that there are two ways to interpret the act of projecting y⃗\color{orange} \vec y onto colsp(X)\text{colsp}({\color{#3d81f6} X}):

  1. The resulting vector is some optimal linear combination of X{\color{#3d81f6} X}'s columns.

  2. The resulting vector is the result of applying the linear transformation PP to y⃗\color{orange} \vec y.

Let’s work out an example. Suppose

X=[30015460],y⃗=[123]{\color{#3d81f6} X = \begin{bmatrix} {\color{#3d81f6} 3} & {\color{#3d81f6} 0} \\ {\color{#3d81f6} 0} & {\color{#3d81f6} 154} \\ {\color{#3d81f6} 6} & {\color{#3d81f6} 0} \end{bmatrix}}, \quad {\color{orange} \vec y = \begin{bmatrix} {\color{orange} 1} \\ {\color{orange} 2} \\ {\color{orange} 3} \end{bmatrix}}

X\color{#3d81f6} X’s columns are linearly independent, so XTX\color{#3d81f6} X^T X is invertible, and

P=X(XTX)βˆ’1XTP = {\color{#3d81f6} X} ({\color{#3d81f6} X^T X})^{-1} \color{#3d81f6} X^T

is well-defined.

array([[0.2, 0. , 0.4], [0. , 1. , 0. ], [0.4, 0. , 0.8]])
array([1.4, 2. , 2.8])

P=[0.200.40100.400.8]P = \begin{bmatrix} 0.2 & 0 & 0.4 \\ 0 & 1 & 0 \\ 0.4 & 0 & 0.8 \end{bmatrix} contains the information we need to project yβƒ—\color{orange} \vec y onto colsp(X)\text{colsp}({\color{#3d81f6} X}). Each row of PP tells us the right mixture of yβƒ—\color{orange} \vec y’s components we need to construct the projection.

Notice that PP’s second row is [010]T\begin{bmatrix} 0 & 1 & 0 \end{bmatrix}^T. This came from the fact that X\color{#3d81f6} X’s first column had a second component of 0 while its second column had a non-zero second component but zeros in the other two components, meaning that we can scale X\color{#3d81f6} X’s second column to exactly match yβƒ—\color{orange} \vec y’s second component. Change the 154 in X\color{#3d81f6} X to any other non-zero value and PP won’t change!

Additionally, if we consider some yβƒ—\color{orange} \vec y that is already in colsp(X)\text{colsp}({\color{#3d81f6} X}), then multiplying it by PP doesn’t change it! For example, if we set yβƒ—=[31546]\color{orange} \vec y = \begin{bmatrix} 3 \\ 154 \\ 6 \end{bmatrix} (the sum of X\color{#3d81f6} X’s columns), then Pyβƒ—=yβƒ—P {\color{orange} \vec y} = {\color{orange} \vec y}.

array([ 3., 154., 6.])

Let’s work through some examples that develop our intuition for PP.

Example: Is PP invertible?ΒΆ

Suppose P=X(XTX)βˆ’1XTP = {\color{#3d81f6} X} ({\color{#3d81f6} X^T X})^{-1} \color{#3d81f6} X^T exists, meaning XTX\color{#3d81f6} X^T X is invertible. Is PP invertible? If so, what is its inverse?

Example: Is PP orthogonal?ΒΆ

Is PP orthogonal?

Example: Is PP symmetric?ΒΆ

Is PP symmetric?

Example: Is PP idempotent?ΒΆ

Recall, an idemponent matrix AA satisfies A2=AA^2 = A. Is PP idempotent?

Example: What is PXPX, and why?ΒΆ

What is PXPX? What does the result mean?

Example: Rotations, Reflections, and ProjectionsΒΆ

Suppose AA is an arbitrary n×dn \times d matrix. Describe the conditions on AA that make the corresponding linear transformation f(x⃗)=Ax⃗f(\vec x) = A \vec x a...

  1. Rotation

  2. Reflection

  3. Projection

To summarize:

TransformationProperties
RotationOrthogonal (ATA=IA^T A = I), determinant = 1
ReflectionOrthogonal (ATA=IA^T A = I), symmetric (AT=AA^T = A) if strict reflection, determinant = -1
ProjectionIdempotent (A2=AA^2 = A), symmetric (AT=AA^T = A)

SummaryΒΆ

Let’s take a step back and walk through our logic from Chapter 6.3 and here in Chapter 6.4 once more, since it’s that important.

Suppose X{\color{#3d81f6} X} is an n×dn \times d matrix and y⃗\color{orange} \vec y is some vector in Rn\mathbb{R}^n.

Orthogonal ProjectionsΒΆ

  1. Our goal is to find the linear combination of X\color{#3d81f6} X’s columns that is closest to yβƒ—\color{orange} \vec y.

  2. This boils down to finding the vector wβƒ—\vec w that minimizes βˆ₯yβƒ—βˆ’Xwβƒ—βˆ₯2\lVert {\color{orange} \vec y} - {\color{#3d81f6}X } \vec w \rVert^2.

  3. The vector wβƒ—βˆ—\vec w^* that minimizes βˆ₯yβƒ—βˆ’Xwβƒ—βˆ₯2\lVert {\color{orange} \vec y} - {\color{#3d81f6}X } \vec w \rVert^2 makes the resulting error vector,

    eβƒ—=yβƒ—βˆ’Xwβƒ—βˆ—{\color{#d81a60} \vec e} = {\color{orange} \vec y} - {\color{#3d81f6} X} \vec w^*

    orthogonal to the columns of X\color{#3d81f6} X.

  4. The wβƒ—βˆ—\vec w^* that makes the error vector orthogonal to the columns of X\color{#3d81f6} X is the one that satisfies the normal equation,

    XTXwβƒ—βˆ—=XTyβƒ—{\color{#3d81f6} X^T} {\color{#3d81f6} X} \vec w^* = {\color{#3d81f6} X^T} {\color{orange} \vec y}
  5. If XTX{\color{#3d81f6} X^T} {\color{#3d81f6} X} is invertible, which happens if and only if X\color{#3d81f6}X’s columns are linearly independent, then wβƒ—βˆ—\vec w^* is the unique vector

    wβƒ—βˆ—=(XTX)βˆ’1XTyβƒ—\vec w^* = ({\color{#3d81f6} X^T} {\color{#3d81f6} X})^{-1} {\color{#3d81f6} X^T} \color{orange} \vec y

    Otherwise, there are infinitely many solutions to the normal equation. All of these infinitely many solutions correspond to the same projection, pβƒ—=Xwβƒ—βˆ—\vec p = {\color{#3d81f6} X} \vec w^*. If wβƒ—β€²\vec w' is one solution (which can be found by removing the linearly dependent columns of X\color{#3d81f6} X), then all other solutions are of the form wβƒ—β€²+nβƒ—\vec w' + \vec n, where nβƒ—\vec n is any vector in nullsp(X)=nullsp(XTX)\text{nullsp}({\color{#3d81f6} X}) = \text{nullsp}({\color{#3d81f6} X^TX}).

The Projection MatrixΒΆ

Assuming X\color{#3d81f6} X has linearly independent columns, the projection matrix is

P=X(XTX)βˆ’1XTP = {\color{#3d81f6} X} ({\color{#3d81f6} X^T X})^{-1} \color{#3d81f6} X^T

PP is defined such that Py⃗P \color{orange} \vec y is the vector in colsp(X)\text{colsp}({\color{#3d81f6} X}) that is closest to y⃗\color{orange} \vec y. PP is symmetric and idemponent, but not invertible nor orthogonal.

We’re now finally ready to head back to the land of machine learning.