Linear algebra part 4 - Dot products

Dot products are the vector version of multiplication. To calculate the dot product of two vectors you multiply the components of the vectors, then add the resulting products together. For example, if we have vectors v\mathbf{v} and w\mathbf{w}, their dot product would look like this.

v=[v1v2v3vn],w=[w1w2w3wn] \mathbf{v} = \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ \vdots \\ v_n \end{bmatrix}, \quad \mathbf{w} = \begin{bmatrix} w_1 \\ w_2 \\ w_3 \\ \vdots \\ w_n \end{bmatrix}
vw=[v1v2v3vn][w1w2w3wn]=v1w1+v2w2+v3w3++vn+wn \begin{aligned} \mathbf{v} \cdot \mathbf{w} &= \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ \vdots \\ v_n \end{bmatrix} \cdot \begin{bmatrix} w_1 \\ w_2 \\ w_3 \\ \vdots \\ w_n \end{bmatrix} \\ &= v_1 \cdot w_1 + v_2 \cdot w_2 + v_3 \cdot w_3 + \cdots + v_n + w_n \end{aligned}
vw=[v1v2v3vn][w1w2w3wn]=v1w1+v2w2+v3w3++vn+wn \mathbf{v} \cdot \mathbf{w} = \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ \vdots \\ v_n \end{bmatrix} \cdot \begin{bmatrix} w_1 \\ w_2 \\ w_3 \\ \vdots \\ w_n \end{bmatrix} = v_1 \cdot w_1 + v_2 \cdot w_2 + v_3 \cdot w_3 + \cdots + v_n + w_n

Let us try that out with some real numbers:

v=[170],w=[331] \mathbf{v} = \begin{bmatrix} 1 \\ 7 \\ 0 \end{bmatrix}, \quad \mathbf{w} = \begin{bmatrix} 3 \\ 3 \\ 1 \end{bmatrix}
vw=[170][331]=13+73+01=3+21+0=24 \begin{aligned} \mathbf{v} \cdot \mathbf{w} &= \begin{bmatrix} 1 \\ 7 \\ 0 \end{bmatrix} \cdot \begin{bmatrix} 3 \\ 3 \\ 1 \end{bmatrix} \\ &= 1 \cdot 3 + 7 \cdot 3 + 0 \cdot 1 \\ &= 3 + 21 + 0 \\ &= 24 \end{aligned}

You can calculate the dot product of as many vectors as you like with as many components as you like. For example, if we define a third vector, x\mathbf{x}, we can calculate the dot product vwx\mathbf{v} \cdot \mathbf{w} \cdot \mathbf{x}:

x=[962] \mathbf{x} = \begin{bmatrix} 9 \\ 6 \\ 2 \end{bmatrix}
vwx=[170][331][962]=139+736+012=27+126+0=153 \begin{aligned} \mathbf{v} \cdot \mathbf{w} \cdot \mathbf{x} &= \begin{bmatrix} 1 \\ 7 \\ 0 \end{bmatrix} \cdot \begin{bmatrix} 3 \\ 3 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 9 \\ 6 \\ 2 \end{bmatrix} \\ &= 1 \cdot 3 \cdot 9 + 7 \cdot 3 \cdot 6 + 0 \cdot 1 \cdot 2 \\ &= 27 + 126 + 0 \\ &= 153 \end{aligned}

A practical application of dot products is calculating the value of all the shares in your portfolio. You could define a price vector, p\mathbf{p}, which contains the current price of each share, and a quantity vector q\mathbf{q}, which holds the number of shares you own. Calculating the dot product pq\mathbf{p} \cdot \mathbf{q} will give us the portfolio value.

CompanyPriceQuantity
Nvidia$174.1817
Microsoft$506.698
Apple$232.1442
Amazon$229.0051

We can turn these prices and quantities into two vectors:

p=[174.18506.69232.14229.00],q=[1784251] \mathbf{p} = \begin{bmatrix} 174.18 \\ 506.69 \\ 232.14 \\ 229.00 \end{bmatrix}, \quad \mathbf{q} = \begin{bmatrix} 17 \\ 8 \\ 42 \\ 51 \end{bmatrix}

The dot product pq\mathbf{p} \cdot \mathbf{q} will give us the total portfolio value.

pq=[174.18506.69232.14229.00][1784251]=174.1817+506.698+232.1442+229.0051=2961.06+4053.52+9749.88+11679.00=$28,443.46 \begin{aligned} \mathbf{p} \cdot \mathbf{q} &= \begin{bmatrix} 174.18 \\ 506.69 \\ 232.14 \\ 229.00 \end{bmatrix} \cdot \begin{bmatrix} 17 \\ 8 \\ 42 \\ 51 \end{bmatrix} \\ &= 174.18 \cdot 17 + 506.69 \cdot 8 + 232.14 \cdot 42 + 229.00 \cdot 51 \\ &= 2961.06 + 4053.52 + 9749.88 + 11679.00 \\ &= \$28,443.46 \end{aligned}

Problems

If you can solve these problems you have understood how to calculate the dot product of vectors.

  1. Calculate the dot product of v\mathbf{v} and w\mathbf{w} if:
v=[251],w=[139] \mathbf{v} = \begin{bmatrix} 2 \\ 5 \\ 1 \end{bmatrix}, \quad \mathbf{w} = \begin{bmatrix} 1 \\ 3 \\ 9 \end{bmatrix}
  1. Calculate the total value of the portfolio below by calculating the dot product of vectors of price and quantity.
CompanyPriceQuantity
Meta$738.7068
Broadcom$297.3941
Alphabet$212.9190
Berkshire Hathaway$502.987

Solutions

  1. To calculate the dot product we first multiply each element of the two vectors together, then add up the results:
vw=[251][139]=21+53+19=26 \begin{aligned} \mathbf{v} \cdot \mathbf{w} &= \begin{bmatrix} 2 \\ 5 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 3 \\ 9 \end{bmatrix} \\ &= 2 \cdot 1 + 5 \cdot 3 + 1 \cdot 9 \\ &= 26 \end{aligned}
vw=[251][139]=21+53+19=26 \mathbf{v} \cdot \mathbf{w} = \begin{bmatrix} 2 \\ 5 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 3 \\ 9 \end{bmatrix} = 2 \cdot 1 + 5 \cdot 3 + 1 \cdot 9 = 26
  1. To calculate the total portfolio value we can turn the prices and quantities of share into vectors, then calculate their dot product:
pq=[738.70297.39212.91502.98][6841907]=738.7068+297.3941+212.9190+502.987=50231.60+12192.99+19161.90+3520.86=$85,107.35 \begin{aligned} \mathbf{p} \cdot \mathbf{q} &= \begin{bmatrix} 738.70 \\ 297.39 \\ 212.91 \\ 502.98 \end{bmatrix} \cdot \begin{bmatrix} 68 \\ 41 \\ 90 \\ 7 \end{bmatrix} \\ &= 738.70 \cdot 68 + 297.39 \cdot 41 + 212.91 \cdot 90 + 502.98 \cdot 7 \\ &= 50231.60 + 12192.99 + 19161.90 + 3520.86 \\ &= \$85,107.35 \end{aligned}

Subscribe to 15 Minute Finance

Be the first to know when new posts are published

Buy me a coffee

If you have learnt something today, please consider supporting me