Binomial Distribution

A few examples of a binomial distribution

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import binom, norm
Flipping 10 coins, number of heads: n = 10, p = 0.5
In [2]:
n = 10
p = 0.5
x = range(0,n+1)
bf = binom.pmf(x,n,p)

plt.bar(x,bf)
plt.xlabel('Heads')
plt.ylabel('Prob.')
plt.show()
 Rolling 3 dice, number of 6s: n = 3, p = 1/6
In [3]:
n = 3
p = 1/6
x = range(0,n+1)
bf = binom.pmf(x,n,p)

plt.bar(x,bf)
plt.xlabel('Sixes')
plt.ylabel('Prob.')
plt.show()

for i in x:
    print(f"Probability of {i} sixes is {100*bf[i]:.3}%")
Probability of 0 sixes is 57.9%
Probability of 1 sixes is 34.7%
Probability of 2 sixes is 6.94%
Probability of 3 sixes is 0.463%
Rolling 5 dice, number of 6s: n = 5, p = 1/6
In [4]:
n = 5
p = 1/6
x = range(0,n+1)
bf = binom.pmf(x,n,p)

plt.bar(x,bf)
plt.xlabel('Sixes')
plt.ylabel('Prob.')
plt.show()

Probability of exactly 3:

In [5]:
print(f"Probability for 3 sixes is {100*bf[3]:.2}%")
print(f"Probability for 3 or more sixes is {100*sum(bf[3:]):.2}%")
print(f"Probability for 3 or more of any value is {6*100*sum(bf[3:]):.3}%")
Probability for 3 sixes is 3.2%
Probability for 3 or more sixes is 3.5%
Probability for 3 or more of any value is 21.3%
Flipping 50 coins: n = 50, p = 0.5
In [6]:
n = 50
p = 0.5
x = range(0,n+1)
bf = binom.pmf(x,n,p)

plt.bar(x,bf)
plt.xlabel('Heads')
plt.ylabel('Prob.')
plt.show()
Compare to Gaussian
In [7]:
x = range(10,40)
bf = binom.pmf(x,n,p)

mu = p*n
sigma = np.sqrt(n*p*(1-p))
gf = norm.pdf(x, mu, sigma)

plt.bar(x,bf)
plt.xlabel('Heads')
plt.ylabel('Prob.')
plt.plot(x,gf,'r')
plt.show()
In [8]:
plt.bar(x, 100*(bf-gf))
plt.xlabel('Heads')
plt.ylabel('Prob. Diff. (Binom - Norm) [%]')
plt.show()
Roll 100 dice, number of Sixes: n = 100, p = 1/6
In [9]:
n = 100
p = 1/6
x = range(0,40)
bf = binom.pmf(x,n,p)

mu = p*n
sigma = np.sqrt(n*p*(1-p))
gf = norm.pdf(x, mu, sigma)

plt.bar(x,bf)
plt.plot(x,gf,'r')
plt.xlabel('Sixes')
plt.ylabel('Prob.')
plt.show()
In [10]:
plt.bar(x, 100*(bf-gf))
plt.xlabel('Heads')
plt.ylabel('Prob. Diff. (Binom - Norm) [%]')
plt.show()
In [ ]: