8.7 C
London
Monday, December 2, 2024
HomePythonProbability Distributions in PythonHow to Use the Binomial Distribution in Python

How to Use the Binomial Distribution in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories