BetaFree coursewareRewritten from MIT OpenCourseWareCC BY-NC-SA 4.0
Open CosmologyPhysics & cosmology, from high school to a graduate year

Projection matrices and least squares

P = A(AᵀA)⁻¹Aᵀ keeps what is in the column space and kills what is perpendicular to it: if b = Ax then Pb = b, and if b is in N(Aᵀ) then Pb = 0. Every b splits as b = p + e, with p = Pb in C(A) and e = (I − P)b in N(Aᵀ); I − P is the projection onto the perpendicular space. Least squares fits the points (1, 1), (2, 2), (3, 2) by the line b = C + Dt by minimizing ‖Ax − b‖² = e₁² + e₂² + e₃², the sum of squared vertical errors (linear regression; outliers can distort it). The normal equations AᵀAx̂ = Aᵀb are 3C + 6D = 5 and 6C + 14D = 11, the same equations calculus gives by setting the partial derivatives to zero. They give D = 1/2, C = 2/3: the best line is 2/3 + t/2. The points on the line are p = (7/6, 5/3, 13/6), and e = b − p = (−1/6, 1/3, −1/6) is perpendicular to p and to both columns, (1, 1, 1) and (1, 2, 3). AᵀA is invertible when A has independent columns: AᵀAx = 0 gives xᵀAᵀAx = ‖Ax‖² = 0, so Ax = 0 and x = 0. Columns that are perpendicular unit vectors, orthonormal, are certainly independent, and then AᵀA = I: the subject of the next lecture.

You need this first

Full reading path (17 pages)
  1. Vectors (8.01)
  2. Vectors and the dot product (18.02)
  3. Determinants and the cross product (18.02)
  4. Planes through three points, and matrices (18.02)
  5. Equations of planes and square systems (18.02)
  6. The geometry of linear equations (18.06)
  7. Elimination with matrices (18.06)
  8. Matrix multiplication and inverse matrices (18.06)
  9. Factorization into A = LU (18.06)
  10. Transposes, permutations and vector spaces (18.06)
  11. Column space and nullspace (18.06)
  12. Solving Ax = 0: pivot variables and special solutions (18.06)
  13. Solving Ax = b: the complete solution (18.06)
  14. Independence, basis and dimension (18.06)
  15. The four fundamental subspaces (18.06)
  16. Orthogonal vectors and subspaces (18.06)
  17. Projections onto subspaces (18.06)
On this page · 4 sections
  1. What P does
  2. The best straight line
  3. Why AᵀA is invertible
  4. Summary

This lecture recaps the projection matrix, then works the best straight line through three points completely (transcript, pp. 1, 4). It continues projections onto subspaces.

What P does#

P=A(ATA)−1ATP = A(A^TA)^{-1}A^T projects bb to the nearest point in the column space (transcript, p. 1). Check the two extreme cases (pp. 1–3):

The two extreme cases
b in N(AT):  Pb=A(ATA)−1ATb=0;b=Ax:  Pb=A(ATA)−1ATAx=Ax=bb \text{ in } N(A^T): \; Pb = A(A^TA)^{-1}A^Tb = 0; \qquad b = Ax: \; Pb = A(A^TA)^{-1}A^TAx = Ax = b
In words

b in N( capital A ^ capital T ): capital P b equals capital A ( capital A ^ capital T capital A ) to the power minus 1 capital A ^ capital T b equals 0; b equals capital A x: capital P b equals capital A ( capital A ^ capital T capital A ) to the power minus 1 capital A ^ capital T capital A x equals capital A x equals b

Vectors perpendicular to the column space are exactly those in N(AT)N(A^T), so ATb=0A^Tb = 0 kills them; a vector in the column space has the form AxAx, and the ATAA^TA cancels against its inverse (pp. 2–3). A typical bb has a part in each (pp. 3–4):

Two projections
b=p+e,p=Pb,e=(I−P)bb = p + e, \qquad p = Pb, \qquad e = (I - P)b
In words

b equals p plus e, p equals capital P b, e equals the quantity capital I minus capital P, b

I−PI - P is the projection onto the perpendicular space N(AT)N(A^T): it is symmetric, and (I−P)2=I−P(I - P)^2 = I - P (p. 4).

The best straight line#

Back to the points (t,b)=(1,1)(t, b) = (1, 1), (2,2)(2, 2), (3,2)(3, 2) (transcript, p. 4). A line C+DtC + Dt through all three would need C+D=1C + D = 1, C+2D=2C + 2D = 2, C+3D=2C + 3D = 2: that is Ax=bAx = b with AA having columns (1,1,1)(1, 1, 1) and (1,2,3)(1, 2, 3), x=(C,D)x = (C, D) and b=(1,2,2)b = (1, 2, 2), which has no solution (p. 5). (Strang says bb is "one, two, three" at one point; it is (1,2,2)(1, 2, 2).) The columns are independent, a basis for the column space, but bb is not in it (p. 5).

Best means: make the errors e1,e2,e3e_1, e_2, e_3, the vertical distances from the points to the line, small in the sense of the sum of squares (pp. 5–7):

Least squares
minimize ∥Ax−b∥2=∥e∥2=e12+e22+e32\text{minimize } \|Ax - b\|^2 = \|e\|^2 = e_1^2 + e_2^2 + e_3^2
In words

minimize the norm of capital A x minus b squared equals the norm of e squared equals e one squared plus e two squared plus e three squared

This is linear regression (p. 7). Statisticians are wary of the squares when there are outliers: one point far off, perhaps a mistake, has a giant squared error that can turn the whole fit topsy-turvy (pp. 7–8). Least squares is still the most used method (p. 8).

Two pictures (pp. 8–9). In the plane of the data, the points p1,p2,p3p_1, p_2, p_3 on the line lie above or below the given b1,b2,b3b_1, b_2, b_3, with the errors between. In R3\mathrm{R}^3, p=(p1,p2,p3)p = (p_1, p_2, p_3) is the closest point to b=(1,2,2)b = (1, 2, 2) in the column space: a line goes through the pp's, so pp is a combination of the columns (pp. 8–9).

The numbers (pp. 9–12). Strang ventures to call the normal equations the most important equation in statistics and estimation (p. 9):

The normal equations
ATA=(36614),ATb=(511):3C+6D=5,6C+14D=11A^TA = \begin{pmatrix} 3 & 6 \\ 6 & 14 \end{pmatrix}, \quad A^Tb = \begin{pmatrix} 5 \\ 11 \end{pmatrix}: \qquad 3C + 6D = 5, \quad 6C + 14D = 11
In words

capital A ^ capital T capital A equals 3 6; 6 14, capital A ^ capital T b equals 5; 11 : 3 C plus 6 D equals 5, 6 C plus 14 D equals 11

ATAA^TA is symmetric and invertible, and later in the course it will be called positive definite (p. 10). Calculus gives the same equations (pp. 10–11): the total squared error

The error as a function of C and D
E=(C+D−1)2+(C+2D−2)2+(C+3D−2)2E = (C + D - 1)^2 + (C + 2D - 2)^2 + (C + 3D - 2)^2
In words

capital E equals the quantity C plus D minus 1, all squared plus the quantity C plus 2 D minus 2, all squared plus the quantity C plus 3 D minus 2, all squared

is minimized where ∂E/∂C=0\partial E/\partial C = 0 and ∂E/∂D=0\partial E/\partial D = 0, and these partial derivatives are linear in CC and DD (p. 11). Eliminating, 2D=12D = 1, so D=12D = \frac{1}{2}, and 3C+3=53C + 3 = 5 gives C=23C = \frac{2}{3} (p. 11):

The best line
b=23+12tb = \frac{2}{3} + \frac{1}{2}t
In words

b equals two thirds plus one half t

At t=1,2,3t = 1, 2, 3 the line gives p=(76,53,136)p = (\frac{7}{6}, \frac{5}{3}, \frac{13}{6}), and the errors are (pp. 12–13)

Projection and error
b=p+e:(122)=(7/65/313/6)+(−1/61/3−1/6)b = p + e: \qquad \begin{pmatrix} 1 \\ 2 \\ 2 \end{pmatrix} = \begin{pmatrix} 7/6 \\ 5/3 \\ 13/6 \end{pmatrix} + \begin{pmatrix} -1/6 \\ 1/3 \\ -1/6 \end{pmatrix}
In words

b equals p plus e: 1; 2; 2 equals 7 over 6; 5 over 3; 13 over 6 plus minus one sixth; 1 over 3; minus one sixth

(Strang first takes the errors with the opposite signs and fixes them so that p+e=bp + e = b, pp. 12–13.) Two checks (pp. 13–14): pTe=−736+2036−1336=0p^Te = -\frac{7}{36} + \frac{20}{36} - \frac{13}{36} = 0, and ee is perpendicular to the whole column space, for instance to (1,1,1)(1, 1, 1) and to (1,2,3)(1, 2, 3): −1+4−3=0-1 + 4 - 3 = 0 (in sixths). The two pictures are the same problem: CC and DD are the combination of the columns that gives pp (p. 14).

Why AᵀA is invertible#

The claim: if AA has independent columns, ATAA^TA is invertible (transcript, p. 15). Suppose ATAx=0A^TAx = 0; the goal is x=0x = 0 (pp. 15–16). The idea: multiply by xTx^T (pp. 16–17):

The proof
xTATAx=(Ax)T(Ax)=∥Ax∥2=0  ⇒  Ax=0  ⇒  x=0x^TA^TAx = (Ax)^T(Ax) = \|Ax\|^2 = 0 \;\Rightarrow\; Ax = 0 \;\Rightarrow\; x = 0
In words

x^ capital T capital A ^ capital T capital A x equals ( capital A x)^ capital T ( capital A x) equals the norm of capital A x squared equals 0 which gives capital A x equals 0 which gives x equals 0

A length squared is zero only for the zero vector, so Ax=0Ax = 0; independent columns then leave only x=0x = 0 in the nullspace (p. 17).

Looking ahead (pp. 18–19). Columns are certainly independent if they are perpendicular unit vectors, such as (1,0,0)(1, 0, 0), (0,1,0)(0, 1, 0), (0,0,1)(0, 0, 1); then ATA=IA^TA = I. Such vectors are orthonormal. Strang's favourite pair is (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta) and (−sin⁡θ,cos⁡θ)(-\sin\theta, \cos\theta) (p. 18). Why they are so good, and how to make a basis orthonormal, comes next (p. 19).

Worked example

A best line of your own

Fit the points (t,b)=(0,1)(t, b) = (0, 1), (1,3)(1, 3), (2,4)(2, 4), (3,4)(3, 4) by a line C+DtC + Dt in the least-squares sense; find pp, ee and the total squared error.

Normal equations

AA has columns (1,1,1,1)(1, 1, 1, 1) and (0,1,2,3)(0, 1, 2, 3). ATA=(46614)A^TA = \begin{pmatrix} 4 & 6 \\ 6 & 14 \end{pmatrix} and ATb=(12,23)A^Tb = (12, 23): 4C+6D=124C + 6D = 12, 6C+14D=236C + 14D = 23.

Solve

The determinant is 56−36=2056 - 36 = 20. C=(12⋅14−6⋅23)/20=3020=1.5C = (12 \cdot 14 - 6 \cdot 23)/20 = \frac{30}{20} = 1.5 and D=(4⋅23−6⋅12)/20=1D = (4 \cdot 23 - 6 \cdot 12)/20 = 1. The best line is b=1.5+tb = 1.5 + t.

Projection and error

p=(1.5,2.5,3.5,4.5)p = (1.5, 2.5, 3.5, 4.5) and e=b−p=(−0.5,0.5,0.5,−0.5)e = b - p = (-0.5, 0.5, 0.5, -0.5). Check: ee adds to 0 (perpendicular to the first column) and 0+0.5+1−1.5=00 + 0.5 + 1 - 1.5 = 0 (second column). The total squared error is 4×0.25=14 \times 0.25 = 1.

Summary#

  • Pb=bPb = b for bb in C(A)C(A), Pb=0Pb = 0 for bb in N(AT)N(A^T); b=Pb+(I−P)bb = Pb + (I - P)b.
  • Least squares minimizes ∥Ax−b∥2\|Ax - b\|^2; the normal equations ATAx^=ATbA^TA\hat x = A^Tb are also what calculus gives.
  • For (1,1)(1, 1), (2,2)(2, 2), (3,2)(3, 2) the best line is 23+12t\frac{2}{3} + \frac{1}{2}t, with e=(−16,13,−16)e = (-\frac{1}{6}, \frac{1}{3}, -\frac{1}{6}) perpendicular to the column space.
  • ATAA^TA is invertible when AA has independent columns (and only then, since Ax=0Ax = 0 gives ATAx=0A^TAx = 0: the page's own remark); orthonormal columns give ATA=IA^TA = I.

Symbols on this page

Show the 34 symbols used here
θ\theta theta.
An angle. Say it: “theta” (sounds like “thay-ta or thee-ta”).
≠\neq not equal.
Is not equal to. Say it: “is not equal to”.
⇒\Rightarrow implies.
Implies: what follows is a consequence of what came before. Say it: “which gives”.
×\times times.
Times (multiplication). Between two vectors: the cross product. Say it: “times”.
⋅\cdot dot.
Times (multiplication). Between two vectors: the dot product. Say it: “times”.
∂\partial partial.
Partial derivative: the rate of change with one variable, holding the others fixed. Say it: “partial”.
∑\sum sum.
Sum: add up the terms over the range written below and above. Say it: “the sum of”.
sin⁡\sin sine.
Sine of an angle. Say it: “sine”.
cos⁡\cos cosine.
Cosine of an angle. Say it: “cos”.
ı^\hat\imath hat.
A unit vector: a vector of length 1 that only gives a direction (î, r̂). Say it: “hat”.
TT T.
As a superscript: the transpose. Say it: “capital T”.
xx x.
The unknown vector (C, D); with a hat, the least-squares solution. Say it: “x”.
tt t.
The time (horizontal coordinate) of a data point. Say it: “t”.
ee e.
The error vector b − p. Say it: “e”.
PP P.
The projection matrix A(AᵀA)⁻¹Aᵀ. Say it: “capital P”.
DD D.
The slope of the line C + Dt. Say it: “D”.
AA A.
The matrix with columns (1, 1, 1) and (1, 2, 3): one row per data point. Say it: “capital A”.
CC C.
The intercept of the line C + Dt; as in C(A), the column space. Say it: “C”.
bb b.
The vector of measured values (1, 2, 2); in general the right-hand side of Ax = b. Say it: “b”.
NN N.
As in N(Aᵀ): the left nullspace, perpendicular to the column space. Say it: “N”.
pp p.
The projection of b: the heights of the best line at the data times. Say it: “p”.
tit_{i} initial time.
The time of the i-th data point. Say it: “t sub i”.
EE E.
The total squared error, a function of C and D. Say it: “capital E”.
II I.
The identity matrix. Say it: “capital I”.
p1p_{1} p 1.
The height of the best line at the first data time. Say it: “p one”.
p2p_{2} p 2.
The height of the best line at the second data time. Say it: “p two”.
b1b_{1} b 1.
The first measured value. Say it: “b one”.
b2b_{2} b 2.
The second measured value. Say it: “b two”.
b3b_{3} b 3.
The third measured value. Say it: “b three”.
e1e_{1} e one.
The error at the first data point. Say it: “e one”.
e2e_{2} e two.
The error at the second data point. Say it: “e two”.
e3e_{3} e three.
The error at the third data point. Say it: “e three”.
p3p_{3} p three.
The height of the best line at the third data time. Say it: “p three”.
bib_{i} b i.
The measured value at the i-th data point. Say it: “b i”.

Tap or click a symbol in a formula to see the same explanation. All symbols: symbols and notation.

Words on this page

Show the 28 words used here

New here

projection matrix.
A matrix that sends every vector to its projection onto a fixed line, plane or subspace.
linear regression.
Fitting a straight line, or a combination of chosen functions, to data points, usually by least squares.
outlier.
A data point far from the pattern of the others, often from a mistake, which can pull a least-squares fit badly off.
least squares.
A way to fit a model to data by choosing it to make the sum of the squared errors as small as possible.
orthonormal.
Of a set of vectors: each has length one and each is at right angles to all the others.

Also used

p..
Page, in a reference to a source; pp. means pages, as in pp. 3–5.
projection.
The shadow of a vector on a line or plane: the nearest point to it there, found by dropping a perpendicular. Explained in Projections onto subspaces.
subspace.
A set of vectors inside a larger space that is a space itself: sums and multiples of its vectors stay inside it. Explained in Transposes, permutations and vector spaces.
column space.
All the vectors made by combining the columns of a matrix; Ax = b can be solved exactly when b lies in it. Explained in Matrix multiplication and inverse matrices.
vector.
A list of numbers written as a column, or more generally anything that can be added and multiplied by numbers. Explained in Orthogonal vectors and subspaces.
perpendicular.
At right angles to a line, a surface or another direction. Explained in Inclined planes.
inverse.
The thing that undoes another: a matrix times its inverse gives the identity matrix, as a function followed by its inverse gives back the start. Explained in Planes through three points, and matrices.
symmetric.
Unchanged by some swap, reflection or turn; for an expression in two quantities, unchanged when they are exchanged. Explained in Introduction to Fourier series.
basis.
A set of independent vectors from which every vector in a space can be built, in exactly one way, as a combination. Explained in Independence, basis and dimension.
normal equations.
The equations whose solution gives the least-squares best fit, found by making the error perpendicular to every column.
statistics.
The mathematics of data and chance, or the averages, spreads and other numbers that describe how a random quantity behaves. Explained in Measuring structure: the power spectrum and the correlation function.
invertible.
Of a square matrix or operator: having an inverse that undoes it; for a matrix this happens exactly when its determinant is not zero. Explained in The geometry of linear equations.
positive definite.
Of a symmetric matrix or quadratic form: always giving a positive value for any nonzero input; equivalently, all its eigenvalues are positive. Explained in Symmetric matrices and positive definiteness.
partial derivative.
The rate of change of a function of several variables with respect to one of them, while all the others are held fixed. Explained in Functions of two variables and partial derivatives.
linear.
Of a map, operator or equation: respecting sums and multiples, so that L(au + bv) = aL(u) + bL(v). Explained in The geometry of linear equations.
zero vector.
The vector whose components are all zero; adding it to any vector changes nothing. Explained in Transposes, permutations and vector spaces.
nullspace.
All the vectors x that a matrix sends to zero, that is, all solutions of Ax = 0. Explained in Column space and nullspace.
unit vector.
A vector of length one, used only to point in a direction. Explained in Newton's law of gravitation.
determinant.
A single number worked out from a square array of numbers (a matrix), which measures how the matrix stretches areas or volumes. Explained in Determinants and the cross product.

Academic

recap.
A short summary of what has already been covered.
transcript.
A written record of exactly what was said in a recorded lecture, word for word.
kills.
In maths slang, to make a term or vector zero.
error.
The difference between a measured or computed value and the true one; often how large that difference may be.

Tap or click a word, here or in the text, for its meaning in this lesson. All words: the glossary. How many words are marked in the text: Reading options (the “Aa” button).

Where this leads

In cosmology

32 more cosmology pages that build on this

Pages that build on this

Practise

Check your understanding

  1. If bb is perpendicular to the column space of AA, then PbPb is…
  2. What does least squares minimize when fitting a line C+DtC + Dt to points (ti,bi)(t_i, b_i)?
  3. In the proof that ATAA^TA is invertible, why does xTATAx=0x^TA^TAx = 0 force Ax=0Ax = 0?
0 of 3 answered

Flashcards

1 / 3