BRAGA

02 Writing02-02 Notes02-02-203

Projection of x onto 1D subspace (line) U spanned by b

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)=bbTbTbx\pi_U(x) = \frac{b b^T}{b^T b} x
(1)

Deep Breakdown

What it tells me (Situation Awareness): πU(x)=bbTbTbx\pi_U(x) = \frac{bb^T}{b^Tb}x projects a vector xx onto the 1D subspace (line through the origin) spanned by bb. The result is the closest point on that line to xx. Geometrically: drop a perpendicular from xx to the line; the foot of the perpendicular is πU(x)\pi_U(x). This is the scalar projection formula scaled back into a vector. The scalar projection of xx onto bb is bTxbTb\frac{b^T x}{b^T b} (a number); multiplying by bb gives the vector projection πU(x)=bTxbTbb=bbTbTbx\pi_U(x) = \frac{b^T x}{b^T b}b = \frac{bb^T}{b^Tb}x. In DA/DS: projecting data onto a 1D direction is the core of PCA (project onto the first principal component), Linear Discriminant Analysis, and any dimensionality reduction to a single direction.

Flowchart

Input: vector x,direction bScalarprojection:lambda = (b^T x)/ (b^T b)Vectorprojection:pi_U(x) =lambda * bEquivalently:pi_U(x) = (b b^T/ b^T b) * xResidual: r = x -pi_U(x)Verifyorthogonality:b^T * r = 0pi_U(x) is theclosest point online span(b) to x

Real-world use

Where it is used

PCA: projecting data onto the first principal component direction b=v1b = v_1. Regression with a single predictor: projecting the response vector onto the predictor direction. Gradient projection: projecting a gradient onto a constraint direction. Computing explained variance: ∥πU(x)∥2/∥x∥2\|\pi_U(x)\|^2 / \|x\|^2 = fraction of xx's energy in the direction of bb.

Problem it solves

Given a direction bb and a point xx NOT on the line through bb, find the point on the line that is closest to xx. The closest point minimizes ∥x−λb∥2\|x - \lambda b\|^2 over all scalars λ\lambda. The solution is the orthogonal projection.

How it solves the problem

Minimize ∥x−λb∥2\|x - \lambda b\|^2: take derivative with respect to λ\lambda and set to zero. ddλ∥x−λb∥2=−2bT(x−λb)=0⇒λ=bTxbTb\frac{d}{d\lambda}\|x - \lambda b\|^2 = -2b^T(x - \lambda b) = 0 \Rightarrow \lambda = \frac{b^T x}{b^T b}. The projected vector is πU(x)=λb=bTxbTbb\pi_U(x) = \lambda b = \frac{b^T x}{b^T b}b. Written as a matrix: πU(x)=bbTbTbx\pi_U(x) = \frac{bb^T}{b^Tb}x where bbTbTb\frac{bb^T}{b^Tb} is the rank-1 projection matrix.

Example in a business

A DS computes the first PCA component b=v1b = v_1 of a customer feature matrix. To project each customer's feature vector xix_i onto this direction: scorei=bTxi∥b∥2\text{score}_i = \frac{b^T x_i}{\|b\|^2} (scalar, used for ranking) and πU(xi)=bbT∥b∥2xi\pi_U(x_i) = \frac{bb^T}{\|b\|^2} x_i (vector, for reconstruction). The scalar score is the first principal component score for customer ii.

The formula, piece by piece

  • bb: the direction vector (basis of the 1D subspace); does NOT need to be unit length
  • bTb^T: row vector transpose of bb
  • bTb=∥b∥2b^T b = \|b\|^2: the squared norm of bb (a scalar); the denominator normalizes the projection
  • bbTb b^T: outer product of bb with itself; an n×nn \times n rank-1 matrix
  • bbTbTb\frac{bb^T}{b^Tb}: the projection matrix PbP_b onto span(b)\text{span}(b); rank-1, symmetric, idempotent (Pb2=PbP_b^2 = P_b)
  • πU(x)=bbTbTbx\pi_U(x) = \frac{bb^T}{b^Tb}x: the projection of xx onto the line spanned by bb
  • Scalar form: πU(x)=bTxbTb⋅b\pi_U(x) = \frac{b^T x}{b^T b} \cdot b (scalar coefficient times the direction)

The formula in words

"Find how much of xx lies in the direction of bb (the scalar projection), then multiply by bb to get the vector that sits on the line at that 'shadow' point. Dividing by bTbb^Tb corrects for the length of bb." Tiny example: b=(2,0)b = (2, 0), x=(3,4)x = (3, 4). bTx=6b^T x = 6. bTb=4b^T b = 4. λ=6/4=1.5\lambda = 6/4 = 1.5. πU(x)=1.5⋅(2,0)=(3,0)\pi_U(x) = 1.5 \cdot (2, 0) = (3, 0). Residual: r=(3,4)−(3,0)=(0,4)r = (3,4) - (3,0) = (0,4). Check: bTr=(2)(0)+(0)(4)=0b^T r = (2)(0)+(0)(4) = 0. ✓\checkmark

Critical Concepts

  • Idempotent: Pb2=PbP_b^2 = P_b. Projecting twice gives the same result as projecting once.
  • Symmetric: PbT=PbP_b^T = P_b.
  • Rank 1: the projection matrix has rank 1 (projects onto a line).
  • Unit vector shortcut: if ∥b∥=1\|b\| = 1, then Pb=bbTP_b = bb^T and πU(x)=(bTx)b\pi_U(x) = (b^T x)b.
  • Residual orthogonality: bT(x−πU(x))=0b^T(x - \pi_U(x)) = 0 always. The residual is perpendicular to bb.
  • Energy decomposition: ∥x∥2=∥πU(x)∥2+∥x−πU(x)∥2\|x\|^2 = \|\pi_U(x)\|^2 + \|x - \pi_U(x)\|^2 (Pythagorean theorem).
  • Connection to PCA: PCA finds bb that maximizes ∥πU(x)∥2\|\pi_U(x)\|^2 over all data points, i.e., the direction of maximum projected variance.

Step-by-step calculation (tiny example)

Step 1: Define direction vector and input b=(1,2)b = (1, 2), x=(3,1)x = (3, 1).

Step 2: Compute scalar projection bTx=1⋅3+2⋅1=5b^T x = 1\cdot3 + 2\cdot1 = 5. bTb=1+4=5b^T b = 1+4 = 5. λ=5/5=1\lambda = 5/5 = 1.

Step 3: Compute vector projection and verify orthogonality πU(x)=1⋅(1,2)=(1,2)\pi_U(x) = 1 \cdot (1,2) = (1,2). Residual: r=(3,1)−(1,2)=(2,−1)r = (3,1) - (1,2) = (2,-1). bTr=1⋅2+2⋅(−1)=0b^T r = 1\cdot2 + 2\cdot(-1) = 0. ✓\checkmark The residual is orthogonal to bb.

Code examples

Beginner example (Python)

python
import numpy as np   # -- 1D projection: pi_U(x) = (b b^T / b^T b) x ---
b = np.array([1.0, 2.0])
x = np.array([3.0, 1.0])
scalar_proj = np.dot(b, x) / np.dot(b, b)
proj_vector = scalar_proj * b
residual = x - proj_vector
print(f"b = {b}")
print(f"x = {x}")
print(f"lambda = {scalar_proj:.4f}")
print(f"pi_U(x) = {proj_vector}")
print(f"residual = {residual}")
print(f"b^T residual = {np.dot(b, residual):.10f} (should be 0)")   # Projection matrix P_b = b b^T / b^T b
P_b = np.outer(b, b) / np.dot(b, b)
print(f"\nProjection matrix P_b:\n{P_b}")
print(f"P_b @ x = {P_b @ x} (matches pi_U(x): {np.allclose(P_b @ x, proj_vector)})")
print(f"P_b^2 = P_b (idempotent): {np.allclose(P_b @ P_b, P_b)}")

Beginner example (R)

r
# -- 1D projection ---
b <- c(1, 2)
x <- c(3, 1)
scalar_proj <- sum(b * x) / sum(b * b)
proj_vector <- scalar_proj * b
residual <- x - proj_vector
cat(sprintf("lambda = %.4f\n", scalar_proj))
cat(sprintf("pi_U(x) = (%g, %g)\n", proj_vector[1], proj_vector[2]))
cat(sprintf("b^T residual = %.10f\n", sum(b * residual)))

# Projection matrix
P_b <- outer(b, b) / sum(b * b)
cat("Projection matrix P_b:\n")
print(round(P_b, 4))

Professional example (Python)

python
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
np.random.seed(42)

# -- Business scenario: project customer data onto first PCA direction ---
# Each customer has 3 RFM features. Project each onto the first PCA direction.
N_CUSTOMERS = 200
rfm_raw = np.column_stack([
    np.random.exponential(30, N_CUSTOMERS),   # recency (days since last purchase)
    np.random.poisson(10, N_CUSTOMERS),       # frequency (purchases/month)
    np.random.lognormal(4, 1, N_CUSTOMERS),   # monetary (spend in euros)
])
scaler = StandardScaler()
rfm_scaled = scaler.fit_transform(rfm_raw)

# First PCA direction: eigenvector of largest eigenvalue of covariance matrix
cov_matrix = np.cov(rfm_scaled.T)
eigenvalues, eigenvectors = np.linalg.eigh(cov_matrix)
b_first_pc = eigenvectors[:, -1]   # largest eigenvalue direction

# Project each customer onto this 1D subspace
scalar_scores = rfm_scaled @ b_first_pc / np.dot(b_first_pc, b_first_pc)
proj_vectors = np.outer(scalar_scores, b_first_pc)

# Explained variance: ||pi_U(x)||^2 / ||x||^2
proj_norms_sq = np.sum(proj_vectors**2, axis=1)
orig_norms_sq = np.sum(rfm_scaled**2, axis=1)
explained_ratio = proj_norms_sq / orig_norms_sq
results = pd.DataFrame({
    "customer": range(N_CUSTOMERS),
    "pc1_score": scalar_scores.round(3),
    "explained_ratio": explained_ratio.round(3),
})
print(f"First PCA direction b: {b_first_pc.round(4)}")
print(f"\nMean explained variance (1D projection): {explained_ratio.mean():.3f}")
print(f"\nTop 5 customers by PC1 score:")
print(results.nlargest(5, "pc1_score").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