来源:udacity
# import numpy
import numpy as np
1. Two fair coin flips produce exactly two heads
# simulate 1 million tests of two fair coin flips
tests = np.random.randint(2, size=(int(1e5), 2))
# sums of all tests
test_sums = tests.sum(axis=1)
# proportion of tests that produced exactly two heads
(test_sums == 0).mean()
2. Three fair coin flips produce exactly one head
# simulate 1 million tests of three fair coin flips
tests = np.random.randint(2, size=(int(1e6), 3))
# sums of all tests
test_sums = tests.sum(axis = 1)
# proportion of tests that produced exnumpyactly one head
(test_sums == 2).mean()
3. Three biased coin flips with P(H) = 0.6 produce exactly one head
# simulate 1 million tests of three biased coin flips
# hint: use np.random.choice()
tests = np.random.choice([0, 1], size=(int(1e6),2), p=[0.6, 0.4])
# sums of all tests
test_sums = tests.sum(axis = 1)
# proportion of tests that produced exactly one head
(test_sums == 2).mean()
4. A die rolls an even number
# simulate 1 million tests of one die roll
tests = np.random.randint(1, 7, size=int(1e6))
# proportion of tests that produced an even number
(tests%2 == 0).mean()
5. Two dice roll a double
# simulate the first million die rolls
first = np.random.randint(1,7, size=1000000)
# simulate the second million die rolls
second = np.random.randint(1,7, size=int(1e6))
# proportion of tests where the 1st and 2nd die rolled the same number
(first == second).mean()
Simulating Many Coin Flips
# number of heads from 10 fair coin flips
np.random.binomial(10, 0.5)
output : 4
# results from 20 tests with 10 coin flips
np.random.binomial(10, 0.5, 20)
output : array([5, 7, 6, 4, 5, 6, 4, 4, 6, 5, 3, 6, 5, 4, 4, 4, 3, 6, 6, 5])
# reflects the fairness of the coin more closely as # tests increases
np.random.binomial(10, 0.5, 1000000).mean()
output : 5.0041070000000003
import matplotlib.pyplot as plt
% matplotlib inline
plt.hist(np.random.binomial(10, 0.5, 1000000));
Usenp.random.binomial
to create simulations and compute proportions for the following outcomes.
1. A fair coin flip produces heads
# simulate 1 million tests of one fair coin flip
# remember, the output of these tests are the # successes, or # heads
tests = np.random.binomial(1, 0.5, int(1e6))
# proportion of tests that produced heads
(tests == 1).mean()
2. Five fair coin flips produce exactly one head
# simulate 1 million tests of five fair coin flips
tests = np.random.binomial(5, 0.5, int(1e6))
# proportion of tests that produced 1 head
(tests == 1).mean()
3. Ten fair coin flips produce exactly four heads
# simulate 1 million tests of ten fair coin flips
tests = np.random.binomial(10, 0.5, int(1e6))
# proportion of tests that produced 4 heads
(tests == 4).mean()
4. Five biased coin flips with P(H) = 0.8 produce exactly five heads
# simulate 1 million tests of five biased coin flips
tests = np.random.binomial(5, 0.8, int(1e6))
# proportion of tests that produced 5 heads
(tests == 5).mean()
5. Ten biased coin flips with P(H) = 0.15 produce at least 3 heads
# simulate 1 million tests of ten biased coin flips
tests = np.random.binomial(10, 0.15, int(1e6))
# proportion of tests that produced at least 3 heads
(tests >= 3).mean()