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

Practice setLesson 3.2.4 in Level 3, Linear algebra. About 25 minutes.

Practice set: matrices

Matrices and their products

Matrices and matrix multiplication (lesson 3.2.1)

0 of 6 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
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.

Matrices as maps

Matrices as linear maps (lesson 3.2.2)

0 of 7 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
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.

Transpose, identity and inverse

Transpose, identity and inverse (lesson 3.2.3)

0 of 8 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
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.

Coding exercise

Exercise

Matrix multiplication and the 2 × 2 inverse

Write two functions. Matrices are lists of rows, so (21−13)\begin{pmatrix} 2 & 1 \\ -1 & 3 \end{pmatrix} is [[2, 1], [-1, 3]].

  1. matmul(A, B) returns ABAB as a list of rows, built from the rule of lesson 3.2.1: entry (i,j)(i, j) is row ii of A dotted with column jj of B. Do not use NumPy's @. Raise ValueError if the rows of A are not as long as the columns of B.
  2. inverse_2x2(A) returns the inverse of a 2×22 \times 2 matrix by the formula of lesson 3.2.3. Raise ValueError if ad−bc=0ad - bc = 0.

The tests try square and rectangular matrices, both orders of the same pair, a row times a column, and a pair whose shapes do not match. For the inverse they try whole numbers, decimals, a rotation and two matrices with no inverse.

See the tests
import math

import numpy as np


def test_square():
    """[[2, 1], [−1, 3]] times [[1, 0], [2, 1]] is [[4, 1], [5, 3]]"""
    expect_close(matmul([[2, 1], [-1, 3]], [[1, 0], [2, 1]]), [[4, 1], [5, 3]], what="matmul(A, C)")


def test_order_matters():
    """The same two matrices in the other order give [[2, 1], [3, 5]]"""
    expect_close(matmul([[1, 0], [2, 1]], [[2, 1], [-1, 3]]), [[2, 1], [3, 5]], what="matmul(C, A)")


def test_rectangular():
    """A 2 × 3 matrix times a 3 × 2 matrix gives a 2 × 2 matrix"""
    expect_close(matmul([[1, 2, 0], [3, -1, 4]], [[1, 0], [2, 1], [0, 3]]), [[5, 2], [1, 11]], what="matmul(M, N)")


def test_other_shape():
    """The same pair the other way round gives a 3 × 3 matrix"""
    expected = [[1, 2, 0], [5, 3, 4], [9, -3, 12]]
    expect_close(matmul([[1, 0], [2, 1], [0, 3]], [[1, 2, 0], [3, -1, 4]]), expected, what="matmul(N, M)")


def test_column():
    """A matrix times a column: 6 chairs and 2 tables need 32 legs, 72 screws and 12 boards"""
    expect_close(matmul([[4, 4], [8, 12], [1, 3]], [[6], [2]]), [[32], [72], [12]], what="matmul(P, [[6], [2]])")


def test_row_times_column():
    """A row times a column is a 1 × 1 matrix, the dot product: [[5]]"""
    expect_close(matmul([[3, 1]], [[1], [2]]), [[5]], what="matmul([[3, 1]], [[1], [2]])")


def test_matches_numpy():
    """Random 4 × 3 and 3 × 5 matrices agree with NumPy's @"""
    rng = np.random.default_rng(3)
    A = rng.integers(-5, 6, size=(4, 3))
    B = rng.integers(-5, 6, size=(3, 5))
    expect_close(matmul(A.tolist(), B.tolist()), A @ B, what="matmul of a 4 × 3 and a 3 × 5 matrix")


def test_shape_mismatch():
    """Two 2 × 3 matrices cannot be multiplied: ValueError"""
    try:
        matmul([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]])
    except ValueError:
        return
    raise AssertionError("expected ValueError: rows of length 3 cannot meet columns of length 2")


def test_inverse_whole_numbers():
    """The inverse of [[2, 1], [1, 1]] is [[1, −1], [−1, 2]]"""
    expect_close(inverse_2x2([[2, 1], [1, 1]]), [[1, -1], [-1, 2]], what="inverse_2x2([[2, 1], [1, 1]])")


def test_inverse_decimals():
    """The inverse of [[4, 2], [1, 3]] is [[0.3, −0.2], [−0.1, 0.4]]"""
    expect_close(inverse_2x2([[4, 2], [1, 3]]), [[0.3, -0.2], [-0.1, 0.4]], what="inverse_2x2([[4, 2], [1, 3]])")


def test_inverse_undoes():
    """[[2, 1], [−1, 3]] times its inverse is the identity, in both orders"""
    A = np.array([[2, 1], [-1, 3]])
    inv = np.asarray(inverse_2x2([[2, 1], [-1, 3]]), dtype=float)
    expect_close(A @ inv, np.eye(2), what="A times your inverse")
    expect_close(inv @ A, np.eye(2), what="your inverse times A")


def test_rotation():
    """The inverse of the 30° rotation is the rotation by −30°"""
    t = math.radians(30)
    R = [[math.cos(t), -math.sin(t)], [math.sin(t), math.cos(t)]]
    back = [[math.cos(t), math.sin(t)], [-math.sin(t), math.cos(t)]]
    expect_close(inverse_2x2(R), back, what="inverse of the 30° rotation")


def test_singular():
    """[[1, 2], [0.5, 1]] has ad − bc = 0, so it has no inverse: ValueError"""
    try:
        inverse_2x2([[1, 2], [0.5, 1]])
    except ValueError:
        return
    raise AssertionError("expected ValueError: ad − bc = 1 − 2 × 0.5 = 0")


def test_zero_matrix():
    """The zero matrix has no inverse: ValueError"""
    try:
        inverse_2x2([[0, 0], [0, 0]])
    except ValueError:
        return
    raise AssertionError("expected ValueError for the zero matrix")

Free further reading

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