Python – Binomial Distribution

Binomial distribution
Bernoulli
  • There must be only 2 possible outcomes.
  • Each outcome has a fixed probability of occurring. A success has the probability of p, and a failure has the probability of 1 – p.
  • Each trial is completely independent of all others.
r
n
r
n-r
r
probability mass function(pmf)
r
n-r
6
0.6
success’
r
r0123456
P(r) 0.004096 0.036864 0.138240 0.2764800.311040  0.1866240.046656
np
np(1-p)
Using Python to obtain the distribution :
SciPy
Matplotlib
Modules required :
  • SciPy: SciPy is an Open Source Python library, used in mathematics, engineering, scientific and technical computing. Installation :
    pip install scipy
    
  • Matplotlib: Matplotlib is a comprehensive Python library for plotting static and interactive graphs and visualisations.  Installation :
    pip install matplotlib
    
scipy.stats
stats()
scipy.stats.binom
n
p
Syntax : scipy.stats.binom.stats(n, p)
scipy.stats.binom.pmf()
Syntax : scipy.stats.binom.pmf(r, n, p)
Calculating distribution table :
Approach :
  • Define n and p.
  • Define a list of values of r from 0 to n.
  • Get mean and variance.
  • For each r, calculate the pmf and store in a list.
Code :
from scipy.stats import binom
# setting the values
# of n and p
n = 6
p = 0.6
# defining the list of r values
r_values = list(range(n + 1))
# obtaining the mean and variance 
mean, var = binom.stats(n, p)
# list of pmf values
dist = [binom.pmf(r, n, p) for r in r_values ]
# printing the table
print("r\tp(r)")
for i in range(n + 1):
    print(str(r_values[i]) + "\t" + str(dist[i]))
# printing mean and variance
print("mean = "+str(mean))
print("variance = "+str(var))

                    
Output :
r    p(r)
0    0.004096000000000002
1    0.03686400000000005
2    0.13824000000000003
3    0.2764800000000001
4    0.31104
5    0.18662400000000007
6    0.04665599999999999
mean = 3.5999999999999996
variance = 1.44
Code: Plotting the graph using matplotlib.pyplot.bar() function to plot vertical bars.
from scipy.stats import binom
import matplotlib.pyplot as plt
# setting the values
# of n and p
n = 6
p = 0.6
# defining list of r values
r_values = list(range(n + 1))
# list of pmf values
dist = [binom.pmf(r, n, p) for r in r_values ]
# plotting the graph 
plt.bar(r_values, dist)
plt.show()

                    
Output :
normal
p
0.5