How to Use the Binomial Distribution in Python


The binomial distribution is one of the most commonly used distributions in statistics. It describes the probability of obtaining k successes in n binomial experiments.

If a random variable X follows a binomial distribution, then the probability that X = k successes can be found by the following formula:

P(X=k) = nCk * pk * (1-p)n-k

where:

  • n: number of trials
  • k: number of successes
  • p: probability of success on a given trial
  • nCkthe number of ways to obtain k successes in n trials

This tutorial explains how to use the binomial distribution in Python.

How to Generate a Binomial Distribution

You can generate an array of values that follow a binomial distribution by using the random.binomial function from the numpy library:

from numpy import random

#generate an array of 10 values that follow a binomial distribution
random.binomial(n=10, p=.25, size=10)

array([5, 2, 1, 3, 3, 3, 2, 2, 1, 4])

Each number in the resulting array represents the number of “successes” experienced during 10 trials where the probability of success in a given trial was .25.

How to Calculate Probabilities Using a Binomial Distribution

You can also answer questions about binomial probabilities by using the binom function from the scipy library.

Question 1: Nathan makes 60% of his free-throw attempts. If he shoots 12 free throws, what is the probability that he makes exactly 10?

from scipy.stats import binom

#calculate binomial probability
binom.pmf(k=10, n=12, p=0.6)

0.0639

The probability that Nathan makes exactly 10 free throws is 0.0639.

Question 2: Marty flips a fair coin 5 times. What is the probability that the coin lands on heads 2 times or fewer?

from scipy.stats import binom

#calculate binomial probability
binom.cdf(k=2, n=5, p=0.5)

0.5

The probability that the coin lands on heads 2 times or fewer is 0.5.

Question 3: It is known that 70% of individuals support a certain law. If 10 individuals are randomly selected, what is the probability that between 4 and 6 of them support the law?

from scipy.stats import binom

#calculate binomial probability
binom.cdf(k=6, n=10, p=0.7) - binom.cdf(k=3, n=10, p=0.7)

0.3398

The probability that between 4 and 6 of the randomly selected individuals support the law is 0.3398.

How to Visualize a Binomial Distribution

You can visualize a binomial distribution in Python by using the seaborn and matplotlib libraries:

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

x = random.binomial(n=10, p=0.5, size=1000)

sns.distplot(x, hist=True, kde=False)

plt.show()

Binomial distribution plot in Python

The x-axis describes the number of successes during 10 trials and the y-axis displays the number of times each number of successes occurred during 1,000 experiments.

Leave a Reply

Your email address will not be published. Required fields are marked *