BRAGA

02 Writing02-02 Notes02-02-202

Projection matrix Pπ onto 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
Pπ=B(BTB)−1BTP_\pi = B(B^T B)^{-1} B^T
(1)

Deep Breakdown

What it tells me (Situation Awareness): Pπ=B(BTB)−1BTP_\pi = B(B^TB)^{-1}B^T is the orthogonal projection matrix onto the subspace U=col(B)U = \text{col}(B). Applying it to any vector xx gives Pπx=πU(x)P_\pi x = \pi_U(x), the closest point in UU to xx. Two critical properties define it: (1) idempotent: Pπ2=PπP_\pi^2 = P_\pi (projecting twice = projecting once), and (2) symmetric: PπT=PπP_\pi^T = P_\pi. In OLS, Pπ=X(XTX)−1XTP_\pi = X(X^TX)^{-1}X^T is the hat matrix (maps yy to fitted values y^\hat{y}). Its complement I−PπI - P_\pi projects onto the orthogonal complement of UU and maps yy to the residuals e^=y−y^\hat{e} = y - \hat{y}.

Flowchart

Basis matrix B(n x k, fullcolumn rank)Gram matrix:B^T B (k x k)Invert: (B^TB)^{-1}Projectionmatrix: P_pi = B(B^T B)^{-1} B^TProperties:P_pi^2 = P_pi(idempotent),P_pi^T = P_pi(symmetric)Apply: P_pi * x =projection of xonto col(B)Complement: I -P_pi = projectiononto orthogonalcomplement(I - P_pi) * x =residual(orthogonal toU)

Real-world use

Where it is used

OLS regression: the hat matrix H=X(XTX)−1XTH = X(X^TX)^{-1}X^T is the projection matrix onto col(X)\text{col}(X); leverage values are the diagonal entries HiiH_{ii}, which measure how much each observation influences its own fitted value. Computing residual degrees of freedom: tr(I−Pπ)=n−k\text{tr}(I - P_\pi) = n - k. Checking projection idempotency as a numerical sanity check on computed matrices.

Problem it solves

Encapsulating the projection operation as a matrix enables: applying the projection to multiple vectors at once (PπXnewP_\pi X_{\text{new}}), analyzing the structure of the projection (its eigenvalues are 0 or 1), computing leverage scores, and deriving theoretical properties of OLS estimators (variance of y^\hat{y}, degrees of freedom).

How it solves the problem

PπP_\pi is derived by solving min⁡λ∥Bλ−x∥2\min_\lambda \|B\lambda - x\|^2. Normal equations give λ^=(BTB)−1BTx\hat{\lambda} = (B^TB)^{-1}B^Tx, so πU(x)=Bλ^=B(BTB)−1BTx=Pπx\pi_U(x) = B\hat{\lambda} = B(B^TB)^{-1}B^Tx = P_\pi x. Since this holds for all xx, we can factor out: Pπ=B(BTB)−1BTP_\pi = B(B^TB)^{-1}B^T. Proof of idempotency: Pπ2=B(BTB)−1BTB(BTB)−1BT=B(BTB)−1(BTB)(BTB)−1BT=B(BTB)−1BT=PπP_\pi^2 = B(B^TB)^{-1}B^T B(B^TB)^{-1}B^T = B(B^TB)^{-1}(B^TB)(B^TB)^{-1}B^T = B(B^TB)^{-1}B^T = P_\pi.

Example in a business

In OLS: H=X(XTX)−1XTH = X(X^TX)^{-1}X^T. Diagonal entry HiiH_{ii} is the leverage of observation ii: how much the fitted value y^i\hat{y}_i depends on the observed yiy_i. A leverage Hii>2k/nH_{ii} > 2k/n (where kk is the number of predictors) flags potentially influential observations. A DS uses this to identify outliers that are disproportionately driving the regression fit.

The formula, piece by piece

  • BB: n×kn \times k basis matrix (e.g., design matrix XX in OLS)
  • BTBB^TB: k×kk \times k Gram matrix; must be invertible (full column rank required)
  • (BTB)−1(B^TB)^{-1}: inverse of the Gram matrix
  • BTB^T: appears on the right; maps Rn→Rk\mathbb{R}^n \to \mathbb{R}^k
  • B(BTB)−1BTB(B^TB)^{-1}B^T: full n×nn \times n projection matrix; rank kk
  • Key properties: Pπ2=PπP_\pi^2 = P_\pi (idempotent), PπT=PπP_\pi^T = P_\pi (symmetric), eigenvalues are 0 or 1
  • tr(Pπ)=k\text{tr}(P_\pi) = k (trace = rank = dimension of subspace)
  • I−PπI - P_\pi: complementary projection onto U⊥U^\perp

The formula in words

"The projection matrix PπP_\pi is a linear operator that, when applied to any vector xx, returns the closest point in the subspace UU to xx. It is the 'shadow operator' onto UU. Once you compute it, you can project any vector with a single matrix multiplication." Tiny example: B=(1,1)TB = (1, 1)^T, x=(3,1)Tx = (3, 1)^T. Pπ=12(11)(1,1)=12(1111)P_\pi = \frac{1}{2}\begin{pmatrix}1\\1\end{pmatrix}(1,1) = \frac{1}{2}\begin{pmatrix}1&1\\1&1\end{pmatrix}. Pπx=12(44)=(2,2)TP_\pi x = \frac{1}{2}\begin{pmatrix}4\\4\end{pmatrix} = (2,2)^T. (I−Pπ)x=(3,1)T−(2,2)T=(1,−1)T(I - P_\pi)x = (3,1)^T - (2,2)^T = (1,-1)^T. BT(1,−1)T=1−1=0B^T(1,-1)^T = 1-1 = 0. ✓\checkmark

Critical Concepts

  • Eigenvalues: PπP_\pi has eigenvalues 1 (for vectors in UU) and 0 (for vectors in U⊥U^\perp). Pπv=vP_\pi v = v for v∈Uv \in U; Pπw=0P_\pi w = 0 for w∈U⊥w \in U^\perp.
  • Trace = dimension: tr(Pπ)=k=dim⁡(U)\text{tr}(P_\pi) = k = \dim(U).
  • Orthonormal shortcut: if BTB=IB^TB = I (orthonormal columns), Pπ=BBTP_\pi = BB^T.
  • OLS hat matrix: H=X(XTX)−1XTH = X(X^TX)^{-1}X^T, y^=Hy\hat{y} = Hy, e=(I−H)ye = (I-H)y. Leverage =Hii= H_{ii}.
  • Idempotent + symmetric: any matrix satisfying P2=PP^2 = P and PT=PP^T = P IS an orthogonal projection matrix (onto its column space).
  • Degrees of freedom: residual d.f. =tr(I−Pπ)=n−k= \text{tr}(I - P_\pi) = n - k.

Step-by-step calculation (tiny example)

Step 1: Build the projection matrix B=(110)B = \begin{pmatrix}1\\1\\0\end{pmatrix}. BTB=2B^TB = 2. (BTB)−1=0.5(B^TB)^{-1} = 0.5. Pπ=12(110)(1,1,0)=12(110110000)P_\pi = \frac{1}{2}\begin{pmatrix}1\\1\\0\end{pmatrix}(1,1,0) = \frac{1}{2}\begin{pmatrix}1&1&0\\1&1&0\\0&0&0\end{pmatrix}.

Step 2: Verify idempotency Pπ2=Pπ2P_\pi^2 = P_\pi^2: compute Pπ⋅PπP_\pi \cdot P_\pi. Each entry: first row of PπP_\pi times each column. Row 1: (0.5,0.5,0)(0.5, 0.5, 0). Col 1: (0.5,0.5,0)T(0.5, 0.5, 0)^T. Dot product: 0.25+0.25=0.50.25+0.25 = 0.5. Matches Pπ[1,1]=0.5P_\pi[1,1] = 0.5. ✓\checkmark

Step 3: Verify projection and residual x=(3,1,2)Tx = (3, 1, 2)^T. Pπx=12(3+1,3+1,0)=(2,2,0)TP_\pi x = \frac{1}{2}(3+1, 3+1, 0) = (2, 2, 0)^T. (I−Pπ)x=(1,−1,2)T(I - P_\pi)x = (1, -1, 2)^T. BT(I−Pπ)x=1−1+0=0B^T(I-P_\pi)x = 1-1+0 = 0. ✓\checkmark

Code examples

Beginner example (Python)

python
import numpy as np   # -- Projection matrix P_pi = B(B^T B)^{-1} B^T ---
B = np.array([[1.0], [1.0], [0.0]])   # single basis vector
P_pi = B @ np.linalg.inv(B.T @ B) @ B.T
print(f"B:\n{B}")
print(f"\nProjection matrix P_pi:\n{P_pi.round(4)}")
print(f"Idempotent (P^2 = P): {np.allclose(P_pi @ P_pi, P_pi)}")
print(f"Symmetric (P^T = P): {np.allclose(P_pi, P_pi.T)}")
print(f"Trace (= dim of U): {np.trace(P_pi):.4f}")
print(f"Eigenvalues: {np.round(np.linalg.eigvalsh(P_pi), 4)}")
x = np.array([3.0, 1.0, 2.0])
proj = P_pi @ x
resid = x - proj
print(f"\nx = {x}")
print(f"P_pi @ x = {proj.round(4)}")
print(f"residual = {resid.round(4)}")
print(f"B^T resid = {(B.T @ resid).round(10)}")

Beginner example (R)

r
# -- Projection matrix ---
B <- matrix(c(1, 1, 0), ncol=1)
P_pi <- B %*% solve(t(B) %*% B) %*% t(B)
cat("Projection matrix P_pi:\n"); print(round(P_pi, 4))
cat(sprintf("Idempotent: %s\n", isTRUE(all.equal(P_pi %*% P_pi, P_pi))))
cat(sprintf("Trace: %.4f\n", sum(diag(P_pi))))
x <- c(3, 1, 2)
proj <- P_pi %*% x
resid <- x - proj
cat(sprintf("B^T resid: %.10f\n", t(B) %*% resid))

Professional example (Python)

python
import numpy as np
import pandas as pd
np.random.seed(42)   # -- Business scenario: OLS hat matrix and leverage analysis ---
# Identify high-leverage observations in a regression.
N = 50
k = 3   # intercept + 2 predictors
X = np.column_stack([ np.ones(N), np.random.uniform(0, 10, N), np.random.uniform(0, 5, N),
])   # Add an outlier with extreme predictor values
X[0] = [1.0, 20.0, 15.0]   # far from the mean # OLS hat matrix (projection matrix)
H = X @ np.linalg.solve(X.T @ X, X.T)   # X(X^TX)^{-1}X^T
leverages = np.diag(H)   # Rule of thumb: leverage > 2k/n is high
threshold = 2 * k / N
results = pd.DataFrame({ "obs": range(N), "leverage": leverages.round(4), "high_lev": leverages > threshold,
})
print(f"Projection matrix properties:")
print(f" Idempotent (H^2=H): {np.allclose(H @ H, H)}")
print(f" Symmetric (H^T=H): {np.allclose(H, H.T)}")
print(f" Trace = k = {k}: {np.trace(H):.4f}")
print(f"\nHigh-leverage threshold: {threshold:.4f}")
print(f"High-leverage observations: {results[results['high_lev']]['obs'].tolist()}")
print(f"\nTop 5 by leverage:")
print(results.nlargest(5, 'leverage').to_string(index=False))

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