Contents
Deep Breakdown
What it tells me (Situation Awareness): is the orthogonal projection matrix onto the subspace . Applying it to any vector gives , the closest point in to . Two critical properties define it: (1) idempotent: (projecting twice = projecting once), and (2) symmetric: . In OLS, is the hat matrix (maps to fitted values ). Its complement projects onto the orthogonal complement of and maps to the residuals .
Flowchart
Real-world use
Where it is used
OLS regression: the hat matrix is the projection matrix onto ; leverage values are the diagonal entries , which measure how much each observation influences its own fitted value. Computing residual degrees of freedom: . 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 (), analyzing the structure of the projection (its eigenvalues are 0 or 1), computing leverage scores, and deriving theoretical properties of OLS estimators (variance of , degrees of freedom).
How it solves the problem
is derived by solving . Normal equations give , so . Since this holds for all , we can factor out: . Proof of idempotency: .
Example in a business
In OLS: . Diagonal entry is the leverage of observation : how much the fitted value depends on the observed . A leverage (where 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
- : basis matrix (e.g., design matrix in OLS)
- : Gram matrix; must be invertible (full column rank required)
- : inverse of the Gram matrix
- : appears on the right; maps
- : full projection matrix; rank
- Key properties: (idempotent), (symmetric), eigenvalues are 0 or 1
- (trace = rank = dimension of subspace)
- : complementary projection onto
The formula in words
"The projection matrix is a linear operator that, when applied to any vector , returns the closest point in the subspace to . It is the 'shadow operator' onto . Once you compute it, you can project any vector with a single matrix multiplication." Tiny example: , . . . . .
Critical Concepts
- Eigenvalues: has eigenvalues 1 (for vectors in ) and 0 (for vectors in ). for ; for .
- Trace = dimension: .
- Orthonormal shortcut: if (orthonormal columns), .
- OLS hat matrix: , , . Leverage .
- Idempotent + symmetric: any matrix satisfying and IS an orthogonal projection matrix (onto its column space).
- Degrees of freedom: residual d.f. .
Step-by-step calculation (tiny example)
Step 1: Build the projection matrix . . . .
Step 2: Verify idempotency : compute . Each entry: first row of times each column. Row 1: . Col 1: . Dot product: . Matches .
Step 3: Verify projection and residual . . . .
Code examples
Beginner example (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)
# -- 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)
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