II.18 · Linear Algebra
What is the closest answer when no exact answer exists?
Move three observations and watch a fitted line, residual segments and squared error update. Then build an orthogonal basis and inspect projection onto the column space, including dependent and zero-vector failures.
Find the closest reachable data vector
Move the data; minimize vertical squared residuals
Unique least-squares minimizer: x = (4/3, 1). The minimum residual is nonzero: (−1/3, 2/3, −1/3); minimizing it does not solve every equation. Aᵀ(b − Ax) = 0 exactly.
- Column-space/design matrix
- [1, −1; 1, 0; 1, 1]
- Independent columns
- 2
- One minimizing coefficient vector
- (4/3, 1)
- Projection of the data onto col(A)
- (1/3, 4/3, 7/3)
- Residual b − Ax
- (−1/3, 2/3, −1/3)
- Minimum squared error
- 2/30.666667…
- Exact residual orthogonality
- (0, 0)
Exact values use =; numerical companions use ≈. Rank and zero decisions are exact.
Unique least-squares minimizer: x = (4/3, 1). The minimum residual is nonzero: (−1/3, 2/3, −1/3); minimizing it does not solve every equation. Aᵀ(b − Ax) = 0 exactly.
The column-space question
Which linear combination of the columns lies closest to the data vector? Each row is one equation.
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
from −4 to 4, up to 2 decimal places
Move the middle data point away from the line. The fitted line balances the three vertical errors instead of passing through every observation. Choose Same x: nonunique minimizers to separate a unique projected data vector from nonunique coefficients. Then switch to Orthogonalize two directions and replay the projection-subtraction trace.
Intuition: split a vector into reachable and unreachable parts
Projecting onto one direction separates a parallel component from a perpendicular residual. A subspace works the same way: find a vector inside the subspace whose leftover is perpendicular to every direction in that subspace.
For a system , the reachable vectors form the column space of . If lies outside it, no coefficient vector can solve every equation. Least squares chooses the closest reachable vector. The residual records precisely what the transformation cannot match.
Definition
Vectors are orthogonal when their dot product is zero. An orthogonal basis consists of independent, pairwise perpendicular nonzero vectors. An orthonormal basis additionally gives each vector norm one. If are orthonormal, the projection onto their span is
Least squares minimizes . The minimizer is unique when has full column rank. With deficient rank, all minimizers are for , but their projected output is unique.
Mechanics: observations and column-space geometry
The data explorer uses , where each row is , and contains the observed heights. The coefficient vector is (intercept, slope). Dragging any point changes that canonical system. Its vertical residual segment is an entry of ; the inspector lists the full exact residual vector and its squared norm.
The point plot is in observation coordinates . The three-entry data vector and its projection live in and are shown as exact components, not projected deceptively into that same plane. The direction explorer instead demonstrates two-dimensional Gram–Schmidt and subspace projection directly. Keyboard and numeric inputs provide the same controls.
Derivation: Gram–Schmidt exposes projection mechanics
Begin with . Remove the components of each later column along earlier nonzero orthogonal directions:
A zero residual means the column was dependent; an original zero vector also cannot be normalized. The lesson records those failures and continues with surviving independent directions. Dividing by then gives an orthonormal basis . In the exact model we project using the unnormalized orthogonal directions, avoiding unnecessary irrational arithmetic:
The exact dot products certify orthogonality; normalized unit-vector drawings are approximate companions. No zero direction is ever divided by its norm.
Derivation: normal equations and optimality
Suppose and is perpendicular to the column space. For any coefficient change , belongs to that space. Pythagoras gives
Therefore no change can improve the error. Equality occurs precisely when , explaining nonunique minimizers through the null space.
The orthogonality condition is , equivalently
These are the normal equations. They are a mathematical characterization of a minimum, not a promise that every original equation has been solved.
Worked examples
For observations , the two columns and are orthogonal. Projection coefficients are and . The fitted heights are and the residual is . Its dot product with each column is zero, and its squared norm is .
Replay the worked derivation
The column-space question
Which linear combination of the columns lies closest to the data vector? Each row is one equation.
For columns and , Gram–Schmidt gives and . Their dot product is zero; normalizing gives and . For and , the second residual is zero, so only one basis direction survives. The failure is dependence, not a second unit vector pointing somewhere arbitrary.
If every observation has , the line coefficients only appear through . The closest fitted height is the mean , so every pair satisfying minimizes the error. The inspector chooses one particular pair and displays the null direction .
Executable reference code
The Fraction reference projects onto exact orthogonal directions. NumPy uses lstsq, explicitly computes residuals and cross-checks a QR projection. An empty NumPy residuals array is not proof of zero error; it can occur in rank-deficient or non-overdetermined systems.
"""Exact Gram–Schmidt projection onto a column space."""
from fractions import Fraction as F
def project(columns,b):
dot=lambda u,v: sum(x*y for x,y in zip(u,v))
basis=[]
for col in columns:
u=list(map(F,col))
for q in basis:
gain=dot(u,q)/dot(q,q)
u=[x-gain*y for x,y in zip(u,q)]
if not any(u):
continue # Dependent or zero vector: do not normalize it.
basis.append(u)
p=[F(0)]*len(b)
for u in basis:
gain=dot(b,u)/dot(u,u)
p=[x+gain*y for x,y in zip(p,u)]
residual=[F(x)-y for x,y in zip(b,p)]
assert all(dot(residual,c)==0 for c in columns)
return p,residual,basis
p,r,basis=project([[1,1,1],[-1,0,1]],[0,2,2])
assert p==[F(1,3),F(4,3),F(7,3)]
assert r==[F(-1,3),F(2,3),F(-1,3)]
assert sum(x*x for x in r)==F(2,3)
assert len(project([[1,1],[2,2]],[0,2])[2])==1import numpy as np
A=np.array([[1.,-1.],[1.,0.],[1.,1.]])
b=np.array([0.,2.,2.])
x, reported_residuals, rank, singular_values=np.linalg.lstsq(A,b,rcond=None)
r=b-A@x
assert np.allclose(x,[4/3,1]) and np.allclose(A.T@r,0)
assert np.isclose(r@r,2/3)
# Compute the residual directly: reported_residuals can be empty for deficient systems.
Q,R=np.linalg.qr(A,mode='reduced')
assert np.allclose(Q@(Q.T@b),A@x)
# QR/SVD avoid forming A.T@A, which squares the 2-norm condition number.Explore
Move a point to make all observations exactly collinear and compare zero error with the noisy case. Make all horizontal coordinates equal. Restore independent columns with a small separation and inspect sensitivity. In the direction mode, compare independent, dependent and zero-vector cases; predict which projection remains when one basis direction disappears.
Common mistakes
A residual can be nonzero while being orthogonal to every column. Least squares minimizes the sum of squared residuals, not their signed sum. The segments in the fit are vertical prediction errors, not shortest distances to the drawn line. Nonunique coefficients do not imply multiple closest projected data vectors. Orthonormality requires both perpendicularity and unit length.
Numerical considerations
This small educational domain uses exact rational Gram–Schmidt and exact projection, so zero and rank classifications do not depend on an epsilon. Normalized companion vectors and rendered segments use binary64. In numerical software, classical Gram–Schmidt can lose orthogonality; modified Gram–Schmidt or Householder QR are preferable depending on the problem.
The live sensitivity cue appears when the second column’s relative orthogonal remainder is below ; it describes nearly parallel columns and never changes exact rank.
Forming squares the 2-norm condition number for full-column-rank . That can amplify roundoff even though the normal equations are mathematically correct. QR and SVD methods usually provide a safer practical route. Numerical rank tolerances must reflect scale and uncertainty and can legitimately differ from the exact rank of entered rationals.
Engineering and ML connections
Linear regression projects targets onto features. Redundant feature columns produce unidentifiable parameter directions but still define a closest predicted target vector. Orthogonal features simplify coefficient calculations. SVD extends this picture by organizing the input and output directions by strength and explaining a minimum-norm pseudoinverse solution.
Exercises
Summary and checkpoints
Build an orthogonal basis without normalizing zero residuals, decompose into projection plus perpendicular residual, derive the normal equations, and distinguish unique outputs from unique coefficients. Explain why practical QR/SVD can be safer than forming a Gram matrix.