The ideas behind flipping a coin are important in computing.
Computers rely on bits - a bit is a variable that can take on one of two values, one or zero.
Flipping a coin results in one of two outcomes, heads or tails.
# We'll use numpy and scipy.stats to analyse flipping a coin.
import numpy as np
import scipy.stats as ss
# We'll use this for visualisation.
import matplotlib.pyplot as plt
import seaborn as sns
# This just sets the default plot size to be bigger.
plt.rcParams['figure.figsize'] = (12, 8)
A fair coin will give a head fifty percent of the time and a tail fifty percent of the time.
We say the probability of a head is 0.5 and the probability of a tail is 0.5.
We can use the following function to simulate this - giving a 1 for a head and 0 for a tail.
# (Number of times to flip a coin, probability of a head, number of times to do this)
np.random.binomial(1, 0.5, 1) # returns the head(1) or tail(0) value
array([1])
# Flip a fair coin 1000 times - how many heads?
np.random.binomial(1000, 0.5, 1) # returns the number of heads
array([486])
How likely are we to see a certain number of heads when flipping a coin however many times?
# (No. of heads, no. of flips, probability of a head)
ss.binom.pmf(500, 1000, 0.5) # ss - scipy stats -
0.025225018178380496
sns.distplot(np.random.binomial(1000, 0.5, 1000))
<matplotlib.axes._subplots.AxesSubplot at 0x22dfd7d52e8>
What about an unfair coin?
# Flip an unfair coin 10 times - how many heads?
np.random.binomial(10, 0.2, 1)
array([2])
Suppose we flip an unfair coin ($p = 0.3$) ten times, what is the probability that the flips are as follows?
$$ HHTTHHHTTT $$(0.3)*(0.3)*(1.0-0.3)*(1.0-0.3)*(0.3)*(0.3)*(0.3)*(1.0-0.3)*(1.0-0.3)*(1.0-0.3)
0.0004084101
0.3*0.3*0.7*0.7*.3*.3*.3*.7*.7*.7
0.0004084101
The probability of $r$ heads when flipping an unfair coin $n$ times is
$$ P(r \mid n , p) = {n \choose r} p^r (1-p)^{(n-r)} $$(0.3)**5*(0.7)**5
0.00040841009999999976
noflips = 10
p = 0.3
d = [ss.binom.pmf(i, noflips, p) for i in range(noflips+1)]
d
[0.028247524900000005, 0.12106082100000018, 0.2334744405, 0.26682793200000016, 0.20012094900000013, 0.10291934520000007, 0.03675690899999999, 0.009001692000000002, 0.0014467004999999982, 0.00013778100000000015, 5.904899999999995e-06]
$ {n \choose r} $ is the number of ways to select $r$ items from $n$, ignoring the order you select them in.
import math
n = 10
r = 3
choose = lambda x, y: math.factorial(x) / (math.factorial(y) * math.factorial(x-y))
choose(n, r)
120.0
Note the following for ${n \choose 0}$ and ${n \choose n}$.
choose(10, 0)
1.0
choose(n, n)
1.0
choose(10,3)
120.0
choose(8,4)
70.0
Even though the chances are, with $p = 0.3$ and $10$ flips, that there are three heads, the most probable outcome is all tails.
Clarification: The one instance of tails beats a single instance of three heads, not all 120 possible options of three heads.
(1-0.3)**10
0.02824752489999998
0.7**10
0.02824752489999998
0.3**10
5.9048999999999975e-06
0.7**9*0.3**1
0.012106082099999993
What has all of this got to do with computers and bits?
Would you consider the following a data set?
import itertools
#["".join(seq) for seq in itertools.product("01", repeat=10)]