SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.3.1 Expectation and LOTUSConcept · 30 min
      2. 5.3.2 Variance, and linearity of expectationConcept · 35 min
      3. 5.3.3 Skewness and kurtosisConcept · 30 min
      4. 5.3.4 Practice set: expectation and momentsPractice set · 20 min
      5. 5.3.5 Case study: return moments and value at riskCase study · 35 min

Level 5, Probability and statistics 5.3 Expectation and moments Lesson 4 of 5

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

Practice set: expectation and moments

Expectation and LOTUS

Expectation and LOTUS (lesson 5.3.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.

Variance and linearity

Variance and linearity (lesson 5.3.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.

Skewness and kurtosis

Skewness and kurtosis (lesson 5.3.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 four moments of a discrete distribution

Write moments(values, probs). It takes the values of a discrete random variable and their probabilities, as lists or NumPy arrays. It returns a tuple (mean, variance, skewness, excess_kurtosis), as in lessons 5.3.1 to 5.3.3.

  • Use LOTUS for every moment: the mean, the variance E[(X−μ)2]\mathbb{E}[(X - \mu)^2], and E[Z3]\mathbb{E}[Z^3] and E[Z4]−3\mathbb{E}[Z^4] - 3 with Z=X−μσZ = \frac{X - \mu}{\sigma}.
  • Raise ValueError if the two inputs have different lengths or are empty, or if any probability is negative. Also raise it if the probabilities do not add up to 1, allowing 10−910^{-9} for rounding.
  • Raise ValueError if the variance is 0, because then ZZ is 00\frac{0}{0}.

The tests use a die, a Bernoulli variable, lesson 5.3.3's family with kurtosis 10, a shifted and scaled die, and inputs that must be refused.

See the tests
import math


def _raises(values, probs):
    try:
        moments(values, probs)
    except ValueError:
        return True
    return False


def test_die():
    """A fair die has mean 3.5, variance 35/12, skewness 0 and excess kurtosis about -1.2686"""
    expect_close(moments([1, 2, 3, 4, 5, 6], [1 / 6] * 6), (3.5, 35 / 12, 0.0, -1.2685714285714285), what="moments(die)", rel=1e-9, abs_tol=1e-12)


def test_bernoulli():
    """A Bernoulli variable with p = 0.2 has skewness 1.5 and excess kurtosis 0.25"""
    expect_close(moments([0, 1], [0.8, 0.2]), (0.2, 0.16, 1.5, 0.25), what="moments([0, 1], [0.8, 0.2])", rel=1e-9, abs_tol=1e-12)


def test_heavy_tails():
    """0 with chance 0.9 and ±√10 with chance 0.05 each: variance 1, excess kurtosis 7"""
    a = math.sqrt(10)
    expect_close(moments([-a, 0, a], [0.05, 0.9, 0.05]), (0.0, 1.0, 0.0, 7.0), what="moments of lesson 5.3.3's family with q = 0.1", rel=1e-9, abs_tol=1e-12)


def test_shift_and_scale():
    """10X + 3 for a die: mean 38, variance 100 times 35/12, the same skewness and kurtosis"""
    expect_close(moments([13, 23, 33, 43, 53, 63], [1 / 6] * 6), (38.0, 3500 / 12, 0.0, -1.2685714285714285), what="moments(10X + 3)", rel=1e-9, abs_tol=1e-9)


def test_mirror():
    """-X flips the sign of the skewness and keeps the kurtosis: 0 or -1 with chances 0.8 and 0.2"""
    expect_close(moments([0, -1], [0.8, 0.2]), (-0.2, 0.16, -1.5, 0.25), what="moments([0, -1], [0.8, 0.2])", rel=1e-9, abs_tol=1e-12)


def test_bad_probabilities():
    """Probabilities that add up to 0.9, or include a negative one, raise ValueError"""
    assert _raises([1, 2, 3], [0.3, 0.3, 0.3]), "probabilities adding up to 0.9 should raise ValueError"
    assert _raises([1, 2, 3], [1.2, -0.1, -0.1]), "a negative probability should raise ValueError"


def test_bad_shapes():
    """Lists of different lengths, or empty lists, raise ValueError"""
    assert _raises([1, 2, 3], [0.5, 0.5]), "three values and two probabilities should raise ValueError"
    assert _raises([], []), "empty inputs should raise ValueError"


def test_no_spread():
    """A constant has variance 0, so its skewness is undefined: ValueError"""
    assert _raises([4, 4], [0.5, 0.5]), "a variable that is always 4 should raise ValueError"

Free further reading

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