SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.2.1 Matrices and matrix multiplicationConcept · 30 min
      2. 3.2.2 Matrices as linear mapsConcept · 30 min
      3. 3.2.3 Transpose, identity and inverseConcept · 35 min
      4. 3.2.4 Practice set: matricesPractice set · 25 min
      5. 3.2.5 Case study: the covariance matrix and portfolio varianceCase study · 35 min

Level 3, Linear algebra 3.2 Matrices Lesson 1 of 5

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

Matrices and matrix multiplication

What a matrix is

A matrix is a rectangular table of numbers. A matrix with mm rows and nn columns is an m×nm \times n matrix, read "mm by nn": rows first, then columns. That pair of numbers is its shape.

Each number in the table is an entry. For a matrix AA, the entry in row ii and column jj is aija_{ij}: again, row first.

A workshop makes chairs and tables. A chair needs 4 legs, 8 screws and 1 board; a table needs 4 legs, 12 screws and 3 boards. Put the parts in rows and the products in columns:

P=(4481213)legsscrewsboardsP = \begin{pmatrix} 4 & 4 \\ 8 & 12 \\ 1 & 3 \end{pmatrix} \begin{matrix} \text{legs} \\ \text{screws} \\ \text{boards} \end{matrix}

PP is 3×23 \times 2: three parts, two products. Its first column lists what a chair needs, and its second what a table needs.

Write pijp_{ij} for the entries of PP. Then p22=12p_{22} = 12 is screws per table, and p31=1p_{31} = 1 is boards per chair. Every entry has units: parts per item.

Rule: An m × n matrix

A=(a11a12a13a21a22a23)A = \begin{pmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{pmatrix}

This AA is 2×32 \times 3: 2 rows, 3 columns, 6 entries. In aija_{ij} the first index is the row and the second the column.

A vector written as a column (lesson 3.1.1) is a matrix with one column: a vector with nn components is n×1n \times 1. A matrix with one row, 1×n1 \times n, is a row vector. A matrix with as many rows as columns is square.

Try it

np.array takes a list of rows. .shape gives (rows, columns).

NumPy counts from 0, so p22p_{22} is P[1, 1] and p31p_{31} is P[2, 0]. P[:, 0] is the first column, the chair; P[2] is the third row, the boards.

⌘/Ctrl + Enter runs the cell

A matrix times a vector

An order of 6 chairs and 2 tables is the vector x=(6,2)\mathbf{x} = (6, 2), a column with one entry per product. The parts it needs make the vector PxP\mathbf{x}, and there are two ways to work it out.

Row by row. Legs: 4 per chair times 6 chairs, plus 4 per table times 2 tables, so 4⋅6+4⋅2=324 \cdot 6 + 4 \cdot 2 = 32. That is the dot product of the legs row (4,4)(4, 4) with x\mathbf{x}: multiply matching components and add (lesson 3.1.2). Each entry of PxP\mathbf{x} is one row of PP dotted with x\mathbf{x}.

Column by column. Six chairs need 6 copies of the chair column, and 2 tables need 2 copies of the table column. So PxP\mathbf{x} is the linear combination 6 (4,8,1)+2 (4,12,3)6\,(4, 8, 1) + 2\,(4, 12, 3): vectors scaled by numbers and added, as in lesson 3.1.1. The entries of x\mathbf{x} are the weights.

partrow ⋅ x\cdot\ \mathbf{x}6 × chair column2 × table columntotal
legs4⋅6+4⋅24 \cdot 6 + 4 \cdot 224832
screws8⋅6+12⋅28 \cdot 6 + 12 \cdot 2482472
boards1⋅6+3⋅21 \cdot 6 + 3 \cdot 26612

Both ways add the same six products, in a different order, so they agree: Px=(32,72,12)P\mathbf{x} = (32, 72, 12). The units work out too: parts per item times items gives parts.

Rule: Matrix times vector

(Ax)i=ai1x1+⋯+ainxnAx=x1a1+⋯+xnan\begin{gathered} (A\mathbf{x})_i = a_{i1}x_1 + \cdots + a_{in}x_n \\[4pt] A\mathbf{x} = x_1\mathbf{a}_1 + \cdots + x_n\mathbf{a}_n \end{gathered}

Entry ii of AxA\mathbf{x} is row ii of AA dotted with x\mathbf{x}. The whole of AxA\mathbf{x} is a combination of the columns a1,…,an\mathbf{a}_1, \ldots, \mathbf{a}_n of AA, weighted by the entries of x\mathbf{x}.

Try it

@ is the matrix product. The second line dots each row with x; the third combines the two columns.

A 1-D array such as x has no rows or columns of its own. On the right of @, NumPy reads it as a column.

⌘/Ctrl + Enter runs the cell

Shapes, and a picture in the plane

x\mathbf{x} needs one entry for each column of AA, because each entry weights a column. The answer has one entry for each row. So an m×nm \times n matrix turns vectors with nn components into vectors with mm. PP turns an order (2 numbers) into a parts list (3 numbers).

A 2×22 \times 2 matrix turns arrows in the plane into arrows in the plane. Take

A=(21−13)A = \begin{pmatrix} 2 & 1 \\ -1 & 3 \end{pmatrix}

with columns a1=(2,−1)\mathbf{a}_1 = (2, -1) and a2=(1,3)\mathbf{a}_2 = (1, 3). For x=(2,1)\mathbf{x} = (2, 1) the rows give (2⋅2+1⋅1, −1⋅2+3⋅1)=(5,1)(2 \cdot 2 + 1 \cdot 1,\ -1 \cdot 2 + 3 \cdot 1) = (5, 1). The columns give 2a1+a2=(4,−2)+(1,3)2\mathbf{a}_1 + \mathbf{a}_2 = (4, -2) + (1, 3), the same (5,1)(5, 1).

A negative entry turns its column round. For x=(1,−1)\mathbf{x} = (1, -1), Ax=a1−a2=(1,−4)A\mathbf{x} = \mathbf{a}_1 - \mathbf{a}_2 = (1, -4); row by row, 2−1=12 - 1 = 1 and −1−3=−4-1 - 3 = -4.

AxA\mathbf{x} as a combination of the columns of AA, drawn tip to tail. The entries of x\mathbf{x} are the weights: (2,1)(2, 1) gives 2a1+a22\mathbf{a}_1 + \mathbf{a}_2, and (1,−1)(1, -1) gives a1−a2\mathbf{a}_1 - \mathbf{a}_2.

Try it

Each line prints x\mathbf{x}, then AxA\mathbf{x} by @, then the combination of the columns.

The last lines multiply PP, which has 2 columns, by a vector with 3 entries. NumPy refuses with a ValueError.

⌘/Ctrl + Enter runs the cell

A matrix times a matrix

Two shops send orders: shop 1 wants 6 chairs and 2 tables, shop 2 wants 10 chairs and 1 table. Put each order in a column of a matrix BB, with rows for the products:

B=(61021)B = \begin{pmatrix} 6 & 10 \\ 2 & 1 \end{pmatrix}

The parts for shop jj are PP times column jj of BB. Side by side, these columns make the product PBPB:

PB=(324472921213)PB = \begin{pmatrix} 32 & 44 \\ 72 & 92 \\ 12 & 13 \end{pmatrix}

The first column is the parts list found above. Shop 2 needs 4⋅10+4⋅1=444 \cdot 10 + 4 \cdot 1 = 44 legs, 8⋅10+12⋅1=928 \cdot 10 + 12 \cdot 1 = 92 screws and 10+3=1310 + 3 = 13 boards.

Rule: Matrix times matrix

(AB)ij=ai1b1j+⋯+ainbnj(AB)_{ij} = a_{i1}b_{1j} + \cdots + a_{in}b_{nj}

Column jj of ABAB is AA times column jj of BB. Entry (i,j)(i, j) of ABAB is row ii of AA dotted with column jj of BB. An m×nm \times n matrix times an n×pn \times p matrix gives an m×pm \times p matrix.

The inner sizes must match: each row of AA must be as long as each column of BB. In the workshop the inner size counts the products.

PP is parts by products and BB is products by shops, so PBPB is parts by shops. The sum in each entry runs over the products.

The picture shows the rule for M=(1203−14)M = \begin{pmatrix} 1 & 2 & 0 \\ 3 & -1 & 4 \end{pmatrix}, which is 2×32 \times 3, times N=(102103)N = \begin{pmatrix} 1 & 0 \\ 2 & 1 \\ 0 & 3 \end{pmatrix}, which is 3×23 \times 2.

Entry (2,1)(2, 1) of MNMN comes from row 2 of MM and column 1 of NN. The 3 entries of the row pair off with the 3 entries of the column; that shared size drops out of the shape of the answer.

A second example, with two square matrices: AA from above and

C=(1021)C = \begin{pmatrix} 1 & 0 \\ 2 & 1 \end{pmatrix}

Each entry of ACAC is a row of AA dotted with a column of CC:

entryrow of AAcolumn of CCdot product
(1,1)(1, 1)(2,1)(2, 1)(1,2)(1, 2)2+2=42 + 2 = 4
(1,2)(1, 2)(2,1)(2, 1)(0,1)(0, 1)0+1=10 + 1 = 1
(2,1)(2, 1)(−1,3)(-1, 3)(1,2)(1, 2)−1+6=5-1 + 6 = 5
(2,2)(2, 2)(−1,3)(-1, 3)(0,1)(0, 1)0+3=30 + 3 = 3
AC=(4153)AC = \begin{pmatrix} 4 & 1 \\ 5 & 3 \end{pmatrix}

Try it

The first lines build the shops' parts lists, all at once with P @ B and one column at a time.

The loop rebuilds the table above: for each entry, the row of AA, the column of CC and their dot product.

⌘/Ctrl + Enter runs the cell

Order matters, grouping does not

With numbers, 3×5=5×33 \times 5 = 5 \times 3. With matrices, swapping the order usually changes the answer, and can even make it meaningless. There are four cases to know.

  • Both orders exist, with different answers. CA=(2135)CA = \begin{pmatrix} 2 & 1 \\ 3 & 5 \end{pmatrix}, while AC=(4153)AC = \begin{pmatrix} 4 & 1 \\ 5 & 3 \end{pmatrix}.
  • Both orders exist, with different shapes. MM is 2×32 \times 3 and NN is 3×23 \times 2, so MNMN is 2×22 \times 2 but NMNM is 3×33 \times 3.
  • Only one order exists. PBPB is (3×2)(2×2)(3 \times 2)(2 \times 2). BPBP would be (2×2)(3×2)(2 \times 2)(3 \times 2): rows of length 2 against columns of length 3, so it is not defined.
  • Sometimes the order does not matter. Two matrices with zeros off the diagonal (the entries from top left to bottom right), such as (2003)\begin{pmatrix} 2 & 0 \\ 0 & 3 \end{pmatrix} and (500−1)\begin{pmatrix} 5 & 0 \\ 0 & -1 \end{pmatrix}, give the same product either way. That is the exception, so always check.

The extreme case is a row times a column. The row (3  1)(3\ \ 1) is 1×21 \times 2 and the column (1,2)(1, 2) is 2×12 \times 1. Their product is 1×11 \times 1: the single number 3+2=53 + 2 = 5, their dot product.

The other order is (2×1)(1×2)(2 \times 1)(1 \times 2), a whole 2×22 \times 2 table. Lesson 3.2.3 writes the row as a column turned on its side.

(12)(31)=(3162)\begin{pmatrix} 1 \\ 2 \end{pmatrix} \begin{pmatrix} 3 & 1 \end{pmatrix} = \begin{pmatrix} 3 & 1 \\ 6 & 2 \end{pmatrix}

The grouping of a product, unlike its order, never matters. With D=(0111)D = \begin{pmatrix} 0 & 1 \\ 1 & 1 \end{pmatrix}, multiplying ACAC by DD gives the same as AA times CDCD: both are (1538)\begin{pmatrix} 1 & 5 \\ 3 & 8 \end{pmatrix}.

Rule: Order and grouping

AB≠BA in general(AB)C=A(BC)\begin{gathered} AB \neq BA \text{ in general} \\[4pt] (AB)C = A(BC) \end{gathered}

Keep the matrices in their order. The brackets can go anywhere, so a product of three is written ABCABC.

Try it

CACA against ACAC, the two groupings of ACDACD, and MNMN against NMNM.

np.array_equal checks that the two diagonal matrices give the same product in both orders. The row u and column v are 2-D arrays, so that NumPy keeps their shapes.

After the two groupings comes A * C, which is not a matrix product (see below).

⌘/Ctrl + Enter runs the cell

@, not *

In NumPy, A * C multiplies matching entries: 2⋅12 \cdot 1, 1⋅01 \cdot 0, −1⋅2-1 \cdot 2 and 3⋅13 \cdot 1, giving (20−23)\begin{pmatrix} 2 & 0 \\ -2 & 3 \end{pmatrix}. That is not the matrix product ACAC. Use @ for the product.

Lesson 3.2.2 reads a matrix as a map that moves the plane, and lesson 3.3.1 runs the product backwards to solve Ax=bA\mathbf{x} = \mathbf{b}. Case study 3.2.5 writes the variance of a portfolio as a product of a row, a matrix and a column.

Common mistakes

  1. Rows first. An m×nm \times n matrix has mm rows, and aija_{ij} is in row ii, column jj. In NumPy, counting from 0, a12a_{12} is A[0, 1].
  2. One entry of x\mathbf{x} per column. AxA\mathbf{x} needs x\mathbf{x} as long as a row of AA, and has as many entries as AA has rows.
  3. Check the inner sizes. (m×n)(n×p)(m \times n)(n \times p) works and gives m×pm \times p. If the inner sizes differ, there is no product.
  4. Keep the order. ABAB and BABA usually differ, may have different shapes, and one of them may not exist.
  5. A row times a column is a number; a column times a row is a table.
  6. * is not the matrix product. In NumPy it multiplies entry by entry; the product is @.

Matrices and their products

0 of 13 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.
  13. 13.
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

  • An m×nm \times n matrix has mm rows and nn columns; aija_{ij} is the entry in row ii, column jj.
  • AxA\mathbf{x}: entry ii is row ii of AA dotted with x\mathbf{x}; the whole is x1a1+⋯+xnanx_1\mathbf{a}_1 + \cdots + x_n\mathbf{a}_n, a combination of the columns.
  • ABAB: column jj is AA times column jj of BB; entry (i,j)(i, j) is row ii of AA dotted with column jj of BB.
  • Shapes: (m×n)(n×p)=m×p(m \times n)(n \times p) = m \times p. The inner sizes must match.
  • AB≠BAAB \neq BA in general, but (AB)C=A(BC)(AB)C = A(BC). A row times a column is a number; a column times a row is a matrix.
  • In NumPy, the product is @; * multiplies entry by entry.

Free further reading

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