SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.1.1 Vectors and their arithmeticConcept · 30 min
      2. 3.1.2 The dot product, length and angleConcept · 35 min
      3. 3.1.3 Practice set: vectorsPractice set · 20 min
      4. 3.1.4 Case study: portfolio returns and correlation as an angleCase study · 35 min

Level 3, Linear algebra 3.1 Vectors Lesson 1 of 4

ConceptLesson 3.1.1 in Level 3, Linear algebra. About 30 minutes.

Vectors and their arithmetic

What a vector is

A vector is an ordered list of numbers. Each number in the list is a component. The order matters: each position has its own meaning.

Walk 3 km east, then 1 km north. The vector (3,1)(3, 1) records the walk: its first component is the distance east and its second the distance north, both in km. The list (1,3)(1, 3) is a different walk, 1 km east and 3 km north.

Vectors are named with bold lower-case letters and written as columns:

u=(31)\mathbf{u} = \begin{pmatrix} 3 \\ 1 \end{pmatrix}

In running text, (3,1)(3, 1) means this column. The components of u\mathbf{u} are u1=3u_1 = 3 and u2=1u_2 = 1: the small number says which position.

An ordinary number, such as 3 or −0.5-0.5, is called a scalar, to tell it apart from a vector. The name comes from scaling, later in this lesson.

Two components, or n

R\mathbb{R} stands for the real numbers. R2\mathbb{R}^2 is the set of all vectors with two real components, and Rn\mathbb{R}^n is the set of all vectors with nn components.

A shop that sells four products can record its stock as a vector in R4\mathbb{R}^4. The vector s=(12,0,7,30)\mathbf{s} = (12, 0, 7, 30) says: 12 of the first product, none of the second, 7 of the third and 30 of the fourth. Each position always stands for the same product.

Two vectors are equal when they have the same number of components and match position by position. So (3,1)≠(1,3)(3, 1) \neq (1, 3).

Rule: A vector

u=(u1u2⋮un)\mathbf{u} = \begin{pmatrix} u_1 \\ u_2 \\ \vdots \\ u_n \end{pmatrix}

An ordered list of nn numbers, written as a column: a vector in Rn\mathbb{R}^n. The component uiu_i is the number in position ii, and each position has its own meaning and units.

A vector as an arrow

In the plane, draw u=(3,1)\mathbf{u} = (3, 1) as an arrow from the origin (0,0)(0, 0) to the point (3,1)(3, 1). Its components are its run and rise, as in lesson 1.2.1: 3 across, then 1 up. A negative component steps left or down, so w=(−2,3)\mathbf{w} = (-2, 3) goes 2 left and 3 up.

An arrow records a move, not a place, so it can start anywhere. Drawn from (1,2)(1, 2), the vector u\mathbf{u} ends at (4,3)(4, 3); it still moves 3 across and 1 up. Every copy with the same steps is the same vector.

A vector's components are its steps across and up; a negative component steps left or down. The same vector can start anywhere: each copy of u\mathbf{u} moves 3 across and 1 up.

Drawn from the origin, the tip of u\mathbf{u} is the point with the same coordinates, (3,1)(3, 1). That is why a vector and a point are often written the same way. In R3\mathbb{R}^3 the arrows live in space; beyond three components there is no picture, but the arithmetic below works the same.

Adding vectors

To add two vectors, add the components in matching positions. For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2):

u+v=(3+11+2)=(43)\mathbf{u} + \mathbf{v} = \begin{pmatrix} 3 + 1 \\ 1 + 2 \end{pmatrix} = \begin{pmatrix} 4 \\ 3 \end{pmatrix}

As walks: 3 east and 1 north, then 1 east and 2 north, ends 4 east and 3 north of the start. Only vectors with the same number of components can be added; (1,2)+(1,2,3)(1, 2) + (1, 2, 3) has no meaning.

In the picture, start v\mathbf{v} where u\mathbf{u} ends: tip to tail. The sum is the arrow from the start of u\mathbf{u} to the end of v\mathbf{v}. Walking v\mathbf{v} first and then u\mathbf{u} reaches the same corner, so the two routes form a parallelogram, and u+v=v+u\mathbf{u} + \mathbf{v} = \mathbf{v} + \mathbf{u}.

Rule: Adding vectors

u+v=(u1+v1⋮un+vn)\mathbf{u} + \mathbf{v} = \begin{pmatrix} u_1 + v_1 \\ \vdots \\ u_n + v_n \end{pmatrix}

Add matching components. In the plane, the sum is the far corner of the parallelogram on u\mathbf{u} and v\mathbf{v}. Lesson 1.7.2 added complex numbers the same way, part by part.

Drag the tips of u\mathbf{u} and v\mathbf{v}, and check that each component of u+v\mathbf{u} + \mathbf{v} is a sum. Then turn v\mathbf{v} to point almost opposite to u\mathbf{u}: the parallelogram flattens, and the sum can come out shorter than u\mathbf{u} itself. The lengths in the readout come in lesson 3.1.2.

Explore

Drag the tips of u and v. Their sum u + v is the far corner of the parallelogram they make: walk along u, then along v.

−5−555u + vuv
u
(3, 1), length 3.1623
v
(1, 2), length 2.2361
u + v
(3 + 1, 1 + 2) = (4, 3)

In R4\mathbb{R}^4 there is no picture, but the rule is the same. The shop above receives a delivery d=(6,10,0,12)\mathbf{d} = (6, 10, 0, 12), and adds it to its stock one product at a time:

product1234
stock s\mathbf{s}120730
delivery d\mathbf{d}610012
s+d\mathbf{s} + \mathbf{d}1810742

Try it

np.array turns a list into a NumPy array, which adds component by component. The first line prints [4 3] twice: the order of a sum does not matter.

Python lists are different: + joins two lists end to end. The last lines show that, and the error NumPy gives for vectors of different sizes.

⌘/Ctrl + Enter runs the cell

Scaling a vector

To multiply a vector by a scalar cc, multiply every component by cc. This is called scaling. For u=(3,1)\mathbf{u} = (3, 1), 2u=(6,2)2\mathbf{u} = (6, 2) and 12u=(1.5,0.5)\frac{1}{2}\mathbf{u} = (1.5, 0.5).

Every multiple of u\mathbf{u} lies on the line through the origin and u\mathbf{u}. What cc does depends on its size and its sign:

  • c>1c > 1 stretches. 2u=(6,2)2\mathbf{u} = (6, 2) points the same way and is twice as long.
  • 0<c<10 < c < 1 shrinks. 12u=(1.5,0.5)\frac{1}{2}\mathbf{u} = (1.5, 0.5) is half as long.
  • c<0c < 0 turns the arrow round. −u=(−3,−1)-\mathbf{u} = (-3, -1) points the opposite way. −2u=(−6,−2)-2\mathbf{u} = (-6, -2) is turned round and twice as long.
  • c=0c = 0 gives (0,0)(0, 0).

Scaling u=(3,1)\mathbf{u} = (3, 1) by 2, 12\frac{1}{2}, −1-1 and 0. Every multiple stays on the dotted line through the origin and u\mathbf{u}.

The vector whose components are all 0 is the zero vector, written 0\mathbf{0}. It is an arrow of length 0 with no direction, just the origin. Adding it changes nothing: u+0=u\mathbf{u} + \mathbf{0} = \mathbf{u}.

Rule: Scaling

c u=(c u1⋮c un)c\,\mathbf{u} = \begin{pmatrix} c\,u_1 \\ \vdots \\ c\,u_n \end{pmatrix}

Multiply every component by the scalar cc. The arrow keeps its line; a negative cc turns it round. Lesson 3.1.2 shows that its length is multiplied by ∣c∣|c|, the size of cc (lesson 0.3.3).

For the shop, three weeks of the same delivery is 3d=(18,30,0,36)3\mathbf{d} = (18, 30, 0, 36).

Try it

Multiplying a NumPy array by a number scales every component. 0.5 * u prints [1.5 0.5]: the components have become decimals.

A list multiplied by 2 is repeated instead: 2 * [3, 1] is [3, 1, 3, 1].

⌘/Ctrl + Enter runs the cell

Subtracting vectors

Subtract component by component too: v−u=(1−3,2−1)=(−2,1)\mathbf{v} - \mathbf{u} = (1 - 3, 2 - 1) = (-2, 1). It is the same as adding −1-1 times u\mathbf{u}: v−u=v+(−1)u\mathbf{v} - \mathbf{u} = \mathbf{v} + (-1)\mathbf{u}.

In the picture, v−u\mathbf{v} - \mathbf{u} is the arrow from the tip of u\mathbf{u} to the tip of v\mathbf{v}. Walk u\mathbf{u}, then v−u\mathbf{v} - \mathbf{u}, and you arrive at v\mathbf{v}: u+(v−u)=v\mathbf{u} + (\mathbf{v} - \mathbf{u}) = \mathbf{v}.

The order matters. u−v=(2,−1)\mathbf{u} - \mathbf{v} = (2, -1) is the same arrow turned round, from the tip of v\mathbf{v} to the tip of u\mathbf{u}. And u−u=0\mathbf{u} - \mathbf{u} = \mathbf{0}.

Rule: Subtracting vectors

v−u=v+(−1) u\mathbf{v} - \mathbf{u} = \mathbf{v} + (-1)\,\mathbf{u}

Subtract matching components, vi−uiv_i - u_i. In the plane, v−u\mathbf{v} - \mathbf{u} is the arrow from the tip of u\mathbf{u} to the tip of v\mathbf{v}.

v−u\mathbf{v} - \mathbf{u} runs from the tip of u\mathbf{u} to the tip of v\mathbf{v}; dashed, the same arrow drawn from the origin. The combination 2u+v2\mathbf{u} + \mathbf{v} walks u\mathbf{u} twice, then v\mathbf{v}, and ends at (7,4)(7, 4).

A week of sales q=(5,4,7,20)\mathbf{q} = (5, 4, 7, 20) leaves the shop with s+d−q=(13,6,0,22)\mathbf{s} + \mathbf{d} - \mathbf{q} = (13, 6, 0, 22). The 0 says the third product has sold out.

Try it

The first line prints v−u\mathbf{v} - \mathbf{u}, u−v\mathbf{u} - \mathbf{v} and u−u\mathbf{u} - \mathbf{u}. The second checks that u+(v−u)\mathbf{u} + (\mathbf{v} - \mathbf{u}) lands on v\mathbf{v}.

⌘/Ctrl + Enter runs the cell

Linear combinations

Scaling and adding together build the most important object in linear algebra. With scalars aa and bb, the vector au+bva\mathbf{u} + b\mathbf{v} is a linear combination of u\mathbf{u} and v\mathbf{v}.

With a=2a = 2 and b=1b = 1, 2u+v=(6,2)+(1,2)=(7,4)2\mathbf{u} + \mathbf{v} = (6, 2) + (1, 2) = (7, 4). Tip to tail, that is two steps of u\mathbf{u} and one of v\mathbf{v}, as in the second picture above. With a=1a = 1 and b=−2b = -2, u−2v=(1,−3)\mathbf{u} - 2\mathbf{v} = (1, -3).

Rule: Linear combination

a u+b v=(a u1+b v1⋮a un+b vn)a\,\mathbf{u} + b\,\mathbf{v} = \begin{pmatrix} a\,u_1 + b\,v_1 \\ \vdots \\ a\,u_n + b\,v_n \end{pmatrix}

Scale each vector by its own scalar, then add. Any number of vectors combine the same way: a1v1+⋯+akvka_1\mathbf{v}_1 + \cdots + a_k\mathbf{v}_k, where each bold vi\mathbf{v}_i is a whole vector.

A fruit shop packs two kinds of box. Box A holds 2 apples, 1 pear and no lemons; box B holds 1 apple, 3 pears and 2 lemons. As vectors in R3\mathbb{R}^3, a=(2,1,0)\mathbf{a} = (2, 1, 0) and b=(1,3,2)\mathbf{b} = (1, 3, 2).

An order of 4 box A and 2 box B holds 4a+2b4\mathbf{a} + 2\mathbf{b}:

applespearslemons
4a4\mathbf{a}840
2b2\mathbf{b}264
4a+2b4\mathbf{a} + 2\mathbf{b}10104

In symbols, one component at a time: the apples are 4×2+2×1=104 \times 2 + 2 \times 1 = 10, the pears 4×1+2×3=104 \times 1 + 2 \times 3 = 10 and the lemons 4×0+2×2=44 \times 0 + 2 \times 2 = 4.

Which points can combinations reach?

The point (5,5)(5, 5) is u+2v\mathbf{u} + 2\mathbf{v}. In fact every point of the plane is some combination au+bva\mathbf{u} + b\mathbf{v}, because u\mathbf{u} and v\mathbf{v} point in different directions. If v\mathbf{v} were a multiple of u\mathbf{u}, every combination would stay on the line through u\mathbf{u}.

Module 3.3 finds aa and bb for any target, and lesson 3.5.1 names the set of all combinations. Lesson 3.2.1 shows that a matrix times a vector is a linear combination of the matrix's columns.

Try it

The first line prints the three combinations of u\mathbf{u} and v\mathbf{v} above: (7,4)(7, 4), (1,−3)(1, -3) and (5,5)(5, 5).

The second line is the fruit order, 4a+2b4\mathbf{a} + 2\mathbf{b}.

⌘/Ctrl + Enter runs the cell

The rules of arithmetic carry over

Each component is an ordinary number, so the usual rules of arithmetic hold for vectors too. For vectors u\mathbf{u}, v\mathbf{v}, w\mathbf{w} and scalars aa, bb:

  • u+v=v+u\mathbf{u} + \mathbf{v} = \mathbf{v} + \mathbf{u}, the parallelogram
  • (u+v)+w=u+(v+w)(\mathbf{u} + \mathbf{v}) + \mathbf{w} = \mathbf{u} + (\mathbf{v} + \mathbf{w})
  • a(u+v)=au+ava(\mathbf{u} + \mathbf{v}) = a\mathbf{u} + a\mathbf{v} and (a+b)u=au+bu(a + b)\mathbf{u} = a\mathbf{u} + b\mathbf{u}
  • 1u=u1\mathbf{u} = \mathbf{u} and 0u=00\mathbf{u} = \mathbf{0}

So vector expressions expand and collect like ordinary algebra: 2(u+v)−u=u+2v2(\mathbf{u} + \mathbf{v}) - \mathbf{u} = \mathbf{u} + 2\mathbf{v}.

Try it

Each line computes both sides of one rule, with w=(−2,3)\mathbf{w} = (-2, 3). The last line checks the expanded expression: both sides are (5,5)(5, 5).

⌘/Ctrl + Enter runs the cell

Vectors in NumPy

A NumPy array is the standard way to hold a vector in Python. Three details matter from the start.

  • Positions count from 0. The first component u1u_1 is u[0], and u2u_2 is u[1].
  • len(u) is the number of components nn. NumPy also reports the shape, (2,) for a vector in R2\mathbb{R}^2.
  • A number plus an array adds to every component. u + 1 gives [4 2]. It is a NumPy shortcut; in maths you would write u+(1,1)\mathbf{u} + (1, 1).

Try it

The first line prints u1u_1, u2u_2, the number of components and the shape. The last line reads the third component of the shop's stock, s[2], which is 7.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. The order of components matters. (3,1)(3, 1) and (1,3)(1, 3) are different vectors: 3 east and 1 north is not 1 east and 3 north.
  2. Only vectors of the same size add. (1,2)+(1,2,3)(1, 2) + (1, 2, 3) has no meaning, and NumPy raises ValueError.
  3. Python lists are not vectors. [3, 1] + [1, 2] joins them into [3, 1, 1, 2], and 2 * [3, 1] repeats them. Use np.array.
  4. A number plus a vector is not defined in maths. NumPy's u + 1 adds 1 to every component; in maths, write u+(1,1)\mathbf{u} + (1, 1).
  5. Subtract in the right order. v−u\mathbf{v} - \mathbf{u} runs from the tip of u\mathbf{u} to the tip of v\mathbf{v}; u−v\mathbf{u} - \mathbf{v} points the other way.
  6. A negative scalar turns the arrow round. −2u-2\mathbf{u} is twice as long as u\mathbf{u}, not shorter; it points the opposite way.
  7. NumPy counts from 0. The first component u1u_1 is u[0].

Vectors and their arithmetic

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

  • A vector is an ordered list of numbers, its components, written as a column: u=(u1,…,un)\mathbf{u} = (u_1, \ldots, u_n) in Rn\mathbb{R}^n. An ordinary number is a scalar.
  • In the plane, a vector is an arrow, u1u_1 across and u2u_2 up. The same arrow can start anywhere; drawn from the origin, its tip is the point (u1,u2)(u_1, u_2).
  • Add and subtract component by component. u+v\mathbf{u} + \mathbf{v} goes tip to tail, to the far corner of the parallelogram; v−u\mathbf{v} - \mathbf{u} runs from the tip of u\mathbf{u} to the tip of v\mathbf{v}.
  • cuc\mathbf{u} scales every component: c>1c > 1 stretches, 0<c<10 < c < 1 shrinks, c<0c < 0 turns round, and c=0c = 0 gives the zero vector 0\mathbf{0}.
  • A linear combination au+bva\mathbf{u} + b\mathbf{v} scales each vector, then adds.
  • In NumPy, +, - and c * u work component by component on arrays; Python lists join and repeat instead.

Free further reading

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