Poisson Distribution

📊 Poisson Distribution in Python

The Poisson Distribution models the number of events occurring in a fixed interval of time or space, assuming the events happen independently at a constant rate.

It’s widely used in traffic flow, call center arrivals, and natural event counts.


✅ 1. Characteristics of Poisson Distribution

  • λ (lam) → expected number of events per interval

  • size → number of random samples

  • Values are non-negative integers: 0,1,2,…

  • Probability Mass Function (PMF):

P(X=k)=e−λλkk!P(X=k) = \frac{e^{-\lambda} \lambda^k}{k!}

Where k=0,1,2,…k = 0,1,2,…


✅ 2. Generate Poisson Data Using NumPy

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Parameters
lam = 5 # expected number of events
size = 1000 # number of samples

# Generate random Poisson data
data = np.random.poisson(lam, size)

print(data[:10]) # first 10 values

Output (example):

[5 4 3 7 6 5 2 6 4 5]

✅ 3. Visualize Poisson Distribution

sns.histplot(data, bins=range(0, max(data)+2), kde=False, color='green')
plt.title(f'Poisson Distribution (λ={lam})')
plt.xlabel('Number of Events')
plt.ylabel('Frequency')
plt.show()
  • Peaks near λ

  • Discrete integer values


✅ 4. Change λ to See Effect

data_small = np.random.poisson(lam=2, size=1000)
data_large = np.random.poisson(lam=10, size=1000)

sns.histplot(data_small, bins=range(0, max(data_small)+2), color='red', label='λ=2', kde=False)
sns.histplot(data_large, bins=range(0, max(data_large)+2), color='blue', label='λ=10', kde=False)
plt.legend()
plt.title('Poisson Distribution Comparison')
plt.show()

  • Smaller λ → peak closer to 0

  • Larger λ → distribution spreads and peak moves right


✅ 5. Compute Mean and Variance

mean = np.mean(data)
var = np.var(data)
print("Mean:", mean)
print("Variance:", var)

Theoretical property of Poisson:

  • Mean = Variance = λ


🎯 Practice Exercises

  1. Model number of calls per hour with λ=3 for 1000 hours.

  2. Generate Poisson data with λ=7 and plot histogram.

  3. Compare λ=2, λ=5, λ=10 in one plot.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *