Solve Least-Squares Normal Equations by Elimination

A worked explanation of solve least-squares normal equations by elimination, including the key limitation and a checkable example.

A Best-Fit Problem Becomes a Linear System

The common wrong assumption is that a least-squares line fit requires some kind of curve-fitting magic that a normal equation solver cannot handle. In fact, the statistics define the linear model, and then you solve least-squares normal equations by elimination exactly as you would any 2×2 or 3×3 system. The transformation is purely algebraic: the best-fit condition ATAx = ATb converts a tall, overdetermined matrix into a square, symmetric normal matrix that Gaussian elimination can handle directly. The only surprise is that the condition number of that normal matrix is the square of the condition number of the original design matrix, so what looked like a mild problem can become a numerically dangerous one.

Solve Least-Squares Normal Equations by Elimination Example

Work a concrete case. Fit the line y = β0 + β1x to the points (1, 2), (2, 3), (3, 5). The design matrix A has a first column of ones and a second column of x-values: [[1, 1], [1, 2], [1, 3]]. The vector b is [2, 3, 5]T. Compute the normal matrix ATA: it is [[3, 6], [6, 14]]. Compute ATb: it is [10, 23]T. The normal equations are [[3, 6], [6, 14]] times [β0, β1]T = [10, 23]T.

Build the augmented matrix [ATA | ATb]: [[3, 6, 10], [6, 14, 23]]. The pivot in row 1 is 3. The multiplier to eliminate the entry below is 6/3 = 2. Subtract 2 times row 1 from row 2: row 2 becomes (0, 2, 3). Forward elimination is done; the system is now in row-echelon form. Back-substitute from the last row: 2β1 = 3, so β1 = 1.5. Then 3β0 + 6(1.5) = 10 gives 3β0 + 9 = 10, so β0 = 1/3 ≈ 0.3333. The line is y = 0.3333 + 1.5x.

Check the residuals: at x = 1, y = 1.8333, residual 0.1667; at x = 2, y = 3.3333, residual -0.3333; at x = 3, y = 4.8333, residual 0.1667. Sum of squared residuals is 0.0278 + 0.1111 + 0.0278 = 0.1667. The statistics calculator that produced the normal equations from the raw data has handed you a 2×2 linear system; from that point forward the problem is pure matrix algebra.

Why the Method Works

The reason the normal equations become a linear system is that the least-squares condition sets the gradient of ||Ax - b||² to zero, which yields AT(b - Ax) = 0, or ATAx = ATb. That is a square system of n equations in n unknowns, regardless of how many data points you started with. For the line-fitting case, n = 2, so you get a 2×2 system. For a quadratic fit with three parameters you get a 3×3 system. In every case the structure is identical: symmetric, positive semi-definite, and invertible if the columns of A are linearly independent.

Gaussian elimination on these normal equations proceeds exactly as it would on any square system. Write the augmented matrix, pick the pivot, compute the multiplier, eliminate below, then back-substitute. The only extra step you need to watch for is a zero pivot. If the data points are all at the same x-coordinate, the second column of A is a scalar multiple of the first column, ATA becomes singular, and you will hit a zero pivot during elimination. That is the failure case: the normal equations have infinitely many solutions because the model cannot distinguish slope from intercept with no x-spread. The row operation that would divide by zero simply cannot complete; a row swap will not help because the entire column is zero below the pivot.

Partial pivoting is the standard classroom strategy. For the normal equations, the normal matrix is symmetric, so partial pivoting (swapping rows only) preserves symmetry. Complete pivoting swaps columns too, which changes the variable order, and is unnecessary for the small, well-behaved systems that hand-checking typically targets. For a 3×3 system from a quadratic fit, the practical limit for hand-checking with this calculator is about a 5×5 normal matrix; beyond that the number of row operations climbs past what a person can verify reliably.

Calculation Steps

To do the calculation yourself, follow this sequence. Set up the normal equations from your data using the sums Σx, Σy, Σx², Σxy. Write the augmented matrix [ATA | ATb]. Identify the pivot: the first non-zero entry in the first row. If it is zero, swap rows so the largest absolute value in that column becomes the pivot. Compute the multiplier: (entry below pivot) / (pivot). Subtract multiplier times the pivot row from each row below. Move to the next column and repeat until the matrix is in row-echelon form. Back-substitute: solve the last equation for the last variable, then substitute upward. Every row operation must be applied to the entire augmented row, including the constant column, or the solution set changes.

Failure Case: Nearly Collinear Data

For data points (0, 0), (1, 1), (2, 2), the normal equations are [[3, 3], [3, 5]] with right-hand side [3, 5]; elimination gives β0 = 0, β1 = 1 exactly. But change the points slightly to (0, 0.1), (1, 1), (2, 1.9). The normal matrix is still [[3, 3], [3, 5]], but the right-hand side becomes [3, 4.8]. Solving gives β0 = 0.1, β1 = 0.9. The condition number of the normal matrix is about 9.9, which is the square of the condition number of the original design matrix (about 3.15). For a more extreme case, say points (0, 0), (1, 0.01), (2, 2), the normal matrix is still well-conditioned, but the tiny y-value perturbation at x=1 amplifies through the elimination steps, and the resulting coefficients can shift noticeably. If you use decimal approximations instead of fractions, the round-off error from the back-substitution propagates and the calculated line can miss the true least-squares solution by several percent.

The single most practical thing to do next: before you trust any elimination result on a normal-equations problem, compute the condition number of the original design matrix A and square it. If that squared number exceeds 10⁴, switch to a QR decomposition or singular value decomposition instead of Gaussian elimination. Golub and Van Loan's Matrix Computations (Section 5.3) gives the standard warning: squaring the condition number amplifies rounding errors to the point where the elimination output may be worthless.

When Elimination Fails: The Singular Normal Matrix

A second failure mode occurs when A is not full column rank. If your data has more unknowns than equations, or if two columns of the design matrix are linearly dependent, the normal matrix ATA is singular. Elimination will produce a row of zeros on the left side. Check the augmented column: if the constant is also zero, the system has infinitely many solutions. If the constant is non-zero, the system has no solution. In either case the statistics problem is ill-posed: you cannot determine a unique best-fit line. The practical fix is to reduce the model, dropping a parameter until the columns become independent.

Normal Equation Structure for Common Fit Models
ModelParametersDesign Matrix ANormal Matrix AᵀA
Line y = β₀ + β₁x2[[1, x₁], ... [1, xₘ]][[n, Σx], [Σx, Σx²]]
Quadratic y = β₀ + β₁x + β₂x²3[[1, x₁, x₁²], ... [1, xₘ, xₘ²]][[n, Σx, Σx²], [Σx, Σx², Σx³], [Σx², Σx³, Σx⁴]]
Exponential y = β₀e^(β₁x) after log2[[1, x₁], ... [1, xₘ]] on log(y)Same as line, but data are (x, ln y)

One Thing That Most Often Goes Wrong

Newcomers forget to apply every row operation to the entire augmented row. They scale only the coefficient side of a row, or they add a multiple of one row to another but stop at the vertical bar. That changes the solution set. The augmented matrix notation [A|b] is universal precisely because it forces you to treat the constants as part of every operation. When the back-substitution gives a result that does not fit the original data, check that every row operation was applied to the full row, including the rightmost column.

Common Questions

Why do the normal equations square the condition number?

Because A<sup>T</sup>A multiplies the singular values of A by themselves. If A has a condition number κ, the normal matrix has condition number κ². A condition number of 10³ becomes 10⁶, which is enough to lose six decimal digits of precision in double-precision arithmetic.

Can I use Gaussian elimination on a rectangular normal system?

The normal equations are always square (n×n) by construction. The original least-squares problem may be rectangular (m×n with m > n), but the transformation to A<sup>T</sup>Ax = A<sup>T</sup>b produces a square system. If your normal matrix is rectangular, you have a different problem.

How do I know when to stop forward elimination?

Stop when the matrix is in row-echelon form: every pivot is to the right of the pivot above it, and all entries below each pivot are zero. That visual cue is the first non-zero entry in each row. Once you see zeros below every pivot, begin back-substitution.

What if I get a zero pivot during elimination?

Swap rows to bring a non-zero entry from below into the pivot position. If the entire column below the current row is zero, the matrix is singular. For the normal equations, that means the data points are collinear or the model has redundant parameters.

Should I use fractions or decimals in hand calculation?

Use fractions. Decimal approximations introduce rounding errors at every step, and those errors compound during back-substitution. For the worked example above, 0.3333 is a rounding of 1/3; using 0.3333 instead of 1/3 shifts the result by about 0.0001, which is harmless here but dangerous for ill-conditioned systems.