Home » Python » How to Calculate Binomial Distribution in Python

How to Calculate Binomial Distribution in Python

The binomial distribution is the basis for popular binomial test in statistics. The binomial distribution probability distribution that summarize about probability of getting success in given number of experiments.

Binomial distribution is used to find probability of binomial random variable with given number of repeated trials (n). The probability distribution of binomial random variable is called binomial distribution.

In this article, we will discuss about how to calculate binomial distribution in python.

Binomial Distribution Formula

If binomial random variable X follows a binomial distribution with parameters number of trials (n) and probability of correct guess (P) and results in x successes then binomial probability is given by :

P(X = x) = nCx * px * (1-p)n-x

Where,

n = number of trials in the binomial experiment

x = number of successes in binomial experiment

p = probability of success on given trial

nCx = number of combinations to n trials ,taken x at a time

Scipy for Binomial Distribution

We will be using scipy library to calculate binomial distribution in python.

If you don’t have scipy library installed then use below command on windows command prompt for scipy library installation

pip install scipy

How to Calculate Probabilities using Binomial Distribution?

scipy library provide binom function to calculate binomial probabilities.

binom function takes inputs as k, n and p and given as binom.pmf(k,n,p) , where pmf is Probability mass function.

for example, given k = 15, n = 25, p = 0.6, binomial probability can be calculated as below using python code

from scipy.stats import binom

#calculate binomial probability
result = binom.pmf(k=15, n=25, p=0.6)

#Print the result
print("Binomial Probability: ",result)

//output
Binomial Probability: 0.1611579

In the above code, first we import binom function. As in our above examples, we have below data

n = number of trials = 25

k = number of successes = 15

p = probability of success in given trial is 0.6

using binom.pmf() function, it calculate binomial probability which is 0.1611579

Lets understand calculation of binomial distribution in python using some real world examples as given below

Example #1 Find Binomial Probability

Suppose that a short quiz consists of 6 multiple choice questions. Each question has four possible answers of which any one in correct. A student guesses on every question. Find the probability that a student will answer

a. all questions correctly

Solution

Let X be the number of questions guessed correctly out of 6 questions. Let p be the probability of correct guess.

The random variable X follows a Binomial distribution with parameter n=8 and p=0.25.

a. The probability that student will answers all questions correctly is

n = number of questions = 6

k = number of successes = 6

p = probability of success in given question = 1 out of 4 = 1/4 = 0.25

Based on above data, using binom function, probability mass function calculated as

from scipy.stats import binom

#calculate binomial probability mass function
result = binom.pmf(k=6, n=6, p=0.25)

#Print the result
print("Binomial Probability: ",result)

//output
Binomial Probability: 0.0002

The probability that student will answer all questions is 0.0002

Example #2 Calculate Binomial Distribution

A candidate for public office has claimed that 60% of voters will vote for her. If 5 registered voters were sampled,

What is the probability that exactly 3 would say they favor this candidate?

Solution

Here in above example, we have

n = number of register voters = 5

p = probability of successes = 60% = 0.6

k = probability of success that exactly 3 would favor this candidate is

from scipy.stats import binom

#calculate binomial probability mass function
result = binom.pmf(k=3, n=6, p=0.6)

#Print the result
print("Binomial Probability: ",result)

//output
Binomial Probability: 0.34559

The probability of 3 would say they favor this candidate is 0.34559

Conclusion

I hope you find above article on how to calculate binomial distribution in python code useful and educational.

Leave a Comment