Least Squares and the Pseudoinverse

⚡ Key Takeaways

Least squares finds the vector x^\hat{x} minimizing ∥Ax−b∥2\|Ax-b\|^2 when Ax=bAx=b has no exact solution. The unique minimum-norm solution is x^=A+b\hat{x}=A^{+}b, where A+A^{+} is the Moore–Penrose pseudoinverse. Applies to overdetermined/underdetermined linear systems, data fitting, regression, and signal processing. Originated with Gauss and Legendre (c. 1795–1805); pseudoinverse formalized by Moore (1920) and Penrose (1955).

💡 The Core Insight

You cannot always solve Ax=bAx=b, but you can always project bb onto the column space of AA and solve that. The residual of the best fit is perpendicular to everything AA can produce — this orthogonality is the whole theory in one geometric picture. The pseudoinverse is just the bookkeeping that makes "project, then solve, then pick the smallest" into a single matrix.

Formal Statement

DefinitionThe Least Squares Problem

Given A∈Rm×nA\in\mathbb{R}^{m\times n} and b∈Rmb\in\mathbb{R}^m, the least squares problem is to find x^∈Rn\hat{x}\in\mathbb{R}^n minimizing the squared Euclidean residual:

1
x^=arg⁡min⁡x∈Rn  ∥Ax−b∥2 2\quad \hat{x}=\arg\min_{x\in\mathbb{R}^n}\;\|Ax-b\|_2^{\,2}

Derivation hint: This is the definition; ∥v∥22=∑ivi2=v⊤v\|v\|_2^2=\sum_i v_i^2=v^\top v is the squared length of the residual vector v=Ax−bv=Ax-b. Symbols: AA is the design/coefficient matrix (mm rows = equations, nn columns = unknowns); bb is the observation vector; xx is the parameter vector; r=Ax−br=Ax-b is the residual; ∥⋅∥2\|\cdot\|_2 is the Euclidean norm.

DefinitionThe Normal Equations

Any minimizer x^\hat{x} of ∥Ax−b∥2\|Ax-b\|^2 satisfies the normal equations:

2
A⊤A x^=A⊤b\quad A^\top A\,\hat{x}=A^\top b

Derivation hint: Set the gradient ∇x∥Ax−b∥2=2A⊤(Ax−b)\nabla_x\|Ax-b\|^2=2A^\top(Ax-b) equal to zero. Here A⊤A∈Rn×nA^\top A\in\mathbb{R}^{n\times n} is symmetric and positive semidefinite. If AA has full column rank (rank⁡A=n\operatorname{rank}A=n), then A⊤AA^\top A is invertible and the solution is unique:

3
x^=(A⊤A)−1A⊤b\quad \hat{x}=(A^\top A)^{-1}A^\top b

Derivation hint: Left-multiply [2] by (A⊤A)−1(A^\top A)^{-1}, which exists exactly when AA's columns are linearly independent.

DefinitionThe Moore–Penrose Pseudoinverse

The Moore–Penrose pseudoinverse A+∈Rn×mA^{+}\in\mathbb{R}^{n\times m} of any A∈Rm×nA\in\mathbb{R}^{m\times n} is the unique matrix satisfying the four Penrose conditions:

4
AA+A=A,A+AA+=A+,(AA+)⊤=AA+,(A+A)⊤=A+A\quad AA^{+}A=A,\quad A^{+}AA^{+}=A^{+},\quad (AA^{+})^\top=AA^{+},\quad (A^{+}A)^\top=A^{+}A

Derivation hint: These four equations uniquely determine A+A^+; the first two are "weak inverse" axioms, the last two force the products AA+AA^+ and A+AA^+A to be orthogonal projectors (symmetric). The general least-squares / minimum-norm solution is then

5
x^=A+b\quad \hat{x}=A^{+}b

Derivation hint: A+A^+ projects bb onto col⁡(A)\operatorname{col}(A) and inverts on the row space, automatically selecting the smallest-norm minimizer.

TheoremExistence, Uniqueness, and Minimum Norm

For every AA and bb: 1. A least-squares minimizer of ∥Ax−b∥2\|Ax-b\|^2 always exists. 2. The set of minimizers is {x^+z:z∈ker⁡A}\{\hat{x}+z : z\in\ker A\} — a single point iff AA has full column rank. 3. Among all minimizers, A+bA^{+}b is the unique one of smallest Euclidean norm ∥x∥\|x\|.

TheoremPseudoinverse via the SVD

If A=UΣV⊤A=U\Sigma V^\top is a singular value decomposition with nonzero singular values σ1,…,σr\sigma_1,\dots,\sigma_r, then

6
A+=V Σ+U⊤,Σ+=diag⁡ ⁣(1σ1,…,1σr,0,…,0)\quad A^{+}=V\,\Sigma^{+}U^\top,\qquad \Sigma^{+}=\operatorname{diag}\!\left(\tfrac{1}{\sigma_1},\dots,\tfrac{1}{\sigma_r},0,\dots,0\right)

Derivation hint: Substitute A=UΣV⊤A=U\Sigma V^\top into the four Penrose conditions [4] and verify, using U⊤U=IU^\top U=I, V⊤V=IV^\top V=I, and that Σ+\Sigma^+ inverts the nonzero block while zeroing the rest.

Why It Holds / Derivation

ProofThe Normal Equations from Orthogonality

Step 1. Define f(x)=∥Ax−b∥2=(Ax−b)⊤(Ax−b)=x⊤A⊤Ax−2b⊤Ax+b⊤bf(x)=\|Ax-b\|^2=(Ax-b)^\top(Ax-b)=x^\top A^\top A x-2b^\top A x+b^\top b.

Step 2. Differentiate with respect to xx:

∇f(x)=2A⊤Ax−2A⊤b.\nabla f(x)=2A^\top A x-2A^\top b.

Since ff is convex (its Hessian 2A⊤A⪰02A^\top A\succeq 0), any stationary point is a global minimum.

Step 3. Set ∇f=0\nabla f=0: A⊤Ax=A⊤bA^\top A x=A^\top b, which is equation [2].

Step 4 (geometric meaning). Rewrite as A⊤(b−Ax^)=0A^\top(b-A\hat{x})=0, i.e. A⊤r=0A^\top r=0. This says the residual rr is orthogonal to every column of AA, hence orthogonal to col⁡(A)\operatorname{col}(A). Thus Ax^A\hat{x} is exactly the orthogonal projection of bb onto col⁡(A)\operatorname{col}(A) — the closest point in the column space to bb, confirming it minimizes distance.

Step 5 (full rank). If rank⁡A=n\operatorname{rank}A=n, the columns are independent, so A⊤AA^\top A is symmetric positive definite and invertible, giving the closed form [3].

Step 6 (rank-deficient / minimum norm). If ker⁡A≠{0}\ker A\neq\{0\}, any z∈ker⁡Az\in\ker A gives A(x^+z)=Ax^A(\hat{x}+z)=A\hat{x}, so x^+z\hat{x}+z is also a minimizer. Decompose any solution as x^=x^row+x^null\hat{x}=\hat{x}_{\text{row}}+\hat{x}_{\text{null}} with x^row∈row⁡(A)=ker⁡(A)⊥\hat{x}_{\text{row}}\in\operatorname{row}(A)=\ker(A)^\perp. Since these components are orthogonal, ∥x^∥2=∥x^row∥2+∥x^null∥2\|\hat{x}\|^2=\|\hat{x}_{\text{row}}\|^2+\|\hat{x}_{\text{null}}\|^2, minimized by taking x^null=0\hat{x}_{\text{null}}=0. That unique minimum-norm choice lies in the row space and equals A+bA^{+}b. ■\blacksquare

∎
CF
Carl Friedrich Gauss
1777–1855

Gauss claimed to have used least squares as early as 1795 to fit astronomical and geodetic data, famously predicting the reappearance of the asteroid Ceres in 1801 after it was lost behind the Sun — by fitting an orbit to a handful of noisy observations. He published the method in Theoria Motus Corporum Coelestium (1809) and, crucially, gave it a probabilistic justification: least squares yields the most probable parameters when measurement errors follow what we now call a Gaussian (normal) distribution. This connection between minimizing squared error and maximizing likelihood under normal noise remains the deep reason squared error dominates statistics.

Building Intuition

🧠 Intuition

Think of col⁡(A)\operatorname{col}(A) as a flat plane (or hyperplane) sitting inside Rm\mathbb{R}^m. The vector bb is a point in space that, generically, floats off the plane — that's why Ax=bAx=b has no solution. The best you can do is drop a perpendicular from bb straight down onto the plane; the foot of that perpendicular is Ax^A\hat{x}, and the dropped segment is the residual rr, which by construction makes a right angle with the plane. "Least squares" literally means "shortest perpendicular," and the right-angle condition A⊤r=0A^\top r=0 is the normal equations. The pseudoinverse adds a second idea for the underdetermined case. If the plane is too big (many xx map to the same point), then once you've landed on the plane there are infinitely many "addresses" xx for the same destination — they differ by a null-space direction in which AA is blind. The pseudoinverse chooses the address closest to the origin, refusing to add any invisible null-space component. So A+A^+ does two clean geometric acts: project (kill the part of bb outside the column space) and trim (kill the part of xx inside the null space).

Figure
Least squares as orthogonal projection: b is projected onto the column space of A, leaving a perpendicular residual.
col(A) O b A x̂ r = b − A x̂ A x̂ = projection of b onto col(A); residual r ⊥ col(A), i.e. Aᵀr = 0

Worked Examples

ExampleOverdetermined: Fitting a Line Through Three Points

Given: Fit y=c0+c1ty=c_0+c_1t to data (t,y)=(0,1),(1,3),(2,4)(t,y)=(0,1),(1,3),(2,4). Three equations, two unknowns.

A=(101112),b=(134),x=(c0c1).A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix},\quad b=\begin{pmatrix}1\\3\\4\end{pmatrix},\quad x=\begin{pmatrix}c_0\\c_1\end{pmatrix}.

Step 1 — form A⊤AA^\top A and A⊤bA^\top b:

A⊤A=(3335),A⊤b=(811).A^\top A=\begin{pmatrix}3&3\\3&5\end{pmatrix},\qquad A^\top b=\begin{pmatrix}8\\11\end{pmatrix}.

Step 2 — solve the normal equations (3335)x=(811)\begin{pmatrix}3&3\\3&5\end{pmatrix}x=\begin{pmatrix}8\\11\end{pmatrix}. Determinant =15−9=6=15-9=6.

x^=16(5−3−33)(811)=16(40−33−24+33)=16(79).\hat{x}=\frac{1}{6}\begin{pmatrix}5&-3\\-3&3\end{pmatrix}\begin{pmatrix}8\\11\end{pmatrix}=\frac{1}{6}\begin{pmatrix}40-33\\-24+33\end{pmatrix}=\frac{1}{6}\begin{pmatrix}7\\9\end{pmatrix}.

Step 3 — read off: c0=7/6≈1.167c_0=7/6\approx1.167, c1=9/6=1.5c_1=9/6=1.5.

Residual check: fits are y^=(1.167, 2.667, 4.167)\hat y=(1.167,\,2.667,\,4.167); residuals (−0.167, 0.333, −0.167)(-0.167,\,0.333,\,-0.167) sum to zero (as they must, since the column of ones is in col⁡A\operatorname{col}A).

  y=1.167+1.5 t(best-fit line)  \boxed{\;y=1.167+1.5\,t\quad(\text{best-fit line})\;}
ExampleUnderdetermined: Minimum-Norm Solution via Pseudoinverse

Given: One equation, two unknowns: x1+2x2=5x_1+2x_2=5, so A=(1    2)A=(1\;\;2), b=(5)b=(5). Infinitely many solutions lie on a line; we want the smallest-norm point.

Step 1 — for a full-row-rank wide matrix, A+=A⊤(AA⊤)−1A^{+}=A^\top(AA^\top)^{-1}.

AA⊤=12+22=5,(AA⊤)−1=15.AA^\top=1^2+2^2=5,\qquad (AA^\top)^{-1}=\tfrac15.

Step 2 — compute:

A+=(12)⋅15=(0.20.4),x^=A+b=(0.20.4)⋅5=(12).A^{+}=\begin{pmatrix}1\\2\end{pmatrix}\cdot\tfrac15=\begin{pmatrix}0.2\\0.4\end{pmatrix},\qquad \hat{x}=A^{+}b=\begin{pmatrix}0.2\\0.4\end{pmatrix}\cdot5=\begin{pmatrix}1\\2\end{pmatrix}.

Step 3 — verify minimum norm. Check x^\hat x satisfies the constraint: 1+2(2)=51+2(2)=5 ✓. Its norm is 1+4=5≈2.236\sqrt{1+4}=\sqrt5\approx2.236. Any other solution, e.g. (5,0)(5,0), has norm 55 — larger. The solution (1,2)(1,2) is parallel to the row (1,2)(1,2), confirming it lies in the row space (zero null-space component).

  x^=(1, 2),∥x^∥=5  (minimum-norm solution)  \boxed{\;\hat{x}=(1,\,2),\quad \|\hat{x}\|=\sqrt5\;(\text{minimum-norm solution})\;}

Common Misconceptions

⚠️ Common mistake

Do not invert A⊤AA^\top A in practice. Forming and solving the normal equations [3] squares the condition number (κ(A⊤A)=κ(A)2\kappa(A^\top A)=\kappa(A)^2), amplifying rounding errors badly when AA is ill-conditioned. Professional solvers use QR decomposition (A=QR⇒Rx^=Q⊤bA=QR\Rightarrow R\hat x=Q^\top b) or the SVD, which work directly with κ(A)\kappa(A). The normal equations are pedagogically clean but numerically fragile.

A+A^{+} is not generally a true inverse. Only when AA is square and invertible does A+=A−1A^{+}=A^{-1}. In general A+A≠IA^{+}A\neq I and AA+≠IAA^{+}\neq I; instead these are orthogonal projectors onto row⁡(A)\operatorname{row}(A) and col⁡(A)\operatorname{col}(A) respectively.

Counterexample

Least squares is not invariant to rescaling variables or weighting equations. Consider doubling the second equation in a system: replacing row a2⊤x=b2a_2^\top x=b_2 with 2a2⊤x=2b22a_2^\top x=2b_2 leaves the exact solution set unchanged, but it changes the least-squares answer, because the squared residual of that equation now counts four times as heavily. Concretely, fitting (0,0),(1,0),(1,10)(0,0),(1,0),(1,10) to a constant y=cy=c gives c^=10/3≈3.33\hat c=10/3\approx3.33; if we instead believe the third point is twice as reliable and weight it doubly, c^\hat c shifts toward 55. Least squares optimizes the given metric — choosing the wrong weights silently distorts the fit. (This is why weighted least squares and generalized least squares exist.)

Connections

connection

Least squares sits at a crossroads of linear algebra, geometry, and statistics:

  • <span class="wiki-link-missing">Orthogonal Projection</span> — Ax^=P bA\hat x=P\,b where P=AA+P=AA^{+} is the projector onto col⁡(A)\operatorname{col}(A); least squares is projection.
  • <a class="wiki-link" href="/wiki/cs-ml-singular-value-decomposition">Singular Value Decomposition</a> — provides the universal formula [6] for A+A^{+} and reveals how singular values control conditioning and stability.
  • <a class="wiki-link" href="/wiki/qr-decomposition">QR Decomposition</a> — the numerically preferred route to x^\hat x via Gram–Schmidt or Householder reflections.
  • <a class="wiki-link" href="/wiki/linear-regression">Linear Regression</a> — ordinary least squares is the statistical estimator; under Gaussian noise it is the maximum-likelihood and (Gauss–Markov) best linear unbiased estimator.
  • <span class="wiki-link-missing">Tikhonov Regularization</span> (ridge regression) — replaces A+A^+ with (A⊤A+λI)−1A⊤(A^\top A+\lambda I)^{-1}A^\top to tame ill-posed problems.
  • <span class="wiki-link-missing">Four Fundamental Subspaces</span> — the pseudoinverse maps col⁡(A)→row⁡(A)\operatorname{col}(A)\to\operatorname{row}(A) bijectively and kills the orthogonal complements.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1795 — Gauss (age 18) reportedly uses least squares privately for geodetic computations, but does not publish.
- 1801 — Gauss recovers the lost asteroid Ceres by fitting an orbit to sparse observations, dramatically demonstrating the method's power.
- 1805 — Adrien-Marie Legendre publishes least squares first, in an appendix to Nouvelles méthodes pour la détermination des orbites des comètes, coining the term méthode des moindres carrés.
- 1809 — Gauss publishes Theoria Motus, claiming priority and adding the probabilistic (Gaussian-error) justification, igniting a priority dispute with Legendre.
- 1821–1823 — Gauss proves the Gauss–Markov theorem: least squares gives the best linear unbiased estimator regardless of the error distribution (given finite, equal variances).
- 1900–1909 — Erik Ivar Fredholm and others develop integral-equation analogues; foundations for generalized inverses laid.
- 1920 — E. H. Moore defines a "general reciprocal" (the pseudoinverse) for arbitrary matrices, in obscure notation that goes largely unnoticed.
- 1951 — Arne Bjerhammar independently rediscovers the generalized inverse in a geodesy context.
- 1955 — Roger Penrose gives the clean four-condition characterization [4] in "A generalized inverse for matrices," fusing the concept with modern matrix theory; the object becomes the Moore–Penrose pseudoinverse.
- 1965 — Golub & Kahan develop stable SVD algorithms, making pseudoinverse computation routine and numerically sound.

Subtleties & Limits

open-problem

The pseudoinverse is discontinuous at points where the rank of AA changes: a tiny perturbation that lifts a zero singular value to ε\varepsilon makes A+A^{+} jump by ∼1/ε\sim 1/\varepsilon. This makes naive rank determination numerically delicate — how small is "zero"? — and motivates truncated SVD and regularization, which trade a little bias for vastly better stability. Choosing the truncation rank or the regularization parameter λ\lambda optimally (cross-validation, L-curve, discrepancy principle) is an active, problem-dependent art with no universal answer. A deeper subtlety: squared error is optimal only under Gaussian noise. Real data with outliers or heavy tails make least squares non-robust — a single bad point can dominate. This drives ongoing research into robust regression (ℓ1\ell_1, Huber loss), sparse recovery (ℓ1\ell_1/LASSO, compressed sensing), and the broader question of which loss function the geometry of a given data-generating process actually warrants. The pseudoinverse answers the ℓ2\ell_2 question definitively; for other norms there is no closed-form analogue.

See Also

Orthogonal Projection · Singular Value Decomposition · QR Decomposition · Linear Regression · Gauss–Markov Theorem · Tikhonov Regularization · Four Fundamental Subspaces · Condition Number