0
0
SciPydata~5 mins

Poisson distribution in SciPy

Choose your learning style9 modes available
Introduction

The Poisson distribution helps us find the chance of a number of events happening in a fixed time or space when these events happen independently and at a constant rate.

Counting how many emails you get in an hour.
Finding the number of cars passing a checkpoint in 10 minutes.
Estimating how many calls a call center receives per day.
Counting the number of typos on a page of a book.
Predicting how many customers arrive at a store in an hour.
Syntax
SciPy
from scipy.stats import poisson

# Create a Poisson distribution with mean (lambda) = mu
poisson_dist = poisson(mu)

# Probability of exactly k events
prob_k = poisson_dist.pmf(k)

# Probability of k or fewer events
prob_up_to_k = poisson_dist.cdf(k)

# Generate random samples
samples = poisson_dist.rvs(size=n)

mu is the average number of events in the interval.

pmf(k) gives the probability of exactly k events.

Examples
This calculates the chance of getting exactly 2 emails when the average is 3 per hour.
SciPy
from scipy.stats import poisson

# Average 3 emails per hour
mu = 3
poisson_dist = poisson(mu)

# Probability of exactly 2 emails
prob_2 = poisson_dist.pmf(2)
print(prob_2)
This finds the chance of having 3 or fewer events when the average is 5.
SciPy
from scipy.stats import poisson

mu = 5
poisson_dist = poisson(mu)

# Probability of 0 to 3 events
prob_up_to_3 = poisson_dist.cdf(3)
print(prob_up_to_3)
This creates 5 random numbers following the Poisson distribution with mean 4.
SciPy
from scipy.stats import poisson

mu = 4
poisson_dist = poisson(mu)

# Generate 5 random samples
samples = poisson_dist.rvs(size=5)
print(samples)
Sample Program

This program calculates the chance of exactly 3 cars passing and up to 3 cars passing in one minute, assuming an average of 2 cars per minute. It also shows 10 random samples of cars passing following this distribution.

SciPy
from scipy.stats import poisson

# Suppose on average 2 cars pass a checkpoint per minute
mu = 2
poisson_dist = poisson(mu)

# Probability of exactly 3 cars passing in a minute
prob_3_cars = poisson_dist.pmf(3)

# Probability of 0 to 3 cars passing
prob_up_to_3_cars = poisson_dist.cdf(3)

# Generate 10 random samples of cars passing
samples = poisson_dist.rvs(size=10)

print(f"Probability of exactly 3 cars: {prob_3_cars:.4f}")
print(f"Probability of up to 3 cars: {prob_up_to_3_cars:.4f}")
print(f"Random samples of cars passing: {samples}")
OutputSuccess
Important Notes

The Poisson distribution assumes events happen independently and at a constant average rate.

Use pmf(k) for exact counts and cdf(k) for cumulative counts up to k.

Random samples help simulate real-world scenarios following this distribution.

Summary

Poisson distribution models counts of events in fixed intervals.

Use pmf to find the chance of exactly k events.

Use cdf to find the chance of up to k events.