TExcelTAD
Home › Math for Machine Learning › Orthogonal Projections

Orthogonal Projections

To compress data, approximate a signal or fit a model, we often replace a vector by the closest point in a subspace. That closest point is the orthogonal projection: drop a perpendicular onto the subspace. Projections are the geometry behind least squares, PCA and Gram-Schmidt.

Projecting onto a line

Let the line be spanned by b\mathbf{b}. The projection π(x)=λb\pi(\mathbf{x}) = \lambda\mathbf{b} is the point where the error x−λb\mathbf{x} - \lambda\mathbf{b} is orthogonal to b\mathbf{b}:

⟨x−λb,b⟩=0  ⟹  λ=bTxbTb,π(x)=bbTbTb x\langle \mathbf{x} - \lambda\mathbf{b}, \mathbf{b} \rangle = 0 \implies \lambda = \frac{\mathbf{b}^T\mathbf{x}}{\mathbf{b}^T\mathbf{b}}, \qquad \pi(\mathbf{x}) = \frac{\mathbf{b}\mathbf{b}^T}{\mathbf{b}^T\mathbf{b}}\,\mathbf{x}

The matrix P=bbT/(bTb)P = \mathbf{b}\mathbf{b}^T / (\mathbf{b}^T\mathbf{b}) is the projection matrix. It is symmetric and idempotent, P2=PP^2 = P: projecting twice changes nothing.

Projecting onto a subspace

For a subspace spanned by the independent columns of B∈RD×mB \in \mathbb{R}^{D \times m}, the same condition (error orthogonal to every column) gives the normal equations:

BT(x−Bλ)=0  ⟹  λ=(BTB)−1BTx,P=B(BTB)−1BTB^T(\mathbf{x} - B\boldsymbol{\lambda}) = \mathbf{0} \implies \boldsymbol{\lambda} = (B^TB)^{-1}B^T\mathbf{x}, \qquad P = B(B^TB)^{-1}B^T

If the columns of BB are orthonormal, BTB=IB^TB = I and everything simplifies to λ=BTx\boldsymbol{\lambda} = B^T\mathbf{x}, P=BBTP = BB^T. That is a big reason to prefer orthonormal bases.

python · try it yourself
import numpy as np

B = np.array([[1.0, 0.0],
              [1.0, 1.0],
              [1.0, 2.0]])               # spans a plane in R^3
x = np.array([6.0, 0.0, 0.0])
lam = np.linalg.solve(B.T @ B, B.T @ x)
p = B @ lam
P = B @ np.linalg.inv(B.T @ B) @ B.T
print("coordinates lambda:", np.round(lam, 4))
print("projection:", np.round(p, 4), " error:", np.round(x - p, 4))
print("error orthogonal to the plane:", np.allclose(B.T @ (x - p), 0))
print("P symmetric:", np.allclose(P, P.T), " P^2 = P:", np.allclose(P @ P, P))
coordinates lambda: [ 5. -3.]
projection: [ 5.  2. -1.]  error: [ 1. -2.  1.]
error orthogonal to the plane: True
P symmetric: True  P^2 = P: True

The projection is the closest point

For any u\mathbf{u} in the subspace, x−u=(x−π(x))+(π(x)−u)\mathbf{x} - \mathbf{u} = (\mathbf{x} - \pi(\mathbf{x})) + (\pi(\mathbf{x}) - \mathbf{u}), and the two parts are orthogonal. Pythagoras then gives

∥x−u∥2=∥x−π(x)∥2+∥π(x)−u∥2≥∥x−π(x)∥2\lVert \mathbf{x} - \mathbf{u} \rVert^2 = \lVert \mathbf{x} - \pi(\mathbf{x}) \rVert^2 + \lVert \pi(\mathbf{x}) - \mathbf{u} \rVert^2 \ge \lVert \mathbf{x} - \pi(\mathbf{x}) \rVert^2

so no point of the subspace is closer. Least squares regression is this statement with x=y\mathbf{x} = \mathbf{y} and the subspace equal to the column space of the data matrix (The Geometry of Least Squares).

python · try it yourself
import numpy as np

B = np.array([[1.0, 0.0], [1.0, 1.0], [1.0, 2.0]])
x = np.array([6.0, 0.0, 0.0])
p = B @ np.linalg.solve(B.T @ B, B.T @ x)
rng = np.random.default_rng(0)
others = [np.linalg.norm(x - B @ rng.normal(size=2) * 3) for _ in range(10000)]
print("distance to projection:", round(float(np.linalg.norm(x - p)), 4))
print("closest random point in the plane:", round(min(others), 4))
distance to projection: 2.4495
closest random point in the plane: 2.4514

Gram-Schmidt in one line of thought

Gram-Schmidt builds an orthonormal basis by taking each new vector, subtracting its projection onto the span of the previous ones, and normalizing what is left. The leftover is orthogonal by construction.

In software

Exercises

Exercise 1: Project onto a line

Project x=(2,3)\mathbf{x} = (2, 3) onto the line spanned by b=(1,1)\mathbf{b} = (1, 1).

Answer

(2.5,2.5)(2.5, 2.5).

Solution

λ=2+31+1=2.5\lambda = \frac{2 + 3}{1 + 1} = 2.5, so π(x)=2.5(1,1)\pi(\mathbf{x}) = 2.5(1, 1). The error (−0.5,0.5)(-0.5, 0.5) is orthogonal to (1,1)(1, 1).

Exercise 2: Projection matrix

Write the projection matrix onto the line spanned by (1,2)(1, 2) and check that P2=PP^2 = P for its first row.

Answer

P=15[1224]P = \frac{1}{5}\begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}.

Solution

bbT=[1224]\mathbf{b}\mathbf{b}^T = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix} and bTb=5\mathbf{b}^T\mathbf{b} = 5. Entry (1,1)(1,1) of P2P^2 is 125(1⋅1+2⋅2)=525=15\frac{1}{25}(1 \cdot 1 + 2 \cdot 2) = \frac{5}{25} = \frac{1}{5}, and entry (1,2)(1,2) is 125(1⋅2+2⋅4)=25\frac{1}{25}(1 \cdot 2 + 2 \cdot 4) = \frac{2}{5}, matching PP.

Exercise 3: Orthonormal shortcut (code)

The columns 12(1,0,1)\frac{1}{\sqrt{2}}(1, 0, 1) and (0,1,0)(0, 1, 0) are orthonormal. Project (4,2,0)(4, 2, 0) onto their span using P=BBTP = BB^T.

Answer

(2,2,2)(2, 2, 2).

Solution

Coordinates are BTx=(4/2,2)B^T\mathbf{x} = (4/\sqrt{2}, 2), so the projection is 42⋅12(1,0,1)+2(0,1,0)=(2,2,2)\frac{4}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}(1, 0, 1) + 2(0, 1, 0) = (2, 2, 2).

python · try it yourself
import numpy as np
B = np.column_stack([np.array([1, 0, 1]) / np.sqrt(2), [0, 1, 0]])
print(np.round(B @ B.T @ np.array([4.0, 2.0, 0.0]), 10))
[2. 2. 2.]

Test yourself

A projection matrix PP always satisfies…

  1. P2=PP^2 = P
  2. P2=IP^2 = I
  3. P=−PTP = -P^T
  4. det⁡P=1\det P = 1

The error x−π(x)\mathbf{x} - \pi(\mathbf{x}) of an orthogonal projection is…

  1. parallel to the subspace
  2. orthogonal to the subspace
  3. always zero
  4. equal to x\mathbf{x}

If BB has orthonormal columns, the projection matrix onto their span is…

  1. BTBB^TB
  2. BBTBB^T
  3. B−1B^{-1}
  4. II