Skip to content

Least Squares


Method

Let AA be a real m×nm\times n matrix and b∈Rm\boldsymbol{b}\in\mathbb{R}^m. A least-squares solution of Ax=bA\boldsymbol{x}=\boldsymbol{b} is a vector x^∈Rn\hat{\boldsymbol{x}}\in\mathbb{R}^n such that, for all x∈Rn\boldsymbol{x}\in\mathbb{R}^n,

∥b−Ax^∥≤∥b−Ax∥,\|\boldsymbol{b}-A\hat{\boldsymbol{x}}\|\le\|\boldsymbol{b}-A\boldsymbol{x}\|,

where ∥⋅∥\|\cdot\| is the Euclidean norm. To find it, solve the normal equations

A⊤Ax^=A⊤b.A^{\top}A\hat{\boldsymbol{x}}=A^{\top}\boldsymbol{b}.

The vector b−Ax^\boldsymbol{b}-A\hat{\boldsymbol{x}} is the residual.


Interpretation

When b\boldsymbol{b} is not in the column space Col⁡(A)\operatorname{Col}(A) (the range of AA), the system has no solution, typically because there are more equations than unknowns. Least squares replaces b\boldsymbol{b} by the closest attainable vector, the orthogonal projection p\boldsymbol{p} of b\boldsymbol{b} onto Col⁡(A)\operatorname{Col}(A), and solves Ax^=pA\hat{\boldsymbol{x}}=\boldsymbol{p}.

The normal equations say that the residual is orthogonal to every column, since the rows of the transpose are the columns of AA: A⊤(b−Ax^)=0A^{\top}(\boldsymbol{b}-A\hat{\boldsymbol{x}})=\boldsymbol{0}, that is, the residual lies in Null⁡(A⊤)=Col⁡(A)⊥\operatorname{Null}(A^{\top})=\operatorname{Col}(A)^\perp given by the Fundamental Theorem of Linear Algebra. The same equations follow from setting the gradient of ∥b−Ax∥2\|\boldsymbol{b}-A\boldsymbol{x}\|^2, namely 2A⊤Ax−2A⊤b2A^{\top}A\boldsymbol{x}-2A^{\top}\boldsymbol{b}, to zero.

Misunderstandings to avoid: the fitted vector Ax^A\hat{\boldsymbol{x}} is always unique, but x^\hat{\boldsymbol{x}} need not be; and the formula (A⊤A)−1A⊤b(A^{\top}A)^{-1}A^{\top}\boldsymbol{b} is valid only when the columns of AA are linearly independent.


Key Results

  • The normal equations are always consistent, and their solution set equals the set of least-squares solutions.
  • Since Null⁡(A⊤A)=Null⁡(A)\operatorname{Null}(A^{\top}A)=\operatorname{Null}(A), the least-squares solutions form the affine subspace {x^}+Null⁡(A)\{\hat{\boldsymbol{x}}\}+\operatorname{Null}(A).
  • The solution is unique if and only if AA has full column rank nn; then x^=(A⊤A)−1A⊤b\hat{\boldsymbol{x}}=(A^{\top}A)^{-1}A^{\top}\boldsymbol{b} and p=Hb\boldsymbol{p}=H\boldsymbol{b} with the symmetric idempotent matrix H=A(A⊤A)−1A⊤H=A(A^{\top}A)^{-1}A^{\top}.
  • In general, x^+=A+b\hat{\boldsymbol{x}}^+=A^+\boldsymbol{b}, with A+A^+ the Moore-Penrose inverse, is the unique least-squares solution of minimum norm; it is the one lying in the row space Row⁡(A)\operatorname{Row}(A).

Examples

Fitting a line. Fit y=c0+c1ty=c_0+c_1t to the points (0,1),(1,2),(2,2)(0,1),(1,2),(2,2). Then

A=(101112),b=(122),A⊤A=(3335),A⊤b=(56).A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix},\quad \boldsymbol{b}=\begin{pmatrix}1\\2\\2\end{pmatrix},\qquad A^{\top}A=\begin{pmatrix}3&3\\3&5\end{pmatrix},\quad A^{\top}\boldsymbol{b}=\begin{pmatrix}5\\6\end{pmatrix}.

Solving 3c0+3c1=53c_0+3c_1=5, 3c0+5c1=63c_0+5c_1=6 gives c1=12c_1=\tfrac{1}{2}, c0=76c_0=\tfrac{7}{6}. The fitted values are (76,53,136)⊤(\tfrac{7}{6},\tfrac{5}{3},\tfrac{13}{6})^{\top}, the residual is (−16,13,−16)⊤(-\tfrac{1}{6},\tfrac{1}{3},-\tfrac{1}{6})^{\top}, whose entries sum to 00 and are orthogonal to (0,1,2)⊤(0,1,2)^{\top}, and the minimal sum of squares is 16\tfrac{1}{6}.

The three data points (0,1), (1,2), (2,2), the least-squares line y = 7/6 + t/2 and the vertical residuals -1/6, 1/3 and -1/6 between each point and the line.

In the figure, the residuals are the vertical gaps between the data and the fitted line, −16-\tfrac{1}{6}, 13\tfrac{1}{3} and −16-\tfrac{1}{6}. Their squares add up to 16\tfrac{1}{6}, and no other line does better. Seen in R3\mathbb{R}^3, the fitted values (76,53,136)⊤(\tfrac{7}{6},\tfrac{5}{3},\tfrac{13}{6})^{\top} are the orthogonal projection of b\boldsymbol{b} onto Col⁡(A)\operatorname{Col}(A), and the residual vector is orthogonal to both columns of AA.

Switch on your own line and move c0c_0 and c1c_1: its sum of squared residuals never drops below that of the least-squares line y=76+12ty=\tfrac76+\tfrac12t. Add a fourth point to see the fit move.

Rank-deficient case. For A=(1111)A=\begin{pmatrix}1&1\\1&1\end{pmatrix}, b=(1,3)⊤\boldsymbol{b}=(1,3)^{\top}: the normal equations reduce to x1+x2=2x_1+x_2=2, so every (2−t,t)⊤(2-t,t)^{\top} is a least-squares solution, with fitted vector (2,2)⊤(2,2)^{\top}, the projection of b\boldsymbol{b} onto span⁡{(1,1)⊤}\operatorname{span}\{(1,1)^{\top}\}. The minimum-norm solution is (1,1)⊤=A+b(1,1)^{\top}=A^+\boldsymbol{b}, using A+=14AA^+=\tfrac{1}{4}A.


Prerequisites



Sources

Aliases: method of least squares, linear least squares, least-squares solution, least-squares method, least-squares problem, least-squares approximation Keywords: normal equations, A^T A x = A^T b, (A^T A)^{-1} A^T b, residual, minimum-norm least-squares solution, A^+ b, line fitting Symbols: element of, real numbers

Last updated: 22 October 2026 19:04