Random variables, PMFs and CDFs
Random variables, PMFs and CDFs (lesson 5.2.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.
Bernoulli, binomial and geometric distributions
Bernoulli, binomial and geometric distributions (lesson 5.2.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.
The Poisson distribution
The Poisson distribution (lesson 5.2.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
Binomial and Poisson probabilities
Write three functions with the math module only. The tests compare them with scipy.stats.
binomial_pmf(k, n, p)returns (lesson 5.2.2).poisson_pmf(k, lam)returns (lesson 5.2.3).cdf(pmf, x)returns for a random variable with whole-number values from 0 up, whose pmf is the functionpmf(lesson 5.2.1). It addspmf(k)for up to , and works for any number .
The rules, as in the lessons:
- A pmf is 0 at any the variable cannot take. That means a negative , a that is not whole, such as 2.5, and for the binomial a above .
- Raise
ValueErrorif is outside , if is not a whole number from 0 up, or if . Both and are allowed. - Some tests use large numbers, such as and , which are too big for a float. Compute the logarithm of the pmf first and take
math.expat the end;math.lgamma(k + 1)is .
See the tests
import math
def test_binomial_value():
"""Bin(10, 0.3) gives P(X = 3) = 0.2668"""
expect_close(binomial_pmf(3, 10, 0.3), 0.2668279319999998, what="binomial_pmf(3, 10, 0.3)", rel=1e-9)
def test_binomial_sums_to_one():
"""The pmf of Bin(12, 0.35) adds up to 1 over k = 0 to 12"""
expect_close(sum(binomial_pmf(k, 12, 0.35) for k in range(13)), 1.0, what="sum of the pmf", rel=1e-9)
def test_binomial_outside():
"""Bin(5, 0.5) is 0 at k = −1, 6 and 2.5"""
for k in (-1, 6, 2.5):
expect_close(binomial_pmf(k, 5, 0.5), 0.0, what=f"binomial_pmf({k}, 5, 0.5)")
def test_binomial_certain():
"""With p = 0 there are no successes; with p = 1 every trial succeeds"""
expect_close(binomial_pmf(0, 5, 0), 1.0, what="binomial_pmf(0, 5, 0)")
expect_close(binomial_pmf(2, 5, 0), 0.0, what="binomial_pmf(2, 5, 0)")
expect_close(binomial_pmf(5, 5, 1), 1.0, what="binomial_pmf(5, 5, 1)")
expect_close(binomial_pmf(2, 5, 1), 0.0, what="binomial_pmf(2, 5, 1)")
def test_binomial_large():
"""Bin(2000, 0.5) gives P(X = 1000) = 0.01784, though C(2000, 1000) is too big for a float"""
expect_close(binomial_pmf(1000, 2000, 0.5), 0.017839011145854313, what="binomial_pmf(1000, 2000, 0.5)", rel=1e-9)
def test_binomial_bad_inputs():
"""p = 1.5, p = −0.1 and n = 2.5 raise ValueError"""
for args in ((2, 5, 1.5), (2, 5, -0.1), (2, 2.5, 0.5)):
try:
binomial_pmf(*args)
except ValueError:
continue
raise AssertionError(f"expected ValueError for binomial_pmf{args}")
def test_poisson_values():
"""Poisson(2) gives P(X = 2) = 2e⁻² and Poisson(3) gives P(X = 0) = e⁻³"""
expect_close(poisson_pmf(2, 2), 2 * math.exp(-2), what="poisson_pmf(2, 2)", rel=1e-9)
expect_close(poisson_pmf(0, 3), math.exp(-3), what="poisson_pmf(0, 3)", rel=1e-9)
def test_poisson_outside():
"""Poisson(2) is 0 at k = −1 and 1.5"""
expect_close(poisson_pmf(-1, 2), 0.0, what="poisson_pmf(-1, 2)")
expect_close(poisson_pmf(1.5, 2), 0.0, what="poisson_pmf(1.5, 2)")
def test_poisson_large():
"""Poisson(150) gives P(X = 200) = 1.5038e-05, though 150²⁰⁰ is too big for a float"""
expect_close(poisson_pmf(200, 150), 1.5038027878284683e-05, what="poisson_pmf(200, 150)", rel=1e-8)
def test_poisson_bad_rate():
"""A rate of 0 or below raises ValueError"""
for lam in (0, -1):
try:
poisson_pmf(1, lam)
except ValueError:
continue
raise AssertionError(f"expected ValueError for poisson_pmf(1, {lam})")
def test_cdf_binomial():
"""For Bin(10, 0.3), F(2) = F(2.5) = 0.3828, F(−0.5) = 0 and F(15) = 1"""
pmf = lambda k: binomial_pmf(k, 10, 0.3)
expect_close(cdf(pmf, 2), 0.3827827863999998, what="cdf at 2", rel=1e-9)
expect_close(cdf(pmf, 2.5), 0.3827827863999998, what="cdf at 2.5", rel=1e-9)
expect_close(cdf(pmf, -0.5), 0.0, what="cdf at -0.5")
expect_close(cdf(pmf, 15), 1.0, what="cdf at 15", rel=1e-9)
def test_cdf_interval():
"""For Poisson(2), P(2 < X ≤ 5) = F(5) − F(2) = 0.3068"""
pmf = lambda k: poisson_pmf(k, 2)
expect_close(cdf(pmf, 5) - cdf(pmf, 2), 0.30675997533632215, what="F(5) - F(2)", rel=1e-9)
def test_cdf_geometric():
"""Rolls until a six (values from 1): F(6) = 1 − (5/6)⁶"""
pmf = lambda k: (5 / 6) ** (k - 1) / 6 if k >= 1 else 0.0
expect_close(cdf(pmf, 6), 1 - (5 / 6) ** 6, what="cdf at 6", rel=1e-9)