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

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

Transpose, identity and inverse

The transpose

The transpose of a matrix AA, written A⊤A^\top, turns its rows into columns: row ii of AA becomes column ii of A⊤A^\top. So the entry in row ii, column jj of A⊤A^\top is ajia_{ji}, and an m×nm \times n matrix has an n×mn \times m transpose.

M=(1203−14)M⊤=(132−104)\begin{gathered} M = \begin{pmatrix} 1 & 2 & 0 \\ 3 & -1 & 4 \end{pmatrix} \\[6pt] M^\top = \begin{pmatrix} 1 & 3 \\ 2 & -1 \\ 0 & 4 \end{pmatrix} \end{gathered}

Transposing twice gives back the matrix you started with: (A⊤)⊤=A(A^\top)^\top = A. A column vector turned on its side is a row: for u=(3,1)\mathbf{u} = (3, 1), u⊤=(3  1)\mathbf{u}^\top = (3\ \ 1), a 1×21 \times 2 matrix.

The transpose of a product

With AA and CC from lesson 3.2.1, AC=(4153)AC = \begin{pmatrix} 4 & 1 \\ 5 & 3 \end{pmatrix}, so (AC)⊤=(4513)(AC)^\top = \begin{pmatrix} 4 & 5 \\ 1 & 3 \end{pmatrix}. That equals C⊤A⊤C^\top A^\top, with the order reversed. The other order, A⊤C⊤=(2315)A^\top C^\top = \begin{pmatrix} 2 & 3 \\ 1 & 5 \end{pmatrix}, does not match.

Here is why. Entry (1,2)(1, 2) of (AC)⊤(AC)^\top is entry (2,1)(2, 1) of ACAC: row 2 of AA dotted with column 1 of CC, (−1,3)⋅(1,2)=5(-1, 3) \cdot (1, 2) = 5.

Those two vectors are column 2 of A⊤A^\top and row 1 of C⊤C^\top. So the same dot product is entry (1,2)(1, 2) of C⊤A⊤C^\top A^\top.

Shapes say the same. MM is 2×32 \times 3 and N=(102103)N = \begin{pmatrix} 1 & 0 \\ 2 & 1 \\ 0 & 3 \end{pmatrix} is 3×23 \times 2, so MNMN and its transpose are 2×22 \times 2. N⊤M⊤N^\top M^\top is (2×3)(3×2)(2 \times 3)(3 \times 2), also 2×22 \times 2; but M⊤N⊤M^\top N^\top is (3×2)(2×3)(3 \times 2)(2 \times 3), a 3×33 \times 3 matrix.

Rule: Transpose

(A⊤)ij=aji(AB)⊤=B⊤A⊤\begin{gathered} (A^\top)_{ij} = a_{ji} \\[4pt] (AB)^\top = B^\top A^\top \end{gathered}

The transpose swaps rows and columns. The transpose of a product is the product of the transposes in the reverse order.

Try it

.T is the transpose in NumPy. The first lines print M⊤M^\top and both shapes.

Then (AC)⊤(AC)^\top and C⊤A⊤C^\top A^\top agree, while A⊤C⊤A^\top C^\top does not.

⌘/Ctrl + Enter runs the cell

Symmetric matrices

A square matrix is symmetric when it equals its transpose, A⊤=AA^\top = A: every entry aija_{ij} equals ajia_{ji}. The matrix is a mirror image of itself across its main diagonal, the entries a11,a22,…a_{11}, a_{22}, \ldots from top left to bottom right.

S=(2112)K=(4−10−132025)\begin{gathered} S = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} \\[6pt] K = \begin{pmatrix} 4 & -1 & 0 \\ -1 & 3 & 2 \\ 0 & 2 & 5 \end{pmatrix} \end{gathered}

Both are symmetric. A=(21−13)A = \begin{pmatrix} 2 & 1 \\ -1 & 3 \end{pmatrix} is not: a12=1a_{12} = 1 but a21=−1a_{21} = -1.

Two ways always make a symmetric matrix. M⊤MM^\top M is symmetric for any MM, because (M⊤M)⊤=M⊤(M⊤)⊤=M⊤M(M^\top M)^\top = M^\top (M^\top)^\top = M^\top M by the rule above. And A+A⊤A + A^\top is symmetric for any square AA.

Case study 3.2.5 meets the most important symmetric matrix in finance, the covariance matrix.

Try it

np.array_equal(S, S.T) tests for symmetry: true for SS, false for AA.

M⊤MM^\top M is 3×33 \times 3 and MM⊤MM^\top is 2×22 \times 2; each mirrors itself across its diagonal. So does A+A⊤A + A^\top.

⌘/Ctrl + Enter runs the cell

The dot product as uᵀv

Lesson 3.2.1 found that a row times a column is a single number, their dot product. A column vector u\mathbf{u} with nn components is n×1n \times 1, so u⊤\mathbf{u}^\top is 1×n1 \times n and u⊤v\mathbf{u}^\top\mathbf{v} is 1×11 \times 1.

Rule: The dot product as a matrix product

u⊤v=u⋅vu⊤u=∥u∥2\begin{gathered} \mathbf{u}^\top\mathbf{v} = \mathbf{u} \cdot \mathbf{v} \\[4pt] \mathbf{u}^\top\mathbf{u} = \lVert \mathbf{u} \rVert^2 \end{gathered}

u⊤v=u1v1+⋯+unvn\mathbf{u}^\top\mathbf{v} = u_1v_1 + \cdots + u_nv_n, the dot product of lesson 3.1.2. From here on both notations are used.

For u=(3,1)\mathbf{u} = (3, 1) and v=(1,2)\mathbf{v} = (1, 2), u⊤v=3+2=5\mathbf{u}^\top\mathbf{v} = 3 + 2 = 5, and u⊤u=10\mathbf{u}^\top\mathbf{u} = 10, so the length is ∥u∥=10≈3.162278\lVert \mathbf{u} \rVert = \sqrt{10} \approx 3.162278. The other order, uv⊤\mathbf{u}\mathbf{v}^\top, is (2×1)(1×2)(2 \times 1)(1 \times 2): the 2×22 \times 2 table (3612)\begin{pmatrix} 3 & 6 \\ 1 & 2 \end{pmatrix}, not a number.

A row, a square matrix and a column multiply to a number too: u⊤Sv\mathbf{u}^\top S\mathbf{v} is (1×2)(2×2)(2×1)(1 \times 2)(2 \times 2)(2 \times 1). Here Sv=(4,5)S\mathbf{v} = (4, 5), so u⊤Sv=12+5=17\mathbf{u}^\top S\mathbf{v} = 12 + 5 = 17. Case study 3.2.5 writes a portfolio's variance in exactly this form.

Try it

Here u and v are 2-D columns, so that .T makes a row. u.T @ v prints as a 1×11 \times 1 array.

1-D arrays have no row or column shape. For them a @ b is the dot product directly, and a @ S @ b the number u⊤Sv\mathbf{u}^\top S\mathbf{v}.

⌘/Ctrl + Enter runs the cell

The identity matrix

The identity matrix II is square, with 1 at every place on the main diagonal and 0 everywhere else. I2I_2 and I3I_3 are its 2×22 \times 2 and 3×33 \times 3 versions:

I2=(1001)I3=(100010001)\begin{gathered} I_2 = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \\[6pt] I_3 = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} \end{gathered}

As a map it changes nothing: its columns are e1\mathbf{e}_1 and e2\mathbf{e}_2 themselves, so every vector stays put (lesson 3.2.2). It plays the part that 1 plays for numbers.

Rule: The identity

Ix=xAI=IA=A\begin{gathered} I\mathbf{x} = \mathbf{x} \\[4pt] AI = IA = A \end{gathered}

Multiplying by II on either side leaves a matrix unchanged. For a matrix that is not square, use the identity of the size that fits on each side.

Try it

np.eye(n) builds InI_n. Its entries print as decimals, 1. and 0..

⌘/Ctrl + Enter runs the cell

The inverse

In lesson 3.2.1, A=(21−13)A = \begin{pmatrix} 2 & 1 \\ -1 & 3 \end{pmatrix} sent x=(2,1)\mathbf{x} = (2, 1) to (5,1)(5, 1). Run backwards: which x\mathbf{x} does AA send to (5,1)(5, 1)? The map that answers it for every point is the inverse of AA, written A−1A^{-1}.

Rule: The inverse

AA−1=A−1A=IA A^{-1} = A^{-1} A = I

A−1A^{-1} undoes AA, and AA undoes A−1A^{-1}. Only a square matrix can have an inverse, and not every square matrix has one. The −1-1 is a label, as for an inverse function (lesson 1.1.3): A−1A^{-1} is not 11 divided by each entry.

A map with an inverse, and one without. AA moves the unit square to a parallelogram and A−1A^{-1} brings every point back. ZZ squashes the square onto a segment of one line; three different points land on (2,1)(2, 1), so no map can send it back to just one.

The 2 × 2 formula

For a 2×22 \times 2 matrix the inverse has a formula. Swap the two diagonal entries, change the sign of the other two, and divide by the number ad−bcad - bc.

Rule: Inverse of a 2 × 2 matrix

A=(abcd)A−1=1ad−bc(d−b−ca)\begin{gathered} A = \begin{pmatrix} a & b \\ c & d \end{pmatrix} \\[4pt] A^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix} \end{gathered}

The inverse exists exactly when ad−bc≠0ad - bc \neq 0. Multiplying out shows why the formula works: A(d−b−ca)A\begin{pmatrix} d & -b \\ -c & a \end{pmatrix} is (ad−bc) I(ad - bc)\,I.

Three examples:

  • B=(2111)B = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix} has ad−bc=2−1=1ad - bc = 2 - 1 = 1, so B−1=(1−1−12)B^{-1} = \begin{pmatrix} 1 & -1 \\ -1 & 2 \end{pmatrix}.
  • G=(4213)G = \begin{pmatrix} 4 & 2 \\ 1 & 3 \end{pmatrix} has ad−bc=12−2=10ad - bc = 12 - 2 = 10, so G−1=110(3−2−14)=(0.3−0.2−0.10.4)G^{-1} = \frac{1}{10}\begin{pmatrix} 3 & -2 \\ -1 & 4 \end{pmatrix} = \begin{pmatrix} 0.3 & -0.2 \\ -0.1 & 0.4 \end{pmatrix}.
  • AA has ad−bc=6−(−1)=7ad - bc = 6 - (-1) = 7, so A−1=17(3−112)A^{-1} = \frac{1}{7}\begin{pmatrix} 3 & -1 \\ 1 & 2 \end{pmatrix}. It sends (5,1)(5, 1) to 17(14,7)=(2,1)\frac{1}{7}(14, 7) = (2, 1), the point we started from.

Check BB entry by entry. Row 1 of BB dotted with column 1 of B−1B^{-1} is 2⋅1+1⋅(−1)=12 \cdot 1 + 1 \cdot (-1) = 1, and with column 2 it is 2⋅(−1)+1⋅2=02 \cdot (-1) + 1 \cdot 2 = 0. Row 2 gives 1−1=01 - 1 = 0 and −1+2=1-1 + 2 = 1: together, the identity.

Try it

inverse_2x2 is the formula. For each example the cell prints the inverse, rounded to 4 places, and checks AA−1=IAA^{-1} = I and A−1A=IA^{-1}A = I.

The last line undoes AA: A−1(5,1)=(2,1)A^{-1}(5, 1) = (2, 1).

⌘/Ctrl + Enter runs the cell

Undoing the maps of lesson 3.2.2

Each map in the gallery of lesson 3.2.2 has a natural undo, and the formula agrees with it.

  • Shear (1101)\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}: shear back, (1−101)\begin{pmatrix} 1 & -1 \\ 0 & 1 \end{pmatrix}.
  • Stretch (2000.5)\begin{pmatrix} 2 & 0 \\ 0 & 0.5 \end{pmatrix}: shrink back, (0.5002)\begin{pmatrix} 0.5 & 0 \\ 0 & 2 \end{pmatrix}.
  • Quarter turn (0−110)\begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}: turn back, (01−10)\begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix}. That is also its transpose: for every rotation, R−1=R⊤R^{-1} = R^\top.
  • Reflection (100−1)\begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}: reflect again. It is its own inverse.
  • Projection (1000)\begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}: no undo. Its ad−bcad - bc is 1⋅0−0⋅0=01 \cdot 0 - 0 \cdot 0 = 0.

Undoing two maps

To undo "shear, then turn", undo the turn first and the shear second: the last step is the first one undone. With the shear H=(1101)H = \begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} and the quarter turn R=(0−110)R = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}, RHRH means shear first, so its inverse is H−1R−1H^{-1}R^{-1}.

Rule: Inverse of a product

(AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}

The order reverses, as for the transpose. Both AA and BB must have inverses.

Follow (3,2)(3, 2). The shear sends it to (5,2)(5, 2) and the turn to (−2,5)(-2, 5).

Applying H−1R−1H^{-1}R^{-1} brings (−2,5)(-2, 5) back to (3,2)(3, 2). The wrong order, R−1H−1R^{-1}H^{-1}, sends it to (5,7)(5, 7) instead.

Try it

The first line prints (3,2)(3, 2) after the shear, then y, the point after the shear and the turn. Undoing in the right order gives (3,2)(3, 2) back; the wrong order does not.

np.linalg.inv(R @ H) matches H−1R−1H^{-1}R^{-1}. Adding 0.0 turns a -0. into 0.. The last line confirms R−1=R⊤R^{-1} = R^\top.

⌘/Ctrl + Enter runs the cell

When there is no inverse

A 2×22 \times 2 matrix with ad−bc=0ad - bc = 0 has no inverse. Take

Z=(120.51)Z = \begin{pmatrix} 1 & 2 \\ 0.5 & 1 \end{pmatrix}

with ad−bc=1−1=0ad - bc = 1 - 1 = 0. Its second column, (2,1)(2, 1), is twice its first, (1,0.5)(1, 0.5). So every image Zx=x1(1,0.5)+x2(2,1)Z\mathbf{x} = x_1(1, 0.5) + x_2(2, 1) is a multiple of (1,0.5)(1, 0.5): the whole plane is flattened onto the line x2=x1/2x_2 = x_1/2.

A flattened plane cannot be unflattened. The points (2,0)(2, 0), (0,1)(0, 1), (1,0.5)(1, 0.5) and (4,−1)(4, -1) all land on (2,1)(2, 1), so no map can send (2,1)(2, 1) back to a single start. The vector (2,−1)(2, -1) even lands on (0,0)(0, 0), alongside 0\mathbf{0} itself.

A matrix with no inverse is called singular. Lesson 3.4.1 calls ad−bcad - bc the determinant and reads it as the factor by which the map scales areas, so 0 means flattened. Lesson 3.3.2 finds the same failure in larger matrices.

Try it

The four points on the line x1+2x2=2x_1 + 2x_2 = 2 all land on (2,1)(2, 1), and (2,−1)(2, -1) lands on (0,0)(0, 0).

np.linalg.inv refuses ZZ with a LinAlgError. Not every singular matrix is caught this way: rounding can hide one, as lesson 3.3.2 shows.

⌘/Ctrl + Enter runs the cell

Solving Ax = b in NumPy

To find x\mathbf{x} with Ax=bA\mathbf{x} = \mathbf{b}, multiply both sides by A−1A^{-1}: x=A−1b\mathbf{x} = A^{-1}\mathbf{b}. That is the right idea, but not the right code.

np.linalg.solve(A, b) finds x\mathbf{x} without building A−1A^{-1}. It does less arithmetic, and so it usually makes smaller rounding errors, which matters for large or nearly singular systems. Lesson 3.3.1 shows the method it uses, elimination.

Rule: Solving Ax = b

x=A−1bcomputed by np.linalg.solve\begin{gathered} \mathbf{x} = A^{-1}\mathbf{b} \\[4pt] \text{computed by } \texttt{np.linalg.solve} \end{gathered}

Use np.linalg.inv when you need the inverse matrix itself. To solve a system, use np.linalg.solve.

For AA and b=(5,1)\mathbf{b} = (5, 1), both give (2,1)(2, 1). For the 3×33 \times 3 system Lx=cL\mathbf{x} = \mathbf{c},

(210131014)x=(359)\begin{pmatrix} 2 & 1 & 0 \\ 1 & 3 & 1 \\ 0 & 1 & 4 \end{pmatrix}\mathbf{x} = \begin{pmatrix} 3 \\ 5 \\ 9 \end{pmatrix}

solve gives x≈(1.2222,0.5556,2.1111)\mathbf{x} \approx (1.2222, 0.5556, 2.1111), and multiplying back by LL gives c\mathbf{c}. An exactly singular matrix such as ZZ makes solve raise the same LinAlgError as inv.

Try it

The inverse of AA to 4 places, then A−1bA^{-1}\mathbf{b} and solve side by side.

np.allclose checks that Lx=cL\mathbf{x} = \mathbf{c} and that the two methods agree. The last lines try ZZ.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. The order reverses. (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top and (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}.
  2. A−1A^{-1} is not the reciprocal of each entry. For B=(2111)B = \begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix} the reciprocals give (0.5111)\begin{pmatrix} 0.5 & 1 \\ 1 & 1 \end{pmatrix}, but B−1=(1−1−12)B^{-1} = \begin{pmatrix} 1 & -1 \\ -1 & 2 \end{pmatrix}.
  3. In the 2×22 \times 2 formula, swap aa and dd and change the signs of bb and cc; do not swap bb and cc. Then divide by ad−bcad - bc.
  4. Check ad−bcad - bc first. If it is 0, there is no inverse: the map flattens the plane.
  5. u⊤v\mathbf{u}^\top\mathbf{v} is a number; uv⊤\mathbf{u}\mathbf{v}^\top is a matrix.
  6. Only square matrices have inverses, and in code, solve systems with np.linalg.solve rather than inv.

Transpose, identity and inverse

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⊤A^\top swaps rows and columns, (A⊤)ij=aji(A^\top)_{ij} = a_{ji}; (AB)⊤=B⊤A⊤(AB)^\top = B^\top A^\top.
  • AA is symmetric when A⊤=AA^\top = A. M⊤MM^\top M and A+A⊤A + A^\top are always symmetric.
  • u⊤v=u⋅v\mathbf{u}^\top\mathbf{v} = \mathbf{u} \cdot \mathbf{v} and u⊤u=∥u∥2\mathbf{u}^\top\mathbf{u} = \lVert \mathbf{u} \rVert^2; uv⊤\mathbf{u}\mathbf{v}^\top is a matrix.
  • II has 1 on the diagonal and 0 elsewhere: Ix=xI\mathbf{x} = \mathbf{x} and AI=IA=AAI = IA = A.
  • AA−1=A−1A=IAA^{-1} = A^{-1}A = I. For 2×22 \times 2: A−1=1ad−bc(d−b−ca)A^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}, which exists exactly when ad−bc≠0ad - bc \neq 0. (AB)−1=B−1A−1(AB)^{-1} = B^{-1}A^{-1}.
  • ad−bc=0ad - bc = 0 means the map flattens the plane and cannot be undone: the matrix is singular.
  • Solve Ax=bA\mathbf{x} = \mathbf{b} with np.linalg.solve, not by computing np.linalg.inv.

Free further reading

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