SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.5.1 Span, independence and basisConcept · 35 min
      2. 3.5.2 Orthogonal projectionConcept · 35 min
      3. 3.5.3 Least squaresConcept · 30 min
      4. 3.5.4 Practice set: spaces and projectionsPractice set · 25 min
      5. 3.5.5 Case study: the minimum-variance hedge as a projectionCase study · 35 min

Level 3, Linear algebra 3.5 Spaces and projections Lesson 2 of 5

ConceptLesson 3.5.2 in Level 3, Linear algebra. About 35 minutes.

Orthogonal projection

The shadow as a vector

Lesson 3.1.2 shone a light straight down onto the line through u\mathbf{u}. The shadow of v\mathbf{v} had signed length u⋅v∥u∥\frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert}. The projection of v\mathbf{v} onto the line is that shadow as a vector: its signed length times the unit vector u∥u∥\frac{\mathbf{u}}{\lVert \mathbf{u} \rVert}.

That divides by ∥u∥\lVert \mathbf{u} \rVert twice, which is dividing by ∥u∥2=u⋅u\lVert \mathbf{u} \rVert^2 = \mathbf{u} \cdot \mathbf{u}. The formula needs no square roots.

Rule: Projection onto a line

p=u⋅vu⋅u u\mathbf{p} = \frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{u} \cdot \mathbf{u}}\,\mathbf{u}

p\mathbf{p} is a multiple of u\mathbf{u}, so it lies on the line through u\mathbf{u}. The number in front is the coefficient of the projection. It needs u≠0\mathbf{u} \neq \mathbf{0}: the zero vector gives no line.

For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2): u⋅v=5\mathbf{u} \cdot \mathbf{v} = 5 and u⋅u=10\mathbf{u} \cdot \mathbf{u} = 10, so p=0.5 u=(1.5,0.5)\mathbf{p} = 0.5\,\mathbf{u} = (1.5, 0.5). Its length is 2.5≈1.5811\sqrt{2.5} \approx 1.5811, the shadow length of lesson 3.1.2.

The cases follow from the sign and size of the coefficient:

  • Behind the origin. (−2,1)(-2, 1) has u⋅v=−5\mathbf{u} \cdot \mathbf{v} = -5, so p=−0.5 u=(−1.5,−0.5)\mathbf{p} = -0.5\,\mathbf{u} = (-1.5, -0.5): the coefficient is negative.
  • Already on the line. (6,2)=2u(6, 2) = 2\mathbf{u} projects onto itself.
  • Orthogonal to the line. (−1,3)(-1, 3) has u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0, so p=0\mathbf{p} = \mathbf{0}.
  • Only the line matters. With 2u2\mathbf{u} in place of u\mathbf{u} the coefficient halves to 1040=0.25\frac{10}{40} = 0.25, and 0.25×2u0.25 \times 2\mathbf{u} is the same p\mathbf{p}. So is −0.5×(−u)-0.5 \times (-\mathbf{u}).

The residual is orthogonal

The residual is what is left of v\mathbf{v} after the projection: r=v−p\mathbf{r} = \mathbf{v} - \mathbf{p}. Here r=(1,2)−(1.5,0.5)=(−0.5,1.5)\mathbf{r} = (1, 2) - (1.5, 0.5) = (-0.5, 1.5), and u⋅r=−1.5+1.5=0\mathbf{u} \cdot \mathbf{r} = -1.5 + 1.5 = 0. The residual is orthogonal to the line (lesson 3.1.2).

That is how the coefficient is chosen. Ask for the multiple c uc\,\mathbf{u} that leaves a residual orthogonal to u\mathbf{u}:

u⋅(v−c u)=0u⋅v−c (u⋅u)=0c=u⋅vu⋅u\begin{gathered} \mathbf{u} \cdot (\mathbf{v} - c\,\mathbf{u}) = 0 \\[4pt] \mathbf{u} \cdot \mathbf{v} - c\,(\mathbf{u} \cdot \mathbf{u}) = 0 \\[4pt] c = \frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{u} \cdot \mathbf{u}} \end{gathered}

So v=p+r\mathbf{v} = \mathbf{p} + \mathbf{r} splits v\mathbf{v} into a part along u\mathbf{u} and a part at right angles to it. The two parts are the legs of a right-angled triangle with hypotenuse v\mathbf{v}, so Pythagoras gives ∥v∥2=∥p∥2+∥r∥2\lVert \mathbf{v} \rVert^2 = \lVert \mathbf{p} \rVert^2 + \lVert \mathbf{r} \rVert^2: here 5=2.5+2.55 = 2.5 + 2.5.

An example in space. Project v=(3,3,0)\mathbf{v} = (3, 3, 0) onto the line through u=(1,2,2)\mathbf{u} = (1, 2, 2). The dot product is 9 and u⋅u=9\mathbf{u} \cdot \mathbf{u} = 9, so p=u=(1,2,2)\mathbf{p} = \mathbf{u} = (1, 2, 2) and r=(2,1,−2)\mathbf{r} = (2, 1, -2). Check: u⋅r=2+2−4=0\mathbf{u} \cdot \mathbf{r} = 2 + 2 - 4 = 0, and 18=9+918 = 9 + 9.

The nearest point on the line

Every point of the line is t ut\,\mathbf{u} for some number tt. Which one is closest to v=(1,2)\mathbf{v} = (1, 2)? Here are five:

tt00.250.50.751
point t ut\,\mathbf{u}(0,0)(0, 0)(0.75,0.25)(0.75, 0.25)(1.5,0.5)(1.5, 0.5)(2.25,0.75)(2.25, 0.75)(3,1)(3, 1)
distance to v\mathbf{v}2.23611.76781.58111.76782.2361

The smallest is at t=0.5t = 0.5, the projection. Algebra shows why: the squared distance is ∥v−t u∥2=5−10t+10t2\lVert \mathbf{v} - t\,\mathbf{u} \rVert^2 = 5 - 10t + 10t^2, a parabola. Its lowest point is at t=102×10=0.5t = \frac{10}{2 \times 10} = 0.5 (lesson 1.2.2).

The projection p\mathbf{p} is the point of the line nearest v\mathbf{v}. The residual r\mathbf{r} meets the line at a right angle; any other point of the line is further from v\mathbf{v}.

The right angle is the reason in every case. For any other point q\mathbf{q} on the line, v−q=r+(p−q)\mathbf{v} - \mathbf{q} = \mathbf{r} + (\mathbf{p} - \mathbf{q}), and those two parts are orthogonal. So ∥v−q∥2=∥r∥2+∥p−q∥2\lVert \mathbf{v} - \mathbf{q} \rVert^2 = \lVert \mathbf{r} \rVert^2 + \lVert \mathbf{p} - \mathbf{q} \rVert^2, which is never less than ∥r∥2\lVert \mathbf{r} \rVert^2.

Rule: The nearest point

p is the point of the linenearest vdistance=∥v−p∥\begin{gathered} \mathbf{p} \text{ is the point of the line} \\ \text{nearest } \mathbf{v} \\[4pt] \text{distance} = \lVert \mathbf{v} - \mathbf{p} \rVert \end{gathered}

The distance from a point to a line is the length of the residual, measured at right angles to the line.

The explorer starts on this example, with the projection as the dark arrow. Drag v\mathbf{v} onto the line and the projection becomes v\mathbf{v}; drag it to a right angle with u\mathbf{u} and the projection shrinks to 0\mathbf{0}. Then stretch or reverse u\mathbf{u}: the projection does not move.

Explore

Drag the tips of u and v. Watch the sign of u · v as the angle passes 90°. The dark arrow is the projection of v on u: the shadow v casts on the line through u.

−5−555uv
u
(3, 1), length 3.1623
v
(1, 2), length 2.2361
u · v
3 × 1 + 1 × 2 = 5
Angle
cos θ = 0.7071, so θ = 45°
Projection of v on u
(1.5, 0.5), signed length u · v / |u| = 1.5811
So
The dot product is positive, so the angle is less than 90°.

Try it

proj(u, v) is the rule. The first lines print p\mathbf{p}, r\mathbf{r}, u⋅r\mathbf{u} \cdot \mathbf{r} and the three squared lengths.

The loop is the table of distances. The last line repeats the example in space.

⌘/Ctrl + Enter runs the cell

Projection onto a plane

A plane through the origin is the span of two independent vectors (lesson 3.5.1). Put them in the columns of a matrix AA. Every point of the plane is AxA\mathbf{x} for some x\mathbf{x}: the plane is the column space of AA.

To project b\mathbf{b} onto the plane, look for p=Ax^\mathbf{p} = A\hat{\mathbf{x}} whose residual b−p\mathbf{b} - \mathbf{p} is orthogonal to the whole plane. It is enough to be orthogonal to both columns, since every vector of the plane is a combination of them. (x^\hat{\mathbf{x}} is read "x hat".)

The two conditions a1⋅(b−Ax^)=0\mathbf{a}_1 \cdot (\mathbf{b} - A\hat{\mathbf{x}}) = 0 and a2⋅(b−Ax^)=0\mathbf{a}_2 \cdot (\mathbf{b} - A\hat{\mathbf{x}}) = 0 are the two rows of one matrix equation. The rows of A⊤A^\top are the columns of AA (lesson 3.2.3), so

A⊤(b−Ax^)=0A⊤A x^=A⊤b\begin{gathered} A^\top(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0} \\[4pt] A^\top A\,\hat{\mathbf{x}} = A^\top\mathbf{b} \end{gathered}

A⊤AA^\top A is square, and it has an inverse when the columns of AA are independent. Lesson 3.5.3 meets the case where they are not.

Rule: Projection onto a column space

x^=(A⊤A)−1A⊤bp=Ax^A⊤(b−p)=0\begin{gathered} \hat{\mathbf{x}} = (A^\top A)^{-1}A^\top\mathbf{b} \\[4pt] \mathbf{p} = A\hat{\mathbf{x}} \\[4pt] A^\top(\mathbf{b} - \mathbf{p}) = \mathbf{0} \end{gathered}

AA has independent columns. p\mathbf{p} is the point of the column space nearest b\mathbf{b}, and the residual is orthogonal to every column. With one column u\mathbf{u}, this is the rule for a line: A⊤A=u⋅uA^\top A = \mathbf{u} \cdot \mathbf{u} and A⊤b=u⋅bA^\top\mathbf{b} = \mathbf{u} \cdot \mathbf{b}.

A worked example

Take lesson 3.5.1's plane, spanned by a1=(1,0,1)\mathbf{a}_1 = (1, 0, 1) and a2=(0,1,1)\mathbf{a}_2 = (0, 1, 1), and the point b=(0,1,4)\mathbf{b} = (0, 1, 4). It is off the plane: its third component is not the sum of the first two.

A=(100111)A⊤A=(2112),A⊤b=(45)\begin{gathered} A = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix} \\[6pt] A^\top A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}, \quad A^\top\mathbf{b} = \begin{pmatrix} 4 \\ 5 \end{pmatrix} \end{gathered}

A⊤AA^\top A is the symmetric matrix SS of lesson 3.2.3, as every A⊤AA^\top A is symmetric. Solving 2x1+x2=42x_1 + x_2 = 4 and x1+2x2=5x_1 + 2x_2 = 5 gives x^=(1,2)\hat{\mathbf{x}} = (1, 2). So p=a1+2a2=(1,2,3)\mathbf{p} = \mathbf{a}_1 + 2\mathbf{a}_2 = (1, 2, 3), a point lesson 3.5.1 found in the plane.

The residual is r=(0,1,4)−(1,2,3)=(−1,−1,1)\mathbf{r} = (0, 1, 4) - (1, 2, 3) = (-1, -1, 1). Check: a1⋅r=−1+0+1=0\mathbf{a}_1 \cdot \mathbf{r} = -1 + 0 + 1 = 0 and a2⋅r=0−1+1=0\mathbf{a}_2 \cdot \mathbf{r} = 0 - 1 + 1 = 0. The distance from b\mathbf{b} to the plane is ∥r∥=3≈1.7321\lVert \mathbf{r} \rVert = \sqrt{3} \approx 1.7321, and Pythagoras holds again: 17=14+317 = 14 + 3.

The projection of b\mathbf{b} onto a plane. The residual r\mathbf{r} is orthogonal to both columns, so it meets the plane at a right angle, and p\mathbf{p} is the nearest point.

Other points of the plane are further away. A(1.5,2)=(1.5,2,3.5)A(1.5, 2) = (1.5, 2, 3.5) is 1.8708 from b\mathbf{b}, and A(1,1)=(1,1,2)A(1, 1) = (1, 1, 2) is 2.2361 away.

In R3\mathbb{R}^3, a plane through the origin leaves only one direction at right angles to it, here that of (1,1,−1)(1, 1, -1). So every residual for this plane is a multiple of (1,1,−1)(1, 1, -1). A point already in the plane, such as (2,3,5)(2, 3, 5), is its own projection, with residual 0\mathbf{0}.

Try it

The first lines build A⊤AA^\top A and A⊤bA^\top\mathbf{b}, solve for x^\hat{\mathbf{x}}, and print p\mathbf{p}, r\mathbf{r}, A⊤rA^\top\mathbf{r} and the distance.

The loop measures other points AxA\mathbf{x} of the plane. The last lines project two more points: one off the plane, whose residual is again along (1,1,−1)(1, 1, -1), and one in it.

⌘/Ctrl + Enter runs the cell

The projection matrix

The formula p=A(A⊤A)−1A⊤b\mathbf{p} = A(A^\top A)^{-1}A^\top\mathbf{b} multiplies b\mathbf{b} by one matrix, the same for every b\mathbf{b}. It is the projection matrix PP onto the column space.

  • Onto the line through u\mathbf{u}: P=uu⊤u⊤uP = \frac{\mathbf{u}\mathbf{u}^\top}{\mathbf{u}^\top\mathbf{u}}, a column times a row (lesson 3.2.3). For u=(3,1)\mathbf{u} = (3, 1), P=110(9331)P = \frac{1}{10}\begin{pmatrix} 9 & 3 \\ 3 & 1 \end{pmatrix}, and Pv=(1.5,0.5)P\mathbf{v} = (1.5, 0.5).
  • Onto an axis: for u=e1\mathbf{u} = \mathbf{e}_1, P=(1000)P = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}, the projection of lesson 3.2.2.
  • Onto the plane above: P=13(2−11−121112)P = \frac{1}{3}\begin{pmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \\ 1 & 1 & 2 \end{pmatrix}, and Pb=(1,2,3)P\mathbf{b} = (1, 2, 3).

Projecting twice changes nothing: p\mathbf{p} is already in the plane, so its projection is itself. In symbols, P2=PP^2 = P. Every projection matrix is also symmetric, and I−PI - P gives the residual, (I−P)b=r(I - P)\mathbf{b} = \mathbf{r}.

Rule: Projection matrix

P=A(A⊤A)−1A⊤P2=P,P⊤=P\begin{gathered} P = A(A^\top A)^{-1}A^\top \\[4pt] P^2 = P, \quad P^\top = P \end{gathered}

PbP\mathbf{b} is the projection of b\mathbf{b} and (I−P)b(I - P)\mathbf{b} its residual. A projection flattens space onto the column space, so unless that is all of Rn\mathbb{R}^n, PP has no inverse (lesson 3.2.3).

Try it

The first block builds PP for the line through (3,1)(3, 1), printed as 10P10P to show whole numbers. The second builds it for the plane, printed as 3P3P.

np.allclose checks P2=PP^2 = P and P⊤=PP^\top = P. The rank of the plane's PP is 2: it flattens space onto a plane.

⌘/Ctrl + Enter runs the cell

Orthonormal columns

Vectors are orthonormal when each has length 1 and every pair is orthogonal. Write QQ for a matrix whose columns q1,…,qk\mathbf{q}_1, \ldots, \mathbf{q}_k are orthonormal. Entry (i,j)(i, j) of Q⊤QQ^\top Q is qi⋅qj\mathbf{q}_i \cdot \mathbf{q}_j, which is 1 when i=ji = j and 0 otherwise, so Q⊤Q=IQ^\top Q = I.

Then (A⊤A)−1(A^\top A)^{-1} in the projection formula is I−1=II^{-1} = I, and nothing needs solving.

Rule: Projection with orthonormal columns

Q⊤Q=Ip=QQ⊤b=(q1⋅b) q1+⋯+(qk⋅b) qk\begin{gathered} Q^\top Q = I \\[4pt] \mathbf{p} = QQ^\top\mathbf{b} \\[4pt] = (\mathbf{q}_1 \cdot \mathbf{b})\,\mathbf{q}_1 + \cdots + (\mathbf{q}_k \cdot \mathbf{b})\,\mathbf{q}_k \end{gathered}

With orthonormal columns, the projection onto the plane is the sum of the projections onto each line qi\mathbf{q}_i. That is lesson 3.5.1's shortcut for coordinates in an orthogonal basis: each coordinate is a dot product.

Making the columns orthonormal. The plane above has columns that are not orthogonal: a1⋅a2=1\mathbf{a}_1 \cdot \mathbf{a}_2 = 1. Keep the direction of a1\mathbf{a}_1 and normalise it: q1=12(1,0,1)\mathbf{q}_1 = \frac{1}{\sqrt{2}}(1, 0, 1).

Then take from a2\mathbf{a}_2 its projection onto a1\mathbf{a}_1. The residual a2−12a1=(−0.5,1,0.5)\mathbf{a}_2 - \frac{1}{2}\mathbf{a}_1 = (-0.5, 1, 0.5) is orthogonal to a1\mathbf{a}_1 and still in the plane. Normalised, it is q2≈(−0.4082,0.8165,0.4082)\mathbf{q}_2 \approx (-0.4082, 0.8165, 0.4082).

For b=(0,1,4)\mathbf{b} = (0, 1, 4), the two dot products are q1⋅b≈2.8284\mathbf{q}_1 \cdot \mathbf{b} \approx 2.8284 and q2⋅b≈2.4495\mathbf{q}_2 \cdot \mathbf{b} \approx 2.4495. So p=2.8284 q1+2.4495 q2=(1,2,3)\mathbf{p} = 2.8284\,\mathbf{q}_1 + 2.4495\,\mathbf{q}_2 = (1, 2, 3), as before.

QQ here is 3×23 \times 2, so Q⊤Q=IQ^\top Q = I but QQ⊤QQ^\top is not II: it is PP. np.linalg.qr builds orthonormal columns for any independent columns; their signs can differ between computers, but QQ⊤QQ^\top cannot. Lesson 3.6.3 meets square QQ, for which Q⊤Q=QQ⊤=IQ^\top Q = QQ^\top = I.

Try it

The cell makes q1\mathbf{q}_1 and q2\mathbf{q}_2 as above and checks Q⊤Q=IQ^\top Q = I, rounded.

Then Q⊤bQ^\top\mathbf{b} holds the two dot products, and QQ⊤bQQ^\top\mathbf{b} is the projection. The last line compares QQ⊤QQ^\top with PP, for this QQ and for the one from np.linalg.qr.

⌘/Ctrl + Enter runs the cell

Lesson 3.5.3 uses the projection onto a column space to solve systems that have no solution.

Common mistakes

  1. The coefficient divides by u⋅u\mathbf{u} \cdot \mathbf{u}, not by ∥u∥\lVert \mathbf{u} \rVert. Dividing by the length once gives the shadow's signed length of lesson 3.1.2, a number; the projection is a vector.
  2. The order matters. v=(1,2)\mathbf{v} = (1, 2) onto the line through u=(3,1)\mathbf{u} = (3, 1) is (1.5,0.5)(1.5, 0.5), but u\mathbf{u} onto the line through v\mathbf{v} is (1,2)(1, 2).
  3. The residual is measured at right angles, not straight down. The point of the plane straight below (0,1,4)(0, 1, 4) is (0,1,1)(0, 1, 1), at distance 3; the nearest is (1,2,3)(1, 2, 3), at 3\sqrt{3}.
  4. Projecting onto each column and adding works only for orthogonal columns. For a1\mathbf{a}_1 and a2\mathbf{a}_2 above, the separate projections of b\mathbf{b} add up to (2,2.5,4.5)(2, 2.5, 4.5), not (1,2,3)(1, 2, 3).
  5. Q⊤Q=IQ^\top Q = I, but QQ⊤=IQQ^\top = I only for a square QQ. For a tall QQ, QQ⊤QQ^\top is the projection matrix.
  6. Check the right angle with a tolerance. A residual's dot product with a column can compute as 10−1610^{-16} rather than 0 (lesson 3.1.2).

Orthogonal projection

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • The projection of v\mathbf{v} onto the line through u\mathbf{u} is p=u⋅vu⋅uu\mathbf{p} = \frac{\mathbf{u} \cdot \mathbf{v}}{\mathbf{u} \cdot \mathbf{u}}\mathbf{u}. It depends only on the line, not on the length or sign of u\mathbf{u}.
  • The residual r=v−p\mathbf{r} = \mathbf{v} - \mathbf{p} is orthogonal to u\mathbf{u}, so ∥v∥2=∥p∥2+∥r∥2\lVert \mathbf{v} \rVert^2 = \lVert \mathbf{p} \rVert^2 + \lVert \mathbf{r} \rVert^2, and p\mathbf{p} is the nearest point of the line.
  • Onto the column space of AA, with independent columns: A⊤A x^=A⊤bA^\top A\,\hat{\mathbf{x}} = A^\top\mathbf{b}, p=Ax^\mathbf{p} = A\hat{\mathbf{x}}, and the residual is orthogonal to every column, A⊤(b−p)=0A^\top(\mathbf{b} - \mathbf{p}) = \mathbf{0}.
  • The projection matrix P=A(A⊤A)−1A⊤P = A(A^\top A)^{-1}A^\top has P2=PP^2 = P and P⊤=PP^\top = P; (I−P)b(I - P)\mathbf{b} is the residual.
  • Orthonormal columns have Q⊤Q=IQ^\top Q = I, and then p=QQ⊤b\mathbf{p} = QQ^\top\mathbf{b}: one dot product per column.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.