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 1 of 5

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

Span, independence and basis

The span of a set of vectors

A linear combination of vectors v1,…,vk\mathbf{v}_1, \ldots, \mathbf{v}_k is c1v1+⋯+ckvkc_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k, for any numbers c1,…,ckc_1, \ldots, c_k (lesson 3.1.1). The span of the vectors is the set of all their linear combinations. Lesson 3.1.1 asked which points combinations of two vectors can reach: the span is the answer.

Rule: Span

span⁡{v1,…,vk}={c1v1+⋯+ckvk}\begin{gathered} \operatorname{span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} \\[4pt] = \{c_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k\} \end{gathered}

Every combination, with any numbers c1c_1 to ckc_k. Setting every ci=0c_i = 0 gives 0\mathbf{0}, so a span always contains the origin.

What the span looks like depends on how the vectors point:

  • One non-zero vector: its multiples, a line through the origin. The span of (1,2)(1, 2) holds (−1,−2)(-1, -2), (0.5,1)(0.5, 1) and (2,4)(2, 4).
  • Two vectors in the plane pointing different ways: the whole plane, R2\mathbb{R}^2. With u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2), lesson 3.1.1 reached (5,5)=u+2v(5, 5) = \mathbf{u} + 2\mathbf{v} and (1,−3)=u−2v(1, -3) = \mathbf{u} - 2\mathbf{v}.
  • Two vectors along one line, such as (1,2)(1, 2) and (2,4)(2, 4): only that line. Lesson 3.3.2 drew this case: (3,6)(3, 6) is reached and (3,5)(3, 5) is not.
  • The zero vector alone: only the origin.

A plane in space

In R3\mathbb{R}^3, take a1=(1,0,1)\mathbf{a}_1 = (1, 0, 1) and a2=(0,1,1)\mathbf{a}_2 = (0, 1, 1). A combination is

s a1+t a2=(sts+t)s\,\mathbf{a}_1 + t\,\mathbf{a}_2 = \begin{pmatrix} s \\ t \\ s + t \end{pmatrix}

So the span is the set of points whose third component is the sum of the first two: a plane through the origin. (1,2,3)=a1+2a2(1, 2, 3) = \mathbf{a}_1 + 2\mathbf{a}_2 lies in it, and (1,2,0)(1, 2, 0) does not, since 1+2≠01 + 2 \neq 0.

The span of a1\mathbf{a}_1 and a2\mathbf{a}_2 is a plane through the origin. Their sum stays in the plane and adds nothing new. c=(1,1,0)\mathbf{c} = (1, 1, 0) lies 2 below it, so adding c\mathbf{c} reaches all of space.

Lesson 3.3.2 gives a test that needs no picture. A vector b\mathbf{b} is in the span exactly when Ax=bA\mathbf{x} = \mathbf{b} has a solution, where AA has the vectors as its columns. So adding b\mathbf{b} as an extra column must not raise the rank.

Try it

The loop builds three combinations sa1+ta2s\mathbf{a}_1 + t\mathbf{a}_2: each third component is the sum of the first two.

Then the rank test. With (1,2,3)(1, 2, 3) added the rank stays 2, so it is in the span; with (1,2,0)(1, 2, 0) it rises to 3.

⌘/Ctrl + Enter runs the cell

Linear independence

Vectors are linearly independent when none of them is a combination of the others. Otherwise they are dependent: at least one of them adds nothing to the span.

  • (3,1)(3, 1) and (1,2)(1, 2) are independent: neither is a multiple of the other.
  • (1,2)(1, 2) and (2,4)(2, 4) are dependent, since (2,4)=2(1,2)(2, 4) = 2(1, 2). Lesson 3.3.2 met them as the columns of a singular matrix.
  • u\mathbf{u}, v\mathbf{v} and (5,5)(5, 5) are dependent, since (5,5)=u+2v(5, 5) = \mathbf{u} + 2\mathbf{v}. Any three vectors in R2\mathbb{R}^2 are.
  • a1\mathbf{a}_1, a2\mathbf{a}_2 and a1+a2\mathbf{a}_1 + \mathbf{a}_2 are dependent; a1\mathbf{a}_1, a2\mathbf{a}_2 and c=(1,1,0)\mathbf{c} = (1, 1, 0) are independent. The figure shows why.
  • Any set that contains 0\mathbf{0} is dependent: 0=0⋅u\mathbf{0} = 0 \cdot \mathbf{u} is a combination of the others.

Dependence also shows as a combination that gives 0\mathbf{0} without every coefficient being 0: 2(1,2)−(2,4)=02(1, 2) - (2, 4) = \mathbf{0}. For independent vectors, the only combination that gives 0\mathbf{0} is 0v1+⋯+0vk0\mathbf{v}_1 + \cdots + 0\mathbf{v}_k.

Two tests

Put the kk vectors in the columns of a matrix AA. Elimination (lesson 3.3.2) finds a pivot in every column exactly when no column is a combination of the others.

Rule: Testing independence

independent  ⟺  rank⁡A=kfor k=n: det⁡A≠0\begin{gathered} \text{independent} \iff \operatorname{rank} A = k \\[4pt] \text{for } k = n\text{: } \det A \neq 0 \end{gathered}

AA has the kk vectors as columns, each with nn components. The determinant test (lesson 3.4.1) needs a square matrix: nn vectors in Rn\mathbb{R}^n. With more vectors than components, k>nk > n, the rank is at most nn, so they are always dependent.

vectorsrankcountindependent?
(3,1)(3, 1), (1,2)(1, 2)22yes, det⁡=5\det = 5
(1,2)(1, 2), (2,4)(2, 4)12no
(3,1)(3, 1), (1,2)(1, 2), (5,5)(5, 5)23no
a1\mathbf{a}_1, a2\mathbf{a}_2, a1+a2\mathbf{a}_1 + \mathbf{a}_223no, det⁡=0\det = 0
a1\mathbf{a}_1, a2\mathbf{a}_2, (1,1,0)(1, 1, 0)33yes, det⁡=−2\det = -2
(3,1)(3, 1), 0\mathbf{0}12no

Try it

Each set becomes the columns of a matrix. The loop prints its rank beside the number of vectors: independent exactly when they match.

The last loop prints the determinants of the three square cases. Adding 0.0 turns a -0.0 into 0.0, as in lesson 3.4.1.

⌘/Ctrl + Enter runs the cell

Basis and dimension

A basis of a span is a set of independent vectors that spans it. It has no spare vectors: drop one and the span shrinks.

  • e1=(1,0)\mathbf{e}_1 = (1, 0) and e2=(0,1)\mathbf{e}_2 = (0, 1) are the standard basis of R2\mathbb{R}^2; e1,e2,e3\mathbf{e}_1, \mathbf{e}_2, \mathbf{e}_3 are that of R3\mathbb{R}^3.
  • u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2) are another basis of R2\mathbb{R}^2. Any two independent vectors in the plane are.
  • a1\mathbf{a}_1 and a2\mathbf{a}_2 are a basis of the plane in the figure. Adding c\mathbf{c} gives a basis of all of R3\mathbb{R}^3.

From a dependent set, drop each vector that is a combination of the others until the rest are independent. a1\mathbf{a}_1, a2\mathbf{a}_2 and a1+a2\mathbf{a}_1 + \mathbf{a}_2 span the same plane as a1\mathbf{a}_1 and a2\mathbf{a}_2 alone.

Every basis of the same span has the same number of vectors, its dimension. Rn\mathbb{R}^n has dimension nn, a line through the origin dimension 1 and a plane dimension 2.

Rule: Dimension of a span

dim⁡span⁡{v1,…,vk}=rank⁡A\dim \operatorname{span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} = \operatorname{rank} A

AA has the vectors as columns. The rank counts the independent columns, so it counts the vectors a basis needs. It is at most kk, and at most nn.

For example, (1,2,3)(1, 2, 3), (2,4,6)(2, 4, 6) and (0,1,1)(0, 1, 1) have rank 2: the second is twice the first. So they span a plane, with (1,2,3)(1, 2, 3) and (0,1,1)(0, 1, 1) as a basis.

Try it

The first line is the rank of the example: its span has dimension 2.

Dropping a1+a2\mathbf{a}_1 + \mathbf{a}_2 leaves the rank at 2, so the span is still the same plane. Adding c\mathbf{c} instead raises it to 3, the dimension of R3\mathbb{R}^3, as for the standard basis.

⌘/Ctrl + Enter runs the cell

Coordinates in a basis

A basis spans its space and has no spare vectors. So every vector in the space is exactly one combination of the basis vectors. The numbers in that combination are the vector's coordinates in that basis.

The components used so far are coordinates in the standard basis: (4,−2)=4e1−2e2(4, -2) = 4\mathbf{e}_1 - 2\mathbf{e}_2. In the basis u=(3,1)\mathbf{u} = (3, 1), v=(1,2)\mathbf{v} = (1, 2), the same point is 2u−2v2\mathbf{u} - 2\mathbf{v}, so its coordinates are (2,−2)(2, -2).

One point, two grids. In the standard basis x=4e1−2e2\mathbf{x} = 4\mathbf{e}_1 - 2\mathbf{e}_2; in the basis u,v\mathbf{u}, \mathbf{v} it is 2u−2v2\mathbf{u} - 2\mathbf{v}.

To find coordinates c1,c2c_1, c_2, solve c1u+c2v=xc_1\mathbf{u} + c_2\mathbf{v} = \mathbf{x}. That is a linear system Bc=xB\mathbf{c} = \mathbf{x}, where BB has the basis vectors as its columns (lesson 3.3.1):

3c1+c2=4c1+2c2=−2\begin{aligned} 3c_1 + c_2 &= 4 \\ c_1 + 2c_2 &= -2 \end{aligned}

Twice the first minus the second gives 5c1=105c_1 = 10, so c1=2c_1 = 2 and then c2=−2c_2 = -2. Check: 2(3,1)−2(1,2)=(4,−2)2(3, 1) - 2(1, 2) = (4, -2).

x\mathbf{x} in the standard basis(5,5)(5, 5)(7,4)(7, 4)(1,−3)(1, -3)(4,−2)(4, -2)(1,0)(1, 0)
coordinates in u,v\mathbf{u}, \mathbf{v}(1,2)(1, 2)(2,1)(2, 1)(1,−2)(1, -2)(2,−2)(2, -2)(0.4,−0.2)(0.4, -0.2)

Whole-number points can have fractional coordinates: e1=0.4u−0.2v\mathbf{e}_1 = 0.4\mathbf{u} - 0.2\mathbf{v}. The order of the basis matters too: in the basis v,u\mathbf{v}, \mathbf{u}, the point (4,−2)(4, -2) has coordinates (−2,2)(-2, 2).

Rule: Coordinates in a basis

x=c1b1+⋯+cnbnc=B−1x\begin{gathered} \mathbf{x} = c_1\mathbf{b}_1 + \cdots + c_n\mathbf{b}_n \\[4pt] \mathbf{c} = B^{-1}\mathbf{x} \end{gathered}

BB has the basis vectors b1,…,bn\mathbf{b}_1, \ldots, \mathbf{b}_n as columns. Its columns are independent, so it has an inverse (lesson 3.4.1). Compute c\mathbf{c} with np.linalg.solve(B, x), as lesson 3.2.3 advises.

Only a basis gives coordinates that exist and are unique. With dependent vectors a point is reached in many ways, as (3,6)(3, 6) was in lesson 3.3.2. With too few vectors, some points are not reached at all.

An orthogonal basis

When the basis vectors are orthogonal (lesson 3.1.2), no system is needed. Take b1=(1,1)\mathbf{b}_1 = (1, 1) and b2=(−1,1)\mathbf{b}_2 = (-1, 1), whose dot product is 0. Each coordinate of (4,−2)(4, -2) is a dot product divided by a squared length:

c1=(4,−2)⋅(1,1)2=1c2=(4,−2)⋅(−1,1)2=−3\begin{gathered} c_1 = \frac{(4, -2) \cdot (1, 1)}{2} = 1 \\[4pt] c_2 = \frac{(4, -2) \cdot (-1, 1)}{2} = -3 \end{gathered}

Check: 1(1,1)−3(−1,1)=(4,−2)1(1, 1) - 3(-1, 1) = (4, -2). Lesson 3.5.2 shows why this works: each coordinate measures a projection.

Try it

B holds u\mathbf{u} and v\mathbf{v} as columns. The loop solves for the coordinates of each point of the table, and checks the combination with np.allclose.

The last line is the shortcut for the orthogonal basis (1,1)(1, 1), (−1,1)(-1, 1).

⌘/Ctrl + Enter runs the cell

The column space and the null space

Two spans come with every matrix AA. The first is the column space: the span of the columns of AA. A product AxA\mathbf{x} is a combination of the columns (lesson 3.2.1), so the column space holds every AxA\mathbf{x}.

So Ax=bA\mathbf{x} = \mathbf{b} has a solution exactly when b\mathbf{b} is in the column space. That is the picture of lesson 3.3.2, where columns along one line reached only that line. The dimension of the column space is rank⁡A\operatorname{rank} A.

Take the tall system of lesson 3.3.2:

T=(111−112)T = \begin{pmatrix} 1 & 1 \\ 1 & -1 \\ 1 & 2 \end{pmatrix}

Its two columns are independent, so its column space is a plane in R3\mathbb{R}^3. b=(3,1,4)\mathbf{b} = (3, 1, 4) lies in it, since T(2,1)=bT(2, 1) = \mathbf{b}. b=(3,1,5)\mathbf{b} = (3, 1, 5) does not, so that system has no solution.

Elimination with letters says which b\mathbf{b} the plane holds. The steps of lesson 3.3.2 leave b3−1.5b1+0.5b2b_3 - 1.5b_1 + 0.5b_2 in the last row, so b\mathbf{b} is reached exactly when 3b1−b2−2b3=03b_1 - b_2 - 2b_3 = 0.

For (3,1,5)(3, 1, 5) that number is −2-2, not 0. Lesson 3.5.3 finds the point of the plane closest to it.

The null space

The null space of AA is the set of all x\mathbf{x} with Ax=0A\mathbf{x} = \mathbf{0}. It always contains x=0\mathbf{x} = \mathbf{0}, and it is a span too: a point, a line, a plane or more, through the origin.

  • Independent columns: only x=0\mathbf{x} = \mathbf{0}, since no other combination of the columns gives 0\mathbf{0}. The null space of TT is just 0\mathbf{0}.
  • (1224)\begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix}: x1+2x2=0x_1 + 2x_2 = 0, so the null space is the line of multiples of (−2,1)(-2, 1). Lesson 3.2.3's singular Z=(120.51)Z = \begin{pmatrix} 1 & 2 \\ 0.5 & 1 \end{pmatrix} has the same one: it sends (2,−1)(2, -1) to 0\mathbf{0}.
  • (121243364)\begin{pmatrix} 1 & 2 & 1 \\ 2 & 4 & 3 \\ 3 & 6 & 4 \end{pmatrix} from lesson 3.3.2: the line of multiples of (−2,1,0)(-2, 1, 0).

That last direction is familiar. Lesson 3.3.2 found the solutions of this system with b=(4,9,13)\mathbf{b} = (4, 9, 13) as (3,0,1)+t(−2,1,0)(3, 0, 1) + t(-2, 1, 0). Adding any vector of the null space to one solution gives another.

Rule: Column space and null space

column space: every Axdimension rank⁡Anull space: every x with Ax=0dimension n−rank⁡A\begin{gathered} \text{column space: every } A\mathbf{x} \\ \text{dimension } \operatorname{rank} A \\[8pt] \text{null space: every } \mathbf{x} \text{ with } A\mathbf{x} = \mathbf{0} \\ \text{dimension } n - \operatorname{rank} A \end{gathered}

AA has nn columns. Ax=bA\mathbf{x} = \mathbf{b} has a solution exactly when b\mathbf{b} is in the column space. The dimension of the null space is the number of free variables of lesson 3.3.2.

Lesson 3.6.1 finds eigenvectors as the null space of a matrix.

Try it

The first loop tests both right-hand sides for TT by rank, and prints 3b1−b2−2b33b_1 - b_2 - 2b_3.

Then a vector from each of two null spaces, and three solutions (3,0,1)+t(−2,1,0)(3, 0, 1) + t(-2, 1, 0) that all give (4,9,13)(4, 9, 13). SciPy's null_space returns basis vectors of length 1, whose sign can vary, so the cell divides by the second entry.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. A span always passes through the origin. The points (x,3)(x, 3) form a line, but not a span: 0\mathbf{0} is not on it.
  2. Independence belongs to the whole set. (1,0,1)(1, 0, 1), (0,1,1)(0, 1, 1) and (1,1,2)(1, 1, 2) are dependent, though no two of them are multiples of each other.
  3. The dimension is the rank, not the count. Three vectors can span a plane, a line or only the origin.
  4. Too many or too few. More than nn vectors in Rn\mathbb{R}^n are always dependent; fewer than nn never span Rn\mathbb{R}^n.
  5. Coordinates depend on the basis and its order. (4,−2)(4, -2) is (2,−2)(2, -2) in the basis u,v\mathbf{u}, \mathbf{v} and (−2,2)(-2, 2) in v,u\mathbf{v}, \mathbf{u}.
  6. The determinant test needs a square matrix. For kk vectors in Rn\mathbb{R}^n with k≠nk \neq n, compare the rank with kk.
  7. A null space is never empty. It always holds 0\mathbf{0}; "only 0\mathbf{0}" means the columns are independent.

Span, independence and basis

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 span of v1,…,vk\mathbf{v}_1, \ldots, \mathbf{v}_k is every combination c1v1+⋯+ckvkc_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k: the origin, a line, a plane or more, always through 0\mathbf{0}.
  • Vectors are independent when none is a combination of the others. With them as the columns of AA: independent exactly when rank⁡A=k\operatorname{rank} A = k, and for nn vectors in Rn\mathbb{R}^n, when det⁡A≠0\det A \neq 0.
  • A basis is an independent set that spans; its size is the dimension, equal to the rank. Rn\mathbb{R}^n has dimension nn.
  • Every vector has unique coordinates c=B−1x\mathbf{c} = B^{-1}\mathbf{x} in a basis BB; for orthogonal basis vectors, each is a dot product over a squared length.
  • The column space of AA holds every AxA\mathbf{x}, with dimension rank⁡A\operatorname{rank} A; Ax=bA\mathbf{x} = \mathbf{b} is solvable exactly when b\mathbf{b} is in it.
  • The null space holds every x\mathbf{x} with Ax=0A\mathbf{x} = \mathbf{0}, with dimension n−rank⁡An - \operatorname{rank} A.

Free further reading

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