A coin is tossed ten times.
(i) What is the probability of it coming down heads 5 times and tails 5 times?
(ii) Which is the more likely: exactly 7 heads or more than 7 heads?
Verify your answers by tossing a coin ten times and counting the number of heads. Repeat the experiment 100000 times and record the frequencies of the various numbers of heads. If you do not have time for 100000 repetitions, (IA engineers generally do), you could alternatively write a Python program to simulate the experiments.
Load the required packages, numpy
and matplotlib
.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
The code below generates the results. Comments are provided where appropriate to assist understanding.
# Edit the line below to change the number of experiments, and observe the effect on the results
num_experiments = 100000
# Initialize an empty list to store the results
results = []
# Main loop over experiments
for i in range(num_experiments):
# Generate ten random numbers in the range 0 to 1, count how
# many of them exceed 0.5 - this is equivalent to tossing a head
num_heads = 0
for j in range(10):
if np.random.rand() > 0.5:
num_heads += 1
# Store how many heads there were in this experiment
results.append(num_heads)
# Plot a historgram of the number of heads
n, bins, patches = plt.hist(results, 10, facecolor='green', alpha=0.7)
plt.xlabel('number of heads')
plt.ylabel('frequency');
We can use the numerical results to determine the answer for parts (i) and (ii).
The probability of the coins coming down on head exactly 5 times is:
$$ P(5) = \mathcal{C}_5^{10} \left( \frac{1}{2} \right)^5 \left( \frac{1}{2} \right)^5 = 0.246 $$The probability of getting 7 heads is:
$$ P(7) = \mathcal{C}_7^{10} \left( \frac{1}{2} \right)^7 \left( \frac{1}{2} \right)^3 = 0.117 $$and getting more than 7 heads:
$$ P(8, 9, 10) = \mathcal{C}_8^{10} \left( \frac{1}{2} \right)^8 \left( \frac{1}{2} \right)^2 + \mathcal{C}_9^{10} \left( \frac{1}{2} \right)^9 \left( \frac{1}{2} \right)^1 + \mathcal{C}_{10}^{10} \left( \frac{1}{2} \right)^{10} \left( \frac{1}{2} \right)^0 = 0.055 $$import operator
def get_proportion(n, op, results):
return sum(op(i, n) for i in results)/len(results)
print('Proportion with five heads: {}'.format(get_proportion(5, operator.eq, results)))
print('Proportion with seven heads: {}'.format(get_proportion(7, operator.eq, results)))
print('Proportion with more than seven heads: {}'.format(get_proportion(7, operator.gt, results)))
Proportion with five heads: 0.24626 Proportion with seven heads: 0.11801 Proportion with more than seven heads: 0.05494