SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.4.1 Determinants as volume scalingConcept · 35 min
      2. 3.4.2 Practice set: determinantsPractice set · 20 min
      3. 3.4.3 Case study: redundant assets in a payoff matrixCase study · 30 min

Level 3, Linear algebra 3.4 Determinants Lesson 2 of 3

Practice setLesson 3.4.2 in Level 3, Linear algebra. About 20 minutes.

Practice set: determinants

Determinants

Determinants as volume scaling (lesson 3.4.1)

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

A determinant by cofactor expansion

Write det(A), the determinant of a square matrix A given as a list of rows, each a list of numbers. Expand along row 1, as in lesson 3.4.1:

  • A 1×11 \times 1 matrix [[a]] has determinant a.
  • Otherwise, for each column jj, delete row 1 and column jj, and find the determinant of what is left with det itself. Add up the entries of row 1 times these minors, with signs +,−,+,−,…+, -, +, -, \ldots
  • Raise ValueError if A is empty or not square.

The tests use the matrices of lesson 3.4.1, a row swap, a 4×44 \times 4 matrix and a triangular one. With whole-number entries your answer is exact: for lesson 3.4.1's SS it must be exactly 0, where NumPy prints 7.5×10−157.5 \times 10^{-15}.

The work grows very fast with the size: a 10×1010 \times 10 matrix has 3,628,800 terms. That is why NumPy eliminates instead.

See the tests
import numpy as np


def test_two_by_two():
    """det([[3, 1], [1, 2]]) is 3·2 − 1·1 = 5"""
    expect_equal(det([[3, 1], [1, 2]]), 5, what="det([[3, 1], [1, 2]])")


def test_one_by_one():
    """A 1 × 1 matrix [[7]] has determinant 7"""
    expect_equal(det([[7]]), 7, what="det([[7]])")


def test_three_by_three():
    """Lesson 3.4.1's C = [[2, 1, 3], [0, 4, 1], [1, 0, 2]] has determinant 5"""
    expect_equal(det([[2, 1, 3], [0, 4, 1], [1, 0, 2]]), 5, what="det(C)")


def test_box():
    """Lesson 3.4.1's box B = [[2, 1, 1], [0, 2, 1], [0, 0, 2]] has volume 8"""
    expect_equal(det([[2, 1, 1], [0, 2, 1], [0, 0, 2]]), 8, what="det(B)")


def test_row_swap():
    """Swapping rows 1 and 3 of C flips the sign: −5"""
    expect_equal(det([[1, 0, 2], [0, 4, 1], [2, 1, 3]]), -5, what="det(C with rows 1 and 3 swapped)")


def test_singular_exact():
    """Lesson 3.4.1's S = [[1, 3, 4], [3, 1, 4], [1, 6, 7]] gives exactly 0"""
    expect_equal(det([[1, 3, 4], [3, 1, 4], [1, 6, 7]]), 0, what="det(S), which NumPy prints as 7.5e-15")


def test_four_by_four():
    """A 4 × 4 matrix matches NumPy: [[1, 2, 0, 1], [0, 3, 1, 2], [2, 0, 1, 1], [1, 1, 0, 3]] gives 17"""
    A = [[1, 2, 0, 1], [0, 3, 1, 2], [2, 0, 1, 1], [1, 1, 0, 3]]
    expect_close(det(A), float(np.linalg.det(np.array(A, dtype=float))), what="det of the 4 × 4 matrix", rel=1e-9)
    expect_equal(det(A), 17, what="det of the 4 × 4 matrix")


def test_triangular():
    """A triangular 4 × 4 matrix with diagonal 2, −1, 3, 0.5 gives their product, −3"""
    A = [[2, 2, 3, 4], [0, -1, 7, 8], [0, 0, 3, 12], [0, 0, 0, 0.5]]
    expect_close(det(A), -3.0, what="det of the triangular matrix")


def test_not_square():
    """A 2 × 3 matrix has no determinant: ValueError"""
    try:
        det([[1, 2, 3], [4, 5, 6]])
    except ValueError:
        return
    raise AssertionError("expected ValueError for a 2 × 3 matrix")


def test_ragged():
    """Rows of different lengths, [[1, 2], [3]], are not a square matrix: ValueError"""
    try:
        det([[1, 2], [3]])
    except ValueError:
        return
    except IndexError:
        raise AssertionError("expected ValueError for rows of different lengths, got IndexError")
    raise AssertionError("expected ValueError for rows of different lengths")


def test_empty():
    """An empty matrix [] has no determinant: ValueError"""
    try:
        det([])
    except ValueError:
        return
    raise AssertionError("expected ValueError for an empty matrix")

Free further reading

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