15.1 C
London
Friday, July 5, 2024
HomePythonProbability Distributions in PythonHow to Use the Poisson Distribution in Python

How to Use the Poisson 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 Poisson distribution describes the probability of obtaining k successes during a given time interval.

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

P(X=k) = λk * e– λ / k!

where:

  • λ: mean number of successes that occur during a specific interval
  • k: number of successes
  • e: a constant equal to approximately 2.71828

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

How to Generate a Poisson Distribution

You can use the poisson.rvs(mu, size) function to generate random values from a Poisson distribution with a specific mean value and sample size:

from scipy.stats import poisson

#generate random values from Poisson distribution with mean=3 and sample size=10
poisson.rvs(mu=3, size=10)

array([2, 2, 2, 0, 7, 2, 1, 2, 5, 5])

How to Calculate Probabilities Using a Poisson Distribution

You can use the poisson.pmf(k, mu) and poisson.cdf(k, mu) functions to calculate probabilities related to the Poisson distribution.

Example 1: Probability Equal to Some Value

A store sells 3 apples per day on average. What is the probability that they will sell 5 apples on a given day? 

from scipy.stats import poisson

#calculate probability
poisson.pmf(k=5, mu=3)

0.100819

The probability that the store sells 5 apples in a given day is 0.100819.

Example 2: Probability Less than Some Value

A certain store sells seven footballs per day on average. What is the probability that this store sells four or less footballs in a given day?

from scipy.stats import poisson

#calculate probability
poisson.cdf(k=4, mu=7)

0.172992

The probability that the store sells four or less footballs in a given day is 0.172992.

Example 3: Probability Greater than Some Value

A certain store sells 15 cans of tuna per day on average. What is the probability that this store sells more than 20 cans of tuna in a given day?

from scipy.stats import poisson

#calculate probability
1-poisson.cdf(k=20, mu=15)

0.082971

The probability that the store sells more than 20 cans of tuna in a given day is 0.082971.

How to Plot a Poisson Distribution

You can use the following syntax to plot a Poisson distribution with a given mean:

from scipy.stats import poisson
import matplotlib.pyplot as plt

#generate Poisson distribution with sample size 10000
x = poisson.rvs(mu=3, size=10000)

#create plot of Poisson distribution
plt.hist(x, density=True, edgecolor='black')

Additional Resources

An Introduction to the Poisson Distribution
5 Real-Life Examples of the Poisson Distribution
Online Poisson Distribution Calculator

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