Ran Wei/ Linear Algebra
中文
Linear Algebra · Structure and approximation

Module 8Least squares and conditioning

Fit inconsistent data by projecting onto the column space and assess the sensitivity of the coefficients.

80 minYou need Modules 5 and 7

In this module you will

  • Form a design matrix for a fit
  • Derive the normal equations from residual orthogonality
  • Compare QR with the normal equations for a numerical solve
1

The closest reachable observation

Suppose b\mathbf b consists of measurements and AxA\mathbf x is the vector of predictions. There may be no exact solution. Least squares minimises ∥Ax−b∥2.\|A\mathbf x-\mathbf b\|^2\mathclose{\text{.}} The fitted vector is the orthogonal projection of b\mathbf b onto the column space, because that is where every candidate prediction lies. The residual r=b−Ax^\mathbf r=\mathbf b-A\widehat{\mathbf x} is orthogonal to every column. This gives the normal equations

ATAx^=ATb.A^\mathsf TA\widehat{\mathbf x}=A^\mathsf T\mathbf b.

Conversely, any solution of these equations is a least-squares solution by the squared-distance decomposition. The fitted vector is unique even if the coefficients are not. Any two coefficient vectors producing the same fit differ by a null-space vector.

Example 8.1Fit a line with intercept✓ Checked

For data (0,1),(1,2),(2,2),(0,1),(1,2),(2,2)\mathclose{\text{,}} use columns of ones and input values, in that order. The normal equations for intercept aa and slope bb are 3a+3b=53a+3b=5 and 3a+5b=6.3a+5b=6\mathclose{\text{.}} Subtraction gives b=1/2b=1/2 and then a=7/6.a=7/6\mathclose{\text{.}} The residuals (−1/6,1/3,−1/6)(-1/6,1/3,-1/6) sum to zero and also have zero dot product with (0,1,2)T.(0,1,2)^\mathsf T\mathclose{\text{.}}

2

Solve with QR

For independent columns, write A=QRA=QR and express the projection as QQTb.QQ^\mathsf T\mathbf b\mathclose{\text{.}} Matching coefficients in the columns of QQ gives Rx^=QTb.R\widehat{\mathbf x}=Q^\mathsf T\mathbf b\mathclose{\text{.}} This is a triangular solve. It avoids forming ATA,A^\mathsf TA\mathclose{\text{,}} whose condition number is the square of that of A.A\mathclose{\text{.}} Module 11 derives that ratio.

Small perturbations in observations can force a large change in coefficients when the columns are nearly dependent. For A=(110ε),A=\begin{pmatrix}1&1\\0&\varepsilon\end{pmatrix}\mathclose{\text{,}} increasing the second observation by ε\varepsilon increases the second coefficient by 11 and decreases the first by 1.1\mathclose{\text{.}} The prediction has only changed a little. Treat a small residual as evidence about fit to the measurements, and examine sensitivity separately.

Compute itSolve the line fit with QR
Python
from sympy import Matrix

A = Matrix([[1, 0], [1, 1], [1, 2]])
b = Matrix([1, 2, 2])
Q, R = A.QRdecomposition()
x = R.upper_triangular_solve(Q.T * b)
print(list(x))
print(list(A.T * (b - A * x)))
Output
[7/6, 1/2]
[0, 0]
Compute itFit a line to floating-point data

NumPy’s least-squares solver returns the coefficients and a numerical rank. Give the column order a name before interpreting the result. The residual norm measures the size of the fit error in observation coordinates.

Python
import numpy as np

x_data = np.array([0., 1., 2.])
y_data = np.array([1., 2., 2.])
design = np.column_stack((np.ones(x_data.size), x_data))
coefficients, _, rank, _ = np.linalg.lstsq(design, y_data, rcond=None)
residual = y_data - design @ coefficients
print(f"intercept={coefficients[0]:.3f}, slope={coefficients[1]:.3f}")
print(f"rank={rank}, residual norm={np.linalg.norm(residual):.3f}")
Output
intercept=1.167, slope=0.500
rank=2, residual norm=0.408
3

Exercises

Exercise 8.1★Best constant

What constant best fits 1,4,71,4,7 in the least-squares sense?

Show solution✓ Checked

The design matrix is a column of ones. Residual orthogonality says their sum vanishes, so the fitted constant is the mean 4.4\mathclose{\text{.}}

Exercise 8.2★★Fit with redundant columns

For A=(1100)A=\begin{pmatrix}1&1\\0&0\end{pmatrix} and b=(3,2)T,\mathbf b=(3,2)^\mathsf T\mathclose{\text{,}} find the fitted vector and describe all coefficients.

Show solution✓ Checked

The column space is the first axis, so the fit is (3,0)T.(3,0)^\mathsf T\mathclose{\text{.}} Any coefficients with x1+x2=3x_1+x_2=3 produce it, for instance (3−t,t)T.(3-t,t)^\mathsf T\mathclose{\text{.}}

Exercise 8.3★★★Sensitivity

For A=(1101/1000),A=\begin{pmatrix}1&1\\0&1/1000\end{pmatrix}\mathclose{\text{,}} compare the coefficient change when the second observation moves by 1/10001/1000 with the first observation fixed.

Show solution✓ Checked

The second row forces the second coefficient to increase by 1.1\mathclose{\text{.}} The first row fixes the sum, so the first coefficient decreases by 1.1\mathclose{\text{.}}

4

Check yourself

1
In an unweighted least-squares fit, what is perpendicular to each design column?
2
Can a rank-deficient design have a unique fitted vector?
3
What do you solve after a thin QR factorisation with independent columns?
Finished this module?