Orthogonal projection
正交投影
Find the closest approximation when data can use only one direction, and connect a perpendicular residual to least squares.
orthogonal-projectionKeep only one direction
Given , suppose we can represent it only as a multiple of . Candidates include , , and points between or beyond them. Which is closest to the original data point ? The distance being minimized is between the data and its approximation, not between the data and the coordinate origin.
Geometrically, drop a perpendicular from onto the line through the coordinate origin. Its foot is the orthogonal projection. Here the result is , with residual . Checking confirms that the residual is perpendicular to the allowed direction.
Projection retains the part expressible along the direction and discards the perpendicular part. It solves a constrained approximation problem but does not establish that the discarded part is noise. If the task depends on that direction, projection can harm prediction.
Prove closeness without calculus
Let , use Euclidean distance, and represent a candidate as . Requiring the residual to be orthogonal to gives:
Finding a perpendicular residual is not yet a proof of optimality. Set . For any coefficient , the cross term vanishes because :
The last term is nonnegative and vanishes only at , proving a unique optimum. In the example, and , so the coefficient is . No differentiation is needed for this derivation. MML, Section 3.8
Writing gives and . Symmetry relates to Euclidean orthogonality; idempotence means that projecting an already projected point changes nothing. Idempotence alone is insufficient: an oblique projection can be idempotent without returning the Euclidean closest point.
For a subspace spanned by the linearly independent columns of , the least-squares orthogonality condition becomes:
This expression assumes full column rank and does not recommend explicitly inverting a matrix in numerical code. Practical solvers commonly use QR or SVD. Dependent columns require handling nonunique coefficients; the pseudoinverse provides a more general expression. Deep Learning, Section 2.9
Check the residual and the distance
This complete program projects onto a one-dimensional subspace. A zero direction cannot define a one-dimensional line and is rejected. The zero subspace itself still has a projection, but representing it requires a different input convention.
import math
def dot(x, y):
if len(x) != len(y):
raise ValueError("dimension mismatch")
return math.fsum(a * b for a, b in zip(x, y))
def project_line(x, u):
denominator = dot(u, u)
if denominator == 0:
raise ValueError("a line needs a nonzero direction")
coefficient = dot(u, x) / denominator
return [coefficient * value for value in u]
x, u = [2.0, 3.0], [1.0, 2.0]
p = project_line(x, u)
r = [a - b for a, b in zip(x, p)]
assert all(abs(a - b) < 1e-12 for a, b in zip(p, [1.6, 3.2]))
assert abs(dot(r, u)) < 1e-12
assert abs(dot(x, x) - dot(p, p) - dot(r, r)) < 1e-12
assert all(abs(a - b) < 1e-12 for a, b in zip(project_line(p, u), p))
for a in [-2, 0, 1, 1.6, 2, 4]:
error = [v - a * direction for v, direction in zip(x, u)]
assert dot(error, error) >= dot(r, r) - 1e-12
For vectors of length , line projection takes time and output space without constructing a matrix. The final trial coefficients check the implementation. Optimality over the continuous range follows from the square expansion above.
Lines, affine sets, and numerical error
The line in this article passes through the coordinate origin. For an affine set , first shift the input to , project it, and add back. Omitting that offset solves a different problem.
“Closest” also depends on the distance. Reweighting coordinates or using L1 distance can change the optimum. The Euclidean orthogonal-projection proof cannot simply be transferred to every loss function.
When contains nearly parallel directions, coefficients can be sensitive to small perturbations, and forming worsens conditioning. Numerical stability matters even when the expression exists algebraically. A residual being orthogonal to the retained subspace also does not establish statistical independence.
Change the direction and constraint
- Does replacing with change the projection point? Answer: no; it spans the same line, although the coefficient changes.
- Project onto the horizontal axis. Answer: , with residual .
- Project onto the affine line . Answer: . Subtract the anchor point before projecting.
- Is an orthogonal projection matrix? Answer: no. It is idempotent but not symmetric. For example, it sends to , although the closest horizontal-axis point is .
The shared structure of least squares and dimension reduction
Linear regression restricts predictions to the design matrix’s column space, producing the least-squares orthogonality condition. PCA additionally selects a retained subspace from data. Projecting onto a given subspace and choosing the subspace are different steps. Separating them clarifies what dimension reduction optimizes and why preserving variance need not preserve task-relevant information.
Make a prediction, then check
Replace a line direction u with -3u. What happens to its orthogonal projection point?
Learning objectives
- Prove optimality of Euclidean line projection without calculus.
- Distinguish orthogonal projection, oblique projection, and affine sets.
Prerequisites & connections
Explore related knowledge →References
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Deep Learning — Chapter 2: Linear Algebra
Ian Goodfellow; Yoshua Bengio; Aaron Courville · 2016
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.
- Rederived the unique optimal coefficient by expanding a square; recomputed 8/5, projection (1.6,3.2), and the perpendicular residual.
- Checked the idempotent nonsymmetric counterexample, affine translation, and full-column-rank assumption; distinguished an inverse formula from numerical-solver advice.
- Compared the main arguments, numerical examples, exercises, and answers across languages; structural checks additionally compare display equations, source lists, and code.
- Reviewed the answer-label rendering fix: only Markdown emphasis markers changed to strong tags; normalized text and all code are identical.
Source locations checked:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §3.8; §2.3 numerical-solution discussion
- Deep Learning — Chapter 2: Linear Algebra · §2.9
Related topics