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

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. This is because if vectors are linearly dependent, then their linear combinations aren’t unique, but if they are linearly independent, their linear combinations are unique.

Let me drive this point home further. Let’s suppose both w1\vec w_1 and w2\vec w_2 satisfy

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

Then,

XTXw1XTXw2=yy=0X^TX \vec w_1 - X^TX \vec w_2 = \vec y - \vec y = \vec 0

which means that

(XTX)(w1w2)=0(X^TX)(\vec w_1 - \vec w_2) = \vec 0

i.e. the difference between the two vectors, w1w2\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(w1w2)=0X(\vec w_1 - \vec w_2) = \vec 0

too, but that just means

Xw1=Xw2X \vec w_1 = X \vec w_2

meaning that even though w1\vec w_1 and w2\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 try and find a parameterized solution.

I’m going to take a different approach here: instead, 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=(XTX)1XTy=[5/24]\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/24]\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 ws\vec w_s satisfies the normal equations, then so does ws+n\vec w_s + \vec n for any nnullsp(XTX)\vec n \in \text{nullsp}({\color{#3d81f6} X^TX}).

XTXws=XTyXTX(ws+n)=XTXws+XTXn0, 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 spaces).

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 32=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.

X[130]=[310621310][130]=[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({[130]})\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/24]+t[130],tRthere 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+6w28=7    18t+6w2=156t+2w24=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 w2w_2^* in terms of tt:

w2=56t2w_2^* = \frac{5-6t}{2}

which gives us the complete solution

w=[t56t24],tR\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/24]\begin{bmatrix} 0 \\ 5/2 \\ -4 \end{bmatrix}, for example. Again, this is not a subspace, since it doesn’t contain the zero vector.

We now address the linearly dependent case and derive the complete solution.


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.

X = np.array([[3, 0], 
              [0, 154], 
              [6, 0]])

P = X @ np.linalg.inv(X.T @ X) @ X.T
P
array([[0.2, 0. , 0.4], [0. , 1. , 0. ], [0.4, 0. , 0.8]])
P @ np.array([1, 2, 3])
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=yP {\color{orange} \vec y} = {\color{orange} \vec y}.

P @ np.array([3, 154, 6])
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)=Axf(\vec x) = A \vec x a...

  1. Rotation

  2. Reflection

  3. Projection


Summary

Let’s take a step back and walk through our logic from this section 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 yXw2\lVert {\color{orange} \vec y} - {\color{#3d81f6}X } \vec w \rVert^2.

  3. The vector w\vec w^* that minimizes yXw2\lVert {\color{orange} \vec y} - {\color{#3d81f6}X } \vec w \rVert^2 makes the resulting error vector, e=yXw{\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 PyP \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.