Least squares: normal equations vs QR

Part 4 — Least squares

Numerical analysis for biologists

About this lecture

Part 3 solved square systems A\mathbf x = \mathbf b. A calibration curve gives more equations than unknowns: 21 measurements, 2 parameters. No \mathbf x satisfies them all.

  • Least squares picks the \mathbf x that makes the sum of squared residuals smallest. Geometrically: a projection.
  • The textbook route, the normal equations A^TA\mathbf x = A^T\mathbf b, is short but squares the condition number: it can lose twice as many digits as necessary.
  • The route software takes is the QR factorisation (Gram–Schmidt in principle, Householder reflections in practice). It loses only what the problem itself forces.
  • Biology: qPCR and protein-assay standard curves; a polynomial calibration in µg/mL that is so badly conditioned that R refuses to solve it.
  • You have used lm(). This is what is inside it.

A biology problem first: the qPCR standard curve

A 10-fold dilution series of a standard, 10^1 to 10^7 copies, each in triplicate. Theory: \text{Cq} = x_1 + x_2 \log_{10} N.

logN <- rep(1:7, each = 3)
cq <- c(34.52, 34.69, 34.48, 31.62, 31.37,
  31.14, 28.05, 28.10, 28.07, 24.54, 24.90,
  24.68, 21.13, 20.81, 21.47, 17.89, 17.90,
  18.09, 14.71, 14.67, 14.73)
coef(lm(cq ~ logN))
# (Intercept)        logN
#   37.981429   -3.333929

  • Slope -3.33: amplification efficiency 10^{-1/x_2} - 1 = 99.5\,\%. An unknown with Cq 24.0 holds about 10^{4.19} \approx 15\,600 copies.
  • 21 equations, 2 unknowns. What does lm actually solve?

More equations than unknowns

Each standard gives one equation x_1 + x_2 \log_{10} N_i = \text{Cq}_i. Stacked:

\underbrace{\begin{pmatrix} 1 & 1\\ 1 & 1\\ 1 & 1\\ 1 & 2\\ \vdots & \vdots\\ 1 & 7 \end{pmatrix}}_{A,\ 21\times 2} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} \approx \underbrace{\begin{pmatrix} 34.52\\ 34.69\\ 34.48\\ 31.62\\ \vdots\\ 14.73 \end{pmatrix}}_{\mathbf b,\ 21\times 1}

  • A is the design matrix (in R: model.matrix). One row per measurement, one column per parameter.
  • m = 21 rows, n = 2 columns: overdetermined. The first three rows already disagree (34.52, 34.69, 34.48 for the same x), so there is no exact solution.
  • We need a rule that picks the “best” \mathbf x.

Residuals and the least-squares criterion

For any trial \mathbf x, the residual vector is what the model misses:

\mathbf r = \mathbf b - A\mathbf x, \qquad r_i = \text{Cq}_i - (x_1 + x_2 \log_{10} N_i)

Least squares: choose \mathbf x to minimise \;\|\mathbf r\|^2 = \sum_i r_i^2.

trial \mathbf x r_1, r_2, r_3 \sum_i r_i^2 (all 21)
(38, -3.3) -0.18,\; -0.01,\; -0.22 1.23
(37.98, -3.334), the fit -0.13,\; \phantom{-}0.04,\; -0.17 0.627
  • Why squares: smooth (we can differentiate), and it is the maximum-likelihood fit for independent Gaussian noise of equal size.
  • The minimum is not zero. Its size, 0.627 over 19 degrees of freedom, estimates the noise: \hat\sigma = \sqrt{0.627/19} = 0.18 cycles.

Least squares is a projection

  • A\mathbf x can only reach the column space of A (here a plane in \mathbb R^{21}). \mathbf b sticks out of it.
  • The closest point A\hat{\mathbf x} is the orthogonal projection of \mathbf b: the residual is perpendicular to every column of A: A^T \mathbf r = \mathbf 0.

The normal equations

Put \mathbf r = \mathbf b - A\mathbf x into A^T\mathbf r = \mathbf 0:

A^TA\,\mathbf x = A^T\mathbf b \qquad (\text{a square } n\times n \text{ system})

For the qPCR data (\sum 1 = 21, \sum \log_{10} N_i = 84, \sum (\log_{10} N_i)^2 = 420):

\begin{pmatrix} 21 & 84 \\ 84 & 420 \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} 517.56 \\ 1790.19 \end{pmatrix} \quad\Rightarrow\quad x_1 = 37.981, \; x_2 = -3.334

  • A^TA is 2\times 2, symmetric and positive definite: Cholesky (Structure: symmetric, banded, sparse) solves it.
  • Entries of A^TA are sums of products of columns; entries of A^T\mathbf b are \sum \text{Cq}_i and \sum \text{Cq}_i \log_{10}N_i.
  • The same answer comes from setting the derivative of \sum r_i^2 to zero.

Normal equations in R

A <- cbind(1, logN)                         # 21 x 2 design matrix
x <- solve(crossprod(A), crossprod(A, cq))  # (A^T A) x = A^T b
x[, 1]
#                logN
# 37.981429 -3.333929
r <- cq - A %*% x
crossprod(A, r)[, 1]                        # A^T r: zero up to rounding
#                       logN
# -6.572520e-14  1.225686e-13
  • crossprod(A) is A^TA, crossprod(A, b) is A^T\mathbf b, faster than t(A) %*% A.
  • Use solve(M, v), never solve(M) %*% v.
  • Same numbers as lm. So why does lm not do this?

Recap: the condition number of a matrix

From Pivoting, norms and conditioning (Part 3):

  • \kappa(A) = \dfrac{\text{largest stretch}}{\text{smallest stretch}} = \dfrac{\max \|A\mathbf v\|}{\min \|A\mathbf v\|} over unit vectors \mathbf v. This works for rectangular A too.
  • Rule of thumb: solving with A loses about \log_{10}\kappa(A) digits of the 16 we have.
  • \kappa \ge 1/\varepsilon \approx 4.5\times10^{15}: as good as singular in double precision.
kappa(A, exact = TRUE)                      # qPCR design matrix
# [1] 10.40388

kappa(A, exact = TRUE) gives the 2-norm \kappa; without exact it is a cheap estimate.

The normal equations square \kappa

\|A\mathbf v\|^2 = \mathbf v^T A^TA\,\mathbf v

So every stretch factor of A^TA is the square of a stretch factor of A:

\kappa(A^TA) = \kappa(A)^2

c(kappa(A, exact = TRUE), kappa(crossprod(A), exact = TRUE))
# [1]  10.40388 108.24076                   # 10.40388^2 = 108.24
  • Solving through A^TA loses 2\log_{10}\kappa(A) digits; the data only force \log_{10}\kappa(A).
  • \kappa(A) = 10^8: the problem allows 8 good digits, the normal equations give zero.
  • The damage is done the moment A^TA is formed and rounded. A better solver for A^TA\mathbf x = A^T\mathbf b cannot repair it.

A second assay: the BCA protein standard curve

BSA standards 0–2000 µg/mL in duplicate; absorbance at 562 nm. The curve bends.

  • The straight line leaves runs of residuals with the same sign: ------+++++++++---. That is lack of fit, not noise.
  • A quadratic removes the pattern. Kit manuals suggest a quadratic or a 4-parameter logistic.

A polynomial fit is a Vandermonde system

a_i \approx c_0 + c_1 x_i + c_2 x_i^2 + c_3 x_i^3, \qquad A = \begin{pmatrix} 1 & x_1 & x_1^2 & x_1^3 \\ \vdots & & & \vdots \\ 1 & x_m & x_m^2 & x_m^3 \end{pmatrix}

This A is a Vandermonde matrix: outer(conc, 0:3, "^"). With x in µg/mL:

column 1 x x^2 x^3
largest entry 1 2000 4\times10^6 8\times10^9
  • Columns of wildly different size: \kappa(A) = 5.8\times 10^9 for the cubic.
  • Normal equations: \kappa(A^TA) = \kappa(A)^2 \approx 3\times10^{19}, above 1/\varepsilon.

The failure: R refuses the normal equations

conc <- rep(c(0, 25, 125, 250, 500, 750, 1000, 1500, 2000), each = 2)
a562 <- c(0.087, 0.103, 0.150, 0.109, 0.226, 0.229, 0.358, 0.344, 0.601,
          0.569, 0.780, 0.789, 0.951, 0.942, 1.300, 1.238, 1.545, 1.532)
V <- outer(conc, 0:3, "^")                       # cubic, ug/mL
solve(crossprod(V), crossprod(V, a562))
# Error in solve.default(crossprod(V), crossprod(V, a562)) :
#   system is computationally singular: reciprocal condition number = 2.98348e-20
qr.solve(V, a562)                                # QR: no complaint
# [1]  9.750013e-02  1.095169e-03 -2.847165e-07  4.881629e-11
  • solve estimates 1/\kappa(A^TA) \approx 3\times10^{-20} < \varepsilon and stops. numpy’s solve does not check, and returns numbers without a warning.
  • lm(a562 ~ conc + I(conc^2) + I(conc^3)) gives the QR numbers.

Two sources of ill-conditioning

  • Units. Measure x in mg/mL (conc / 1000): the cubic’s \kappa drops from 5.8\times10^9 to 72. Same fit, rescaled coefficients (c_k \to 1000^k c_k).
  • Look-alike columns. What is left (72, 506, 4624, …) still grows with degree: x^k and x^{k+1} look alike. poly() builds orthogonal polynomial columns: \kappa = 4.2 at any degree.

Genuine ill-conditioning: digits lost

Polynomials of degree 1–12 on 50 points in [0, 1] (no unit problem), data with known coefficients \mathbf x = (1, \dots, 1):

  • Normal equations follow \kappa^2\varepsilon; QR follows \kappa\varepsilon. At \kappa = 2\times10^7: error 7\times10^{-3} vs 2\times10^{-10}.
  • From degree 11 solve refuses; from degree 13 qr.solve does too (its rank tolerance is 10^{-7}).

Try it: fit a calibration curve two ways

A failure you can do by hand

A = \begin{pmatrix} 1 & 1 \\ \delta & 0 \\ 0 & \delta \end{pmatrix}, \qquad A^TA = \begin{pmatrix} 1 + \delta^2 & 1 \\ 1 & 1+\delta^2 \end{pmatrix}

With \delta = 10^{-8}: \delta^2 = 10^{-16} < \varepsilon/2, so 1 + \delta^2 is stored as 1. A has rank 2, the computed A^TA is singular.

d <- 1e-8
A <- rbind(c(1, 1), c(d, 0), c(0, d));  b <- c(2, d, d)   # exact x = (1, 1)
solve(crossprod(A), crossprod(A, b))
# Error in solve.default(crossprod(A), crossprod(A, b)) :
#   Lapack routine dgesv: system is exactly singular: U[2,2] = 0
qr.solve(A, b, tol = 1e-12)
# [1] 1 1

The idea of QR

Write A = QR with

  • Q: m\times n with orthonormal columns, Q^TQ = I (perpendicular, length 1);
  • R: n\times n upper triangular.

Then the normal equations simplify without ever forming A^TA:

A^TA\mathbf x = A^T\mathbf b \;\Leftrightarrow\; R^T\underbrace{Q^TQ}_{I}R\,\mathbf x = R^TQ^T\mathbf b \;\Leftrightarrow\; R\,\mathbf x = Q^T\mathbf b

  • One back substitution, as in Gaussian elimination as LU.
  • Q does not stretch: \|Q\mathbf y\| = \|\mathbf y\|. So \|A\mathbf v\| = \|R\mathbf v\| and \kappa(R) = \kappa(A), not squared.

QR in R

A <- cbind(1, logN)                          # qPCR again
f <- qr(A)
R <- qr.R(f);  R
#                      logN
# [1,] -4.582576 -18.330303
# [2,]  0.000000   9.165151
backsolve(R, crossprod(qr.Q(f), cq))[, 1]    # R x = Q^T b
# [1] 37.981429 -3.333929
qr.solve(A, cq)                              # the same in one call
  • qr() returns the factorisation in compact form; qr.Q, qr.R unpack it. backsolve solves an upper triangular system.
  • r_{11} = -\sqrt{21}: the length of the column of ones (the sign is a convention).

Gram–Schmidt by hand

Make the columns orthonormal one at a time. A with columns \mathbf a_1 = (1,1,1,1), \mathbf a_2 = (1,2,3,4):

  1. r_{11} = \|\mathbf a_1\| = 2, \;\mathbf q_1 = \mathbf a_1 / 2 = (\tfrac12,\tfrac12,\tfrac12,\tfrac12).
  2. Remove the \mathbf q_1 part of \mathbf a_2: r_{12} = \mathbf q_1^T\mathbf a_2 = 5, \;\mathbf v = \mathbf a_2 - 5\,\mathbf q_1 = (-1.5, -0.5, 0.5, 1.5).
  3. r_{22} = \|\mathbf v\| = \sqrt5, \;\mathbf q_2 = \mathbf v/\sqrt5.

A = \begin{pmatrix} \mathbf q_1 & \mathbf q_2\end{pmatrix}\begin{pmatrix} 2 & 5 \\ 0 & \sqrt5 \end{pmatrix}

  • Against the column of ones, “remove the \mathbf q_1 part” means subtract the mean: \mathbf v = \mathbf a_2 - \bar a_2.
  • If \mathbf a_2 were a multiple of \mathbf a_1, \mathbf v = \mathbf 0 and r_{22} = 0: rank deficiency shows up on the diagonal of R.

Gram–Schmidt in R: classical vs modified

gs <- function(A, modified = FALSE) {
  n <- ncol(A); Q <- A; R <- matrix(0, n, n)
  for (j in 1:n) {
    v <- A[, j]
    for (i in seq_len(j - 1)) {
      R[i, j] <- sum(Q[, i] * (if (modified) v else A[, j]))
      v <- v - R[i, j] * Q[, i]
    }
    R[j, j] <- sqrt(sum(v^2)); Q[, j] <- v / R[j, j]
  }
  list(Q = Q, R = R)
}
  • Classical (CGS): project the original column \mathbf a_j on each \mathbf q_i.
  • Modified (MGS): project the current \mathbf v, already cleaned of \mathbf q_1, \dots, \mathbf q_{i-1}. Same in exact arithmetic; one word differs in the code.

Gram–Schmidt loses orthogonality

  • \|Q^TQ - I\| should be 0. CGS: grows like \kappa^2\varepsilon (no better than the normal equations). MGS: like \kappa\varepsilon. Householder qr(): \varepsilon, whatever \kappa.
  • Check any Q you compute: norm(crossprod(Q) - diag(ncol(Q))).

Householder reflections

Reflect a column onto the first axis with a mirror:

H = I - 2\frac{\mathbf v\mathbf v^T}{\mathbf v^T\mathbf v}, \quad \mathbf v = \mathbf a + \text{sign}(a_1)\|\mathbf a\|\mathbf e_1

\mathbf a = (3, 4), \|\mathbf a\| = 5, \mathbf v = (8, 4):

H\mathbf a = (-5, 0)

  • H is orthogonal (H^TH = I): lengths are kept exactly in theory, to \varepsilon in practice.
  • The sign choice avoids cancellation in a_1 \pm \|\mathbf a\|.

Householder QR in outline

H_1 zeroes column 1 below the diagonal, H_2 column 2, and so on: H_n\cdots H_2H_1\,A = R \quad\Rightarrow\quad A = \underbrace{H_1H_2\cdots H_n}_{Q} R Apply the same reflections to \mathbf b to get Q^T\mathbf b (Q is never formed), then back-substitute.

flops for m\times n accuracy
normal equations + Cholesky mn^2 + \tfrac13 n^3 \kappa^2\varepsilon
Householder QR 2mn^2 - \tfrac23 n^3 \kappa\varepsilon

For m \gg n, QR costs about twice as much. For a standard curve that is microseconds.

What lm() actually does

lm → lm.fit → Householder QR (LINPACK dqrdc2) with column pivoting and a rank tolerance tol = 1e-7.

mgml <- conc / 1000                          # same concentration, other units
coef(lm(a562 ~ conc + mgml))
#  (Intercept)         conc         mgml
# 0.1592784830 0.0007305681           NA
qr(cbind(1, conc, mgml))$rank
# [1] 2
  • A column whose remaining length drops below tol (relative) is declared aliased: coefficient NA, not an error.
  • numpy linalg.lstsq uses the SVD (LAPACK gelsd), returns the rank, and gives a minimum-norm answer instead of NA.

Fix the problem, not only the algorithm

fit_raw  <- lm(a562 ~ poly(conc, 2, raw = TRUE))   # columns 1, x, x^2
fit_orth <- lm(a562 ~ poly(conc, 2))               # orthogonal columns
predict(fit_raw,  data.frame(conc = 600))
#         1
# 0.6531236
predict(fit_orth, data.frame(conc = 600))
#         1
# 0.6531236
kappa(model.matrix(fit_orth), exact = TRUE)
# [1] 4.242641
  • Scale predictors to O(1) units and centre them before building powers.
  • Use orthogonal bases (poly()) for polynomial fits; predictions are identical, coefficients are just in another basis.
  • Ask whether degree 5 is needed: a high-degree calibration polynomial is ill-conditioned and wiggles between standards (Polynomial interpolation, Part 6).

Checklist: is my least-squares fit trustworthy?

  1. Residual plot: runs of equal sign → wrong model (degree too low), not noise.
  2. kappa(model.matrix(fit), exact = TRUE): expect to lose \log_{10}\kappa digits. Huge? Rescale, centre, or use poly().
  3. Never solve t(X) %*% X yourself unless \kappa(X) is small. “Computationally singular” from solve means \kappa(X)^2 \gtrsim 10^{16}.
  4. NA coefficients in lm (or rank < ncol from lstsq): columns are linearly dependent. Find the duplicate or confounded predictor.
  5. Orthogonality check for a hand-made Q: norm(crossprod(Q) - diag(ncol(Q))) near 10^{-15}.
  6. Projection check: crossprod(X, resid(fit)) near zero.

Summary

  • Overdetermined A\mathbf x \approx \mathbf b: minimise \|\mathbf b - A\mathbf x\|^2. The residual is perpendicular to the columns: A^T\mathbf r = \mathbf 0.
  • Normal equations A^TA\mathbf x = A^T\mathbf b: short, cheap, but \kappa(A^TA) = \kappa(A)^2, so twice the digit loss. Fine for small \kappa.
  • QR: A = QR, solve R\mathbf x = Q^T\mathbf b. Q keeps lengths, so only \kappa(A) counts.
  • Gram–Schmidt explains QR (and centring); classical GS loses orthogonality like \kappa^2\varepsilon, modified like \kappa\varepsilon. Householder reflections keep Q orthogonal to \varepsilon; that is what qr, lm, numpy.linalg.qr and nalgebra use.
  • Polynomial calibration in raw units is a badly conditioned Vandermonde system: rescale, centre, or use poly().
  • lm flags dependent columns with NA; lstsq uses the SVD and reports the rank.

Next: The SVD, rank deficiency and regularisation: what to do when columns really are (nearly) dependent.