Is there any Python function/code to plot Binomial distribution (both PDF and CDF)
Question:
All the code in the net do not let us decide both the parameters of binomial distribution.
How we can use a user interface (CLI) to ask user to enter any parameter (number of trials, probability of success), then plot the graphs in a suitable way?
Answers:
Binomial distribution (PDF) is defined as : p(x;n) = nCr(n,x) * (p**x) * ((1-p)**(n-x))
The CDF can be easily generated by sending the PDF through a accumulator system (a summer). For that simply sum the PDF; up to and including the interested point.
How ever plotting the PDF and CDF can be nicely done if you can use stem function for low number of "n" (= number of trials) and plot function for larger values of "n".
Use these links if you do not know matplotlib.pyplot stem, plot functions:
1.
Python matplotlib.pyplot stem function
2.
Python matplotlib.pyplot plot function
import numpy as np
import matplotlib.pyplot as plt
import math
import time
def nCr(n,r):n=int(n);r=int(r);f = math.factorial;return int(f(n)/(f(r)*f(n-r)))
def p(n,p,x):return nCr(n,x) * (p**x) * ((1-p)**(n-x))
def cumilate(n,pS,x_):
o=0
for i in range(x_+1):
o += p(n,pS,i)
return o;
while True:
input_n = input('Enter No. of trials:')
input_pS = input('Enter Probability of success:')
try:
n = int(input_n)
pS = float(input_pS)
except :
print('Please enter valid data...')
continue
if(n<=0):print('n should be larger than 0');continue
if(not(0<=pS<=1)):print('p should be in [0,1]');continue
if(n>11500):print('n is too large.It will stuck your pc.Try less than 500');continue
x = [i for i in range(n+1)]
y = [p(n,pS,x1) for x1 in x]
fig,ax = plt.subplots(2,1,figsize=(20,10))
if(n>=150):
ax[0].plot(x,y)
else:
ax[0].stem(x,y)
ax[0].title.set_text('Binomial PDF n=' + str(n) +' pS=' + str(pS))
cum = [cumilate(n,pS,x1) for x1 in x]
if(n>=150):
ax[1].plot(x,cum)
else:
ax[1].stem(x,cum)
ax[1].title.set_text('Binomial CDF n=' + str(n) +' pS=' + str(pS))
plt.show()
print('-------------------- Plotted -----------------')
Results will look like following:
n = 120 , p = 0.5
n = 10, p =0.8
When the number of trials are so high, stem function is not visually nice to use. So switched to plot function and shown as a continuous function.
n = 200, p = 0.3
All the code in the net do not let us decide both the parameters of binomial distribution.
How we can use a user interface (CLI) to ask user to enter any parameter (number of trials, probability of success), then plot the graphs in a suitable way?
Binomial distribution (PDF) is defined as : p(x;n) = nCr(n,x) * (p**x) * ((1-p)**(n-x))
The CDF can be easily generated by sending the PDF through a accumulator system (a summer). For that simply sum the PDF; up to and including the interested point.
How ever plotting the PDF and CDF can be nicely done if you can use stem function for low number of "n" (= number of trials) and plot function for larger values of "n".
Use these links if you do not know matplotlib.pyplot stem, plot functions:
1.
Python matplotlib.pyplot stem function
2.
Python matplotlib.pyplot plot function
import numpy as np
import matplotlib.pyplot as plt
import math
import time
def nCr(n,r):n=int(n);r=int(r);f = math.factorial;return int(f(n)/(f(r)*f(n-r)))
def p(n,p,x):return nCr(n,x) * (p**x) * ((1-p)**(n-x))
def cumilate(n,pS,x_):
o=0
for i in range(x_+1):
o += p(n,pS,i)
return o;
while True:
input_n = input('Enter No. of trials:')
input_pS = input('Enter Probability of success:')
try:
n = int(input_n)
pS = float(input_pS)
except :
print('Please enter valid data...')
continue
if(n<=0):print('n should be larger than 0');continue
if(not(0<=pS<=1)):print('p should be in [0,1]');continue
if(n>11500):print('n is too large.It will stuck your pc.Try less than 500');continue
x = [i for i in range(n+1)]
y = [p(n,pS,x1) for x1 in x]
fig,ax = plt.subplots(2,1,figsize=(20,10))
if(n>=150):
ax[0].plot(x,y)
else:
ax[0].stem(x,y)
ax[0].title.set_text('Binomial PDF n=' + str(n) +' pS=' + str(pS))
cum = [cumilate(n,pS,x1) for x1 in x]
if(n>=150):
ax[1].plot(x,cum)
else:
ax[1].stem(x,cum)
ax[1].title.set_text('Binomial CDF n=' + str(n) +' pS=' + str(pS))
plt.show()
print('-------------------- Plotted -----------------')
Results will look like following:
n = 120 , p = 0.5
n = 10, p =0.8
When the number of trials are so high, stem function is not visually nice to use. So switched to plot function and shown as a continuous function.
n = 200, p = 0.3