How to Use the Poisson Distribution in Python


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

Leave a Reply

Your email address will not be published.