Python

How to calculate binomial distribution in Python

We provide effective and economically affordable online training courses for R and Python, click here for more details and course registration !

Binomial distribution in statistics is a type of discrete probability distribution, modeling the probability of success times among a predetermined Bernoulli process. A Bernoulli process is a binary experiment with result success or fail, in which the probability of success or fail is constant for each independent trial. The most striking point for a binomial distribution is the probability associated with discrete success number, 1,2,3,,,, until N, which is the trial size. In Python, binomial distribution probability can be calculated using pmf() function from scipy.stats.binom module. Say, we are interested in N = 25 Bernoulli process with success rate 0.15 for each trial. The following code shows the calculation of probability for every possible success count from 0 to 25.


#total 15 Bernoulli trial, with success probability 0.15 for each trial
n= 15
p = 0.15
#a list for integer 0 to n
sn = list(range(n + 1))
#calculate probability for each count number, and store in a list
pn = [binom.pmf(s, n, p) for s in sn ]
#print out each possible count and their corresponding probability
for i in range(n + 1):
    print(str(sn[i]) + "\t" + str(pn[i]))
#output
r p(r)
0 0.08735421910125173
1 0.2312317564444899
2 0.28563922854907725
3 0.21842999830223525
4 0.11563941086588901
5 0.0448953006891098
6 0.01320450020267937
7 0.0029959790375827166
8 0.0005287021831028324
9 7.256696630823176e-05
10 7.68356113851866e-06
11 6.163284335710162e-07
12 3.625461373947157e-08
13 1.4764322337341365e-09
14 3.722098068237306e-11
15 4.378938903808602e-13
#sum the value for all probabilities
sum(pn)
#result is one, verified that it is correct
1.0000000000000033

For getting more knowledge of Python and a preview of our training course, you can watch Python tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago