How to Write Least Squares in Matrix Form
At a glance
- 1. Set up the system
Put the unknown coefficients in x, the observed values in b, and the model coefficients in the rows of A.
- 2. Define the objective
Choose x to minimize the squared residual length, ||Ax - b||².
- 3. Solve the equations
For full-column-rank A, solve AᵀAx = Aᵀb; use QR or SVD for numerical work.
- 4. Check the fit
Compute the residual and verify it is orthogonal to every column of A.
Try it: Matrix calculator
Least squares is a way to find the best approximate solution when a matrix equation has no exact solution, as commonly happens when fitting a model to more observations than unknown coefficients. Its matrix form is a minimization problem: choose the coefficient vector that makes the residual as small as possible.
This article sets up the problem, derives the normal equations, and works through a straight-line fit. The closed-form formula is useful for understanding the method, while QR factorization and singular value decomposition are generally preferable for numerical computation.
The matrix formulation
Let be an matrix, the unknown coefficient vector, and the observed data vector. The system is
When , there are more equations than unknowns, and the equations may be inconsistent. The residual for a proposed is . The least-squares solution, written , minimizes the squared Euclidean length of that residual:
Squaring the norm does not change which minimizes it, but makes the objective convenient to differentiate. The entries of encode the model, the entries of are the coefficients to estimate, and the entries of are the measured values.
Deriving the normal equations
Expand the objective as a function of :
Its gradient is . At a minimum, the gradient is zero, so
or equivalently,
These are the normal equations. If the columns of are linearly independent, then is invertible, giving the familiar formula
This formula is a theoretical expression, not usually the best way to calculate the answer: explicitly forming an inverse can magnify numerical error. For numerical work, solve the least-squares problem with QR factorization or SVD instead.
Geometrically, is the point in the column space of closest to . The residual is perpendicular to that column space, which is why . This orthogonality condition is another way to understand the normal equations.
Worked example: fitting a straight line
Fit a line to the points , , and by minimizing the sum of squared vertical errors. The unknown vector is , and the data give
Each row of contains the coefficients of and for one observation. Calculate the two sides of the normal equations:
Thus and . Subtracting the first equation from the second gives , so and . The fitted line is
Its predictions at the three observed -values are , , and . Therefore the residual vector, using , is
As a check, the residuals sum to zero and their products with the corresponding -values also sum to zero. In matrix terms, , confirming that the residual is orthogonal to both columns of .
When the formula needs care
The expression requires to have full column rank: its columns must be linearly independent. If they are dependent, there can be multiple coefficient vectors with the same best fit, and is singular. The Moore–Penrose pseudoinverse gives the minimum-norm least-squares solution, while SVD makes rank deficiencies easier to identify.
For a weighted least-squares fit, where observations have different weights, minimize for a suitable weight matrix . If is symmetric positive definite, the corresponding normal equations are . Ordinary least squares is the special case .
Common mistakes
- Trying to invert a rectangular matrix: A non-square has no ordinary matrix inverse. Least squares instead solves a minimization problem.
- Using the closed-form inverse without checking rank: The formula involving applies only when has full column rank.
- Assuming the fit has zero residual: A least-squares solution need not satisfy . It minimizes the residual; it does not necessarily eliminate it.
- Confusing residual conventions: This article uses . The alternative changes the sign, but not the residual norm or the least-squares solution.
- Using normal equations blindly in computation: When is poorly conditioned, solving with QR or SVD is generally more stable than forming .
References
- Introduction to Linear Algebra, Gilbert Strang
- Matrix Computations, Gene H. Golub and Charles F. Van Loan
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.