SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.6.1 Functions of random variablesConcept · 35 min
      2. 5.6.2 Moment generating and characteristic functionsConcept · 30 min
      3. 5.6.3 Sums of independent random variablesConcept · 25 min
      4. 5.6.4 Practice set: transformationsPractice set · 20 min
      5. 5.6.5 Case study: the lognormal from the normalCase study · 30 min

Level 5, Probability and statistics 5.6 Transformations and generating functions Lesson 4 of 5

Practice setLesson 5.6.4 in Level 5, Probability and statistics. About 20 minutes.

Practice set: transformations

Functions of random variables

Functions of random variables (lesson 5.6.1)

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.

Moment generating and characteristic functions

Moment generating and characteristic functions (lesson 5.6.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.

Sums of independent random variables

Sums of independent random variables (lesson 5.6.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.

Coding exercise

Exercise

The pmf of a sum

Write pmf_of_sum(p, q) and pmf_of_n_sum(p, n), as in lesson 5.6.3. A pmf is a list of probabilities for the values 0, 1, 2, …: p[k] is P(X=k)P(X = k).

  • pmf_of_sum(p, q) returns the pmf of X+YX + Y for independent XX with pmf p and YY with pmf q, by convolution. It has len(p) + len(q) - 1 entries.
  • pmf_of_n_sum(p, n) returns the pmf of the sum of n independent copies. With n = 1 it returns p itself.
  • Raise ValueError for an empty list, a negative entry, or entries that do not add up to 1 within 10−910^{-9}. pmf_of_n_sum also raises it for n below 1.

A die's pmf is [0, 1/6, …, 1/6], since it never shows 0. The tests try two and three dice, a coin and a die, Poisson counts, and the binomial as a sum of coins.

See the tests
import math

DIE = [0] + [1 / 6] * 6


def _poisson(lam, n):
    return [math.exp(-lam) * lam**k / math.factorial(k) for k in range(n)]


def test_two_dice():
    """Two dice: 13 entries (sums 0 to 12), and the sum is 7 with probability 6/36"""
    got = list(pmf_of_sum(DIE, DIE))
    expect_equal(len(got), 13, what="number of entries for two dice")
    expect_close(got[7], 6 / 36, what="P(sum = 7)")
    expect_close(got[2], 1 / 36, what="P(sum = 2)")
    expect_close(got[1], 0.0, what="P(sum = 1)")


def test_coin_and_die():
    """A fair coin (0 or 1) plus a die: P(6) = 1/6 and P(7) = 1/12"""
    got = list(pmf_of_sum([0.5, 0.5], DIE))
    expect_close(got[6], 1 / 6, what="P(coin + die = 6)")
    expect_close(got[7], 1 / 12, what="P(coin + die = 7)")


def test_adds_to_one():
    """The pmf of a sum adds up to 1"""
    got = pmf_of_sum([0.2, 0.5, 0.3], [0.6, 0.1, 0.1, 0.2])
    expect_close(math.fsum(got), 1.0, what="total probability")
    expect_close(got[0], 0.12, what="P(sum = 0)")


def test_poisson():
    """Poisson counts with rates 1 and 2 add to a Poisson with rate 3"""
    got = list(pmf_of_sum(_poisson(1, 40), _poisson(2, 40)))
    want = _poisson(3, 15)
    expect_close(got[:15], want, what="first 15 entries against the rate-3 Poisson", rel=1e-9, abs_tol=1e-12)


def test_three_dice():
    """Three dice: the sum is 10 with probability 27/216 and 3 with probability 1/216"""
    got = list(pmf_of_n_sum(DIE, 3))
    expect_equal(len(got), 19, what="number of entries for three dice")
    expect_close(got[10], 27 / 216, what="P(sum of three dice = 10)")
    expect_close(got[3], 1 / 216, what="P(sum of three dice = 3)")


def test_binomial():
    """Ten coins with p = 0.3 give the binomial: P(3) = 0.266828"""
    got = list(pmf_of_n_sum([0.7, 0.3], 10))
    want = [math.comb(10, k) * 0.3**k * 0.7 ** (10 - k) for k in range(11)]
    expect_close(got, want, what="pmf of the sum of 10 coins", rel=1e-9, abs_tol=1e-15)


def test_one_copy():
    """With n = 1, pmf_of_n_sum returns the pmf itself"""
    expect_close(list(pmf_of_n_sum([0.25, 0.75], 1)), [0.25, 0.75], what="pmf_of_n_sum(p, 1)")


def test_bad_inputs():
    """Negative entries, totals other than 1, an empty list and n = 0 raise ValueError"""
    cases = [
        ("a negative entry", lambda: pmf_of_sum([1.2, -0.2], [1.0])),
        ("entries adding up to 0.9", lambda: pmf_of_sum([0.5, 0.4], [1.0])),
        ("an empty list", lambda: pmf_of_sum([], [1.0])),
        ("n = 0", lambda: pmf_of_n_sum([0.5, 0.5], 0)),
    ]
    for name, call in cases:
        try:
            call()
        except ValueError:
            continue
        raise AssertionError(f"expected ValueError for {name}")

Free further reading

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