Math Core

Lesson 6.3 · Orthogonality and Least Squares

Orthogonal projections

Given a subspace WW and a vector y\mathbf{y} that is not in it, which vector in WW is closest to y\mathbf{y}? The answer is the orthogonal projection of y\mathbf{y} onto WW: drop a perpendicular from y\mathbf{y} to WW. This one idea powers least-squares data fitting, the Gram-Schmidt process, and much of approximation theory.

Projecting onto a line

Start with a line L=Span⁡{u}L = \operatorname{Span}\{\mathbf{u}\}, where u≠0\mathbf{u} \neq \mathbf{0}. You want to split y\mathbf{y} into two pieces,

y=y^+z,\mathbf{y} = \hat{\mathbf{y}} + \mathbf{z},

with y^=αu\hat{\mathbf{y}} = \alpha\mathbf{u} on the line and z\mathbf{z} orthogonal to u\mathbf{u}. The condition z⋅u=0\mathbf{z} \cdot \mathbf{u} = 0 reads (y−αu)⋅u=0(\mathbf{y} - \alpha\mathbf{u}) \cdot \mathbf{u} = 0, so y⋅u=α(u⋅u)\mathbf{y} \cdot \mathbf{u} = \alpha(\mathbf{u} \cdot \mathbf{u}), which determines α\alpha.

Projection onto a line

The orthogonal projection of y\mathbf{y} onto L=Span⁡{u}L = \operatorname{Span}\{\mathbf{u}\} is

y^=proj⁡Ly=y⋅uu⋅u u.\hat{\mathbf{y}} = \operatorname{proj}_L \mathbf{y} = \frac{\mathbf{y} \cdot \mathbf{u}}{\mathbf{u} \cdot \mathbf{u}}\,\mathbf{u}.

The component of y\mathbf{y} orthogonal to u\mathbf{u} is z=y−y^\mathbf{z} = \mathbf{y} - \hat{\mathbf{y}}.

The projection depends only on the line, not on which nonzero u\mathbf{u} you pick: replacing u\mathbf{u} by cuc\mathbf{u} multiplies the fraction by 1c\tfrac{1}{c} and the vector by cc. Notice the fraction is exactly the weight formula from the previous lesson.

Worked example: Projection in the plane

Let y=(7,6)\mathbf{y} = (7, 6) and u=(4,2)\mathbf{u} = (4, 2). Find the projection of y\mathbf{y} onto Span⁡{u}\operatorname{Span}\{\mathbf{u}\}, write y\mathbf{y} as a sum of a vector on the line and a vector orthogonal to it, and find the distance from y\mathbf{y} to the line.

y⋅u=28+12=40\mathbf{y} \cdot \mathbf{u} = 28 + 12 = 40 and u⋅u=16+4=20\mathbf{u} \cdot \mathbf{u} = 16 + 4 = 20, so

y^=4020(4,2)=(8,4),z=y−y^=(−1,2).\hat{\mathbf{y}} = \frac{40}{20}(4, 2) = (8, 4), \qquad \mathbf{z} = \mathbf{y} - \hat{\mathbf{y}} = (-1, 2).

Check: z⋅u=−4+4=0\mathbf{z} \cdot \mathbf{u} = -4 + 4 = 0. So (7,6)=(8,4)+(−1,2)(7, 6) = (8, 4) + (-1, 2).

The closest point on the line to y\mathbf{y} is y^\hat{\mathbf{y}}, so the distance from y\mathbf{y} to the line is ∥z∥=1+4=5\|\mathbf{z}\| = \sqrt{1 + 4} = \sqrt{5}.

ŷ = (8, 4) is the foot of the perpendicular from y = (7, 6) to the line through u. The dashed segment z = (−1, 2) is orthogonal to the line.Open in grapher →

Projecting onto a subspace

The same splitting works for any subspace WW of Rn\mathbb{R}^n, as long as you have an orthogonal basis for it.

The orthogonal decomposition theorem

Let WW be a subspace of Rn\mathbb{R}^n. Every y\mathbf{y} in Rn\mathbb{R}^n can be written uniquely as

y=y^+z,y^ in W,z in W⊥.\mathbf{y} = \hat{\mathbf{y}} + \mathbf{z}, \qquad \hat{\mathbf{y}} \text{ in } W, \quad \mathbf{z} \text{ in } W^\perp.

If {u1,…,up}\{\mathbf{u}_1, \ldots, \mathbf{u}_p\} is any orthogonal basis of WW, then

y^=proj⁡Wy=y⋅u1u1⋅u1u1+⋯+y⋅upup⋅upup.\hat{\mathbf{y}} = \operatorname{proj}_W \mathbf{y} = \frac{\mathbf{y} \cdot \mathbf{u}_1}{\mathbf{u}_1 \cdot \mathbf{u}_1}\mathbf{u}_1 + \cdots + \frac{\mathbf{y} \cdot \mathbf{u}_p}{\mathbf{u}_p \cdot \mathbf{u}_p}\mathbf{u}_p.

In words: proj⁡Wy\operatorname{proj}_W \mathbf{y} is the sum of the projections of y\mathbf{y} onto the separate basis lines. To see that z=y−y^\mathbf{z} = \mathbf{y} - \hat{\mathbf{y}} is orthogonal to WW, dot it with u1\mathbf{u}_1. Orthogonality kills the cross terms, leaving y⋅u1−y⋅u1u1⋅u1(u1⋅u1)=0\mathbf{y} \cdot \mathbf{u}_1 - \dfrac{\mathbf{y} \cdot \mathbf{u}_1}{\mathbf{u}_1 \cdot \mathbf{u}_1}(\mathbf{u}_1 \cdot \mathbf{u}_1) = 0, and likewise for each uj\mathbf{u}_j.

Two quick consequences: if y\mathbf{y} is already in WW, then proj⁡Wy=y\operatorname{proj}_W \mathbf{y} = \mathbf{y} (this is the weight formula from the last lesson), and if y\mathbf{y} is in W⊥W^\perp, then proj⁡Wy=0\operatorname{proj}_W \mathbf{y} = \mathbf{0}.

Worked example: Projection onto a plane in R³

Let u1=(1,1,0)\mathbf{u}_1 = (1, 1, 0), u2=(1,−1,2)\mathbf{u}_2 = (1, -1, 2) and y=(3,1,5)\mathbf{y} = (3, 1, 5). Write y\mathbf{y} as a vector in W=Span⁡{u1,u2}W = \operatorname{Span}\{\mathbf{u}_1, \mathbf{u}_2\} plus a vector orthogonal to WW.

First check the basis is orthogonal: u1⋅u2=1−1+0=0\mathbf{u}_1 \cdot \mathbf{u}_2 = 1 - 1 + 0 = 0. Then

y^=3+12u1+3−1+106u2=2(1,1,0)+2(1,−1,2)=(4,0,4).\hat{\mathbf{y}} = \frac{3 + 1}{2}\mathbf{u}_1 + \frac{3 - 1 + 10}{6}\mathbf{u}_2 = 2(1, 1, 0) + 2(1, -1, 2) = (4, 0, 4).

So z=y−y^=(−1,1,1)\mathbf{z} = \mathbf{y} - \hat{\mathbf{y}} = (-1, 1, 1). Check: z⋅u1=−1+1=0\mathbf{z} \cdot \mathbf{u}_1 = -1 + 1 = 0 and z⋅u2=−1−1+2=0\mathbf{z} \cdot \mathbf{u}_2 = -1 - 1 + 2 = 0. The decomposition is

(3,1,5)=(4,0,4)+(−1,1,1).(3, 1, 5) = (4, 0, 4) + (-1, 1, 1).

Common mistake

The projection formula requires an orthogonal basis. If u1⋅u2≠0\mathbf{u}_1 \cdot \mathbf{u}_2 \neq 0, adding up the one-line projections gives the wrong answer, and y−y^\mathbf{y} - \hat{\mathbf{y}} won't be orthogonal to WW. Always check orthogonality first; if the basis isn't orthogonal, make it so with Gram-Schmidt (next lesson).

The best approximation theorem

Best approximation

Let WW be a subspace of Rn\mathbb{R}^n and y^=proj⁡Wy\hat{\mathbf{y}} = \operatorname{proj}_W \mathbf{y}. Then y^\hat{\mathbf{y}} is the point of WW closest to y\mathbf{y}:

∥y−y^∥<∥y−v∥for every v in W with v≠y^.\|\mathbf{y} - \hat{\mathbf{y}}\| < \|\mathbf{y} - \mathbf{v}\| \quad \text{for every } \mathbf{v} \text{ in } W \text{ with } \mathbf{v} \neq \hat{\mathbf{y}}.

The proof is the Pythagorean theorem. For v\mathbf{v} in WW, write y−v=(y−y^)+(y^−v)\mathbf{y} - \mathbf{v} = (\mathbf{y} - \hat{\mathbf{y}}) + (\hat{\mathbf{y}} - \mathbf{v}). The first piece is in W⊥W^\perp and the second is in WW, so they are orthogonal and

∥y−v∥2=∥y−y^∥2+∥y^−v∥2,\|\mathbf{y} - \mathbf{v}\|^2 = \|\mathbf{y} - \hat{\mathbf{y}}\|^2 + \|\hat{\mathbf{y}} - \mathbf{v}\|^2,

which is larger than ∥y−y^∥2\|\mathbf{y} - \hat{\mathbf{y}}\|^2 unless v=y^\mathbf{v} = \hat{\mathbf{y}}.

So the distance from y\mathbf{y} to WW is ∥y−y^∥=∥z∥\|\mathbf{y} - \hat{\mathbf{y}}\| = \|\mathbf{z}\|. In the plane example above, the distance from (3,1,5)(3, 1, 5) to WW is ∥(−1,1,1)∥=3\|(-1, 1, 1)\| = \sqrt{3}.

Projection matrices

With an orthonormal basis {u1,…,up}\{\mathbf{u}_1, \ldots, \mathbf{u}_p\} the denominators are all 11, so proj⁡Wy=(y⋅u1)u1+⋯+(y⋅up)up\operatorname{proj}_W \mathbf{y} = (\mathbf{y} \cdot \mathbf{u}_1)\mathbf{u}_1 + \cdots + (\mathbf{y} \cdot \mathbf{u}_p)\mathbf{u}_p. With U=[u1 ⋯ up]U = [\mathbf{u}_1 \ \cdots \ \mathbf{u}_p], the vector of weights is UTyU^T\mathbf{y}, and combining the columns with those weights gives:

Projection with orthonormal columns

If the columns of UU are an orthonormal basis of WW, then proj⁡Wy=UUTy\operatorname{proj}_W \mathbf{y} = UU^T\mathbf{y} for every y\mathbf{y}.

Worked example: A projection matrix

Let WW be the line spanned by u=(35,45)\mathbf{u} = \left(\tfrac{3}{5}, \tfrac{4}{5}\right), a unit vector. Find the matrix PP of proj⁡W\operatorname{proj}_W and use it to project y=(5,0)\mathbf{y} = (5, 0).

P=uuT=[3/54/5][3/54/5]=125[9121216],Py=125(45,60)=(95,125).P = \mathbf{u}\mathbf{u}^T = \begin{bmatrix} 3/5 \\ 4/5 \end{bmatrix}\begin{bmatrix} 3/5 & 4/5 \end{bmatrix} = \frac{1}{25}\begin{bmatrix} 9 & 12 \\ 12 & 16 \end{bmatrix}, \qquad P\mathbf{y} = \frac{1}{25}(45, 60) = \left(\tfrac{9}{5}, \tfrac{12}{5}\right).

Check with the line formula: y⋅u=3\mathbf{y} \cdot \mathbf{u} = 3, and 3u=(95,125)3\mathbf{u} = \left(\tfrac{9}{5}, \tfrac{12}{5}\right).

Tip

Every projection matrix satisfies P2=PP^2 = P (projecting twice changes nothing) and PT=PP^T = P. These are fast checks on a computed PP.

Practice

Practice 1

Find the orthogonal projection of y=(1,7)\mathbf{y} = (1, 7) onto the line spanned by u=(−4,2)\mathbf{u} = (-4, 2).

Enter a point like (2, -3)

Practice 2

Find the distance from y=(1,7)\mathbf{y} = (1, 7) to the line spanned by u=(−4,2)\mathbf{u} = (-4, 2). Give an exact answer.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

Let u=(1,0,1)\mathbf{u} = (1, 0, 1) and y=(2,−1,5)\mathbf{y} = (2, -1, 5). Find the component z\mathbf{z} of y\mathbf{y} orthogonal to u\mathbf{u}.

Enter a point like (2, -3)

Practice 4

Let u1=(1,2,2)\mathbf{u}_1 = (1, 2, 2), u2=(2,1,−2)\mathbf{u}_2 = (2, 1, -2), W=Span⁡{u1,u2}W = \operatorname{Span}\{\mathbf{u}_1, \mathbf{u}_2\} and y=(7,2,−1)\mathbf{y} = (7, 2, -1). Find the point of WW closest to y\mathbf{y}.

Enter a point like (2, -3)

Practice 5

With WW and y\mathbf{y} as in the previous problem, find the distance from y\mathbf{y} to WW.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

In R4\mathbb{R}^4, let u1=(1,1,1,1)\mathbf{u}_1 = (1, 1, 1, 1), u2=(1,−1,1,−1)\mathbf{u}_2 = (1, -1, 1, -1) and y=(3,1,−1,5)\mathbf{y} = (3, 1, -1, 5). Find the distance from y\mathbf{y} to W=Span⁡{u1,u2}W = \operatorname{Span}\{\mathbf{u}_1, \mathbf{u}_2\}.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

The columns of the 4×24 \times 2 matrix UU form an orthonormal basis for a subspace WW of R4\mathbb{R}^4. Which matrix PP satisfies Py=proj⁡WyP\mathbf{y} = \operatorname{proj}_W \mathbf{y} for every y\mathbf{y} in R4\mathbb{R}^4?