Foundations to Frontier
Tests
A. Mathematical Foundations › A2 Linear Algebra › Decompositions

The Moore–Penrose Pseudo-Inverse and Least Squares

When an equation has no exact solution, find the best one. When it has infinitely many, find the smallest. The pseudo-inverse does both, and it's the engine of regression, sensor calibration and robot inverse kinematics.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

When the clues disagree

One clock says it’s 3:00, another says 3:04. They can’t both be right. A sensible compromise is 3:02, which is “least wrong” overall. Three friends guess a jar holds 4, 5 and 6 sweets. The single guess that’s closest to all of them, measured by adding up the squared misses, is the average, 5.

When measurements don’t agree exactly, we look for the answer that makes the total squared mistake as small as possible. That’s the method of least squares.

The best straight line

Dots on a graph never sit perfectly on a line. The least-squares line is the one with the smallest total squared vertical gaps. Drag the orange point and watch the line respond:

Least-squares line fitting
least squares: y = 0.94 + 0.705x · SSE = 0.23
Dashed orange = residuals. Check: Σ residuals = 0.0000 and Σ x·residual = 0.0000. Both are 0, because the residual is orthogonal to the columns of X. Ridge shrinks the slope toward 0.
How it started

In 1801, astronomers lost track of the newly found dwarf planet Ceres behind the Sun. A 24-year-old Carl Friedrich Gauss used least squares on a handful of noisy observations to predict where it would reappear, and he was right.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Points (0,0), (1,1), (2,2). Slope of the best line?
  2. 2. Guesses 4, 5, 6. Best single guess (least squared miss)?
  3. 3. Clues x = 3 and x = 5 disagree. The least-squares compromise?

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The idea in numbers

Fit y=mxy = mx (a line through the origin) to the points (1,2)(1,2) and (2,3)(2,3). We minimize (2−m)2+(3−2m)2(2 - m)^2 + (3 - 2m)^2. Setting the derivative to zero gives

m=∑xiyi∑xi2=1⋅2+2⋅31+4=1.6m = \frac{\sum x_iy_i}{\sum x_i^2} = \frac{1\cdot2 + 2\cdot3}{1 + 4} = 1.6

The residuals (misses) are 2−1.6=0.42 - 1.6 = 0.4 and 3−3.2=−0.23 - 3.2 = -0.2.

The mean is a least-squares answer

The number cc minimizing ∑(xi−c)2\sum(x_i - c)^2 is the mean (A2.1-1). For 2, 4 and 9, that’s 5.

A first look at the pseudo-inverse

For a number aa, the “pseudo-inverse” is 1/a1/a if a≠0a\ne0, and 0 if a=0a = 0. Matrices get the same idea: invert what can be inverted, and leave the rest at zero.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Best c minimizing Σ(xᵢ − c)² for 2, 4, 9?
  2. 2. Pseudo-inverse of the number 4?
  3. 3. Pseudo-inverse of the number 0?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Least squares in matrix form

Given an overdetermined system Ax≈bA\mathbf x\approx\mathbf b (more equations than unknowns), minimize ∥Ax−b∥2\lVert A\mathbf x - \mathbf b\rVert^2. The solution satisfies the normal equations

A⊤A x^=A⊤bA^\top A\,\hat{\mathbf x} = A^\top\mathbf b

Geometrically, Ax^A\hat{\mathbf x} is the projection of b\mathbf b onto the column space, and the residual r=b−Ax^\mathbf r = \mathbf b - A\hat{\mathbf x} is perpendicular to every column: A⊤r=0A^\top\mathbf r = \mathbf 0. You can see this in the demo, where both residual sums are 0.

Example: a line y=a+bxy = a + bx through (0,1),(1,3),(2,4)(0,1), (1,3), (2,4). Here A=(101112)A = \begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}, A⊤A=(3335)A^\top A = \begin{pmatrix}3&3\\3&5\end{pmatrix} and A⊤b=(8,11)A^\top\mathbf b = (8, 11). Solving gives b=1.5b = 1.5 and a=7/6≈1.167a = 7/6\approx1.167.

The Moore–Penrose pseudo-inverse

With the SVD A=UΣV⊤A = U\Sigma V^\top:

A+=VΣ+U⊤,Σ+=diag⁡(1/σ1,…,1/σr,0,…,0)A^+ = V\Sigma^+U^\top, \qquad \Sigma^+ = \operatorname{diag}(1/\sigma_1,\dots,1/\sigma_r, 0,\dots,0)

x^=A+b\hat{\mathbf x} = A^+\mathbf b is always the right answer:

  • overdetermined (no exact solution): the least-squares solution;
  • underdetermined (infinitely many solutions): the one with the smallest norm;
  • square and invertible: just A−1bA^{-1}\mathbf b.
🐍 Exercise 1: the pseudo-inverse from the SVD
Build A⁺ = V Σ⁺ Uᵀ (zeroing tiny σ). Check it against np.linalg.pinv and the four Penrose conditions, including on a rank-deficient matrix.

Ridge regression: stabilized least squares

When columns are nearly dependent, small σi\sigma_i make 1/σi1/\sigma_i huge, and the solution swings wildly with noise. Ridge (Tikhonov) regularization adds λ∥x∥2\lambda\lVert\mathbf x\rVert^2:

x^λ=(A⊤A+λI)−1A⊤b=Vdiag⁡(σiσi2+λ)U⊤b\hat{\mathbf x}_\lambda = (A^\top A + \lambda I)^{-1}A^\top\mathbf b = V\operatorname{diag}\Big(\frac{\sigma_i}{\sigma_i^2 + \lambda}\Big)U^\top\mathbf b

Each 1/σi1/\sigma_i becomes σi/(σi2+λ)\sigma_i/(\sigma_i^2+\lambda), which is bounded by 1/(2λ)1/(2\sqrt\lambda). This is ridge regression (D2), weight decay (E4), and the trick that stabilizes inverse kinematics near singularities (Level 4).

📝 Quick check (Level 2)
0/3 answered
  1. 1. At the least-squares solution, the residual is perpendicular to…
  2. 2. For an underdetermined consistent system, A⁺b gives…
  3. 3. Ridge replaces 1/σ with…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

Why the normal equations give the minimum

Theorem

x^\hat{\mathbf x} minimizes ∥Ax−b∥\lVert A\mathbf x - \mathbf b\rVert if and only if A⊤(Ax^−b)=0A^\top(A\hat{\mathbf x} - \mathbf b) = \mathbf 0.

Proof. For any x\mathbf x, write Ax−b=(Ax^−b)+A(x−x^)A\mathbf x - \mathbf b = (A\hat{\mathbf x} - \mathbf b) + A(\mathbf x - \hat{\mathbf x}). The second term is in the column space, which is orthogonal to the first term by the condition. By Pythagoras, ∥Ax−b∥2=∥Ax^−b∥2+∥A(x−x^)∥2≥∥Ax^−b∥2\lVert A\mathbf x - \mathbf b\rVert^2 = \lVert A\hat{\mathbf x} - \mathbf b\rVert^2 + \lVert A(\mathbf x - \hat{\mathbf x})\rVert^2 \ge\lVert A\hat{\mathbf x} - \mathbf b\rVert^2. ∎ (The converse follows by setting the gradient 2A⊤(Ax−b)2A^\top(A\mathbf x - \mathbf b) to zero.)

The four Penrose conditions

A+A^+ is the unique matrix XX satisfying

AXA=A,XAX=X,(AX)⊤=AX,(XA)⊤=XA.AXA = A, \quad XAX = X, \quad (AX)^\top = AX, \quad (XA)^\top = XA.

AA+AA^+ is the orthogonal projector onto im⁡A\operatorname{im}A, and A+AA^+A projects onto the row space. For full column rank, A+=(A⊤A)−1A⊤A^+ = (A^\top A)^{-1}A^\top. For full row rank, A+=A⊤(AA⊤)−1A^+ = A^\top(AA^\top)^{-1}.

Minimum norm

If Ax=bA\mathbf x = \mathbf b has many solutions, they are xp+ker⁡A\mathbf x_p + \ker A (A2.1-6). A+bA^+\mathbf b lies in the row space, which is orthogonal to ker⁡A\ker A, so by Pythagoras every other solution is longer. For x1+x2=2x_1 + x_2 = 2, the minimum-norm solution is (1,1)(1,1), with norm 2\sqrt2.

Statistics: the Gauss–Markov theorem

If b=Ax+ε\mathbf b = A\mathbf x + \boldsymbol\varepsilon with zero-mean, uncorrelated, equal-variance noise, the least-squares estimate is the best linear unbiased estimator (BLUE): no other linear unbiased estimator has smaller variance. With Gaussian noise, least squares is also the maximum-likelihood estimate (A5).

Exercise 3.1: Compute the pseudo-inverse of the column vector a = (3, 4)

Full column rank, so a+=(a⊤a)−1a⊤=125(3,4)=(0.12,0.16)\mathbf a^+ = (\mathbf a^\top\mathbf a)^{-1}\mathbf a^\top = \tfrac1{25}(3, 4) = (0.12, 0.16). Check: a+a=1\mathbf a^+\mathbf a = 1.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A⁺ of the column (3, 4): its first entry?
  2. 2. trace(AA⁺) equals…
  3. 3. Least squares is BLUE under…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Inverse kinematics with the pseudo-inverse

A robot arm needs joint steps Δq\Delta\mathbf q that move its hand by Δx\Delta\mathbf x. Linearize, JΔq≈ΔxJ\Delta\mathbf q\approx\Delta\mathbf x, and solve with Δq=J+Δx\Delta\mathbf q = J^+\Delta\mathbf x. That’s least squares if the target can’t be reached exactly, and minimum norm (the smallest joint motion) for redundant arms. Near a singularity, a tiny σmin⁡\sigma_{\min} makes J+J^+ explode. Damped least squares (Wampler, 1986; Nakamura and Hanafusa, 1986) uses J⊤(JJ⊤+λ2I)−1J^\top(JJ^\top + \lambda^2I)^{-1}, which is ridge for robots. It trades a little accuracy for bounded, safe joint speeds. Most industrial and research IK solvers use it.

🐍 Exercise 2 (research): pseudo-inverse vs damped IK near a singularity
For a 2-link arm approaching a straight elbow, compare the joint step from the plain pseudo-inverse with damped least squares.

Least squares everywhere in research

  • SLAM and bundle adjustment solve huge nonlinear least-squares problems by repeated linear least squares (Gauss–Newton and Levenberg–Marquardt, A6, T3). Levenberg–Marquardt is exactly ridge applied at each step.
  • Kalman filters are recursive least squares with uncertainty (T1).
  • Implicit bias in deep learning. Gradient descent from zero on an underdetermined linear problem converges to the minimum-norm (pseudo-inverse) solution. This is a starting point for explaining why over-parameterized models generalize (“benign overfitting”, Bartlett et al., 2020; B4).
  • Physics-informed and scientific ML. Many inverse problems (A1-1, J7) are regularized least squares, and the choice of regularizer encodes prior physical knowledge.

References

  1. A.-M. Legendre (1805). Nouvelles méthodes pour la détermination des orbites des comètes. (First publication of least squares.)
  2. C. F. Gauss (1809). Theoria motus corporum coelestium. (Least squares and the Ceres prediction.)
  3. E. H. Moore (1920) and R. Penrose (1955). “A generalized inverse for matrices.” Proceedings of the Cambridge Philosophical Society, 51.
  4. A. Tikhonov (1963). “Solution of incorrectly formulated problems and the regularization method.” Soviet Mathematics Doklady, 4.
  5. A. Hoerl and R. Kennard (1970). “Ridge regression: biased estimation for nonorthogonal problems.” Technometrics, 12(1).
  6. C. Wampler (1986). “Manipulator inverse kinematic solutions based on vector formulations and damped least-squares methods.” IEEE Transactions on Systems, Man, and Cybernetics, 16(1).
  7. Y. Nakamura and H. Hanafusa (1986). “Inverse kinematic solutions with singularity robustness for robot manipulator control.” Journal of Dynamic Systems, Measurement, and Control, 108(3).
  8. P. Bartlett, P. Long, G. Lugosi, A. Tsigler (2020). “Benign overfitting in linear regression.” PNAS, 117(48).