A few examples of a binomial distribution
%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
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
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}%")
Rolling 5 dice, number of 6s: n = 5, p = 1/6
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:
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}%")
Flipping 50 coins: n = 50, p = 0.5
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
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()
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
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()
plt.bar(x, 100*(bf-gf))
plt.xlabel('Heads')
plt.ylabel('Prob. Diff. (Binom - Norm) [%]')
plt.show()