Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

Projections and least squares

P1.B1145 min●●○ IntermediateReviewed Nov 30, 2026

Why this matters for machine learning

Projection answers the question “what is the closest thing I can build from these ingredients?”, and least squares is that question asked of data: it is the geometry behind fitting a line, behind linear regression, and behind keeping only the most important directions of a dataset.

  • Linear regression chooses weights so that the predictions are the projection of the targets onto the span of the feature columns, which is why the prediction errors are perpendicular to every feature. (P3.A2: Linear regression, coming soon)
  • PCA keeps a few directions of a dataset by projecting every example onto the span of those directions, and it picks the directions that leave the smallest squared error. (P3.C3: Principal Component Analysis, coming soon)
  • Ridge regression changes the least-squares problem so that it still has one stable answer when features are copies or near-copies of each other, the case where A⊤A\mathbf{A}^\top\mathbf{A} is singular or nearly so. (P3.A6: Regularization: L1 and L2, coming soon)

Before this: Dot product and cosine similarity, Matrices as data and as transformations, Matrix multiplication, Span, independence, basis and rank, Inverse and linear systems, Transpose, symmetric and orthogonal matrices

After this lesson you can

  • Project a vector b\mathbf{b} onto the line through a\mathbf{a} with p=a⊤ba⊤aa\mathbf{p}=\frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}}\mathbf{a}, write the projection matrix P=aa⊤a⊤a\mathbf{P}=\frac{\mathbf{a}\mathbf{a}^\top}{\mathbf{a}^\top\mathbf{a}}, and use P2=P\mathbf{P}^2=\mathbf{P}, P⊤=P\mathbf{P}^\top=\mathbf{P} and I−P\mathbf{I}-\mathbf{P}
  • Project onto the span of the columns of A\mathbf{A} by requiring the error to be perpendicular to every column, which gives the normal equations A⊤Ax^=A⊤b\mathbf{A}^\top\mathbf{A}\hat{\mathbf{x}}=\mathbf{A}^\top\mathbf{b}, P=A(A⊤A)−1A⊤\mathbf{P}=\mathbf{A}(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top, and QQ⊤\mathbf{Q}\mathbf{Q}^\top for orthonormal columns
  • Explain least squares as “the closest point is the projection” (Pythagoras), fit a line to data, and read the residuals in both the scatter picture and the data-space picture
  • Say what happens when A⊤A\mathbf{A}^\top\mathbf{A} is singular, why you should not form A⊤A\mathbf{A}^\top\mathbf{A} in code, and what np.linalg.lstsq returns

Three promises are waiting. P1.B3 gave the dot product a “shadow” of one vector on another, and said that projecting onto whole planes comes later. P1.B5 dropped every point of the plane onto the line y=xy=x, and noticed that (3,1)(3,1) and (1,3)(1,3) both land on (2,2)(2,2). And P1.B9 met tall systems, with more equations than unknowns, and said that the best we can do there is “least squares”. This lesson keeps all three. It is one idea seen three ways: drop a point straight onto a flat set, and the place where it lands is the closest point of that set. Fitting a line to data turns out to be exactly that, in a space with one axis for each data point.

Projection onto a line

Start with the picture of P1.B3. A vector b\mathbf{b} casts a shadow on the line through a\mathbf{a}. There the shadow was a length, a⋅b/∥a∥\mathbf{a}\cdot\mathbf{b}/\lVert\mathbf{a}\rVert. Now we want the shadow as a vector: the point p\mathbf{p} of the line that is closest to b\mathbf{b}. It is called the projection of b\mathbf{b} onto the line.

Use the vectors of P1.B5: a=(1,1)\mathbf{a}=(1,1) and b=(3,1)\mathbf{b}=(3,1). The line through a\mathbf{a} is the line y=xy=x. Every point of it has the form p=x^ a\mathbf{p}=\hat{x}\,\mathbf{a} for some number x^\hat{x}, so the question is which multiple of a\mathbf{a} lands closest to b\mathbf{b}. The leftover is the error e=b−p\mathbf{e}=\mathbf{b}-\mathbf{p}. The closest point is the one where the error meets the line at a right angle, like the shortest way from a point to a straight road. (The picture of the road is flat; later the “road” will be a whole plane, and the same right-angle rule still holds.) So we ask that e\mathbf{e} be perpendicular to a\mathbf{a}:

a⊤(b−x^a)=0\mathbf{a}^\top(\mathbf{b}-\hat{x}\mathbf{a})=0

Expand it: a⊤b−x^ a⊤a=0\mathbf{a}^\top\mathbf{b}-\hat{x}\,\mathbf{a}^\top\mathbf{a}=0, so

x^=a⊤ba⊤a,p=a⊤ba⊤a a\hat{x}=\frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}},\qquad \mathbf{p}=\frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}}\,\mathbf{a}

For our numbers a⊤b=4\mathbf{a}^\top\mathbf{b}=4 and a⊤a=2\mathbf{a}^\top\mathbf{a}=2, so x^=2\hat{x}=2, p=(2,2)\mathbf{p}=(2,2) and e=(3,1)−(2,2)=(1,−1)\mathbf{e}=(3,1)-(2,2)=(1,-1). Check the right angle: a⋅e=1−1=0\mathbf{a}\cdot\mathbf{e}=1-1=0. That is the (2,2)(2,2) of P1.B5. It also agrees with P1.B3: the shadow’s length is a⋅b/∥a∥=4/2≈2.828\mathbf{a}\cdot\mathbf{b}/\lVert\mathbf{a}\rVert=4/\sqrt2\approx2.828, and ∥(2,2)∥=8≈2.828\lVert(2,2)\rVert=\sqrt8\approx2.828. The vector projection is the scalar projection times the unit vector a/∥a∥\mathbf{a}/\lVert\mathbf{a}\rVert.

Three small remarks.

  • The a⊤a\mathbf{a}^\top\mathbf{a} is there so that the length of a\mathbf{a} does not matter. Use 2a=(2,2)2\mathbf{a}=(2,2) instead: the top becomes 88, the bottom 88, x^=1\hat{x}=1, and p=1⋅(2,2)\mathbf{p}=1\cdot(2,2), the same point. The projection depends on the line, not on which vector along it you picked. If a\mathbf{a} has length 11 the bottom is 11 and p=(a⋅b)a\mathbf{p}=(\mathbf{a}\cdot\mathbf{b})\mathbf{a}.
  • The coefficient has a sign. If b\mathbf{b} points backwards (more than 90∘90^\circ from a\mathbf{a}), x^\hat{x} is negative: for b=(−3,−1)\mathbf{b}=(-3,-1) it is −2-2. If b\mathbf{b} is perpendicular to a\mathbf{a}, x^=0\hat{x}=0 and p=0\mathbf{p}=\mathbf{0}.
  • a=0\mathbf{a}=\mathbf{0} is not allowed. Then a⊤a=0\mathbf{a}^\top\mathbf{a}=0, and the zero vector spans no line to project on.

Pythagoras sits in the picture: b\mathbf{b}, p\mathbf{p} and e\mathbf{e} form a right triangle, so ∥b∥2=∥p∥2+∥e∥2\lVert\mathbf{b}\rVert^2=\lVert\mathbf{p}\rVert^2+\lVert\mathbf{e}\rVert^2. Here 10=8+210=8+2.

The projection matrix

The projection is a linear map: p\mathbf{p} is the vector a\mathbf{a} times the number 1a⊤aa⊤b\frac{1}{\mathbf{a}^\top\mathbf{a}}\mathbf{a}^\top\mathbf{b}, and since matrix products can be regrouped (P1.B6), a (a⊤b)=(aa⊤) b\mathbf{a}\,(\mathbf{a}^\top\mathbf{b})=(\mathbf{a}\mathbf{a}^\top)\,\mathbf{b}. So p=Pb\mathbf{p}=\mathbf{P}\mathbf{b} with

P=aa⊤a⊤a\mathbf{P}=\frac{\mathbf{a}\mathbf{a}^\top}{\mathbf{a}^\top\mathbf{a}}

The top is the outer product of P1.B6 (a column times a row, a full matrix). For a=(1,1)\mathbf{a}=(1,1):

P=12(1111)=(0.50.50.50.5)\mathbf{P}=\frac12\begin{pmatrix}1&1\\1&1\end{pmatrix}=\begin{pmatrix}0.5&0.5\\0.5&0.5\end{pmatrix}

This is the matrix that P1.B5 used for “projection onto the line y=xy=x”. It sends (3,1)(3,1) and (1,3)(1,3) both to (2,2)(2,2), and (1,−1)(1,-1) to (0,0)(0,0). Its two columns are equal, so its rank is 11 (P1.B7): every output lies on the line. The matrix has three properties that every projection matrix has.

  • P2=P\mathbf{P}^2=\mathbf{P}. Projecting twice changes nothing, because the first projection already put the point on the line, and a point of the line is its own projection. In symbols, P(Pb)=Pb\mathbf{P}(\mathbf{P}\mathbf{b})=\mathbf{P}\mathbf{b} for every b\mathbf{b}. A matrix with this property is called idempotent.
  • P⊤=P\mathbf{P}^\top=\mathbf{P}. It is symmetric (P1.B10): (aa⊤)⊤=aa⊤(\mathbf{a}\mathbf{a}^\top)^\top=\mathbf{a}\mathbf{a}^\top.
  • It cannot be undone. Many inputs share one output (this holds for every projection onto something smaller than the whole space), so P\mathbf{P} has no inverse (P1.B9). This is the “squashing” of P1.B5 and P1.B8: det⁡P=0\det\mathbf{P}=0.

The error is a projection too. Since e=b−Pb=(I−P)b\mathbf{e}=\mathbf{b}-\mathbf{P}\mathbf{b}=(\mathbf{I}-\mathbf{P})\mathbf{b}, the matrix I−P\mathbf{I}-\mathbf{P} projects onto the perpendicular direction. For our P\mathbf{P} it is (0.5−0.5−0.50.5)\begin{pmatrix}0.5&-0.5\\-0.5&0.5\end{pmatrix}, and (I−P)(3,1)=(1,−1)(\mathbf{I}-\mathbf{P})(3,1)=(1,-1), the error found above. Every vector splits into two perpendicular pieces, b=Pb+(I−P)b\mathbf{b}=\mathbf{P}\mathbf{b}+(\mathbf{I}-\mathbf{P})\mathbf{b}: the part along the line and the part across it.

Projection onto a plane

Now the signature picture. A line has one direction. A plane through the origin in 3-D is the span of two independent vectors a1\mathbf{a}_1 and a2\mathbf{a}_2 (P1.B7), and it is the set of all combinations x^1a1+x^2a2\hat{x}_1\mathbf{a}_1+\hat{x}_2\mathbf{a}_2. Put them side by side as the columns of a 3×23\times2 matrix A\mathbf{A}, and every point of the plane is Ax^\mathbf{A}\hat{\mathbf{x}} for some x^\hat{\mathbf{x}}. A point b\mathbf{b} above or below the plane is not of that form. Which point of the plane is closest to it? Play with the widget first.

Drop b straight onto the plane

a₁a₂bpexyz
Columns: a₁ = (1, 0, 1), a₂ = (0, 1, 1)
b = (0, 1, 4)
coefficients x̂ = (1, 2)
p = A x̂ = (1, 2, 3)
e = b − p = (−1, −1, 1)
‖e‖ = 1.732
Aᵀe = (0, 0)
‖b‖² = ‖p‖² + ‖e‖²: 17 = 14 + 3

b is off the plane. p is the closest point of the plane to b, and e = b − p meets it at a right angle: every Aᵀe entry is 0.

Blue and pink arrows: the columns of A. The tinted patch (or the thick line) is the part of the plane they span. Dark dot: b. Green square: p. Red arrow: e, with a small square marking the right angle. It is a flat picture of a 3-D scene, so turn the view to read depth.

Projection onto a plane. The blue and pink arrows are the columns of A. The tinted patch is the plane they span, and the dark dot is the point b. Its perpendicular drop onto the plane is the green square p, and the red arrow is the error e = b − p, with a small square marking the right angle. Three sliders move b and a fourth turns the view. Buttons choose what to project onto (a line, three planes), put b in the plane, or make b perpendicular to it. The checkbox adds a second point q of the plane, set by weights on the columns. The readouts show p, e, the length of e, Aᵀe and the Pythagoras check.

Things to try:

  1. Start with the plane of P1.B7 (the columns (1,0,1)(1,0,1) and (0,1,1)(0,1,1), whose span is the plane z=x+yz=x+y) and b=(0,1,4)\mathbf{b}=(0,1,4). The drop is p=(1,2,3)\mathbf{p}=(1,2,3), the error is e=(−1,−1,1)\mathbf{e}=(-1,-1,1) with ∥e∥=3≈1.732\lVert\mathbf{e}\rVert=\sqrt3\approx1.732, and A⊤e=(0,0)\mathbf{A}^\top\mathbf{e}=(0,0). Turn the view. The right-angle marker can look slanted from some directions, because the picture is a flat drawing of a 3-D scene; the readout A⊤e=(0,0)\mathbf{A}^\top\mathbf{e}=(0,0) is the proof.
  2. Move the sliders for b\mathbf{b}. The drop p\mathbf{p} slides about in the plane, the length of e\mathbf{e} changes, but A⊤e\mathbf{A}^\top\mathbf{e} stays (0,0)(0,0) and the Pythagoras line stays true. At the starting b\mathbf{b} it reads 17=14+317=14+3.
  3. Press Put b in the plane: b=(1,2,3)\mathbf{b}=(1,2,3), so p=b\mathbf{p}=\mathbf{b} and e=0\mathbf{e}=\mathbf{0}. Press Make b perpendicular: b=(1,1,−1)\mathbf{b}=(1,1,-1), so p=0\mathbf{p}=\mathbf{0} and e=b\mathbf{e}=\mathbf{b}. These are the two extremes.
  4. Tick Compare with another point q. Slide its weights. The squared distance ∥b−q∥2\lVert\mathbf{b}-\mathbf{q}\rVert^2 is never below ∥e∥2\lVert\mathbf{e}\rVert^2, and it equals ∥e∥2+∥p−q∥2\lVert\mathbf{e}\rVert^2+\lVert\mathbf{p}-\mathbf{q}\rVert^2. It touches ∥e∥2\lVert\mathbf{e}\rVert^2 only when q=p\mathbf{q}=\mathbf{p}, at the weights x^=(1,2)\hat{\mathbf{x}}=(1,2).
  5. Press the other buttons. A line is the case of the first section (a=(1,1,0)\mathbf{a}=(1,1,0) is the line y=xy=x of P1.B5, lying in the floor). Orthonormal columns uses the matrix of P1.B10. Fit a line to 3 points is the data-space picture of the next sections.

Why the error is perpendicular to the whole plane

We want p=Ax^\mathbf{p}=\mathbf{A}\hat{\mathbf{x}} with the error e=b−Ax^\mathbf{e}=\mathbf{b}-\mathbf{A}\hat{\mathbf{x}} at a right angle to the plane. A vector is perpendicular to a plane when it is perpendicular to every vector in the plane, and for that it is enough to check the two columns (anything in the plane is a combination of them, and the dot product respects combinations, P1.B3). Two dot products must be zero, a1⊤e=0\mathbf{a}_1^\top\mathbf{e}=0 and a2⊤e=0\mathbf{a}_2^\top\mathbf{e}=0. Stacked as the rows of A⊤\mathbf{A}^\top (P1.B10), they are one equation:

A⊤(b−Ax^)=0\mathbf{A}^\top(\mathbf{b}-\mathbf{A}\hat{\mathbf{x}})=\mathbf{0}

Multiply out and move one term across, and these are the normal equations:

A⊤A x^=A⊤b\mathbf{A}^\top\mathbf{A}\,\hat{\mathbf{x}}=\mathbf{A}^\top\mathbf{b}

“Normal” is an old word for “perpendicular”. This is the equation P1.B9 and P1.B10 pointed to. It is a square linear system, A⊤A\mathbf{A}^\top\mathbf{A} is 2×22\times2 here and symmetric (P1.B10), and when the columns of A\mathbf{A} are independent (P1.B7), A⊤A\mathbf{A}^\top\mathbf{A} is invertible (the box below), so

x^=(A⊤A)−1A⊤bp=A(A⊤A)−1A⊤b\begin{aligned} \hat{\mathbf{x}}&=(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{b}\\ \mathbf{p}&=\mathbf{A}(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{b} \end{aligned}

The matrix in front of b\mathbf{b} is the projection matrix of the plane, P=A(A⊤A)−1A⊤\mathbf{P}=\mathbf{A}(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top. With one column a\mathbf{a} it is the matrix of the line case above, because a⊤a\mathbf{a}^\top\mathbf{a} is then a single number.

For the widget’s start, A=(100111)\mathbf{A}=\begin{pmatrix}1&0\\0&1\\1&1\end{pmatrix} and b=(0,1,4)\mathbf{b}=(0,1,4):

A⊤A=(2112),A⊤b=(45)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}2&1\\1&2\end{pmatrix},\qquad \mathbf{A}^\top\mathbf{b}=\begin{pmatrix}4\\5\end{pmatrix}

The system 2x^1+x^2=42\hat{x}_1+\hat{x}_2=4, x^1+2x^2=5\hat{x}_1+2\hat{x}_2=5 gives x^=(1,2)\hat{\mathbf{x}}=(1,2), so p=1⋅(1,0,1)+2⋅(0,1,1)=(1,2,3)\mathbf{p}=1\cdot(1,0,1)+2\cdot(0,1,1)=(1,2,3) and e=(0,1,4)−(1,2,3)=(−1,−1,1)\mathbf{e}=(0,1,4)-(1,2,3)=(-1,-1,1). Check: A⊤e=(−1+1, −1+1)=(0,0)\mathbf{A}^\top\mathbf{e}=(-1+1,\,-1+1)=(0,0). The projection matrix is

P=13(2−11−121112)\mathbf{P}=\frac13\begin{pmatrix}2&-1&1\\-1&2&1\\1&1&2\end{pmatrix}

and P(0,1,4)=13(3,6,9)=(1,2,3)\mathbf{P}(0,1,4)=\frac13(3,6,9)=(1,2,3). As for a line, P2=P\mathbf{P}^2=\mathbf{P} (a point of the plane stays put) and P⊤=P\mathbf{P}^\top=\mathbf{P}, now with rank 22. The matrix I−P\mathbf{I}-\mathbf{P} projects onto the direction perpendicular to the plane, here the line through (1,1,−1)(1,1,-1).

Go deeper Why independent columns make AᵀA invertible

Suppose A⊤Ax=0\mathbf{A}^\top\mathbf{A}\mathbf{x}=\mathbf{0}. Multiply by x⊤\mathbf{x}^\top on the left: x⊤A⊤Ax=(Ax)⊤(Ax)=∥Ax∥2=0\mathbf{x}^\top\mathbf{A}^\top\mathbf{A}\mathbf{x}=(\mathbf{A}\mathbf{x})^\top(\mathbf{A}\mathbf{x})=\lVert\mathbf{A}\mathbf{x}\rVert^2=0 (the trick of the positive semi-definite box in P1.B10). A vector of length 00 is the zero vector, so Ax=0\mathbf{A}\mathbf{x}=\mathbf{0}. If the columns are independent, that forces x=0\mathbf{x}=\mathbf{0}, so only the zero vector is sent to zero: A⊤A\mathbf{A}^\top\mathbf{A} is invertible (P1.B9). Conversely, if the columns are dependent there is an x≠0\mathbf{x}\ne\mathbf{0} with Ax=0\mathbf{A}\mathbf{x}=\mathbf{0}, so A⊤Ax=0\mathbf{A}^\top\mathbf{A}\mathbf{x}=\mathbf{0} too and A⊤A\mathbf{A}^\top\mathbf{A} is singular. So “A⊤A\mathbf{A}^\top\mathbf{A} is invertible” and “the columns are independent” are the same statement.

Go deeper The calculus route to the same equations

The squared distance from b\mathbf{b} to the point Ax\mathbf{A}\mathbf{x} is f(x)=∥Ax−b∥2=x⊤A⊤Ax−2b⊤Ax+b⊤bf(\mathbf{x})=\lVert\mathbf{A}\mathbf{x}-\mathbf{b}\rVert^2=\mathbf{x}^\top\mathbf{A}^\top\mathbf{A}\mathbf{x}-2\mathbf{b}^\top\mathbf{A}\mathbf{x}+\mathbf{b}^\top\mathbf{b}. Its gradient (P1.C11 teaches how to take it) is 2A⊤Ax−2A⊤b=2A⊤(Ax−b)2\mathbf{A}^\top\mathbf{A}\mathbf{x}-2\mathbf{A}^\top\mathbf{b}=2\mathbf{A}^\top(\mathbf{A}\mathbf{x}-\mathbf{b}). Setting it to 0\mathbf{0} gives A⊤(Ax−b)=0\mathbf{A}^\top(\mathbf{A}\mathbf{x}-\mathbf{b})=\mathbf{0}: the normal equations again, with no picture. The picture says more, though: it shows why the point found is a minimum and not a maximum, and what the error looks like. Gradient descent (P1.F3) can also minimise ff step by step, and P3.A2 compares it with this one-step formula.

Orthonormal columns: the formula collapses

If the columns of Q\mathbf{Q} are orthonormal (perpendicular, each of length 11; P1.B10), then Q⊤Q=I\mathbf{Q}^\top\mathbf{Q}=\mathbf{I}, and the normal equations become trivial:

x^=Q⊤b,P=QQ⊤\hat{\mathbf{x}}=\mathbf{Q}^\top\mathbf{b},\qquad \mathbf{P}=\mathbf{Q}\mathbf{Q}^\top

Each coefficient is one dot product, x^i=qi⋅b\hat{x}_i=\mathbf{q}_i\cdot\mathbf{b}, and p=∑i(qi⋅b) qi\mathbf{p}=\sum_i(\mathbf{q}_i\cdot\mathbf{b})\,\mathbf{q}_i: the projection is the sum of the shadows on the perpendicular directions. This is the payoff promised in P1.B7 and P1.B10: perpendicular bases are nicer because the coordinates come from dot products, with nothing to solve. Take the tall matrix of P1.B10, T=13(122−221)\mathbf{T}=\tfrac13\begin{pmatrix}1&2\\2&-2\\2&1\end{pmatrix}, and b=(3,3,3)\mathbf{b}=(3,3,3). The coefficients are T⊤b=(5,1)\mathbf{T}^\top\mathbf{b}=(5,1), the projection is p=5q1+q2=13(7,8,11)≈(2.333,2.667,3.667)\mathbf{p}=5\mathbf{q}_1+\mathbf{q}_2=\tfrac13(7,8,11)\approx(2.333,2.667,3.667), and the error is e=13(2,1,−2)\mathbf{e}=\tfrac13(2,1,-2), of length exactly 11. Pythagoras: 27=(25+1)+127=(25+1)+1. And TT⊤=19(5−24−282425)\mathbf{T}\mathbf{T}^\top=\tfrac19\begin{pmatrix}5&-2&4\\-2&8&2\\4&2&5\end{pmatrix}, the matrix that P1.B10 said was a projection, is the projection matrix of this plane. (The Orthonormal columns button of the widget shows these numbers.)

A warning: the projection onto a plane is the sum of the projections onto its two columns only when the columns are perpendicular. For a1=(1,0,1)\mathbf{a}_1=(1,0,1) and a2=(0,1,1)\mathbf{a}_2=(0,1,1) the projections of b=(0,1,4)\mathbf{b}=(0,1,4) onto the two lines are (2,0,2)(2,0,2) and (0,2.5,2.5)(0,2.5,2.5), and their sum (2,2.5,4.5)(2,2.5,4.5) is not p=(1,2,3)\mathbf{p}=(1,2,3). The columns are not perpendicular: a1⋅a2=1\mathbf{a}_1\cdot\mathbf{a}_2=1. The shadows overlap, and the off-diagonal 11s of A⊤A\mathbf{A}^\top\mathbf{A} are what the normal equations use to correct for it.

Every plane has many orthonormal bases, and the projection matrix depends only on the plane, not on which columns you chose to describe it. Gram–Schmidt (the box in P1.B10) turns any independent columns into orthonormal ones for the same plane, and then P=QQ⊤\mathbf{P}=\mathbf{Q}\mathbf{Q}^\top.

Go deeper Rank, trace and eigenvalues of a projection

A projection onto a kk-dimensional subspace has rank kk, and its trace (sum of the diagonal) is also kk: for the plane above 13(2+2+2)=2\tfrac13(2+2+2)=2, and for the line of P1.B5 it is 0.5+0.5=10.5+0.5=1. The reason is the cyclic property of the trace, tr⁡(A(A⊤A)−1A⊤)=tr⁡((A⊤A)−1A⊤A)=tr⁡(Ik)=k\operatorname{tr}(\mathbf{A}(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top)=\operatorname{tr}((\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{A})=\operatorname{tr}(\mathbf{I}_k)=k. A vector in the subspace is unchanged by P\mathbf{P} and a vector perpendicular to it is sent to 0\mathbf{0}, so the eigenvalues (P1.B12) of a projection are 11 and 00 only: 11 with multiplicity kk and 00 with the rest. For the matrix of P1.B5, P(1,1)=(1,1)\mathbf{P}(1,1)=(1,1) and P(1,−1)=(0,0)\mathbf{P}(1,-1)=(0,0). Idempotence explains it: if Pv=λv\mathbf{P}\mathbf{v}=\lambda\mathbf{v} then P2v=λ2v=λv\mathbf{P}^2\mathbf{v}=\lambda^2\mathbf{v}=\lambda\mathbf{v}, so λ2=λ\lambda^2=\lambda.

Least squares: when there is no solution

Now the promise of P1.B9. A tall system Ax=b\mathbf{A}\mathbf{x}=\mathbf{b} (more equations than unknowns) asks for a combination of the columns of A\mathbf{A} that equals b\mathbf{b}. The combinations make up the plane (or a bigger flat set) of the columns, and a b\mathbf{b} outside it is out of reach. The honest fix is to ask for the closest we can get: choose x\mathbf{x} to make the residual b−Ax\mathbf{b}-\mathbf{A}\mathbf{x} as short as possible,

min⁡x ∥Ax−b∥2\min_{\mathbf{x}}\ \lVert\mathbf{A}\mathbf{x}-\mathbf{b}\rVert^2

The length squared is a sum of squares, ∑i(error in equation i)2\sum_i(\text{error in equation }i)^2, which gives the method its name: least squares. The answer is the projection. Take any x\mathbf{x} at all. Then

b−Ax=(b−p)⏟e+(p−Ax)⏟inside the plane\mathbf{b}-\mathbf{A}\mathbf{x}=\underbrace{(\mathbf{b}-\mathbf{p})}_{\mathbf{e}}+\underbrace{(\mathbf{p}-\mathbf{A}\mathbf{x})}_{\text{inside the plane}}

and the first piece is perpendicular to the second (the error is perpendicular to the whole plane, and p−Ax\mathbf{p}-\mathbf{A}\mathbf{x} lies in it). By Pythagoras,

∥b−Ax∥2=∥e∥2+∥p−Ax∥2≥∥e∥2\begin{aligned} \lVert\mathbf{b}-\mathbf{A}\mathbf{x}\rVert^2&=\lVert\mathbf{e}\rVert^2+\lVert\mathbf{p}-\mathbf{A}\mathbf{x}\rVert^2\\ &\ge\lVert\mathbf{e}\rVert^2 \end{aligned}

with equality exactly when Ax=p\mathbf{A}\mathbf{x}=\mathbf{p}. So no point of the plane beats p\mathbf{p}, and the least-squares solution x^\hat{\mathbf{x}} is the one that reaches it. The widget’s comparison with the point q\mathbf{q} shows exactly this. When the columns are independent, Ax=p\mathbf{A}\mathbf{x}=\mathbf{p} has exactly one solution, so x^\hat{\mathbf{x}} is unique. If b\mathbf{b} happens to lie in the plane, e=0\mathbf{e}=\mathbf{0}, and least squares returns the exact solution of Ax=b\mathbf{A}\mathbf{x}=\mathbf{b}: it contains ordinary solving as a special case.

Fitting a line to data

Here is the problem that least squares was made for. Three measurements: at x=0x=0 we saw y=1y=1, at x=1x=1 we saw 33, at x=2x=2 we saw 22. We believe yy is roughly a straight-line function of xx, y≈c+mxy\approx c+mx with an intercept cc and a slope mm. Each point gives one equation:

c+m⋅0=1c+m⋅1=3c+m⋅2=2that is(101112)⏟A(cm)=(132)⏟y\begin{gathered} \begin{aligned} c+m\cdot0&=1\\ c+m\cdot1&=3\\ c+m\cdot2&=2 \end{aligned} \\ \text{that is} \\ \underbrace{\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}}_{\mathbf{A}}\begin{pmatrix}c\\m\end{pmatrix}=\underbrace{\begin{pmatrix}1\\3\\2\end{pmatrix}}_{\mathbf{y}} \end{gathered}

Three equations, two unknowns: a tall system. The first column of A\mathbf{A} is all ones (it multiplies the intercept) and the second holds the xx values. The three points do not lie on one line, so there is no exact solution. The normal equations give the best compromise:

A⊤A=(3335),A⊤y=(67)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}3&3\\3&5\end{pmatrix},\quad \mathbf{A}^\top\mathbf{y}=\begin{pmatrix}6\\7\end{pmatrix}

so 3c+3m=63c+3m=6 and 3c+5m=73c+5m=7, which give m=0.5m=0.5 and c=1.5c=1.5.

The best line is y=1.5+0.5xy=1.5+0.5x. Its predictions are y^=Aw^=(1.5, 2, 2.5)\hat{\mathbf{y}}=\mathbf{A}\hat{\mathbf{w}}=(1.5,\,2,\,2.5), the residuals are e=y−y^=(−0.5, 1, −0.5)\mathbf{e}=\mathbf{y}-\hat{\mathbf{y}}=(-0.5,\,1,\,-0.5), and the sum of squared residuals is 0.25+1+0.25=1.50.25+1+0.25=1.5. (In regression the unknowns are called weights and written w\mathbf{w}, and the data matrix is called X\mathbf{X}; P3.A2 does that. Here we keep A\mathbf{A}.)

Two pictures of the same fit

This is the classic confusion, so take it slowly. There are two different pictures, and they look nothing alike.

  • The scatter picture. Axes xx and yy. Each data point is a point. The model is a line. A residual is a vertical gap between a point and the line. Nothing here looks perpendicular to anything.
  • The data-space picture. One axis for each data point, so three points mean three axes. The whole list of heights y=(1,3,2)\mathbf{y}=(1,3,2) is now one point of 3-D space. The columns 1=(1,1,1)\mathbf{1}=(1,1,1) and x=(0,1,2)\mathbf{x}=(0,1,2) are two vectors, and their span is a plane: the set of all lists of heights that some line c+mxc+mx could produce. A choice of (c,m)(c,m) is a point of that plane. The best choice is the foot of the perpendicular from y\mathbf{y}, and the residual vector e=(−0.5,1,−0.5)\mathbf{e}=(-0.5,1,-0.5) is the three vertical gaps listed together. Its length squared is the sum of squared residuals.

The right angle belongs to the second picture. There the error is perpendicular to both columns: 1⊤e=−0.5+1−0.5=0\mathbf{1}^\top\mathbf{e}=-0.5+1-0.5=0 and x⊤e=0⋅(−0.5)+1⋅1+2⋅(−0.5)=0\mathbf{x}^\top\mathbf{e}=0\cdot(-0.5)+1\cdot1+2\cdot(-0.5)=0. Press Fit a line to 3 points in the projection widget: it shows this plane, this y\mathbf{y} and these numbers. Then use the second widget for the scatter picture.

Fit a line to the points

123456123456xy
best line your line residuals of the best line
Best line: y = 1.5 + 0.5 x
Smallest sum of squared residuals = 1.5
Your line y = 1 + 1 x: sum of squared residuals = 2 (best + 0.5; the formula ‖A(w − ŵ)‖² gives 0.5)
Σ residuals = 0
Σ x · residual = 0
In “data space” (one axis per point): x = (0, 1, 2), y = (1, 3, 2), fitted ŷ = (1.5, 2, 2.5), residual e = (−0.5, 1, −0.5)

The best line makes the sum of squared residuals as small as possible. Its residuals add up to 0 and are perpendicular to the x values: Aᵀe = 0.

Your line is worse by 0.5. That is exactly ‖A(w − ŵ)‖², the squared length of the step between your weights and the best ones.

Drag a point (or Tab to it and use the arrow keys). The vertical segments are the residuals: how far each point is above or below the line. Least squares minimises the sum of their squares, not their perpendicular distances to the line. The centre of the points, (1, 2), lies on the best line (when there is one).

Line fit. Up to six points can be dragged (or reached with Tab and the arrow keys). The solid blue line is the least-squares line, and the red vertical segments are its residuals. The dashed pink line is yours, set with two sliders, with its own residuals drawn beside the points. The readouts give both sums of squared residuals, the gap between them, the sums Σ residuals and Σ x · residual, and the same fit written as vectors in data space. Buttons add or remove a point or set up special cases.

Things to try:

  1. At the start the points are (0,1)(0,1), (1,3)(1,3), (2,2)(2,2), and the best line is y=1.5+0.5xy=1.5+0.5x with a sum of squares of 1.51.5. The two sums of residuals read Σ residuals=0\Sigma\,\text{residuals}=0 and Σ x⋅residual=0\Sigma\,x\cdot\text{residual}=0: these are the two entries of A⊤e=0\mathbf{A}^\top\mathbf{e}=\mathbf{0}.
  2. Your line starts at y=1+xy=1+x, with a sum of 22. That is the best sum plus 0.50.5, and the readout shows that gap is the squared length ∥A(w−w^)∥2\lVert\mathbf{A}(\mathbf{w}-\hat{\mathbf{w}})\rVert^2, the Pythagoras step above. Try to push your sum below 1.51.5 with the two sliders: you cannot. Press Copy the best line into mine and, for the starting points, the gap becomes 00 (the sliders move in steps of 0.050.05, so for other data the copy can be a hair off and the readout shows a tiny gap).
  3. Drag the points around. The two sums stay 00 whatever you do, and the line passes through the centre of the points (the average xx and the average yy, printed under the plot).
  4. Press Add a point. The fourth point (3,4)(3,4) gives y=1.3+0.8xy=1.3+0.8x with a sum of 1.81.8 and residuals (−0.3,0.9,−0.9,0.3)(-0.3,0.9,-0.9,0.3).
  5. Press Two points: the line passes through both and the sum is 00. Press All the same x: all points sit above one another, the slope is undefined (see below), and every non-vertical line through the centre is equally good.

Why the residuals add up to zero

The residual vector is perpendicular to every column of A\mathbf{A}, and the first column is 1\mathbf{1}, the all-ones vector. So 1⊤e=∑iei=0\mathbf{1}^\top\mathbf{e}=\sum_ie_i=0: with an intercept column, the residuals of the least-squares fit add up to 00. The condition matters. If you fit y=mxy=mx with no intercept (a line forced through the origin), there is only the column x\mathbf{x}, and x⊤e=0\mathbf{x}^\top\mathbf{e}=0 is all you get. For the same three points m^=x⊤y/x⊤x=7/5=1.4\hat{m}=\mathbf{x}^\top\mathbf{y}/\mathbf{x}^\top\mathbf{x}=7/5=1.4, the residuals are (1, 1.6, −0.8)(1,\,1.6,\,-0.8), and they add up to 1.81.8, not 00. The same equation also puts the average point on the line: the first normal equation is ∑i(yi−c−mxi)=0\sum_i(y_i-c-mx_i)=0, that is yˉ=c+mxˉ\bar{y}=c+m\bar{x}.

The same recipe, other columns

Nothing about the method needs the columns to be 1\mathbf{1} and x\mathbf{x}. They can be any features: a column of house sizes and a column of room counts (a plane fitted in 3-D, with two features), or 1\mathbf{1}, x\mathbf{x} and x2\mathbf{x}^2 (a parabola, which is still “linear” because it is linear in the unknown weights). The model y^=Aw\hat{\mathbf{y}}=\mathbf{A}\mathbf{w} is a combination of the columns, the best w\mathbf{w} gives the projection of y\mathbf{y} onto their span, and the residual is perpendicular to every feature. That is linear regression (P3.A2), and its loss and its statistics are that lesson’s job. What this lesson gave it is the geometry and the normal equations.

When the columns are dependent

The formula (A⊤A)−1(\mathbf{A}^\top\mathbf{A})^{-1} needs independent columns (see the box above). What if they are not? Take the “same xx” case of the widget: the points (2,1)(2,1), (2,3)(2,3), (2,2)(2,2). The columns are 1=(1,1,1)\mathbf{1}=(1,1,1) and x=(2,2,2)\mathbf{x}=(2,2,2), and the second is twice the first (P1.B7). Then

A⊤A=(36612)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}3&6\\6&12\end{pmatrix}

and its determinant is 3⋅12−6⋅6=03\cdot12-6\cdot6=0.

Geometrically the “plane” is only a line, the span of (1,1,1)(1,1,1), and the projection of y=(1,3,2)\mathbf{y}=(1,3,2) onto it is its average: p=(2,2,2)\mathbf{p}=(2,2,2). So the projection is still unique (p\mathbf{p} exists for any set of columns, because a closest point of a flat set exists), but now many weights produce it: any c+2m=2c+2m=2 does, for example (c,m)=(2,0)(c,m)=(2,0) or (0,1)(0,1). There are infinitely many least-squares solutions, one for each way to write p\mathbf{p} with the dependent columns (the null direction of P1.B7: An=0\mathbf{A}\mathbf{n}=\mathbf{0} with n=(2,−1)\mathbf{n}=(2,-1)). The normal equations are still true, but they are singular, so x^\hat{\mathbf{x}} is not given by an inverse. Redundant (copied) features in a regression do exactly this, and a wide matrix, with more columns than rows, always has dependent columns. Two ways out:

  • Pick one solution by a rule. np.linalg.lstsq returns the one with the smallest length ∥x∥\lVert\mathbf{x}\rVert; here (0.4,0.8)(0.4,0.8). The pseudo-inverse of P1.B13 is the matrix that gives it.
  • Change the problem so it has one answer. Ridge regression (P3.A6) adds a penalty that makes the answer unique, and it also calms the wild weights that nearly dependent columns cause (P1.B9).

In practice: use lstsq, not the normal equations

For hand work and for understanding, the normal equations are perfect. In code, avoid forming A⊤A\mathbf{A}^\top\mathbf{A}. The reason is the condition number of P1.B9: the condition number of A⊤A\mathbf{A}^\top\mathbf{A} is the square of that of A\mathbf{A}, cond⁡(A⊤A)=cond⁡(A)2\operatorname{cond}(\mathbf{A}^\top\mathbf{A})=\operatorname{cond}(\mathbf{A})^2 (for the usual 2-norm condition number). By the rule of thumb of P1.B9, squaring doubles the number of digits you may lose. Example: the four points at x=1000,1001,1002,1003x=1000,1001,1002,1003 have cond⁡(A)≈9×105\operatorname{cond}(\mathbf{A})\approx9\times10^{5} but cond⁡(A⊤A)≈8×1011\operatorname{cond}(\mathbf{A}^\top\mathbf{A})\approx8\times10^{11} (the cell below prints both). Of the 16 digits of a floating-point number, solving with A\mathbf{A} may lose about 6, and solving the normal equations about 12.

Library routines work on A\mathbf{A} itself:

  • np.linalg.lstsq(A, b, rcond=None) returns four things, in this order: the solution x; the residuals, the sum of squared residuals ∥b−Ax∥2\lVert\mathbf{b}-\mathbf{A}\mathbf{x}\rVert^2 (one number per column of b); the rank of A; and the singular values of A (P1.B13). The residuals array is empty when the rank of A is less than its number of columns, or when A has no more rows than columns; so for a square or wide matrix, or for dependent columns, compute ∥b−Ax∥2\lVert\mathbf{b}-\mathbf{A}\mathbf{x}\rVert^2 yourself. It also returns the smallest-length solution when there are many. NumPy computes it from the singular value decomposition of A\mathbf{A} (it calls the LAPACK routine gelsd). Pass rcond=None, as above, to use the current default cutoff.
  • QR. Write A=QR\mathbf{A}=\mathbf{Q}\mathbf{R} with orthonormal columns in Q\mathbf{Q} and R\mathbf{R} upper triangular (the box in P1.B10). Then P=QQ⊤\mathbf{P}=\mathbf{Q}\mathbf{Q}^\top and the normal equations reduce to Rx^=Q⊤b\mathbf{R}\hat{\mathbf{x}}=\mathbf{Q}^\top\mathbf{b}, easy to solve from the bottom up and as well behaved as A\mathbf{A} itself. This is the orthonormal shortcut from above, used as an algorithm.
Go deeper What lstsq's four return values look like

For the three points (0,1)(0,1), (1,3)(1,3), (2,2)(2,2), np.linalg.lstsq(A, y, rcond=None) returns the solution (1.5,0.5)(1.5,0.5), the residuals [1.5] (the sum of squares), the rank 22, and two singular values. For the “same xx” points it returns the solution (0.4,0.8)(0.4,0.8), an empty residuals array, and rank 11. For two points with different xx (a square 2×22\times2 system) the residuals array is also empty, even though the fit is exact. If you need the residual sum in those cases, compute np.sum((b - A @ x) ** 2).

Try it in NumPy

The first cell does the projection calculations of this lesson: onto the line y=xy=x, onto the plane of the widget, and with orthonormal columns.

Try it: projections

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

What to expect: p=(2,2)\mathbf{p}=(2,2), e=(1,−1)\mathbf{e}=(1,-1), a⋅e=0\mathbf{a}\cdot\mathbf{e}=0 and P\mathbf{P} with every entry 0.50.5, with both tests True. For the plane, x^=(1,2)\hat{\mathbf{x}}=(1,2), p=(1,2,3)\mathbf{p}=(1,2,3), e=(−1,−1,1)\mathbf{e}=(-1,-1,1), A⊤e=(0,0)\mathbf{A}^\top\mathbf{e}=(0,0), 3P3\mathbf{P} equal to the integer matrix above, True, and trace 22. (The + 0.0 turns a possible −0-0 into 00.) For the orthonormal columns the coordinates are (5,1)(5,1), p≈(2.3333,2.6667,3.6667)\mathbf{p}\approx(2.3333,2.6667,3.6667) and ∥e∥=1\lVert\mathbf{e}\rVert=1. Notice that cell 1 used np.linalg.solve(A.T @ A, A.T @ b) and not the inverse, as P1.B9 advised, and the explicit inverse only to print P\mathbf{P}.

The second cell fits the line to four points with lstsq, then shows the dependent-columns case and the squaring of the condition number.

Try it: least squares with lstsq

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

What to expect: the fit of the four points is c=1.3c=1.3, m=0.8m=0.8, the residuals array holds one number, 1.81.8, the rank is 22, and the residual vector is (−0.3,0.9,−0.9,0.3)(-0.3,0.9,-0.9,0.3) with both checks equal to 00. For the dependent columns the solution is (0.4,0.8)(0.4,0.8), the residuals array is empty ([]) and the rank is 11; the two other weight pairs give the same predictions (2,2,2)(2,2,2), so they fit exactly as well. The last block prints about 9.0×1059.0\times10^{5}, 8.0×10118.0\times10^{11} and 8.0×10118.0\times10^{11} (when this lesson was checked; the digits of condition numbers can differ a little between machines), and the intercept of −798.7-798.7 for the shifted data, since the line 1.3+0.8x1.3+0.8x moved 10001000 units to the right has intercept 1.3−8001.3-800. (Shifting the xx values so that they are centred near 00 is a standard way to make the columns less parallel.)

Where you will meet it next

  • Linear regression (P3.A2). Fit y^=Xw\hat{\mathbf{y}}=\mathbf{X}\mathbf{w} by least squares: the normal equations X⊤Xw=X⊤y\mathbf{X}^\top\mathbf{X}\mathbf{w}=\mathbf{X}^\top\mathbf{y}, the prediction as a projection of y\mathbf{y}, and the fact that the error is perpendicular to every feature. The loss, MSE and the choice between this formula and gradient descent (P1.F3) are in that lesson.
  • PCA (P3.C3). Project each (centred) example onto the span of a few orthonormal directions: with the directions as the columns of Q\mathbf{Q}, the coordinates are Q⊤x\mathbf{Q}^\top\mathbf{x} and the projected point is QQ⊤x\mathbf{Q}\mathbf{Q}^\top\mathbf{x}. PCA chooses the directions to leave the least squared error.
  • Ridge regression (P3.A6). Makes the least-squares answer unique and stable when the columns are copies or near-copies.
  • Eigenvalues (P1.B12) and SVD (P1.B13). A projection has eigenvalues 11 and 00. The SVD gives the pseudo-inverse, the general least-squares solver that lstsq is built on.
  • Orthogonalisation. Gram–Schmidt and QR build the orthonormal columns that make projections cheap and stable.

Common mistakes

Check your understanding

  1. What is the projection of b=(2,4)\mathbf{b}=(2,4) onto the line through a=(1,3)\mathbf{a}=(1,3)?

  2. P\mathbf{P} is the 3×33\times3 matrix that projects onto a plane through the origin. Which statement is true?

  3. A\mathbf{A} is m×nm\times n with independent columns, and x^\hat{\mathbf{x}} is the least-squares solution of Ax=b\mathbf{A}\mathbf{x}=\mathbf{b}. Which equation does x^\hat{\mathbf{x}} satisfy?

  4. A line y=c+mxy=c+mx with an intercept is fitted to data by least squares. Why do the residuals add up to 00?

  5. The columns of A\mathbf{A} are dependent, and you call np.linalg.lstsq(A, b, rcond=None). What does the first returned value, the solution, contain?

  6. Q\mathbf{Q} is a tall m×nm\times n matrix (m>nm>n) with orthonormal columns. What is the coefficient vector x^\hat{\mathbf{x}} of the projection of b\mathbf{b} onto the span of the columns?

Exercises

  1. Project b=(2,1)\mathbf{b}=(2,1) onto the line through a=(1,2)\mathbf{a}=(1,2). Find p\mathbf{p}, e\mathbf{e} and check a⋅e=0\mathbf{a}\cdot\mathbf{e}=0.
    Show answera⊤b=4\mathbf{a}^\top\mathbf{b}=4 and a⊤a=5\mathbf{a}^\top\mathbf{a}=5, so x^=0.8\hat{x}=0.8 and p=(0.8,1.6)\mathbf{p}=(0.8,1.6). The error is e=(1.2,−0.6)\mathbf{e}=(1.2,-0.6) and a⋅e=1.2−1.2=0\mathbf{a}\cdot\mathbf{e}=1.2-1.2=0. Pythagoras: ∥p∥2=0.64+2.56=3.2\lVert\mathbf{p}\rVert^2=0.64+2.56=3.2 and ∥e∥2=1.44+0.36=1.8\lVert\mathbf{e}\rVert^2=1.44+0.36=1.8, and 3.2+1.8=5=∥b∥23.2+1.8=5=\lVert\mathbf{b}\rVert^2.
  2. Write the projection matrix P\mathbf{P} for a=(1,2)\mathbf{a}=(1,2). Check that P2=P\mathbf{P}^2=\mathbf{P}, find its trace, and say what I−P\mathbf{I}-\mathbf{P} projects onto.
    Show answerP=15(1224)\mathbf{P}=\tfrac15\begin{pmatrix}1&2\\2&4\end{pmatrix}. P2=125(5101020)=P\mathbf{P}^2=\tfrac1{25}\begin{pmatrix}5&10\\10&20\end{pmatrix}=\mathbf{P}. The trace is 15+45=1\tfrac15+\tfrac45=1, the rank. I−P\mathbf{I}-\mathbf{P} projects onto the perpendicular line, the line through (2,−1)(2,-1).
  3. Project b=(1,2,3)\mathbf{b}=(1,2,3) onto the line through (1,1,1)(1,1,1).
    Show answera⊤b=6\mathbf{a}^\top\mathbf{b}=6, a⊤a=3\mathbf{a}^\top\mathbf{a}=3, so p=2 (1,1,1)=(2,2,2)\mathbf{p}=2\,(1,1,1)=(2,2,2) and e=(−1,0,1)\mathbf{e}=(-1,0,1), with a⋅e=0\mathbf{a}\cdot\mathbf{e}=0. The projection onto the all-ones direction replaces every entry by the average of the entries.
  4. The vector b=(1,3,2)\mathbf{b}=(1,3,2) is projected onto the span of the single column (1,1,1)(1,1,1) (fitting a constant to three data values). What is the best constant and the sum of squared residuals?
    Show answerThe best constant is the average 22, with residuals (−1,1,0)(-1,1,0) and a sum of squares of 22. (Fitting a line instead lowers it to 1.51.5, as in the lesson: a bigger set of columns can only do as well or better.)
  5. Fit y=c+mxy=c+mx to (0,1)(0,1), (1,3)(1,3), (2,2)(2,2), (3,4)(3,4) with the normal equations.
    Show answerA⊤A=(46614)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}4&6\\6&14\end{pmatrix} and A⊤y=(1019)\mathbf{A}^\top\mathbf{y}=\begin{pmatrix}10\\19\end{pmatrix}. The determinant is 56−36=2056-36=20, so c=(14⋅10−6⋅19)/20=1.3c=(14\cdot10-6\cdot19)/20=1.3 and m=(4⋅19−6⋅10)/20=0.8m=(4\cdot19-6\cdot10)/20=0.8. The residuals are (−0.3,0.9,−0.9,0.3)(-0.3,0.9,-0.9,0.3), which add up to 00, and the sum of squares is 1.81.8.
  6. For the points (0,1),(1,3),(2,2)(0,1),(1,3),(2,2), fit the model y=mxy=mx with no intercept. What are the residuals, and why do they not add up to 00?
    Show answerm^=x⊤y/x⊤x=7/5=1.4\hat{m}=\mathbf{x}^\top\mathbf{y}/\mathbf{x}^\top\mathbf{x}=7/5=1.4 and the residuals are (1,1.6,−0.8)(1,1.6,-0.8), which add up to 1.81.8. The residual vector is perpendicular to the one column x=(0,1,2)\mathbf{x}=(0,1,2) (0+1.6−1.6=00+1.6-1.6=0) but not to the all-ones vector, which is not a column of this A\mathbf{A}.
  7. Is b=(2,3,5)\mathbf{b}=(2,3,5) in the plane spanned by (1,0,1)(1,0,1) and (0,1,1)(0,1,1)? If yes, give x^\hat{\mathbf{x}}, p\mathbf{p} and e\mathbf{e}.
    Show answerYes, b=2(1,0,1)+3(0,1,1)\mathbf{b}=2(1,0,1)+3(0,1,1), since 2+3=52+3=5. Then x^=(2,3)\hat{\mathbf{x}}=(2,3), p=b\mathbf{p}=\mathbf{b}, e=0\mathbf{e}=\mathbf{0}, and the least-squares problem has an exact solution. Test: the plane is z=x+yz=x+y.
  8. With the orthonormal columns q1=13(1,2,2)\mathbf{q}_1=\tfrac13(1,2,2) and q2=13(2,−2,1)\mathbf{q}_2=\tfrac13(2,-2,1), project b=(3,0,0)\mathbf{b}=(3,0,0).
    Show answerThe coefficients are q1⋅b=1\mathbf{q}_1\cdot\mathbf{b}=1 and q2⋅b=2\mathbf{q}_2\cdot\mathbf{b}=2, so p=q1+2q2=13(5,−2,4)\mathbf{p}=\mathbf{q}_1+2\mathbf{q}_2=\tfrac13(5,-2,4) and e=b−p=13(4,2,−4)\mathbf{e}=\mathbf{b}-\mathbf{p}=\tfrac13(4,2,-4), of length 36/3=2\sqrt{36}/3=2. Check: 9=(1+4)+49=(1+4)+4. (The projection is the first column of TT⊤\mathbf{T}\mathbf{T}^\top times 33.)
  9. Which of these is a projection matrix onto a line: (a) (1000)\begin{pmatrix}1&0\\0&0\end{pmatrix}, (b) (1111)\begin{pmatrix}1&1\\1&1\end{pmatrix}, (c) (0.60.80.8−0.6)\begin{pmatrix}0.6&0.8\\0.8&-0.6\end{pmatrix}?
    Show answerTest P2=P\mathbf{P}^2=\mathbf{P} and P⊤=P\mathbf{P}^\top=\mathbf{P}. (a) is symmetric and P2=P\mathbf{P}^2=\mathbf{P}: it projects onto the xx-axis (it is aa⊤/a⊤a\mathbf{a}\mathbf{a}^\top/\mathbf{a}^\top\mathbf{a} for a=(1,0)\mathbf{a}=(1,0)). (b) is symmetric but P2=2P\mathbf{P}^2=2\mathbf{P}: it is twice the projection onto y=xy=x, so it is not a projection. (c) is symmetric but P2=I\mathbf{P}^2=\mathbf{I}: it is a reflection (P1.B10), which keeps lengths, while a projection loses the perpendicular part.
  10. A tall matrix A\mathbf{A} has independent columns. Why is A(A⊤A)−1A⊤\mathbf{A}(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top not equal to I\mathbf{I}?
    Show answerA\mathbf{A} has no inverse, so (A⊤A)−1(\mathbf{A}^\top\mathbf{A})^{-1} cannot be split into A−1(A⊤)−1\mathbf{A}^{-1}(\mathbf{A}^\top)^{-1}. The product is a projection onto the column space, which has rank nn (the number of columns) smaller than mm; the identity has rank mm. Its trace is nn, not mm.
  11. Compute ∥e∥\lVert\mathbf{e}\rVert for the projection of b=(0,1,4)\mathbf{b}=(0,1,4) on the plane of the lesson, and then the length of the vector from b\mathbf{b} to q=(1,0,1)\mathbf{q}=(1,0,1), which is in the plane. Which is shorter?
    Show answer∥e∥=∥(−1,−1,1)∥=3≈1.732\lVert\mathbf{e}\rVert=\lVert(-1,-1,1)\rVert=\sqrt3\approx1.732. And b−q=(−1,1,3)\mathbf{b}-\mathbf{q}=(-1,1,3) has length 11≈3.317\sqrt{11}\approx3.317. The first is shorter, and the difference follows Pythagoras: 11=3+∥p−q∥211=3+\lVert\mathbf{p}-\mathbf{q}\rVert^2 with p−q=(0,2,2)\mathbf{p}-\mathbf{q}=(0,2,2), ∥⋅∥2=8\lVert\cdot\rVert^2=8.
  12. The matrix A\mathbf{A} has columns (1,2,3)(1,2,3) and (2,4,6)(2,4,6). Say what is special, what its normal equations look like for y=(1,2,4)\mathbf{y}=(1,2,4), and what lstsq would return.
    Show answerThe second column is twice the first, so the columns are dependent and A⊤A=(14282856)\mathbf{A}^\top\mathbf{A}=\begin{pmatrix}14&28\\28&56\end{pmatrix} is singular (determinant 00). The two normal equations are the same equation up to a factor of 22: 14w1+28w2=1714w_1+28w_2=17. Every pair with w1+2w2=17/14w_1+2w_2=17/14 gives the same projection 1714(1,2,3)\tfrac{17}{14}(1,2,3). lstsq returns the pair of smallest length, (17/70, 17/35)≈(0.243,0.486)(17/70,\,17/35)\approx(0.243,0.486), and an empty residuals array with rank 11.
  13. Code: fit y=c+mxy=c+mx to the points (0,1),(1,3),(2,2),(3,4)(0,1),(1,3),(2,2),(3,4) with np.linalg.lstsq, then check that A.T @ e is 00 (up to rounding) and that moving the second point to (1,5)(1,5) keeps both ∑iei\sum_ie_i and ∑ixiei\sum_ix_ie_i at 00.
    Show answerw, res, rank, sv = np.linalg.lstsq(A, y, rcond=None) with A = np.column_stack([np.ones(4), x]) gives (1.3,0.8)(1.3,0.8) and e = y - A @ w. Both entries of A.T @ e are about 10−1610^{-16} or less (use np.allclose(A.T @ e, 0)). Changing y2y_2 to 55 gives a new line, and the new A.T @ e is 00 again, because the normal equations hold for any data.

Key takeaways

  • The projection of b\mathbf{b} onto the line through a\mathbf{a} is p=a⊤ba⊤aa\mathbf{p}=\frac{\mathbf{a}^\top\mathbf{b}}{\mathbf{a}^\top\mathbf{a}}\mathbf{a}. The error e=b−p\mathbf{e}=\mathbf{b}-\mathbf{p} is perpendicular to a\mathbf{a}, and that single condition a⊤(b−p)=0\mathbf{a}^\top(\mathbf{b}-\mathbf{p})=0 is how the formula is found.
  • A projection matrix P=aa⊤a⊤a\mathbf{P}=\frac{\mathbf{a}\mathbf{a}^\top}{\mathbf{a}^\top\mathbf{a}} has rank 1 and satisfies P2=P\mathbf{P}^2=\mathbf{P} (projecting twice changes nothing) and P⊤=P\mathbf{P}^\top=\mathbf{P}. The matrix I−P\mathbf{I}-\mathbf{P} projects onto the perpendicular direction, and it gives the error. A projection onto a line or plane (anything smaller than the whole space) cannot be undone.
  • To project onto the span of the columns of A\mathbf{A}, ask for an error perpendicular to every column: A⊤(b−Ax^)=0\mathbf{A}^\top(\mathbf{b}-\mathbf{A}\hat{\mathbf{x}})=\mathbf{0}. This gives the normal equations A⊤Ax^=A⊤b\mathbf{A}^\top\mathbf{A}\hat{\mathbf{x}}=\mathbf{A}^\top\mathbf{b}, and for independent columns x^=(A⊤A)−1A⊤b\hat{\mathbf{x}}=(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{b} and P=A(A⊤A)−1A⊤\mathbf{P}=\mathbf{A}(\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top. With orthonormal columns Q\mathbf{Q} it collapses to x^=Q⊤b\hat{\mathbf{x}}=\mathbf{Q}^\top\mathbf{b} and P=QQ⊤\mathbf{P}=\mathbf{Q}\mathbf{Q}^\top.
  • A tall system Ax=b\mathbf{A}\mathbf{x}=\mathbf{b} usually has no solution, so least squares minimises ∥Ax−b∥2\lVert\mathbf{A}\mathbf{x}-\mathbf{b}\rVert^2. The minimiser is the projection, because ∥b−Ax∥2=∥e∥2+∥p−Ax∥2\lVert\mathbf{b}-\mathbf{A}\mathbf{x}\rVert^2=\lVert\mathbf{e}\rVert^2+\lVert\mathbf{p}-\mathbf{A}\mathbf{x}\rVert^2 by Pythagoras.
  • Fitting a line y=c+mxy=c+mx uses the columns 1\mathbf{1} and x\mathbf{x}. In the scatter plot the residuals are vertical gaps. In data space (one axis per data point) the residual vector is perpendicular to the columns, so with an intercept column the residuals add up to 00.
  • If the columns are dependent, A⊤A\mathbf{A}^\top\mathbf{A} is singular: the projection p\mathbf{p} is still unique but there are infinitely many x^\hat{\mathbf{x}}. In code, call np.linalg.lstsq (or use QR) instead of forming A⊤A\mathbf{A}^\top\mathbf{A}, whose condition number is the square of that of A\mathbf{A}.
Found a mistake? Tell us