The Normal Equations
The least-squares solution has a clean characterization: the residual must be orthogonal to the columns of . Writing that condition as equations gives the normal equations , a small square system. They are fast and fine for well-conditioned problems, but they square the condition number, which makes them dangerous in general.
Derivation
The vectors form the column space of . The closest one to is the orthogonal projection, so the residual is orthogonal to every column:
(Equivalently: set the gradient of to zero.) If has full column rank, is symmetric positive definite and Cholesky solves the system (Banded and Positive Definite Matrices). The cost is about flops.
import numpy as np
rng = np.random.default_rng(31)
t = np.linspace(0, 1, 50)
y = np.cos(3 * t) + rng.normal(0, 0.02, t.size)
A = np.vander(t, 4, increasing=True) # cubic: 1, t, t^2, t^3
N = A.T @ A
L = np.linalg.cholesky(N)
z = np.linalg.solve(L, A.T @ y)
c_ne = np.linalg.solve(L.T, z)
c_ls = np.linalg.lstsq(A, y, rcond=None)[0]
r = y - A @ c_ne
print("normal equations:", np.round(c_ne, 6))
print("lstsq :", np.round(c_ls, 6))
print(f"A^T r = {np.max(np.abs(A.T @ r)):.1e} (residual orthogonal to the columns)")normal equations: [ 0.997079 0.25278 -6.233855 3.998381]
lstsq : [ 0.997079 0.25278 -6.233855 3.998381]
A^T r = 1.7e-14 (residual orthogonal to the columns)
The pseudoinverse
For full-rank , the solution is with the pseudoinverse . The matrix projects onto the column space. These are useful formulas, but like they should not be formed explicitly to solve a problem.
The squared condition number
The trouble: . Even when the least-squares problem itself is reasonably conditioned, the normal equations lose twice as many digits. Polynomial fitting with monomials shows it clearly:
import numpy as np
t = np.linspace(0, 1, 100)
for deg in (5, 9, 13):
A = np.vander(t, deg + 1, increasing=True)
c_true = np.ones(deg + 1)
y = A @ c_true # exact data: the answer is all ones
c_ne = np.linalg.solve(A.T @ A, A.T @ y)
c_qr = np.linalg.lstsq(A, y, rcond=None)[0]
print(f"degree {deg:2d}: cond(A) = {np.linalg.cond(A):.1e}, cond(A^T A) = {np.linalg.cond(A.T @ A):.1e}, "
f"error NE = {np.max(np.abs(c_ne - 1)):.1e}, error lstsq = {np.max(np.abs(c_qr - 1)):.1e}")degree 5: cond(A) = 3.7e+03, cond(A^T A) = 1.4e+07, error NE = 4.0e-10, error lstsq = 1.1e-12
degree 9: cond(A) = 3.7e+06, cond(A^T A) = 1.4e+13, error NE = 1.2e-04, error lstsq = 1.4e-10
degree 13: cond(A) = 4.0e+09, cond(A^T A) = 1.8e+17, error NE = 1.3e+01, error lstsq = 4.6e-07
At degree 13 the normal equations return garbage while lstsq (orthogonal factorizations) still has a few digits.
When the normal equations are fine
- is well conditioned (orthogonal-ish columns, standardized features, Fourier bases).
- and speed matters: is small and can be accumulated in one pass over data that does not fit in memory.
- Regularized problems: (ridge regression) has condition number at most .
In software
- Many statistics packages and streaming regressions accumulate and (the "sufficient statistics"); robust ones use QR updates instead.
- scikit-learn's
Ridge(solver="cholesky")solves regularized normal equations;LinearRegressionusesscipy.linalg.lstsq.
Exercises
Exercise 1: Solve by hand
Fit (a constant) to data with the normal equations.
Answer
, the mean.
Solution
is a column of ones: , , so .
Exercise 2: A line by hand
Fit to .
Answer
, .
Solution
, . Solving: , , so , .
Exercise 3: Condition squared (code)
For a random matrix whose columns are scaled by , compare with .
Answer
They agree to within rounding.
Solution
import numpy as np
rng = np.random.default_rng(32)
A = rng.normal(size=(100, 5)) * np.array([1, 10, 100, 1000, 10000])
print(f"{np.linalg.cond(A) ** 2:.6e} {np.linalg.cond(A.T @ A):.6e}")9.097799e+07 9.097799e+07
Test yourself
The normal equations are…
The main numerical drawback of the normal equations is that…
- they are slow
- they need pivoting
- they only work for lines
At the least-squares solution, the residual is…
- zero
- orthogonal to the columns of
- parallel to
- the largest possible