Sample spaces, events and axioms
Sample spaces, events and the axioms (lesson 5.1.1)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
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.
Counting
Counting (lesson 5.1.2)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
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.
Conditional probability and independence
Conditional probability and independence (lesson 5.1.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.
Bayes' rule
Bayes' rule (lesson 5.1.4)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
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
Exactly k special items in a draw
Lesson 5.1.2 found the chance of exactly special items when items are drawn from , of which are special: over . Write two functions.
draw_exactly(N, K, n, k)returns that probability as a float. If cannot happen, for example more aces than cards dealt, return 0.0. Note thatmath.combraisesValueErrorfor a negative argument, so check the range first.draw_at_least(N, K, n, k)returns the chance of or more special items. It is 1 when .
Both raise ValueError if , or is negative, or if or .
The tests use two aces in a hand (0.0399) and the lottery (0.01765 for exactly 3 matches, 0.01864 for at least 3). They also try impossible draws, and check that the probabilities for add up to 1.
See the tests
def _raises(f, *args, what=""):
try:
f(*args)
except ValueError:
return
raise AssertionError(f"expected ValueError for {what}")
def test_two_aces():
"""Exactly two aces in a 5-card hand has probability about 0.0399"""
expect_close(draw_exactly(52, 4, 5, 2), 0.03992981808107859, what="draw_exactly(52, 4, 5, 2)", rel=1e-9)
def test_lottery():
"""Matching exactly 3 of 6 numbers drawn from 49 has probability about 0.01765"""
expect_close(draw_exactly(49, 6, 6, 3), 0.017650403866870102, what="draw_exactly(49, 6, 6, 3)", rel=1e-9)
def test_all_hearts():
"""Five hearts in a 5-card hand has probability about 0.000495"""
expect_close(draw_exactly(52, 13, 5, 5), 0.0004951980792316927, what="draw_exactly(52, 13, 5, 5)", rel=1e-9)
def test_impossible_draws():
"""Impossible counts give 0: five aces, 3 aces in 2 cards, too few others, or k = -1"""
expect_close(draw_exactly(52, 4, 5, 5), 0.0, what="five aces in a hand")
expect_close(draw_exactly(52, 4, 2, 3), 0.0, what="three aces in two cards")
expect_close(draw_exactly(10, 8, 5, 2), 0.0, what="2 special of 5 when only 2 others exist")
expect_close(draw_exactly(52, 4, 5, -1), 0.0, what="k = -1")
def test_adds_up_to_one():
"""The chances of k = 0, 1, ..., n special items add up to 1"""
total = sum(draw_exactly(49, 6, 6, k) for k in range(7))
expect_close(total, 1.0, what="sum over k for the lottery", rel=1e-12)
total = sum(draw_exactly(10, 6, 3, k) for k in range(4))
expect_close(total, 1.0, what="sum over k for a committee of 3 from 6 juniors and 4 seniors", rel=1e-12)
def test_at_least():
"""At least 3 lottery matches is about 0.01864; at least one ace in a hand about 0.3412"""
expect_close(draw_at_least(49, 6, 6, 3), 0.01863754500202234, what="draw_at_least(49, 6, 6, 3)", rel=1e-9)
expect_close(draw_at_least(52, 4, 5, 1), 0.34115800166220334, what="draw_at_least(52, 4, 5, 1)", rel=1e-9)
def test_at_least_edges():
"""At least 0 is certain, and at least 5 aces in a hand is impossible"""
expect_close(draw_at_least(52, 4, 5, 0), 1.0, what="draw_at_least(52, 4, 5, 0)")
expect_close(draw_at_least(52, 4, 5, 5), 0.0, what="draw_at_least(52, 4, 5, 5)")
def test_bad_inputs():
"""Drawing more than N, more special items than N, or a negative size raises ValueError"""
_raises(draw_exactly, 10, 4, 11, 2, what="n > N")
_raises(draw_exactly, 10, 12, 3, 2, what="K > N")
_raises(draw_exactly, 10, -1, 3, 0, what="K < 0")
_raises(draw_at_least, 10, 4, 11, 2, what="n > N in draw_at_least")