TExcelTAD
Home › Numerical Methods › The Normal Equations

The Normal Equations

The least-squares solution has a clean characterization: the residual must be orthogonal to the columns of AA. Writing that condition as equations gives the normal equations ATAc=ATyA^TA\mathbf{c} = A^T\mathbf{y}, 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 AcA\mathbf{c} form the column space of AA. The closest one to y\mathbf{y} is the orthogonal projection, so the residual r=y−Ac\mathbf{r} = \mathbf{y} - A\mathbf{c} is orthogonal to every column:

AT(y−Ac)=0⟺ATA c=ATy.A^T(\mathbf{y} - A\mathbf{c}) = \mathbf{0} \quad\Longleftrightarrow\quad A^TA\,\mathbf{c} = A^T\mathbf{y}.

(Equivalently: set the gradient of ∥y−Ac∥2\lVert\mathbf{y} - A\mathbf{c}\rVert^2 to zero.) If AA has full column rank, ATAA^TA is symmetric positive definite and Cholesky solves the system (Banded and Positive Definite Matrices). The cost is about mn2+13n3mn^2 + \frac{1}{3}n^3 flops.

python · try it yourself
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 AA, the solution is c=A+y\mathbf{c} = A^+\mathbf{y} with the pseudoinverse A+=(ATA)−1ATA^+ = (A^TA)^{-1}A^T. The matrix P=AA+P = AA^+ projects onto the column space. These are useful formulas, but like A−1A^{-1} they should not be formed explicitly to solve a problem.

The squared condition number

The trouble: κ(ATA)=κ(A)2\kappa(A^TA) = \kappa(A)^2. 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:

python · try it yourself
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

  • AA is well conditioned (orthogonal-ish columns, standardized features, Fourier bases).
  • m≫nm \gg n and speed matters: ATAA^TA is small and can be accumulated in one pass over data that does not fit in memory.
  • Regularized problems: (ATA+λI)c=ATy(A^TA + \lambda I)\mathbf{c} = A^T\mathbf{y} (ridge regression) has condition number at most σ12+λλ\frac{\sigma_1^2 + \lambda}{\lambda}.

In software

  • Many statistics packages and streaming regressions accumulate ATAA^TA and ATyA^T\mathbf{y} (the "sufficient statistics"); robust ones use QR updates instead.
  • scikit-learn's Ridge(solver="cholesky") solves regularized normal equations; LinearRegression uses scipy.linalg.lstsq.

Exercises

Exercise 1: Solve by hand

Fit y=cy = c (a constant) to data y1,…,ymy_1, \ldots, y_m with the normal equations.

Answer

c=yˉc = \bar{y}, the mean.

Solution

AA is a column of ones: ATA=mA^TA = m, ATy=∑yiA^T\mathbf{y} = \sum y_i, so c=1m∑yic = \frac{1}{m}\sum y_i.

Exercise 2: A line by hand

Fit y=c1+c2ty = c_1 + c_2t to (0,1),(1,2),(2,2)(0, 1), (1, 2), (2, 2).

Answer

c1=76c_1 = \frac{7}{6}, c2=12c_2 = \frac{1}{2}.

Solution

ATA=(3335)A^TA = \begin{pmatrix} 3 & 3 \\ 3 & 5\end{pmatrix}, ATy=(5,6)A^T\mathbf{y} = (5, 6). Solving: 3c1+3c2=53c_1 + 3c_2 = 5, 3c1+5c2=63c_1 + 5c_2 = 6, so c2=12c_2 = \frac{1}{2}, c1=76c_1 = \frac{7}{6}.

Exercise 3: Condition squared (code)

For a random 100×5100 \times 5 matrix whose columns are scaled by 1,10,100,1000,100001, 10, 100, 1000, 10000, compare κ(A)2\kappa(A)^2 with κ(ATA)\kappa(A^TA).

Answer

They agree to within rounding.

Solution
python · try it yourself
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…

  1. Ac=yA\mathbf{c} = \mathbf{y}
  2. ATAc=ATyA^TA\mathbf{c} = A^T\mathbf{y}
  3. AATc=yAA^T\mathbf{c} = \mathbf{y}
  4. ATc=yA^T\mathbf{c} = \mathbf{y}

The main numerical drawback of the normal equations is that…

  1. they are slow
  2. κ(ATA)=κ(A)2\kappa(A^TA) = \kappa(A)^2
  3. they need pivoting
  4. they only work for lines

At the least-squares solution, the residual is…

  1. zero
  2. orthogonal to the columns of AA
  3. parallel to y\mathbf{y}
  4. the largest possible