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
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 and are orthogonal if and only if: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:- Orthogonal: Every pair has dot product zero
- 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.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 onto vector gives the component of in the direction of . 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 onto is the “shadow” of when you shine light perpendicular to . The residual ( minus its projection) is the part of that is orthogonal to — the part that “sticks up” out of the shadow. This formula has two parts: the scalar tells you how far along to go, and then you multiply by to get the actual vector in that direction.Projecting onto a Subspace (Least Squares!)
When we have a matrix with columns spanning a subspace, the projection of onto this subspace is: This is exactly the normal equations for least squares!QR Decomposition: The Practical Tool
QR decomposition factors a matrix as: Where:- is orthogonal (columns are orthonormal)
- is upper triangular
Why QR Is Better Than Normal Equations
The normal equations approach computes , which requires forming the matrix . 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 is orthogonal, , so applying is perfectly conditioned — it never amplifies errors. The remaining solve with the triangular matrix 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
Exercise 1: Orthogonal Projection
Exercise 1: Orthogonal Projection
Problem: Given vectors and (which are orthogonal), find the projection of onto the plane spanned by and .Hint: For orthogonal bases, projections can be computed independently and summed.
Exercise 2: Gram-Schmidt by Hand
Exercise 2: Gram-Schmidt by Hand
Problem: Apply Gram-Schmidt to vectors and .Steps:
Exercise 3: Orthogonal Regression
Exercise 3: Orthogonal Regression
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 , and when each approach shines or fails.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.