Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Poisson Distribution



What is a Poisson Distribution?

The Poisson distribution is characterized by a single parameter, (lambda), which is the average number of events in the given interval. The probability mass function (PMF) of the Poisson distribution gives the probability of observing k events in the interval and is defined as −

P(X = k) = (k * e(-)) / k!

Where,

  • : It is the average number of events in the interval.
  • k: It represents the number of events.
  • e: It is the Euler's number (approximately 2.71828).

Poisson Distributions in NumPy

NumPy provides the numpy.random.poisson() function to generate samples from a Poisson distribution. You can specify the mean rate () and the size of the generated samples.

Example

In this example, we generate 10 random samples from a Poisson distribution with a mean rate of 3 events per interval −

import numpy as np

# Generate 10 random samples from a Poisson distribution with =3
samples = np.random.poisson(lam=3, size=10)
print("Random samples from Poisson distribution:", samples)

Following is the output obtained −

Random samples from Poisson distribution: [3 1 2 2 1 1 2 5 5 3]

Visualizing Poisson Distributions

Visualizing Poisson distributions helps to understand their properties better. We can use libraries such as Matplotlib to create histograms that display the distribution of generated samples.

Example

In the following example, we are first generating random samples from a Poisson distribution with =3. We then create a histogram to visualize this distribution −

import numpy as np
import matplotlib.pyplot as plt

# Generate 1000 random samples from a Poisson distribution with =3
samples = np.random.poisson(lam=3, size=1000)

# Create a histogram to visualize the distribution
plt.hist(samples, bins=range(10), edgecolor='black', density=True)
plt.title('Poisson Distribution')
plt.xlabel('Number of events')
plt.ylabel('Frequency')
plt.xticks(range(10))
plt.show()

The histogram shows the frequency of the number of events in the Poisson trials. The bars represent the probability of each possible outcome, which forms the characteristic shape of a Poisson distribution −

Poisson Distribution

Applications of Poisson Distributions

Poisson distributions are used in various fields to model the occurrence of events over time or space. Here are a few practical applications −

  • Traffic Engineering: Modeling the number of cars passing through a checkpoint.
  • Finance: Modeling the number of trades executed on a stock exchange.
  • Queueing Theory: Modeling the number of customers arriving at a service point.

Generating Cumulative Poisson Distributions

Sometimes, we are interested in the cumulative distribution function (CDF) of a Poisson distribution, which gives the probability of getting up to and including k events in the interval.

NumPy does not have a built-in function for the CDF of a Poisson distribution, but we can calculate it using a loop and the scipy.stats.poisson.cdf() function from the SciPy library.

Example

In the example below, we are generating cumulative poisson distribution in NumPy −

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

# Define the mean rate
lam = 3

# Generate the cumulative distribution function (CDF) values
x = np.arange(0, 10)
cdf = poisson.cdf(x, lam)

# Plot the CDF
plt.plot(x, cdf, marker='o', linestyle='-', color='b')
plt.title('Cumulative Poisson Distribution')
plt.xlabel('Number of events')
plt.ylabel('Cumulative probability')
plt.grid(True)
plt.show()

The plot shows the cumulative probability of getting up to and including each number of events in the Poisson trials. The CDF is a step function that increases to 1 as the number of events increases −

Cumulative Poisson Distribution

Properties of Poisson Distributions

Poisson distributions have several key properties, they are −

  • Discrete Nature: The Poisson distribution is discrete, meaning it only takes on integer values.
  • Mean and Variance: The mean and variance of a Poisson distribution are both equal to .
  • Skewness: The distribution is skewed to the right, especially for smaller values of .

Poisson Distribution for Hypothesis Testing

Poisson distributions are often used in hypothesis testing, particularly in tests for counts of events.

One common test is the Poisson test, which is used to determine if the observed number of events is significantly different from the expected number. Here is an example using the scipy.stats.poisson() function.

Example

In this example, we perform a Poisson test to determine if the observed number of events (10) is significantly different from the expected rate (5). The p-value indicates the probability of obtaining a result at least as extreme as the observed result, assuming the null hypothesis is true −

from scipy.stats import poisson

# Observed number of events
observed_events = 10

# Expected number of events (mean rate )
expected_rate = 5

# Perform the Poisson test
p_value = poisson.sf(observed_events-1, expected_rate)
print("P-value from Poisson test:", p_value)

The output obtained is as shown below −

P-value from Poisson test: 0.03182805730620481

Seeding for Reproducibility

To ensure reproducibility, you can set a specific seed before generating Poisson distributions. This ensures that the same sequence of random numbers is generated each time you run the code.

Example

By setting the seed, you ensure that the random generation produces the same result every time the code is executed as shown in the example below −

import numpy as np

# Set the seed for reproducibility
np.random.seed(42)

# Generate 10 random samples from a Poisson distribution with =3
samples = np.random.poisson(lam=3, size=10)
print("Random samples with seed 42:", samples)

The result produced is as follows −

Random samples with seed 42: [4 1 3 3 2 3 2 3 0 2]
Advertisements