SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.1.1 Sample spaces, events and the axioms of probabilityConcept · 30 min
      2. 5.1.2 CountingConcept · 30 min
      3. 5.1.3 Conditional probability and independenceConcept · 30 min
      4. 5.1.4 Bayes' ruleConcept · 25 min
      5. 5.1.5 Practice set: probabilityPractice set · 25 min
      6. 5.1.6 Case study: default and rating-migration probabilitiesCase study · 35 min

Level 5, Probability and statistics 5.1 Probability basics Lesson 5 of 6

Practice setLesson 5.1.5 in Level 5, Probability and statistics. About 25 minutes.

Practice set: probability

Sample spaces, events and axioms

Sample spaces, events and the axioms (lesson 5.1.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.

Counting

Counting (lesson 5.1.2)

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.

Conditional probability and independence

Conditional probability and independence (lesson 5.1.3)

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.

Bayes' rule

Bayes' rule (lesson 5.1.4)

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.

Coding exercise

Exercise

Exactly k special items in a draw

Lesson 5.1.2 found the chance of exactly kk special items when nn items are drawn from NN, of which KK are special: (Kk)(N−Kn−k)\binom{K}{k}\binom{N - K}{n - k} over (Nn)\binom{N}{n}. Write two functions.

  1. draw_exactly(N, K, n, k) returns that probability as a float. If kk cannot happen, for example more aces than cards dealt, return 0.0. Note that math.comb raises ValueError for a negative argument, so check the range first.
  2. draw_at_least(N, K, n, k) returns the chance of kk or more special items. It is 1 when k≤0k \le 0.

Both raise ValueError if NN, KK or nn is negative, or if K>NK > N or n>Nn > N.

The tests use two aces in a hand (0.0399) and the lottery (0.01765 for exactly 3 matches, 0.01864 for at least 3). They also try impossible draws, and check that the probabilities for k=0,…,nk = 0, \ldots, n add up to 1.

See the tests
def _raises(f, *args, what=""):
    try:
        f(*args)
    except ValueError:
        return
    raise AssertionError(f"expected ValueError for {what}")


def test_two_aces():
    """Exactly two aces in a 5-card hand has probability about 0.0399"""
    expect_close(draw_exactly(52, 4, 5, 2), 0.03992981808107859, what="draw_exactly(52, 4, 5, 2)", rel=1e-9)


def test_lottery():
    """Matching exactly 3 of 6 numbers drawn from 49 has probability about 0.01765"""
    expect_close(draw_exactly(49, 6, 6, 3), 0.017650403866870102, what="draw_exactly(49, 6, 6, 3)", rel=1e-9)


def test_all_hearts():
    """Five hearts in a 5-card hand has probability about 0.000495"""
    expect_close(draw_exactly(52, 13, 5, 5), 0.0004951980792316927, what="draw_exactly(52, 13, 5, 5)", rel=1e-9)


def test_impossible_draws():
    """Impossible counts give 0: five aces, 3 aces in 2 cards, too few others, or k = -1"""
    expect_close(draw_exactly(52, 4, 5, 5), 0.0, what="five aces in a hand")
    expect_close(draw_exactly(52, 4, 2, 3), 0.0, what="three aces in two cards")
    expect_close(draw_exactly(10, 8, 5, 2), 0.0, what="2 special of 5 when only 2 others exist")
    expect_close(draw_exactly(52, 4, 5, -1), 0.0, what="k = -1")


def test_adds_up_to_one():
    """The chances of k = 0, 1, ..., n special items add up to 1"""
    total = sum(draw_exactly(49, 6, 6, k) for k in range(7))
    expect_close(total, 1.0, what="sum over k for the lottery", rel=1e-12)
    total = sum(draw_exactly(10, 6, 3, k) for k in range(4))
    expect_close(total, 1.0, what="sum over k for a committee of 3 from 6 juniors and 4 seniors", rel=1e-12)


def test_at_least():
    """At least 3 lottery matches is about 0.01864; at least one ace in a hand about 0.3412"""
    expect_close(draw_at_least(49, 6, 6, 3), 0.01863754500202234, what="draw_at_least(49, 6, 6, 3)", rel=1e-9)
    expect_close(draw_at_least(52, 4, 5, 1), 0.34115800166220334, what="draw_at_least(52, 4, 5, 1)", rel=1e-9)


def test_at_least_edges():
    """At least 0 is certain, and at least 5 aces in a hand is impossible"""
    expect_close(draw_at_least(52, 4, 5, 0), 1.0, what="draw_at_least(52, 4, 5, 0)")
    expect_close(draw_at_least(52, 4, 5, 5), 0.0, what="draw_at_least(52, 4, 5, 5)")


def test_bad_inputs():
    """Drawing more than N, more special items than N, or a negative size raises ValueError"""
    _raises(draw_exactly, 10, 4, 11, 2, what="n > N")
    _raises(draw_exactly, 10, 12, 3, 2, what="K > N")
    _raises(draw_exactly, 10, -1, 3, 0, what="K < 0")
    _raises(draw_at_least, 10, 4, 11, 2, what="n > N in draw_at_least")

Free further reading

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