1. When Exact Solutions Do Not Exist
In practical engineering and data science, we frequently encounter overdetermined systems: systems with far more equations (observations) than unknowns (features):
$$\mathbf{A}\mathbf{x} = \mathbf{b}, \quad \mathbf{A} \in \mathbb{R}^{m \times n} \text{ with } m \gg n$$
Because real-world telemetry contains sensor noise, measurement error, or physical imperfections, the target vector $\mathbf{b}$ rarely lies perfectly in the column space $C(\mathbf{A})$. An exact solution $\mathbf{x}$ almost never exists.
Instead of abandoning the problem, we seek the Least Squares solution $\mathbf{\hat{x}}$ that minimizes the Euclidean length of the error residual vector:
$$\mathbf{\hat{x}} = \arg\min_{\mathbf{x} \in \mathbb{R}^n} |\mathbf{A}\mathbf{x} - \mathbf{b}|_2^2$$
text
2. Derivation of the Normal Equations
Let $S(\mathbf{x}) = |\mathbf{A}\mathbf{x} - \mathbf{b}|_2^2 = (\mathbf{A}\mathbf{x} - \mathbf{b})^T (\mathbf{A}\mathbf{x} - \mathbf{b})$.
Expanding the scalar quadratic product:
$$S(\mathbf{x}) = \mathbf{x}^T \mathbf{A}^T \mathbf{A}\mathbf{x} - 2\mathbf{x}^T \mathbf{A}^T \mathbf{b} + \mathbf{b}^T \mathbf{b}$$
Taking the gradient with respect to $\mathbf{x}$:
$$\nabla_\mathbf{x} S(\mathbf{x}) = 2\mathbf{A}^T \mathbf{A}\mathbf{x} - 2\mathbf{A}^T \mathbf{b}$$
Setting the gradient to zero yields the fundamental Normal Equations:
$$\mathbf{A}^T \mathbf{A} \mathbf{x} = \mathbf{A}^T \mathbf{b}$$
text
If $\mathbf{A}$ has full column rank ($\text{rank}(\mathbf{A}) = n$), the $n \times n$ Gram matrix $\mathbf{A}^T \mathbf{A}$ is strictly symmetric and positive definite (invertible):
$$\mathbf{\hat{x}} = (\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T \mathbf{b}$$
3. The Moore-Penrose Pseudoinverse ($\mathbf{A}^+$)
What if $\mathbf{A}$ is rectangular ($m \neq n$) or rank-deficient (columns are linearly dependent)?
Matrix inversion $\mathbf{A}^{-1}$ does not exist. The Moore-Penrose Pseudoinverse $\mathbf{A}^+ \in \mathbb{R}^{n \times m}$ generalizes matrix inversion to all arbitrary matrices.
3.1 The Four Penrose Axioms
For any matrix $\mathbf{A}$, there exists a unique matrix $\mathbf{A}^+$ satisfying all four Moore-Penrose conditions:
- $\mathbf{A} \mathbf{A}^+ \mathbf{A} = \mathbf{A}$
- $\mathbf{A}^+ \mathbf{A} \mathbf{A}^+ = \mathbf{A}^+$
- $(\mathbf{A} \mathbf{A}^+)^T = \mathbf{A} \mathbf{A}^+$ (Hermitian / Symmetric)
- $(\mathbf{A}^+ \mathbf{A})^T = \mathbf{A}^+ \mathbf{A}$ (Hermitian / Symmetric)
3.2 Closed-Form Representations
- Tall Matrix with Full Column Rank ($m > n$, rank $= n$): $$\mathbf{A}^+ = (\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T \implies \mathbf{A}^+ \mathbf{A} = \mathbf{I}_n \quad (\text{Left Inverse})$$
- Wide Matrix with Full Row Rank ($m < n$, rank $= m$): $$\mathbf{A}^+ = \mathbf{A}^T (\mathbf{A} \mathbf{A}^T)^{-1} \implies \mathbf{A} \mathbf{A}^+ = \mathbf{I}_m \quad (\text{Right Inverse})$$
- Universal SVD Formulation: Let $\mathbf{A} = \mathbf{U} \mathbf{\Sigma} \mathbf{V}^T$ be the Singular Value Decomposition of $\mathbf{A}$. Then: $$\mathbf{A}^+ = \mathbf{V} \mathbf{\Sigma}^+ \mathbf{U}^T$$ where $\mathbf{\Sigma}^+$ is formed by transposing $\mathbf{\Sigma}$ and inverting every non-zero singular value ($\sigma_i \to 1/\sigma_i$).
4. Condition Numbers & Numerical Ill-Conditioning
When computing $(\mathbf{A}^T \mathbf{A})^{-1}$, a severe numerical danger emerges: squaring the condition number.
The Condition Number of a matrix measures how sensitive the solution $\mathbf{x}$ is to small perturbations in input $\mathbf{b}$:
$$\kappa(\mathbf{A}) = |\mathbf{A}|2 |\mathbf{A}^+|2 = \frac{\sigma{\max}(\mathbf{A})}{\sigma{\min}(\mathbf{A})}$$
- If $\kappa(\mathbf{A}) \approx 1$: Well-conditioned; errors are dampened.
- If $\kappa(\mathbf{A}) \ge 10^{12}$: Ill-conditioned; small floating-point roundoff errors completely corrupt the solution.
The Gram Matrix Squaring Penalty:
$$\kappa(\mathbf{A}^T \mathbf{A}) = (\kappa(\mathbf{A}))^2$$
If $\mathbf{A}$ has condition number $\kappa(\mathbf{A}) = 10^5$, the normal equations matrix $\mathbf{A}^T \mathbf{A}$ has condition number $10^{10}$! In 64-bit precision ($\approx 15\text{--}16$ decimal digits), you lose 10 digits of accuracy.
Production Numerical Rule: In high-precision scientific libraries (LAPACK, NumPy, Eigen), least squares is never solved via normal equations $(\mathbf{A}^T \mathbf{A})^{-1}\mathbf{A}^T \mathbf{b}$. It is solved via QR Factorization ($\mathbf{A} = \mathbf{Q}\mathbf{R} \implies \mathbf{R}\mathbf{x} = \mathbf{Q}^T \mathbf{b}$) or SVD, maintaining linear condition number $\kappa(\mathbf{A})$.
5. Tikhonov Regularization (Ridge Regression)
When features are near-collinear, $\sigma_{\min} \to 0$, causing the normal equations to explode.
Tikhonov Regularization (known as Ridge Regression in machine learning) adds a diagonal penalty to condition the matrix:
$$\min_{\mathbf{x}} \left( |\mathbf{A}\mathbf{x} - \mathbf{b}|_2^2 + \lambda |\mathbf{x}|_2^2 \right)$$
where $\lambda > 0$ is the regularization parameter.
Closed-Form Solution:
$$\nabla_\mathbf{x} \left( |\mathbf{A}\mathbf{x} - \mathbf{b}|_2^2 + \lambda \mathbf{x}^T \mathbf{x} \right) = 2\mathbf{A}^T (\mathbf{A}\mathbf{x} - \mathbf{b}) + 2\lambda \mathbf{x} = 0$$
$$(\mathbf{A}^T \mathbf{A} + \lambda \mathbf{I})\mathbf{x} = \mathbf{A}^T \mathbf{b}$$
$$\mathbf{\hat{x}}_{\text{ridge}} = (\mathbf{A}^T \mathbf{A} + \lambda \mathbf{I})^{-1} \mathbf{A}^T \mathbf{b}$$
text
6. Python Implementation: Least Squares Comparison (Normal vs QR vs SVD)
python
7. Key Takeaways
- Normal Equations: $\mathbf{A}^T \mathbf{A}\mathbf{x} = \mathbf{A}^T \mathbf{b}$ minimizes squared Euclidean error by enforcing that residual $\mathbf{b} - \mathbf{A}\mathbf{x}$ is orthogonal to $C(\mathbf{A})$.
- Never Square Condition Numbers: In production code, solve least squares using QR or SVD to avoid the $(\kappa(\mathbf{A}))^2$ condition squaring penalty.
- Tikhonov / Ridge Stabilization: Adding $\lambda \mathbf{I}$ shifts singular values away from zero, stabilizing ill-conditioned matrices.