Expectation and LOTUS
Expectation and LOTUS (lesson 5.3.1)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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.
- 2.
- 3.
- 4.
- 5.
- 6.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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.
- 2.
- 3.
- 4.
- 5.
- 6.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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
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 , and and with .
- Raise
ValueErrorif 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 for rounding. - Raise
ValueErrorif the variance is 0, because then is .
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"