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 2 of 4

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

The dot product, length and angle

The dot product

The dot product of two vectors multiplies matching components and adds the results. It turns two vectors into one number, a scalar. Both vectors must have the same number of components.

For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2), the dot product is u⋅v=3×1+1×2=5\mathbf{u} \cdot \mathbf{v} = 3 \times 1 + 1 \times 2 = 5.

Rule: Dot product

u⋅v=u1v1+⋯+unvn=∑i=1nuivi\begin{gathered} \mathbf{u} \cdot \mathbf{v} = u_1 v_1 + \cdots + u_n v_n \\[4pt] = \sum_{i=1}^{n} u_i v_i \end{gathered}

Multiply component ii of u\mathbf{u} by component ii of v\mathbf{v}, for every ii, and add. The Σ\Sigma is the sum notation of lesson 0.2.2. The answer is a number, not a vector.

A weighted sum

The dot product is a weighted sum: each component of one vector weights the matching component of the other. The shop of lesson 3.1.1 holds stock s=(12,0,7,30)\mathbf{s} = (12, 0, 7, 30). Its four products sell at prices p=(2.50,4,10,0.80)\mathbf{p} = (2.50, 4, 10, 0.80) dollars each.

product1234total
stock sis_i120730
price pip_i ($)2.504100.80
sipis_i p_i ($)3007024124

So s⋅p=124\mathbf{s} \cdot \mathbf{p} = 124: the stock is worth $124. The units multiply, items times dollars per item, giving dollars. Case study 3.1.4 uses the same weighted sum for a portfolio's return.

Its rules

The dot product follows rules much like ordinary multiplication. For vectors u\mathbf{u}, v\mathbf{v}, w\mathbf{w} of the same size and a scalar cc:

  • Order does not matter: u⋅v=v⋅u\mathbf{u} \cdot \mathbf{v} = \mathbf{v} \cdot \mathbf{u}.
  • Scalars come out: (cu)⋅v=c (u⋅v)(c\mathbf{u}) \cdot \mathbf{v} = c\,(\mathbf{u} \cdot \mathbf{v}). So (2u)⋅v=10(2\mathbf{u}) \cdot \mathbf{v} = 10.
  • It spreads over sums: u⋅(v+w)=u⋅v+u⋅w\mathbf{u} \cdot (\mathbf{v} + \mathbf{w}) = \mathbf{u} \cdot \mathbf{v} + \mathbf{u} \cdot \mathbf{w}.
  • A vector with itself: u⋅u=u12+⋯+un2\mathbf{u} \cdot \mathbf{u} = u_1^2 + \cdots + u_n^2, never negative. For u=(3,1)\mathbf{u} = (3, 1) it is 10.

Try it

In NumPy the dot product is u @ v, or np.dot(u, v). Both print 5.

u * v is something else: the vector of products, [3 2]. Adding its components gives the dot product. The last line is the stock's value.

⌘/Ctrl + Enter runs the cell

Length

The length of a vector, written ∥u∥\lVert \mathbf{u} \rVert, is the length of its arrow. In the plane the components are the legs of a right-angled triangle. So Pythagoras' theorem (lesson 1.7.1) gives ∥u∥=u12+u22\lVert \mathbf{u} \rVert = \sqrt{u_1^2 + u_2^2}.

The sum of squares under the root is u⋅u\mathbf{u} \cdot \mathbf{u}. So the length of (3,4)(3, 4) is 9+16=5\sqrt{9 + 16} = 5, and the length of u=(3,1)\mathbf{u} = (3, 1) is 10≈3.1623\sqrt{10} \approx 3.1623. Lesson 1.7.2 measured the modulus of a complex number the same way.

In R3\mathbb{R}^3 Pythagoras applies twice and gives the same formula: (2,3,6)(2, 3, 6) has length 4+9+36=7\sqrt{4 + 9 + 36} = 7. In Rn\mathbb{R}^n the formula is the definition of length.

Rule: Length

∥u∥=u⋅u=u12+⋯+un2\begin{gathered} \lVert \mathbf{u} \rVert = \sqrt{\mathbf{u} \cdot \mathbf{u}} \\[4pt] = \sqrt{u_1^2 + \cdots + u_n^2} \end{gathered}

The length is in the same units as the components. It is never negative, and it is 0 only for the zero vector.

Squares remove signs, so (−3,4)(-3, 4) also has length 5. Scaling by cc multiplies the length by ∣c∣|c|, the size of cc (lesson 0.3.3). So ∥2u∥=210≈6.3246\lVert 2\mathbf{u} \rVert = 2\sqrt{10} \approx 6.3246, and ∥−3u∥=310≈9.4868\lVert -3\mathbf{u} \rVert = 3\sqrt{10} \approx 9.4868.

Distance between two points

The arrow from the tip of v\mathbf{v} to the tip of u\mathbf{u} is u−v\mathbf{u} - \mathbf{v} (lesson 3.1.1). Its length is the distance between the two points. For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2) it is ∥(2,−1)∥=5≈2.2361\lVert (2, -1) \rVert = \sqrt{5} \approx 2.2361.

Try it

np.linalg.norm computes the length. The first line prints it for u\mathbf{u}, next to u⋅u\sqrt{\mathbf{u} \cdot \mathbf{u}}.

The loop measures the other examples, then the last line gives the distance between the tips of u\mathbf{u} and v\mathbf{v}.

⌘/Ctrl + Enter runs the cell

Unit vectors

A unit vector has length 1. Dividing a non-zero vector by its length gives the unit vector pointing the same way; this is called normalising it.

(3,4)(3, 4) has length 5, so its unit vector is (0.6,0.8)(0.6, 0.8). For u=(3,1)\mathbf{u} = (3, 1) it is 110(3,1)≈(0.9487,0.3162)\frac{1}{\sqrt{10}}(3, 1) \approx (0.9487, 0.3162).

The zero vector has length 0 and no direction, so it has no unit vector: the division would be by 0.

Rule: Unit vector

1∥u∥ ufor u≠0\frac{1}{\lVert \mathbf{u} \rVert}\,\mathbf{u} \quad \text{for } \mathbf{u} \neq \mathbf{0}

A vector of length 1 pointing the same way as u\mathbf{u}. In the plane every unit vector is (cos⁡α,sin⁡α)(\cos\alpha, \sin\alpha) for some angle α\alpha: a point on the unit circle of lesson 1.7.1.

The unit vectors along the axes are e1=(1,0)\mathbf{e}_1 = (1, 0) and e2=(0,1)\mathbf{e}_2 = (0, 1). A dot product with them picks out a component: u⋅e1=u1=3\mathbf{u} \cdot \mathbf{e}_1 = u_1 = 3 and u⋅e2=u2=1\mathbf{u} \cdot \mathbf{e}_2 = u_2 = 1.

Try it

The first line normalises (3,4)(3, 4) and u\mathbf{u}, then checks that the second unit vector has length 1, after rounding.

The last line picks out the two components of u\mathbf{u} with e1\mathbf{e}_1 and e2\mathbf{e}_2.

⌘/Ctrl + Enter runs the cell

The angle between two vectors

The dot product measures how far two vectors point the same way. Take two unit vectors on the unit circle, at angles α\alpha and β\beta (alpha and beta) from the positive horizontal axis. Their dot product is the cosine of the angle between them, cos⁡(β−α)\cos(\beta - \alpha):

α\alphaβ\betadot productcos⁡(β−α)\cos(\beta - \alpha)
0°60°0.50.5
30°75°0.7071070.707107
20°140°−0.5-0.5−0.5-0.5
50°140°00

Any non-zero vector is its length times a unit vector, and scalars come out of a dot product. So u⋅v=∥u∥ ∥v∥cos⁡θ\mathbf{u} \cdot \mathbf{v} = \lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert \cos\theta, where θ\theta (theta) is the angle between u\mathbf{u} and v\mathbf{v}.

Rule: Angle between two vectors

cos⁡θ=u⋅v∥u∥ ∥v∥\cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert}

For non-zero u\mathbf{u} and v\mathbf{v}. The angle θ\theta is taken between 0 and π\pi, that is 0° to 180°, which is what math.acos returns (lesson 1.7.1).

Example 1. For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2), the dot product is 5 and the lengths are 10\sqrt{10} and 5\sqrt{5}:

cos⁡θ=510 5=550=12≈0.7071\begin{aligned} \cos\theta &= \frac{5}{\sqrt{10}\,\sqrt{5}} = \frac{5}{\sqrt{50}} \\ &= \frac{1}{\sqrt{2}} \approx 0.7071 \end{aligned}

That is the cosine of 45°, so θ=45°\theta = 45°.

Example 2. In R3\mathbb{R}^3, take (1,1,0)(1, 1, 0) and (0,1,1)(0, 1, 1). The dot product is 1 and both lengths are 2\sqrt{2}, so cos⁡θ=12\cos\theta = \frac{1}{2} and θ=60°\theta = 60°. With more than three components there is no picture, and the formula defines the angle.

The two extremes: 2u=(6,2)2\mathbf{u} = (6, 2) points the same way as u\mathbf{u}, with cosine 1 and angle 0°. −u-\mathbf{u} points the opposite way, with cosine −1-1 and angle 180°. The angle is always taken the short way round, and it does not say which vector is clockwise from the other.

Try it

The loop checks the table: two unit vectors at angles α\alpha and β\beta, their dot product, then cos⁡(β−α)\cos(\beta - \alpha).

Then the cell prints the cosine of Example 1. angle is the rule above: np.clip keeps the cosine inside [−1,1][-1, 1], for a reason given below, and np.degrees converts the radians from np.arccos.

⌘/Ctrl + Enter runs the cell

The sign of the dot product

Lengths are positive, so u⋅v\mathbf{u} \cdot \mathbf{v} has the sign of cos⁡θ\cos\theta. The sign alone says whether the angle is below, at or above 90°.

  • Positive: acute, below 90°. (3,1)⋅(1,2)=5(3, 1) \cdot (1, 2) = 5, and the angle is 45°.
  • Zero: a right angle. (3,1)⋅(−1,3)=−3+3=0(3, 1) \cdot (-1, 3) = -3 + 3 = 0, and the angle is 90°.
  • Negative: obtuse, above 90°. (3,1)⋅(−2,1)=−6+1=−5(3, 1) \cdot (-2, 1) = -6 + 1 = -5, and the angle is 135°.

u=(3,1)\mathbf{u} = (3, 1) against three vectors of the same length, 5\sqrt{5}. The dot product is positive for an acute angle, zero for a right angle and negative for an obtuse one.

Drag the tip of v\mathbf{v} round u\mathbf{u} and watch the sign of u⋅v\mathbf{u} \cdot \mathbf{v} change as the angle passes 90°. Then make v\mathbf{v} a multiple of u\mathbf{u}: the angle is 0° or 180°, and ∣u⋅v∣|\mathbf{u} \cdot \mathbf{v}| is as large as the two lengths allow. The dark arrow is the shadow of v\mathbf{v}, explained at the end of this lesson.

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°.

Orthogonal vectors

Two vectors are orthogonal when their dot product is 0. For non-zero vectors that means they meet at a right angle; "perpendicular" is the everyday word.

  • (3,1)(3, 1) and (−1,3)(-1, 3) are orthogonal. In general, (a,b)(a, b) turned a quarter turn is (−b,a)(-b, a) (lesson 1.7.2), and a(−b)+ba=0a(-b) + ba = 0.
  • In R3\mathbb{R}^3, (1,2,3)⋅(3,0,−1)=3+0−3=0(1, 2, 3) \cdot (3, 0, -1) = 3 + 0 - 3 = 0, so they are orthogonal too.
  • The axes are orthogonal: e1⋅e2=0\mathbf{e}_1 \cdot \mathbf{e}_2 = 0.

Rule: Orthogonal vectors

u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0

u\mathbf{u} and v\mathbf{v} are orthogonal. A dot product is far easier to check than an angle, in any number of dimensions. The zero vector is orthogonal to every vector, though it has no angle.

Module 3.5 builds projections and least squares on this one idea.

Try it

Each dot product here is 0: (3,1)(3, 1) with (−1,3)(-1, 3), its quarter turn; the pair in R3\mathbb{R}^3; and e1\mathbf{e}_1 with e2\mathbf{e}_2.

⌘/Ctrl + Enter runs the cell

The Cauchy–Schwarz inequality

In the plane, u⋅v=∥u∥ ∥v∥cos⁡θ\mathbf{u} \cdot \mathbf{v} = \lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert \cos\theta, and a cosine lies between −1-1 and 1. So the size of a dot product never exceeds the product of the two lengths. This is the Cauchy–Schwarz inequality; it holds in every Rn\mathbb{R}^n, and is stated here without proof.

Rule: Cauchy–Schwarz inequality

∣u⋅v∣≤∥u∥ ∥v∥|\mathbf{u} \cdot \mathbf{v}| \le \lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert

Equality holds exactly when one vector is a multiple of the other. In Rn\mathbb{R}^n it is why the ratio in the angle rule always lies in [−1,1][-1, 1], so every pair of non-zero vectors has an angle.

For (3,1)(3, 1) and (1,2)(1, 2): ∣5∣≤10 5≈7.0711|5| \le \sqrt{10}\,\sqrt{5} \approx 7.0711. For u\mathbf{u} and 2u2\mathbf{u} the two sides are equal: 20=10 4020 = \sqrt{10}\,\sqrt{40}.

Try it

The loop draws 10,000 random pairs of vectors in R5\mathbb{R}^5 and records the largest ∣u⋅v∣∥u∥ ∥v∥\frac{|\mathbf{u} \cdot \mathbf{v}|}{\lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert}. It never reaches 1; the largest is 0.995.

rng is a random number generator with a fixed seed, so every run gives the same numbers.

⌘/Ctrl + Enter runs the cell

Two rounding traps

Computers round every decimal, and two things can go wrong. For vectors pointing the same way, the computed cosine can come out a hair above 1. Then math.acos stops with an error, and np.arccos returns nan, "not a number".

The fix is to clip the cosine into [−1,1][-1, 1] first, with np.clip(cos, -1, 1). The second trap is the reverse: a dot product that should be 0 can come out as a tiny number such as 1.4×10−171.4 \times 10^{-17}. Test orthogonality with a tolerance, not with == 0.

Try it

This cell uses plain Python arithmetic, so its last digits are the same on every computer. For u=(3,1)\mathbf{u} = (3, 1) and 1.3u1.3\mathbf{u}, the cosine prints as 1.0000000000000002, and math.acos raises ValueError.

The last lines test two orthogonal vectors: the dot product is not exactly 0, but it is within 10−1210^{-12} of it.

⌘/Ctrl + Enter runs the cell

The shadow of one vector on another

Shine a light straight down onto the line through u\mathbf{u}. The shadow of v\mathbf{v} on that line has signed length u⋅v∥u∥\frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert}, which is ∥v∥cos⁡θ\lVert \mathbf{v} \rVert \cos\theta. It is positive when the shadow falls along u\mathbf{u} and negative when it falls behind the origin.

For v=(1,2)\mathbf{v} = (1, 2) it is 510≈1.5811\frac{5}{\sqrt{10}} \approx 1.5811, the same as 5cos⁡45°\sqrt{5}\cos 45°. For (−2,1)(-2, 1), at 135° to u\mathbf{u}, it is −1.5811-1.5811. For a vector orthogonal to u\mathbf{u} the shadow is a single point, of length 0.

The shadow of a vector on the line through u=(3,1)\mathbf{u} = (3, 1), cut off by a drop at right angles. Its signed length is u⋅v∥u∥\frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert}: 1.58 for (1,2)(1, 2), and −1.58-1.58 for (−2,1)(-2, 1), which points away from u\mathbf{u}.

The shadow is also called the projection of v\mathbf{v} on u\mathbf{u}. Lesson 3.5.2 develops it: the shadow as a vector, and the nearest point on a line or a plane.

Try it

Each line prints the signed length of the shadow on u\mathbf{u}: for (1,2)(1, 2), for (−2,1)(-2, 1) and for the orthogonal (−1,3)(-1, 3).

The second value on the first line is ∥v∥cos⁡45°\lVert \mathbf{v} \rVert \cos 45°, the same number.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. The dot product is a number. In NumPy, u * v is the vector of products, [3 2]; the dot product is u @ v.
  2. Length squares the components. ∥(3,4)∥=5\lVert (3, 4) \rVert = 5, not 3+4=73 + 4 = 7. And (−3,4)(-3, 4) has length 5 too: the squares remove the signs.
  3. Scaling by a negative number does not give a negative length. ∥−3u∥=3∥u∥\lVert -3\mathbf{u} \rVert = 3\lVert \mathbf{u} \rVert.
  4. The zero vector has no direction. It has no unit vector and no angle with anything.
  5. Divide by both lengths. The cosine is u⋅v∥u∥ ∥v∥\frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert}; the dot product 5 on its own is not a cosine.
  6. np.arccos gives radians. Convert with np.degrees, and clip the cosine into [−1,1][-1, 1] first.
  7. The shadow of v\mathbf{v} on u\mathbf{u} divides by ∥u∥\lVert \mathbf{u} \rVert, the length of the vector it falls on, not ∥v∥\lVert \mathbf{v} \rVert.
  8. Test for 0 with a tolerance. A dot product that should be 0 may compute as about 10−1710^{-17}.

The dot product, length and angle

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

  • u⋅v=u1v1+⋯+unvn\mathbf{u} \cdot \mathbf{v} = u_1v_1 + \cdots + u_nv_n, a number: a weighted sum. It is symmetric, scalars come out, and it spreads over sums. In NumPy, u @ v.
  • ∥u∥=u⋅u\lVert \mathbf{u} \rVert = \sqrt{\mathbf{u} \cdot \mathbf{u}}, by Pythagoras; ∥cu∥=∣c∣ ∥u∥\lVert c\mathbf{u} \rVert = |c|\,\lVert \mathbf{u} \rVert; the distance between two points is ∥u−v∥\lVert \mathbf{u} - \mathbf{v} \rVert.
  • A unit vector has length 1; 1∥u∥u\frac{1}{\lVert \mathbf{u} \rVert}\mathbf{u} normalises a non-zero u\mathbf{u}.
  • cos⁡θ=u⋅v∥u∥ ∥v∥\cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert}, with θ\theta from 0° to 180°. A positive dot product means acute, zero a right angle, negative obtuse.
  • Orthogonal means u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0.
  • Cauchy–Schwarz: ∣u⋅v∣≤∥u∥ ∥v∥|\mathbf{u} \cdot \mathbf{v}| \le \lVert \mathbf{u} \rVert\,\lVert \mathbf{v} \rVert, so the cosine stays in [−1,1][-1, 1]. Clip it before arccos.
  • The shadow of v\mathbf{v} on the line through u\mathbf{u} has signed length u⋅v∥u∥\frac{\mathbf{u} \cdot \mathbf{v}}{\lVert \mathbf{u} \rVert}.

Free further reading

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