---
title: Projections
heading: The closest point in a subspace
eyebrow: A7 · Lesson 25 · Projections
xp: 40
---

::: hook
A sensor signal carries 60 Hz hum from the mains. You know the hum lives in a two-dimensional space, combinations of $\cos(2\pi \cdot 60t)$ and $\sin(2\pi \cdot 60t)$, but you don't know its size or phase. Remove the part of the signal that lies in that space and the hum is gone, whatever its amplitude. Subtracting a signal's average to remove its DC offset is the same operation in one dimension. Both are **projections**: finding the closest point in a subspace, and the leftover is perpendicular to it.
:::

::: play
- The dashed line is the direction of $\mathbf{a}$. The vector $\mathbf{b} = (1, 3)$ is fixed.
- The green arrow is the projection $\mathbf{p}$ of $\mathbf{b}$ onto the line, the closest point on it. The red arrow is the error $\mathbf{e} = \mathbf{b} - \mathbf{p}$.
- Turn the line. The error always meets the line at a right angle, and the readout of $\mathbf{e}\cdot\mathbf{a}$ stays at zero.
:::
:::

```sim plot
view: complex
range: 4
slider: th = 0..180 step 1 = 30 °
curve: u*(1∠th) | over: u = -4..4 | dashed | label: line through a
arrow: 1 + 3*j | label: b
arrow: (cos(th°) + 3*sin(th°))*(1∠th) | color: ch3 | label: p
arrow: (1 + 3*j) - (cos(th°) + 3*sin(th°))*(1∠th) | from: (cos(th°) + 3*sin(th°))*(1∠th) | color: ch2 | label: e
readout: e · a = (1 - (cos(th°) + 3*sin(th°))*cos(th°))*cos(th°) + (3 - (cos(th°) + 3*sin(th°))*sin(th°))*sin(th°) | digits: 3
readout: |e| = abs(-sin(th°) + 3*cos(th°)) | digits: 3
```

::: predict
## You project $\mathbf{b} = (1, 2, 6)$ onto the line through $(1, 1, 1)$. What is the projection?

- [x] $(3, 3, 3)$, the average of the entries in each slot
- [ ] $(1, 2, 6)$
- [ ] $(9, 9, 9)$
- [ ] $(1, 1, 1)$

::: reveal
$(3, 3, 3)$. The coefficient is $\frac{\mathbf{a}^T\mathbf{b}}{\mathbf{a}^T\mathbf{a}} = \frac{9}{3} = 3$. Projecting onto the constant vector gives the mean in every slot, and the error $(-2, -1, 3)$ is the signal with its DC removed. Its entries add to zero: it's perpendicular to $(1, 1, 1)$.
:::
:::

::: nameit
## Onto a line

The projection of $\mathbf{b}$ onto the line through $\mathbf{a}$ is

$$\mathbf{p} = \frac{\mathbf{a}^T\mathbf{b}}{\mathbf{a}^T\mathbf{a}}\,\mathbf{a}$$

The error $\mathbf{e} = \mathbf{b} - \mathbf{p}$ is perpendicular to $\mathbf{a}$, which is what makes $\mathbf{p}$ the closest point. The **projection matrix** $P = \frac{\mathbf{a}\mathbf{a}^T}{\mathbf{a}^T\mathbf{a}}$ does it in one multiplication: $\mathbf{p} = P\mathbf{b}$.

## Onto a subspace

To project onto the column space of $A$ (independent columns), require the error to be perpendicular to every column: $A^T(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0}$. That gives the **normal equations**

$$A^TA\,\hat{\mathbf{x}} = A^T\mathbf{b} \qquad \mathbf{p} = A\hat{\mathbf{x}} \qquad P = A(A^TA)^{-1}A^T$$

If the columns are orthonormal ($A = Q$), $Q^TQ = I$ and everything simplifies: $P = QQ^T$, and each coordinate is a dot product.

## Properties

- $P^2 = P$: projecting twice changes nothing.
- $P^T = P$: projection matrices are symmetric.
- $I - P$ projects onto the orthogonal complement, giving the error directly.
- Pythagoras: $|\mathbf{b}|^2 = |\mathbf{p}|^2 + |\mathbf{e}|^2$.

## Hum removal

Sample a signal at $N$ times, making a vector $\mathbf{b}$. Put the samples of $\cos 2\pi f_0t$ and $\sin 2\pi f_0t$ in the two columns of $A$. Then $(I - P)\mathbf{b}$ is the signal with every trace of $f_0$ removed, at any amplitude and phase. Over a whole number of cycles the two columns are orthogonal (A6, lesson 2), so the projection is just two dot products.
:::

::: example
## Project $\mathbf{b} = (1, 2, 2)$ onto $\mathbf{a} = (1, 1, 1)$, and check the error.

::: solution
1. **The two dot products.** $\mathbf{a}^T\mathbf{b} = 5$ and $\mathbf{a}^T\mathbf{a} = 3$.
2. **The projection.**
   $$\mathbf{p} = \frac{5}{3}(1, 1, 1)$$
   the mean in every slot.
3. **The error.**
   $$\begin{aligned} \mathbf{e} &= (1, 2, 2) - \tfrac{5}{3}(1, 1, 1) \\ &= \left(-\tfrac{2}{3}, \tfrac{1}{3}, \tfrac{1}{3}\right) \end{aligned}$$
4. **Check.** $\mathbf{e}\cdot\mathbf{a} = 0$.

**Answer:** $\mathbf{p} = \frac53(1, 1, 1)$, and the error $\left(-\frac23, \frac13, \frac13\right)$ is perpendicular to $\mathbf{a}$.
:::
:::

::: example
## Find the projection matrix onto the line through $(1, 2)$, and check $P^2 = P$.

::: solution
1. **The projection matrix.**
   $$P = \frac{1}{5}\begin{bmatrix}1 & 2\\2 & 4\end{bmatrix}$$
2. **Square it.**
   $$\begin{aligned} P^2 &= \frac{1}{25}\begin{bmatrix}1 + 4 & 2 + 8\\2 + 8 & 4 + 16\end{bmatrix} \\ &= \frac{1}{5}\begin{bmatrix}1 & 2\\2 & 4\end{bmatrix} \\ &= P \end{aligned}$$

**Answer:** $P = \frac15\begin{bmatrix}1 & 2\\2 & 4\end{bmatrix}$, and $P^2 = P$.
:::
:::

::: example
## Project $\mathbf{b} = (1, 0, 0)$ onto the plane $x + y + z = 0$.

::: solution
1. **Part along the normal.** It's easiest to remove the part along the normal $\mathbf{n} = (1, 1, 1)$:
   $$\frac{\mathbf{b}\cdot\mathbf{n}}{\mathbf{n}\cdot\mathbf{n}}\mathbf{n} = \frac13(1, 1, 1)$$
2. **What's left is the projection.**
   $$\begin{aligned} \mathbf{p} &= (1, 0, 0) - \tfrac13(1, 1, 1) \\ &= \left(\tfrac23, -\tfrac13, -\tfrac13\right) \end{aligned}$$
   Its entries sum to zero, so it's in the plane.
3. **Distance to the plane.**
   $$\begin{aligned} |\tfrac13(1, 1, 1)| &= \frac{1}{\sqrt3} \\ &= 0.577 \end{aligned}$$
4. **Where this shows up.** Removing the average from a signal is exactly this projection.

**Answer:** $\mathbf{p} = \left(\frac23, -\frac13, -\frac13\right)$, and $\mathbf{b}$ is 0.577 from the plane.
:::
:::

::: check
given: a1 = 1 | 2; a2 = 2 | -1; b1 = 3 | 4; b2 = 1 | 5
answer: (a1*b1 + a2*b2)/(a1^2 + a2^2)
tolerance: 0.5%

## Project $\mathbf{b} = (\given{b1}, \given{b2})$ onto the line through $\mathbf{a} = (\given{a1}, \given{a2})$. The projection is $t\mathbf{a}$. What is $t$?

::: right
Right. $t = \frac{\mathbf{a}^T\mathbf{b}}{\mathbf{a}^T\mathbf{a}}$ = {answer}.
:::

::: misses
- [a1*b1 + a2*b2] Divide by $\mathbf{a}^T\mathbf{a}$.
- [(a1*b1 + a2*b2)/(b1^2 + b2^2)] Divide by the length squared of $\mathbf{a}$, the direction you project onto.
- [any] Not yet. $\frac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{a}\cdot\mathbf{a}}$.
:::
:::

::: check
given: a1 = 1 | 3; a2 = 2 | 4
answer: a1^2/(a1^2 + a2^2)
tolerance: 0.5%

## What is the top-left entry of the matrix that projects onto the line through $(\given{a1}, \given{a2})$?

::: right
Right. $P = \frac{\mathbf{a}\mathbf{a}^T}{\mathbf{a}^T\mathbf{a}}$, so $P_{11} = \frac{a_1^2}{a_1^2 + a_2^2}$ = {answer}.
:::

::: misses
- [a1^2] Divide by $\mathbf{a}^T\mathbf{a}$.
- [a1*a2/(a1^2 + a2^2)] That's the off-diagonal entry.
- [any] Not yet. Compute $\mathbf{a}\mathbf{a}^T$ and divide.
:::
:::

::: check
given: b1 = 4 | 7; b2 = 1 | 2; b3 = 2 | 6
answer: b1 - (b1 + b2 + b3)/3
tolerance: 0.5%

## The samples $(\given{b1}, \given{b2}, \given{b3})$ are projected onto $(1, 1, 1)$ and the projection is subtracted. What is the first entry of what remains?

::: right
Right. The projection puts the mean in every slot; subtracting leaves {answer}.
:::

::: misses
- [(b1 + b2 + b3)/3] That's the mean itself, the projection.
- [b1 - (b1 + b2 + b3)] Divide the sum by 3 to get the projection's entries.
- [any] Not yet. Subtract the mean.
:::
:::

::: check
given: b1 = 3 | 5; b2 = 1 | -2
answer: abs(b1 - b2)/sqrt(2)
tolerance: 0.5%

## Project $(\given{b1}, \given{b2})$ onto the line through $(1, 1)$. How long is the error vector?

::: right
Right. The error is $\frac{b_1 - b_2}{2}(1, -1)$, of length $\frac{|b_1 - b_2|}{\sqrt 2}$ = {answer}.
:::

::: misses
- [abs(b1 - b2)/2] That's each component's size; the length includes both.
- [abs(b1 + b2)/sqrt(2)] That's the projection's length.
- [any] Not yet. $\mathbf{e} = \mathbf{b} - \mathbf{p}$, then its length.
:::
:::

::: check
kind: choice

## $P$ is a projection matrix. What is $P^2$?

- [x] $P$
- [ ] $I$
- [ ] $0$
- [ ] $2P$

::: right
Right. A projected vector is already in the subspace, so projecting it again leaves it where it is.
:::

::: misses
- [2] That would mean $P$ undoes itself, like a reflection.
- [any] What happens if you project something that's already in the subspace?
:::
:::

::: check
given: n = 100 | 1000; k = 2 | 3
answer: n - k

## A signal of {n} samples is projected onto a {k}-dimensional hum subspace and the projection subtracted. In how many dimensions can what remains still vary?

::: right
Right. $I - P$ projects onto the orthogonal complement, of dimension $n - k$ = {answer}.
:::

::: misses
- [k] That's the dimension of the subspace that was removed.
- [any] Not yet. Dimensions of complements add to $n$.
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 3 | 4

## Project $(\given{a}, \given{b})$ onto the line through $(1, 1)$.

::: part
answer: (a + b)/2
tolerance: 0.5%

### What is the projection's $x$ coordinate?

::: misses
- [a + b] Divide by $\mathbf{a}\cdot\mathbf{a} = 2$.
- [any] $\frac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{a}\cdot\mathbf{a}}\mathbf{a}$.
:::
:::

::: part
answer: (b - a)/sqrt(2)
tolerance: 0.5%

### How far is the point from the line?

::: misses
- [b - a] Use the length of the error, $\frac{b - a}{2}(-1, 1)$.
- [any] The length of $\mathbf{b} - \mathbf{p}$.
:::
:::
:::

::: check
kind: derivation
vars: x = -3..3, y = -3..3, z = -3..3

## Project $(x, y, z)$ onto $(1, 1, 1)$.

::: step
answer: (x + y + z)/3

### What is the coefficient $\frac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{a}\cdot\mathbf{a}}$?
:::

::: step
answer: x - (x + y + z)/3

### What is the first entry of the error $\mathbf{b} - \mathbf{p}$?
:::

::: right
Right. Projecting onto $(1, 1, 1)$ gives the average; the error is each entry minus the average.
:::
:::

::: explain
## Why is the projection the closest point in the subspace, and why is the error perpendicular to it?

::: model
Take any other point $\mathbf{q}$ in the subspace. Then $\mathbf{b} - \mathbf{q} = (\mathbf{b} - \mathbf{p}) + (\mathbf{p} - \mathbf{q})$, where $\mathbf{p} - \mathbf{q}$ lies in the subspace. If the error $\mathbf{b} - \mathbf{p}$ is perpendicular to the subspace, Pythagoras gives $|\mathbf{b} - \mathbf{q}|^2 = |\mathbf{b} - \mathbf{p}|^2 + |\mathbf{p} - \mathbf{q}|^2$, which is smallest when $\mathbf{q} = \mathbf{p}$. So choosing $\mathbf{p}$ to make the error perpendicular is exactly what makes it the closest point, and that condition is the normal equations.
:::
:::

::: practice

::: check
given: b1 = 2 | 6; b2 = 4 | 1; b3 = 3 | 8
answer: (b1 + b2 + b3)/3
tolerance: 0.5%

## Project $(\given{b1}, \given{b2}, \given{b3})$ onto $(1, 1, 1)$. What is each entry of the projection?

::: misses
- [b1 + b2 + b3] Divide by $\mathbf{a}^T\mathbf{a} = 3$.
- [any] The mean.
:::
:::

::: check
given: a1 = 1 | 2; a2 = 3 | 1
answer: a1*a2/(a1^2 + a2^2)
tolerance: 0.5%

## What is the top-right entry of the projection matrix onto the line through $(\given{a1}, \given{a2})$?

::: misses
- [any] $\frac{a_1a_2}{a_1^2 + a_2^2}$.
:::
:::

::: check
given: b = 10 | 13; p = 6 | 5
answer: sqrt(b^2 - p^2)
tolerance: 0.5%

## A vector of length {b} has a projection of length {p} onto a subspace. How long is the error?

::: misses
- [b - p] Lengths don't subtract; squares do (Pythagoras).
- [any] $\sqrt{|\mathbf{b}|^2 - |\mathbf{p}|^2}$.
:::
:::
:::
