Skip to main content
Orthogonality & Projections

Orthogonality & Projections

Why Orthogonality Matters

In machine learning and data science, orthogonality is everywhere:
Estimated Time: 3-4 hours
Difficulty: Intermediate
Prerequisites: Vectors and Matrices modules
What You’ll Build: Image compression, signal denoising, and robust regression

The Intuition: Perpendicular = Independent

A Simple Example

Two vectors are orthogonal (perpendicular) if they point in completely independent directions. Here is why this matters so much. Think about describing a location: you say “3 blocks east and 4 blocks north.” East and north are orthogonal directions — knowing how far east you went tells you absolutely nothing about how far north you went. They carry completely independent information. If instead you used “3 blocks east and 4 blocks northeast,” those directions overlap — some of the “northeast” information is redundant with the “east” information. Orthogonal bases give you zero redundancy. Every component carries unique information. This is why PCA looks for orthogonal directions, why Fourier transforms use orthogonal frequencies, and why QR decomposition creates orthogonal columns. Orthogonality is nature’s way of saying “these things are truly independent.”

The Mathematical Test

Two vectors u\mathbf{u} and v\mathbf{v} are orthogonal if and only if: uv=i=1nuivi=0\mathbf{u} \cdot \mathbf{v} = \sum_{i=1}^{n} u_i v_i = 0
Geometric Interpretation: When the dot product is zero, the vectors form a 90° angle. No component of one vector points in the direction of the other.

Orthonormal Bases: The Gold Standard

An orthonormal basis is a set of vectors that are:
  1. Orthogonal: Every pair has dot product zero
  2. Normalized: Each vector has length 1

Why Orthonormal Is Amazing

With an orthonormal basis, decomposing a vector is trivially easy — just take dot products. No matrix inversion, no system of equations, no numerical instability. This is why algorithms go out of their way to build orthonormal bases.
ML connection: This is exactly what happens in PCA. The principal components form an orthonormal basis, so projecting your data onto them is just a matrix multiply — no expensive inversions needed.

Gram-Schmidt: Making Any Basis Orthonormal

Given any set of linearly independent vectors, we can create an orthonormal basis. The algorithm works by repeatedly projecting and subtracting: take each vector, remove its components along all previously computed orthonormal vectors (making it orthogonal to them), then normalize it. It is like straightening a set of leaning poles one at a time — each new pole gets adjusted to stand perfectly perpendicular to all the others.

Projections: The Heart of Least Squares

Projecting onto a Line

The projection of vector b\mathbf{b} onto vector a\mathbf{a} gives the component of b\mathbf{b} in the direction of a\mathbf{a}. Think of it like a shadow. If you shine a light straight down onto a surface, the shadow of a stick is its projection onto that surface. The projection of b\mathbf{b} onto a\mathbf{a} is the “shadow” of b\mathbf{b} when you shine light perpendicular to a\mathbf{a}. The residual (b\mathbf{b} minus its projection) is the part of b\mathbf{b} that is orthogonal to a\mathbf{a} — the part that “sticks up” out of the shadow. projab=abaaa\text{proj}_{\mathbf{a}} \mathbf{b} = \frac{\mathbf{a} \cdot \mathbf{b}}{\mathbf{a} \cdot \mathbf{a}} \mathbf{a} This formula has two parts: the scalar abaa\frac{\mathbf{a} \cdot \mathbf{b}}{\mathbf{a} \cdot \mathbf{a}} tells you how far along a\mathbf{a} to go, and then you multiply by a\mathbf{a} to get the actual vector in that direction.

Projecting onto a Subspace (Least Squares!)

When we have a matrix AA with columns spanning a subspace, the projection of b\mathbf{b} onto this subspace is: x^=(ATA)1ATb\hat{\mathbf{x}} = (A^T A)^{-1} A^T \mathbf{b} This is exactly the normal equations for least squares!

QR Decomposition: The Practical Tool

QR decomposition factors a matrix as: A=QRA = QR Where:
  • QQ is orthogonal (columns are orthonormal)
  • RR is upper triangular

Why QR Is Better Than Normal Equations

The normal equations approach computes (ATA)1ATb(A^T A)^{-1} A^T b, which requires forming the matrix ATAA^T A. This squaring operation is a disaster for numerical stability: it squares the condition number and can turn a mildly ill-conditioned problem into an unsolvable one. QR decomposition sidesteps this entirely. Since QQ is orthogonal, QTQ=IQ^T Q = I, so applying QTQ^T is perfectly conditioned — it never amplifies errors. The remaining solve with the triangular matrix RR is also well-conditioned. The result: QR-based least squares is numerically stable even when the normal equations produce garbage.

Application: Signal Denoising with Orthogonal Bases

The Discrete Cosine Transform (DCT)

Signals can be decomposed into orthogonal frequency components.

Application: Image Compression

Images can be compressed by keeping only the most important orthogonal components.

Practice Exercises

Problem: Given vectors u1=[1,1,0]\mathbf{u}_1 = [1, 1, 0] and u2=[1,1,0]\mathbf{u}_2 = [1, -1, 0] (which are orthogonal), find the projection of b=[3,4,2]\mathbf{b} = [3, 4, 2] onto the plane spanned by u1\mathbf{u}_1 and u2\mathbf{u}_2.Hint: For orthogonal bases, projections can be computed independently and summed.
Problem: Apply Gram-Schmidt to vectors v1=[1,1]\mathbf{v}_1 = [1, 1] and v2=[1,2]\mathbf{v}_2 = [1, 2].Steps:
  1. q1=v1/v1\mathbf{q}_1 = \mathbf{v}_1 / \|\mathbf{v}_1\|
  2. u2=v2(v2q1)q1\mathbf{u}_2 = \mathbf{v}_2 - (\mathbf{v}_2 \cdot \mathbf{q}_1)\mathbf{q}_1
  3. q2=u2/u2\mathbf{q}_2 = \mathbf{u}_2 / \|\mathbf{u}_2\|
Problem: In standard linear regression, we minimize vertical errors. What if we minimize perpendicular (orthogonal) distances to the line? This is called orthogonal regression or total least squares.Implement orthogonal regression using PCA: the first principal component gives the best-fit line that minimizes orthogonal distances.

Least Squares Solvers: A Stability Comparison

This table summarizes the three main approaches to solving the least squares problem minAxb2\min \|Ax - b\|^2, and when each approach shines or fails.
The takeaway: QR is the right default for production code. Use the normal equations only when you have verified that the problem is well-conditioned. Use SVD when you suspect rank deficiency or when you need the pseudoinverse.
Practical Advice: In Python, np.linalg.lstsq() uses SVD internally. This is the safest single-call solution. If you need speed for a hot loop and have verified good conditioning, switch to QR via np.linalg.qr() followed by back-substitution. Only use the normal equations formula (ATA)1ATb(A^TA)^{-1}A^Tb if you have a specific performance reason and have checked κ(A)<106\kappa(A) < 10^6.

Summary

Key Takeaway: Orthogonality simplifies everything. Decomposing into orthogonal components makes calculations independent and numerically stable. This is why PCA (finding orthogonal directions of variance) is so powerful for dimensionality reduction. It is also why Fourier analysis works (orthogonal frequencies), why QR decomposition is numerically superior to the normal equations, and why orthogonal weight initialization helps neural networks train faster. When you see orthogonality in a method, it is almost always there to prevent information from “leaking” between components and to keep computations clean.
Gram-Schmidt Numerical Pitfall: The classical Gram-Schmidt algorithm described above can lose orthogonality for nearly-dependent vectors due to floating-point errors. In practice, use the Modified Gram-Schmidt variant (which re-orthogonalizes against updated vectors rather than original ones) or simply call np.linalg.qr(), which uses Householder reflections — a numerically superior approach that avoids the problem entirely.