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 . The projection is the point where the error is orthogonal to :
The matrix is the projection matrix. It is symmetric and idempotent, : projecting twice changes nothing.
Projecting onto a subspace
For a subspace spanned by the independent columns of , the same condition (error orthogonal to every column) gives the normal equations:
If the columns of are orthonormal, and everything simplifies to , . That is a big reason to prefer orthonormal bases.
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 in the subspace, , and the two parts are orthogonal. Pythagoras then gives
so no point of the subspace is closer. Least squares regression is this statement with and the subspace equal to the column space of the data matrix (The Geometry of Least Squares).
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
np.linalg.lstsqreturns the coordinates of the projection of onto the column space.- PCA projects data onto the span of the top principal directions (PCA: Minimum Reconstruction Error).
- In signal processing, projecting onto sines and cosines gives Fourier coefficients (Orthogonal Projections and Fourier Series).
Exercises
Exercise 1: Project onto a line
Project onto the line spanned by .
Answer
.
Solution
, so . The error is orthogonal to .
Exercise 2: Projection matrix
Write the projection matrix onto the line spanned by and check that for its first row.
Answer
.
Solution
and . Entry of is , and entry is , matching .
Exercise 3: Orthonormal shortcut (code)
The columns and are orthonormal. Project onto their span using .
Answer
.
Solution
Coordinates are , so the projection is .
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 always satisfies…
The error of an orthogonal projection is…
- parallel to the subspace
- orthogonal to the subspace
- always zero
- equal to
If has orthonormal columns, the projection matrix onto their span is…