Functions of random variables
Functions of random variables (lesson 5.6.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.
Moment generating and characteristic functions
Moment generating and characteristic functions (lesson 5.6.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.
Sums of independent random variables
Sums of independent random variables (lesson 5.6.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 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 .
pmf_of_sum(p, q)returns the pmf of for independent with pmfpand with pmfq, by convolution. It haslen(p) + len(q) - 1entries.pmf_of_n_sum(p, n)returns the pmf of the sum ofnindependent copies. Withn = 1it returnspitself.- Raise
ValueErrorfor an empty list, a negative entry, or entries that do not add up to 1 within .pmf_of_n_sumalso raises it fornbelow 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}")