4.5.41Linear Algebra (Full)

Least squares — normal equations, QR approach

1,717 words8 min readdifficulty · medium6 backlinks

WHAT is the least-squares problem?

WHY squared length? It is differentiable (smooth) everywhere, while \|\cdot\| has a kink at zero. Minimising the square is equivalent to minimising the length (same minimiser), and the algebra stays linear.


HOW to derive the Normal Equations (from scratch)

We want the minimum of f(x)=bAx2f(\mathbf{x}) = \|\mathbf{b} - A\mathbf{x}\|^2. Two independent derivations — pick whichever clicks.

Derivation 1 — Geometry (projection / orthogonality)

The set {Ax}\{A\mathbf{x}\} is the column space Col(A)\text{Col}(A), a subspace. The closest point in a subspace to b\mathbf{b} is the orthogonal projection. So the residual must be perpendicular to every column of AA:

aj(bAx^)=0for every column aj.\mathbf{a}_j^\top (\mathbf{b} - A\hat{\mathbf{x}}) = 0 \quad \text{for every column } \mathbf{a}_j.

Why this step? If the residual had any component lying inside Col(A)\text{Col}(A), we could move AxA\mathbf{x} a little in that direction and get closer — so at the true minimum the residual has zero component in the column space, i.e. it's orthogonal to all columns.

Stacking all columns gives A(bAx^)=0A^\top(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0}, hence

AAx^=Ab\boxed{A^\top A\,\hat{\mathbf{x}} = A^\top \mathbf{b}}

Derivation 2 — Calculus

Expand: f(x)=(bAx)(bAx)=bb2xAb+xAAx.f(\mathbf{x}) = (\mathbf{b}-A\mathbf{x})^\top(\mathbf{b}-A\mathbf{x}) = \mathbf{b}^\top\mathbf{b} - 2\mathbf{x}^\top A^\top \mathbf{b} + \mathbf{x}^\top A^\top A \mathbf{x}. Set the gradient to zero (using x(xMx)=2Mx\nabla_\mathbf{x}(\mathbf{x}^\top M\mathbf{x}) = 2M\mathbf{x} for symmetric M=AAM=A^\top A, and x(xc)=c\nabla_\mathbf{x}(\mathbf{x}^\top\mathbf{c}) = \mathbf{c}): f=2Ab+2AAx=0    AAx^=Ab.\nabla f = -2A^\top\mathbf{b} + 2A^\top A\,\mathbf{x} = \mathbf{0} \;\Rightarrow\; A^\top A\hat{\mathbf{x}} = A^\top\mathbf{b}. Why a minimum, not max? The Hessian 2AA2A^\top A is positive semidefinite (since vAAv=Av20\mathbf{v}^\top A^\top A\mathbf{v} = \|A\mathbf{v}\|^2 \ge 0), so the critical point is a minimum.


HOW the QR approach works (and why it's better)

WHY not just use normal equations? Forming AAA^\top A squares the condition number (κ(AA)=κ(A)2\kappa(A^\top A)=\kappa(A)^2), amplifying rounding errors. QR avoids forming AAA^\top A at all.

Derivation. Substitute A=QRA=QR into the normal equations: (QR)(QR)x^=(QR)b    RQQIRx^=RQb    RRx^=RQb.(QR)^\top(QR)\hat{\mathbf{x}} = (QR)^\top\mathbf{b} \;\Rightarrow\; R^\top\underbrace{Q^\top Q}_{I}R\,\hat{\mathbf{x}} = R^\top Q^\top\mathbf{b} \;\Rightarrow\; R^\top R\hat{\mathbf{x}}=R^\top Q^\top\mathbf{b}. Since RR is invertible (full rank), cancel RR^\top:

Rx^=Qb\boxed{R\,\hat{\mathbf{x}} = Q^\top\mathbf{b}}

Why this step? RR is triangular, so solve by back-substitution — fast and numerically stable. No AAA^\top A ever appears.

Figure — Least squares — normal equations, QR approach

Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine throwing darts that should all hit one straight line, but your hand wobbles so they scatter. You can't draw a line through all of them. So you draw the line that makes the total miss-distance (measured up-and-down) as small as possible. The "miss" arrows must point straight away from the line — if any leaned along the line, you could slide the line and do better. Least squares finds that fairest line. QR is just a tidier way to do the arithmetic that doesn't blow up your calculator's rounding errors.


Active Recall

What does least squares minimise?
The squared residual norm bAx22\|\mathbf{b}-A\mathbf{x}\|_2^2.
State the normal equations.
AAx^=AbA^\top A\hat{\mathbf{x}} = A^\top\mathbf{b}.
Geometrically, why must the residual be orthogonal to Col(A)?
Otherwise it has a component inside the column space; moving AxA\mathbf{x} along it would reduce the distance, contradicting minimality.
When is AAA^\top A invertible?
Exactly when AA has full column rank (independent columns).
What is the projection matrix onto Col(A)?
P=A(AA)1AP = A(A^\top A)^{-1}A^\top, with P2=PP^2=P, P=PP^\top=P.
In A=QRA=QR, what properties do Q and R have?
QQ has orthonormal columns (QQ=IQ^\top Q=I); RR is upper triangular and invertible.
What equation does QR reduce least squares to?
Rx^=QbR\hat{\mathbf{x}} = Q^\top\mathbf{b}, solved by back-substitution.
Why prefer QR over normal equations numerically?
Forming AAA^\top A squares the condition number (κ(AA)=κ(A)2\kappa(A^\top A)=\kappa(A)^2); QR avoids it, staying stable.
Why use the squared norm rather than the norm itself?
It's smooth/differentiable everywhere and gives the same minimiser, keeping the equations linear.

Connections

  • Orthogonal Projection — least squares is projection onto Col(A).
  • QR Factorization — the stable engine behind the solve.
  • Gram-Schmidt Process — one way to build QQ.
  • Column Space and Rank — full rank ⇔ unique solution.
  • Pseudoinverse (Moore-Penrose) — generalises when rank is deficient.
  • Condition Number — why squaring κ\kappa hurts.
  • Linear Regression — statistics application of these equations.

Concept Map

minimise residual norm

defines

solved via

solved via

residual orthogonal to Col A

set gradient to zero

Hessian 2 A^T A is PSD

if full column rank

gives

closest point to b

better conditioned via

Tall system Ax=b, no exact solution

Least-squares solution x-hat

Residual r = b - Ax-hat

Geometry derivation

Calculus derivation

Normal equations A^T A x = A^T b

Guarantees a minimum

x-hat = A^T A inverse times A^T b

Projection A x-hat onto Col A

QR approach

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, problem ye hai: tumhare paas Ax=bA\mathbf{x}=\mathbf{b} hai jisme equations zyada hain aur unknowns kam (tall matrix). Aksar exact solution milta hi nahi, kyunki b\mathbf{b} column space ke andar nahi baithta. Toh hum perfection chhod ke poochte hain — "kaunsa x^\hat{\mathbf{x}} lagaye ki Ax^A\hat{\mathbf{x}} aur b\mathbf{b} ke beech distance sabse kam ho?" Yahi least squares hai, aur iska answer ek projection hai: Ax^A\hat{\mathbf{x}} banta hai b\mathbf{b} ka column space par shadow.

Key insight: jo bachi hui galti (residual bAx^\mathbf{b}-A\hat{\mathbf{x}}) hai, woh column space ke perpendicular honi chahiye. Agar woh thodi bhi plane ke andar lean kare, toh hum line ko adjust karke aur paas ja sakte the — matlab minimum nahi tha. Is orthogonality se seedha nikalta hai normal equation: AAx^=AbA^\top A\hat{\mathbf{x}}=A^\top\mathbf{b}. Yahi yaad rakho — "AT-A, AT-b" drumbeat ki tarah.

QR approach kyun? Jab tum AAA^\top A banate ho, toh condition number square ho jata hai, yaani rounding errors double bure ho jaate hain. Isliye hum A=QRA=QR likhte hain (QQ ke columns orthonormal, RR upper triangular). Substitute karne par sab simplify hoke ban jata hai Rx^=QbR\hat{\mathbf{x}}=Q^\top\mathbf{b} — jo triangular hai, toh back-substitution se phataphat aur safely solve ho jata hai. Answer dono methods ka same aata hai, bas QR zyada stable hai.

Practical baat: ye exactly linear regression ka maths hai — best-fit line/curve nikaalna. Toh jab bhi tum data par line fit karoge, andar yahi normal equations ya QR chal raha hota hai. Full column rank hona zaroori hai warna AAA^\top A invertible nahi hoga aur solution unique nahi rahega.

Go deeper — visual, from zero

Test yourself — Linear Algebra (Full)

Connections