The Markov, Chebyshev and Jensen inequalities
The Markov, Chebyshev and Jensen inequalities (lesson 5.7.1)
0 of 9 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
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 law of large numbers
The law of large numbers (lesson 5.7.2)
0 of 8 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
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 central limit theorem
The central limit theorem (lesson 5.7.3)
0 of 8 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
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
How many draws?
Write draws_needed(sigma, eps, delta). It returns the pair (n_chebyshev, n_normal). Each is a number of draws that keeps at or below .
Here = sigma is the standard deviation of one draw, = eps and = delta.
n_chebyshevis the smallest whole with (lesson 5.7.2): a guarantee for every distribution.n_normalis the smallest whole with , where isnorm.ppf(1 - delta / 2)(lesson 5.7.3): the central limit theorem's answer.- Both are at least 1; with
sigma = 0one draw is enough. Usemath.ceilto round up. - Raise
ValueErrorifsigmais negative,epsis 0 or below, ordeltais not strictly between 0 and 1.
For a die and eps = 0.1, delta = 0.05, the answers are 5,834 and 1,121, as in lessons 5.7.2 and 5.7.3.
See the tests
import math
def test_die():
"""A die (sigma² = 35/12), within 0.1 with probability 0.95: 5834 rolls by Chebyshev, 1121 by the normal"""
got = draws_needed(math.sqrt(35 / 12), 0.1, 0.05)
expect_equal(tuple(got), (5834, 1121), what="draws_needed(sqrt(35/12), 0.1, 0.05)")
def test_share():
"""A fair coin's share (sigma = 0.5) within 0.015 with probability 0.95: 22223 and 4269 tosses"""
got = draws_needed(0.5, 0.015, 0.05)
expect_equal(tuple(got), (22223, 4269), what="draws_needed(0.5, 0.015, 0.05)")
def test_other_delta():
"""sigma = 2, eps = 0.3, delta = 0.1: the normal uses its 0.95-quantile 1.6449, giving 445 and 121"""
got = draws_needed(2, 0.3, 0.1)
expect_equal(tuple(got), (445, 121), what="draws_needed(2, 0.3, 0.1)")
def test_hundred_times():
"""One more digit (eps divided by 10) needs about 100 times the draws"""
n1 = draws_needed(1.5, 0.05, 0.05)[1]
n2 = draws_needed(1.5, 0.005, 0.05)[1]
expect_close(n2 / n1, 100, what="ratio of draws for eps = 0.005 and eps = 0.05", rel=0.01)
def test_chebyshev_is_cautious():
"""Chebyshev's guarantee always asks for at least as many draws as the normal"""
for delta in [0.5, 0.1, 0.05, 0.01, 0.001]:
cheb, normal = draws_needed(1.0, 0.01, delta)
assert cheb >= normal, f"with delta = {delta}, Chebyshev gave {cheb} but the normal gave {normal}"
def test_no_spread():
"""With sigma = 0 every draw is mu, so one draw is enough: (1, 1)"""
expect_equal(tuple(draws_needed(0, 0.1, 0.05)), (1, 1), what="draws_needed(0, 0.1, 0.05)")
def test_whole_numbers():
"""Both answers are whole numbers of draws"""
for n in draws_needed(3.0, 0.2, 0.05):
assert isinstance(n, int), f"expected whole numbers (int), got {n!r} of type {type(n).__name__}"
def test_bad_inputs():
"""A negative sigma, eps of 0 or below, or delta outside (0, 1) raises ValueError"""
cases = [
("sigma = -1", lambda: draws_needed(-1, 0.1, 0.05)),
("eps = 0", lambda: draws_needed(1, 0, 0.05)),
("eps = -0.1", lambda: draws_needed(1, -0.1, 0.05)),
("delta = 0", lambda: draws_needed(1, 0.1, 0)),
("delta = 1", lambda: draws_needed(1, 0.1, 1)),
("delta = 1.5", lambda: draws_needed(1, 0.1, 1.5)),
]
for name, call in cases:
try:
call()
except ValueError:
continue
raise AssertionError(f"expected ValueError for {name}")