BRAGA

02 Writing02-02 Notes02-02-204

Projection of x onto general subspace U

Published 16 Apr 20267 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
πU(x)=B(BTB)−1BTx\pi_U(x) = B(B^T B)^{-1} B^T x
(1)

Deep Breakdown

What it tells me (Situation Awareness): πU(x)=B(BTB)−1BTx\pi_U(x) = B(B^TB)^{-1}B^Tx projects a vector xx onto an arbitrary subspace UU spanned by the columns of BB (a basis matrix). The result is the closest point in UU to xx. This generalizes the 1D formula: when BB is a single column bb, it reduces to bbTbTbx\frac{bb^T}{b^Tb}x. When BB is an orthonormal matrix (BTB=IB^TB = I), it simplifies to BBTxBB^Tx. This is the core of orthogonal projection theory and appears directly in ordinary least squares regression (OLS), which is projection of the response vector yy onto the column space of XX.

Flowchart

Input: vector x,basis matrix B(cols span U)Gram matrix:B^T B (k x k, k =dim of U)Invert: (B^TB)^{-1}Pseudoinverse-like: (B^T B)^{-1}B^T xProjection:pi_U(x) = B *(B^T B)^{-1} B^TxResidual: r = x -pi_U(x)Verify: B^T r = 0(residualorthogonal to allbasis vectors)pi_U(x) isclosest point inU to x

Real-world use

Where it is used

OLS regression: y^=X(XTX)−1XTy\hat{y} = X(X^TX)^{-1}X^Ty is projection of yy onto col(X)\text{col}(X). PCA: project data matrix onto the subspace of top kk principal components. Any dimensionality reduction that fits a low-dimensional subspace. Signal processing: project a signal vector onto a subspace spanned by basis functions.

Problem it solves

Given a subspace U=span(b1,…,bk)U = \text{span}(b_1, \ldots, b_k) and a point x∉Ux \notin U, find the point in UU closest to xx. Equivalently: find coefficients λ1,…,λk\lambda_1, \ldots, \lambda_k such that ∥λ1b1+⋯+λkbk−x∥2\|\lambda_1 b_1 + \cdots + \lambda_k b_k - x\|^2 is minimized. This is the least squares problem.

How it solves the problem

The projection πU(x)=Bλ^\pi_U(x) = B\hat{\lambda} where λ^\hat{\lambda} minimizes ∥Bλ^−x∥2\|B\hat{\lambda} - x\|^2. Setting the gradient to zero: BT(x−Bλ^)=0⇒BTBλ^=BTxB^T(x - B\hat{\lambda}) = 0 \Rightarrow B^TB\hat{\lambda} = B^Tx. This is the normal equation. Solving: λ^=(BTB)−1BTx\hat{\lambda} = (B^TB)^{-1}B^Tx (assuming BB has full column rank, so BTBB^TB is invertible). The projection is πU(x)=Bλ^=B(BTB)−1BTx\pi_U(x) = B\hat{\lambda} = B(B^TB)^{-1}B^Tx.

Example in a business

In OLS regression: B=XB = X (design matrix), x=yx = y (response vector). y^=X(XTX)−1XTy\hat{y} = X(X^TX)^{-1}X^Ty is exactly πU(y)\pi_U(y), the projection of yy onto col(X)\text{col}(X). The fitted values y^\hat{y} are the closest values to yy achievable by any linear combination of the predictors.

The formula, piece by piece

  • BB: n×kn \times k basis matrix; its columns are the basis vectors of UU; must have full column rank (k≤nk \leq n)
  • BTB^T: k×nk \times n transpose of BB
  • BTBB^T B: k×kk \times k Gram matrix; measures inner products between all pairs of basis vectors; invertible iff BB has full column rank
  • (BTB)−1(B^T B)^{-1}: inverse of the Gram matrix; exists when columns of BB are linearly independent
  • (BTB)−1BT(B^T B)^{-1} B^T: the left pseudo-inverse of BB; maps Rn\mathbb{R}^n to Rk\mathbb{R}^k (coordinate vector)
  • B(BTB)−1BTB(B^T B)^{-1} B^T: the projection matrix Pπ=PUP_\pi = P_U; symmetric, idempotent, rank kk
  • πU(x)=PUx\pi_U(x) = P_U x: the projection of xx onto UU

The formula in words

"Solve the normal equations: find the coefficients λ^\hat{\lambda} such that Bλ^B\hat{\lambda} is closest to xx. The projection is BB times those coefficients. The matrix B(BTB)−1BTB(B^TB)^{-1}B^T does this automatically for any xx." Tiny example: B=(110)B = \begin{pmatrix}1\\1\\0\end{pmatrix}, x=(203)x = \begin{pmatrix}2\\0\\3\end{pmatrix}. BTB=2B^TB = 2. (BTB)−1=0.5(B^TB)^{-1} = 0.5. λ^=0.5⋅BTx=0.5⋅2=1\hat{\lambda} = 0.5 \cdot B^T x = 0.5\cdot2 = 1. πU(x)=1⋅B=(1,1,0)T\pi_U(x) = 1 \cdot B = (1,1,0)^T. r=(2,0,3)−(1,1,0)=(1,−1,3)r = (2,0,3)-(1,1,0) = (1,-1,3). BTr=1−1+0=0B^Tr = 1-1+0 = 0. ✓\checkmark

Critical Concepts

  • Requires full column rank: BTBB^TB must be invertible. If columns of BB are linearly dependent, use the pseudoinverse instead.
  • Idempotent: PU2=PUP_U^2 = P_U. Projecting twice = projecting once.
  • Symmetric: PUT=PUP_U^T = P_U.
  • Rank: rank(PU)=k=dim⁡(U)\text{rank}(P_U) = k = \dim(U).
  • Orthonormal shortcut: if BTB=IB^TB = I (columns are orthonormal), then PU=BBTP_U = BB^T (no inversion needed).
  • OLS: β^=(XTX)−1XTy\hat{\beta} = (X^TX)^{-1}X^Ty, y^=Xβ^=X(XTX)−1XTy=PUy\hat{y} = X\hat{\beta} = X(X^TX)^{-1}X^Ty = P_U y.
  • Residual: r=x−πU(x)r = x - \pi_U(x) is always orthogonal to every column of BB: BTr=0B^Tr = 0.

Step-by-step calculation (tiny example)

Step 1: Set up B and x B=(100111)B = \begin{pmatrix}1 & 0\\0 & 1\\1 & 1\end{pmatrix} (two basis vectors in R3\mathbb{R}^3), x=(2,3,4)Tx = (2, 3, 4)^T.

Step 2: Compute Gram matrix and its inverse BTB=(2112)B^T B = \begin{pmatrix}2 & 1\\1 & 2\end{pmatrix}. (BTB)−1=13(2−1−12)(B^TB)^{-1} = \frac{1}{3}\begin{pmatrix}2 & -1\\-1 & 2\end{pmatrix}.

Step 3: Compute coordinates and projection BTx=(2+4,3+4)T=(6,7)TB^T x = (2+4, 3+4)^T = (6,7)^T. λ^=13(2−1−12)(67)=13(58)=(5/3,8/3)T\hat{\lambda} = \frac{1}{3}\begin{pmatrix}2&-1\\-1&2\end{pmatrix}\begin{pmatrix}6\\7\end{pmatrix} = \frac{1}{3}\begin{pmatrix}5\\8\end{pmatrix} = (5/3, 8/3)^T. πU(x)=Bλ^=53(1,0,1)T+83(0,1,1)T=(5/3,8/3,13/3)T≈(1.667,2.667,4.333)T\pi_U(x) = B\hat{\lambda} = \frac{5}{3}(1,0,1)^T + \frac{8}{3}(0,1,1)^T = (5/3, 8/3, 13/3)^T \approx (1.667, 2.667, 4.333)^T. Residual: r=(2,3,4)−(1.667,2.667,4.333)=(0.333,0.333,−0.333)r = (2,3,4) - (1.667, 2.667, 4.333) = (0.333, 0.333, -0.333). BTr=(0.333+(−0.333),0.333+(−0.333))=(0,0)B^Tr = (0.333+(-0.333), 0.333+(-0.333)) = (0,0). ✓\checkmark

Code examples

Beginner example (Python)

python
import numpy as np

# -- Projection onto general subspace: pi_U(x) = B(B^T B)^{-1} B^T x ---
B = np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]])
x = np.array([2.0, 3.0, 4.0])
gram_matrix = B.T @ B   # B^T B
gram_inv = np.linalg.inv(gram_matrix)   # (B^T B)^{-1}
coords = gram_inv @ B.T @ x   # hat_lambda
projection = B @ coords   # pi_U(x)
residual = x - projection
print(f"Gram matrix B^T B:\n{gram_matrix}")
print(f"\nCoordinates lambda_hat: {coords.round(4)}")
print(f"Projection pi_U(x): {projection.round(4)}")
print(f"Residual r: {residual.round(4)}")
print(f"B^T r (should be 0): {(B.T @ residual).round(10)}")

# Projection matrix
P_U = B @ gram_inv @ B.T
print(f"\nProjection matrix P_U (symmetric: {np.allclose(P_U, P_U.T)}, idempotent: {np.allclose(P_U @ P_U, P_U)})")

Beginner example (R)

r
# -- Projection onto general subspace ---
B <- matrix(c(1,0,1, 0,1,1), nrow=3, ncol=2)
x <- c(2, 3, 4)
gram_matrix <- t(B) %*% B
gram_inv <- solve(gram_matrix)
coords <- gram_inv %*% t(B) %*% x
projection <- B %*% coords
residual <- x - projection
cat("Coordinates (lambda_hat):", round(coords, 4), "\n")
cat("Projection pi_U(x):", round(projection, 4), "\n")
cat("B^T r (should be 0):", round(t(B) %*% residual, 10), "\n")

Professional example (Python)

python
import numpy as np
import pandas as pd
np.random.seed(42)

# -- Business scenario: OLS regression as projection ---
# Fitted values = projection of y onto col(X).
# Show that this minimizes ||y - y_hat||^2 (closest point in col(X) to y).
N = 100
# Design matrix X (intercept + 2 predictors)
X = np.column_stack([
    np.ones(N),
    np.random.uniform(1, 10, N),
    np.random.uniform(0, 5, N),
])
true_beta = np.array([2.0, 1.5, -0.8])
y = X @ true_beta + np.random.randn(N) * 0.5

# OLS = projection: y_hat = X(X^T X)^{-1} X^T y
gram_matrix = X.T @ X
beta_hat = np.linalg.solve(gram_matrix, X.T @ y)   # solve normal equations
y_hat = X @ beta_hat   # fitted values = pi_U(y)
residuals = y - y_hat

# Verify properties
ortho_check = X.T @ residuals   # should be ~0 (normal equations)
ss_res = np.sum(residuals**2)
ss_tot = np.sum((y - y.mean())**2)
r_squared = 1 - ss_res / ss_tot
print("OLS = projection of y onto col(X):")
print(f" beta_hat (estimated): {beta_hat.round(4)}")
print(f" true_beta: {true_beta}")
print(f" R^2: {r_squared:.4f}")
print(f" X^T * residuals (normal equations, should be ~0):")
print(f" {ortho_check.round(8)}")
print(f"\n||y - y_hat||^2 is minimized by OLS (projection theorem).")

Source: MML1

#linear-algebra#spatial-distance-norms-and-analytic-geometry#orthogonal-projections

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close