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.5. The Gram-Schmidt Process

This section can be thought of as a detour through the main storyline of the course. Chapter 7.1 is where I connect the idea of projecting yโƒ—\vec y onto the column space of XX to that of linear regression.

Instead, here Iโ€™ll introduce a new algorithm that can be used to turn a collection of vectors into a more convenient form, and see how this can make the act of projecting yโƒ—\vec y onto the column space of XX much easier.


Why Orthogonalize?ยถ

Recall, a set of vectors qโƒ—1,qโƒ—2,โ€ฆ,qโƒ—dโˆˆRn\vec q_1, \vec q_2, \ldots, \vec q_d \in \mathbb{R}^n are orthonormal if they are both:

  1. pairwise orthogonal, meaning qโƒ—iโ‹…qโƒ—j=0\vec q_i \cdot \vec q_j = 0 for all iโ‰ ji \neq j, and

  2. each vector is a unit vector, meaning โˆฅqโƒ—iโˆฅ=1\lVert \vec q_i \rVert = 1 for all ii

Orthonormal vectors (and matrices containing them) are convenient to work with. For example, if QQโ€™s columns are the vectors qโƒ—1,qโƒ—2,โ€ฆ,qโƒ—d\vec q_1, \vec q_2, \ldots, \vec q_d, then QTQQ^TQ, the matrix containing the dot products of the columns of QQ, is the identity matrix.

QTQ=[qโƒ—1โ‹…qโƒ—1qโƒ—1โ‹…qโƒ—2โ‹ฏqโƒ—1โ‹…qโƒ—dqโƒ—2โ‹…qโƒ—1qโƒ—2โ‹…qโƒ—2โ‹ฏqโƒ—2โ‹…qโƒ—dโ‹ฎโ‹ฎโ‹ฑโ‹ฎqโƒ—dโ‹…qโƒ—1qโƒ—dโ‹…qโƒ—2โ‹ฏqโƒ—dโ‹…qโƒ—d]=[10โ‹ฏ001โ‹ฏ0โ‹ฎโ‹ฎโ‹ฑโ‹ฎ00โ‹ฏ1]=IQ^TQ = \begin{bmatrix} \vec q_1 \cdot \vec q_1 & \vec q_1 \cdot \vec q_2 & \cdots & \vec q_1 \cdot \vec q_d \\ \vec q_2 \cdot \vec q_1 & \vec q_2 \cdot \vec q_2 & \cdots & \vec q_2 \cdot \vec q_d \\ \vdots & \vdots & \ddots & \vdots \\ \vec q_d \cdot \vec q_1 & \vec q_d \cdot \vec q_2 & \cdots & \vec q_d \cdot \vec q_d \end{bmatrix} = \begin{bmatrix} 1 & 0 & \cdots & 0 \\ 0 & 1 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & 1 \end{bmatrix} = I

(I know that I promised to use superscripts like qโƒ—(1)\vec q^{(1)} to denote columns of a matrix, but I have my reasons for doing it this way in this section.)

As weโ€™ve seen in Chapter 6.3, when projecting yโƒ—\vec y onto the column space of XX, the matrix XTXX^TX โ€“ and its inverse โ€“ plays a big role in finding the optimal coefficients wโƒ—โˆ—\vec w^* to multiply each column of XX by. Most matrices, by default, donโ€™t have orthonormal columns. But if they did, then some of these calculations would be much, much simpler!

So, the goal here is to learn how to turn a linearly independent set of vectors into an orthonormal set of vectors with the same span, i.e. how to โ€œorthogonalizeโ€ a set of vectors.

vโƒ—1,vโƒ—2,โ€ฆ,vโƒ—dlinearlyย independentย setย ofย vectorsโ†’qโƒ—1,qโƒ—2,โ€ฆ,qโƒ—dorthonormalย setย ofย vectorsย withย theย sameย span\underset{\text{linearly independent set of vectors}}{\vec v_1, \vec v_2, \ldots, \vec v_d} \to \underset{\text{orthonormal set of vectors with the same span}}{\vec q_1, \vec q_2, \ldots, \vec q_d}

The Algorithmยถ

The algorithm that produces this orthonomal set of vectors is called the Gram-Schmidt process. It exploits the fact that when you project yโƒ—\vec y onto xโƒ—\vec x, the error vector

eโƒ—=yโƒ—โˆ’pโƒ—\vec e = \vec y - \vec p

is orthogonal to xโƒ—\vec x.

Loading...
Loading...
Image produced in Jupyter

If you look in the figure above, the vectors xโƒ—\color{#3d81f6} \vec x and eโƒ—\color{#d81b60} \vec e are orthogonal, and have the same span as yโƒ—\color{orange} \vec y and xโƒ—\color{#3d81f6} \vec x. The key takeaway is that if youโ€™d like to โ€œinventโ€ vectors that are orthogonal to each other, you can construct them by iteratively projecting!

To illustrate how the algorithm works, letโ€™s use as an example the vectors

vโƒ—1=[1โˆ’11],vโƒ—2=[101],vโƒ—3=[112]\vec v_1 = \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}, \quad \vec v_2 = \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix}, \quad \vec v_3 = \begin{bmatrix} 1 \\ 1 \\ 2 \end{bmatrix}

These are three linearly independent vectors in R3\mathbb{R}^3, though they are not orthogonal. These vectors span some subspace SS. Our goal is to find an orthonormal set of vectors that spans the same SS. (In this case, SS is all of R3\mathbb{R}^3, but in general this process works even if d<nd < n.)

In what follows, let projxโƒ—(yโƒ—)\text{proj}_{\vec x}(\vec y) be the projection of yโƒ—\vec y onto xโƒ—\vec x, i.e. projxโƒ—(yโƒ—)=yโƒ—โ‹…xโƒ—xโƒ—โ‹…xโƒ—xโƒ—\text{proj}_{\vec x}(\vec y) = \frac{\vec y \cdot \vec x}{\vec x \cdot \vec x} \vec x.

  • Iteration 1: Set Qโƒ—1=vโƒ—1{\color{3d81f6} \vec Q_1} = \vec v_1.

    In the first iteration, we simply take the first vector vโƒ—1\vec v_1 and copy it to Qโƒ—1\vec Q_1. From now on, each new vector will be constructed to be orthogonal to all previously constructed Qโƒ—i\vec Q_iโ€™s.

    Qโƒ—1=[1โˆ’11]\color{3d81f6} \vec Q_1 = \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}
  • Iteration 2: Set Qโƒ—2=vโƒ—2โˆ’projQโƒ—1(vโƒ—2){\color{orange} \vec Q_2} = \vec v_2 - \text{proj}_{\color{3d81f6}\vec Q_1}(\vec v_2).

    Qโƒ—2\color{orange} \vec Q_2 is the same as the error vector from projecting vโƒ—2\vec v_2 onto Qโƒ—1\color{3d81f6} \vec Q_1, which we know is orthogonal to Qโƒ—1\color{3d81f6} \vec Q_1.

    Even without actually calculating Qโƒ—2\color{orange} \vec Q_2, we can see that it, by definition, must be orthogonal to Qโƒ—1\color{3d81f6} \vec Q_1.

    Qโƒ—2โ‹…Qโƒ—1=(vโƒ—2โˆ’projQโƒ—1(vโƒ—2))โ‹…Qโƒ—1=(vโƒ—2โˆ’vโƒ—2โ‹…Qโƒ—1Qโƒ—1โ‹…Qโƒ—1Qโƒ—1)โ‹…Qโƒ—1=vโƒ—2โ‹…Qโƒ—1โˆ’vโƒ—2โ‹…Qโƒ—1Qโƒ—1โ‹…Qโƒ—1Qโƒ—1โ‹…Qโƒ—1=vโƒ—2โ‹…Qโƒ—1โˆ’vโƒ—2โ‹…Qโƒ—1=0\begin{align*} {\color{orange} \vec Q_2} \cdot {\color{3d81f6} \vec Q_1} &= \left(\vec v_2 - \text{proj}_{\color{3d81f6}\vec Q_1}(\vec v_2)\right) \cdot {\color{3d81f6} \vec Q_1} \\ &= \left( \vec v_2- \frac{\vec v_2 \cdot {\color{3d81f6} \vec Q_1}}{{\color{3d81f6} \vec Q_1} \cdot {\color{3d81f6} \vec Q_1}} {\color{3d81f6} \vec Q_1} \right) \cdot {\color{3d81f6} \vec Q_1} \\ &= \vec v_2 \cdot {\color{3d81f6} \vec Q_1} - \frac{\vec v_2 \cdot {\color{3d81f6} \vec Q_1}}{{\color{3d81f6} \vec Q_1} \cdot {\color{3d81f6} \vec Q_1}} {\color{3d81f6} \vec Q_1} \cdot {\color{3d81f6} \vec Q_1} \\ &= \vec v_2 \cdot {\color{3d81f6} \vec Q_1} - \vec v_2 \cdot {\color{3d81f6} \vec Q_1} \\ &= 0 \end{align*}

    With that understanding in mind, letโ€™s evaluate Qโƒ—2\color{orange} \vec Q_2 explicitly.

    Qโƒ—2=[101]โŸvโƒ—2โˆ’[101]โ‹…[1โˆ’11][1โˆ’11]โ‹…[1โˆ’11][1โˆ’11]โŸprojQโƒ—1(vโƒ—2)=[101]โˆ’23[1โˆ’11]=[1/32/31/3]{\color{orange} \vec Q_2} = \underbrace{\begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix}}_{\vec v_2} - \underbrace{\frac{\begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix} \cdot \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}}{\color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix} \cdot \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}} \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}}_{\text{proj}_{\color{3d81f6}\vec Q_1}(\vec v_2)} = \begin{bmatrix} 1 \\ 0 \\ 1 \end{bmatrix} - \frac{2}{3} \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix} = \color{orange} \begin{bmatrix} 1/3 \\ 2/3 \\ 1/3 \end{bmatrix}
  • Iteration 3: Set Qโƒ—3=vโƒ—3โˆ’projQโƒ—1(vโƒ—3)โˆ’projQโƒ—2(vโƒ—3){\color{d81b60} \vec Q_3} = \vec v_3 - \text{proj}_{\color{3d81f6}\vec Q_1}(\vec v_3) - \text{proj}_{\color{orange}\vec Q_2}(\vec v_3).

    When constructed this way, Qโƒ—3\color{d81b60} \vec Q_3 is orthogonal to both Qโƒ—1\color{3d81f6} \vec Q_1 and Qโƒ—2\color{orange} \vec Q_2. Think of it this way: span({Qโƒ—1,Qโƒ—2})\text{span}(\{{\color{3d81f6} \vec Q_1}, {\color{orange} \vec Q_2}\}) is a plane in R3\mathbb{R}^3; after projecting vโƒ—3\vec v_3 onto this plane, the remaining part of vโƒ—3\vec v_3 that is orthogonal to the plane is Qโƒ—3\color{d81b60} \vec Q_3. If that doesnโ€™t make sense, execute Qโƒ—3โ‹…Qโƒ—1{\color{d81b60} \vec Q_3} \cdot \color{3d81f6} \vec Q_1 and Qโƒ—3โ‹…Qโƒ—2{\color{d81b60} \vec Q_3} \cdot \color{orange} \vec Q_2 using the same general steps I followed in Iteration 2.

Qโƒ—3=vโƒ—3โˆ’projQโƒ—1(vโƒ—3)โˆ’projQโƒ—2(vโƒ—3)=[112]โˆ’[112]โ‹…[1โˆ’11][1โˆ’11]โ‹…[1โˆ’11][1โˆ’11]โŸprojQโƒ—1(vโƒ—3)โˆ’[112]โ‹…[1/32/31/3][1/32/31/3]โ‹…[1/32/31/3][1/32/31/3]โŸprojQโƒ—2(vโƒ—3)=[โˆ’1/201/2]\begin{align*} {\color{d81b60} \vec Q_3} &= \vec v_3 - \text{proj}_{\color{3d81f6}\vec Q_1}(\vec v_3) - \text{proj}_{\color{orange}\vec Q_2}(\vec v_3) \\ &= \begin{bmatrix} 1 \\ 1 \\ 2 \end{bmatrix} - \underbrace{\frac{\begin{bmatrix} 1 \\ 1 \\ 2 \end{bmatrix} \cdot \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}}{\color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix} \cdot \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}} \color{3d81f6} \begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}}_{\text{proj}_{\color{3d81f6}\vec Q_1}(\vec v_3)} - \underbrace{\frac{\begin{bmatrix} 1 \\ 1 \\ 2 \end{bmatrix} \cdot \color{orange} \begin{bmatrix} 1/3 \\ 2/3 \\ 1/3 \end{bmatrix}}{\color{orange} \begin{bmatrix} 1/3 \\ 2/3 \\ 1/3 \end{bmatrix} \cdot \color{orange} \begin{bmatrix} 1/3 \\ 2/3 \\ 1/3 \end{bmatrix}} \color{orange} \begin{bmatrix} 1/3 \\ 2/3 \\ 1/3 \end{bmatrix}}_{\text{proj}_{\color{orange}\vec Q_2}(\vec v_3)} \\ &= \color{d81b60} \begin{bmatrix} -1/2 \\ 0 \\ 1/2 \end{bmatrix} \end{align*}

If there were more vโƒ—i\vec v_iโ€™s, weโ€™d continue this process, each time constructing a new Qโƒ—i\vec Q_i that is orthogonal to all previously constructed Qโƒ—i\vec Q_iโ€™s by โ€œsubtracting offโ€ the parts weโ€™ve already accounted for through the earlier Qโƒ—i\vec Q_iโ€™s.

Now, Qโƒ—1,Qโƒ—2,Qโƒ—3\vec Q_1, \vec Q_2, \vec Q_3 are orthogonal to one another, but they are not yet unit vectors. To make them unit vectors, we simply need to divide each by its length.

qโƒ—1=Qโƒ—1โˆฅQโƒ—1โˆฅ=[1/3โˆ’1/31/3]qโƒ—2=Qโƒ—2โˆฅQโƒ—2โˆฅ=[1/62/61/6]qโƒ—3=Qโƒ—3โˆฅQโƒ—3โˆฅ=[โˆ’1/201/2]{\color{3d81f6} \vec q_1 = \frac{\color{3d81f6} \vec Q_1}{\lVert \color{3d81f6} \vec Q_1 \rVert} = \boxed{\begin{bmatrix} 1 /\sqrt{3} \\ -1 /\sqrt{3} \\ 1 /\sqrt{3} \end{bmatrix}}} \\ {\color{orange} \vec q_2 = \frac{\color{orange} \vec Q_2}{\lVert \color{orange} \vec Q_2 \rVert} = \boxed{\begin{bmatrix} 1 /\sqrt{6} \\ 2 /\sqrt{6} \\ 1 /\sqrt{6} \end{bmatrix}}} \\ \color{d81b60} \vec q_3 = \frac{\color{d81b60} \vec Q_3}{\lVert \color{d81b60} \vec Q_3 \rVert} = \boxed{\begin{bmatrix} -1 /\sqrt{2} \\ 0 \\ 1 /\sqrt{2} \end{bmatrix}}

Now, the vectors qโƒ—1,qโƒ—2,qโƒ—3\vec q_1, \vec q_2, \vec q_3 are orthonormal to one another, and they span the same subspace SS as the vectors vโƒ—1,vโƒ—2,vโƒ—3\vec v_1, \vec v_2, \vec v_3!

A quick note on signs: you could multiply any of the qโƒ—i\vec q_iโ€™s by -1 without changing the span of the collection, so if using a computer to compute the qโƒ—i\vec q_iโ€™s, you might see them come out with different signs.

Loading...

Notice above that vโƒ—1\color{3d81f6} \vec v_1 in the top and qโƒ—1\color{3d81f6} \vec q_1 in the bottom are parallel, itโ€™s just that qโƒ—1\color{3d81f6} \vec q_1 is a unit vector. The three vโƒ—i\vec v_iโ€™s in the top figure are linearly independent but not orthogonal; the three qโƒ—i\vec q_iโ€™s in the bottom figure are, however, orthonormal.


Gram-Schmidt and Projectionยถ

In general, weโ€™re presumed to have a vector yโƒ—\vec y that weโ€™d like to approximate as a linear combination of matrix XXโ€™s columns. Generally, XXโ€™s columns are not orthonormal โ€“ they may not even be linearly independent.

If we:

  1. Remove the linearly dependent columns from XX

  2. Use the Gram-Schmidt process to orthonormalize the remaining columns, and store them in the columns of QQ

Then, the matrix QQ that results has the same column space as XX, but solving the normal equations for QQ and yโƒ—\vec y is much simpler.

Our problem now is slightly different: weโ€™re trying to find the best linear combination of the columns of QQ (not the columns of XX) that approximates yโƒ—\vec y, i.e. weโ€™re projecting yโƒ—\vec y onto the column space of QQ. If we adjust our objective to this goal, then the best wโƒ—โˆ—\vec w^* โ€“ the one that minimizes โˆฅyโƒ—โˆ’Qwโƒ—โˆฅ2\lVert \vec y - Q \vec w \rVert^2 โ€“ satisfies

QTQwโƒ—=QTyโƒ—Q^TQ \vec w = Q^T \vec y

But, QTQ=IQ^TQ = I as I showed you at the start of this section, so this just reduces to

wโƒ—โˆ—=QTyโƒ—\vec w^* = Q^T \vec y

Thatโ€™s it! No inversion required: we can compute wโƒ—โˆ—\vec w^* with just a single matrix-vector multiplication.