Python - Poisson Discrete Distribution in Statistics

Python - Poisson Discrete Distribution in Statistics

In statistics, the Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space if these events occur with a known constant mean rate and independently of the time since the last event.

Python's scipy.stats module contains a poisson function that you can use to work with Poisson distributions. Below is an example of how to use the poisson function to calculate probabilities and other statistics for a Poisson distribution.

First, ensure you have the required libraries installed:

pip install scipy numpy matplotlib 

Now, you can use the following script to work with the Poisson distribution:

from scipy.stats import poisson import matplotlib.pyplot as plt import numpy as np # Assume the average rate of events (lambda) is 3 rate = 3 # Create a Poisson distribution object with this rate dist = poisson(rate) # Calculate the probability of k events k = 5 prob_k = dist.pmf(k) # Probability mass function print(f"Probability of {k} events: {prob_k}") # Calculate the cumulative probability up to k events cum_prob_k = dist.cdf(k) # Cumulative distribution function print(f"Cumulative probability of up to {k} events: {cum_prob_k}") # Generate random samples from the distribution samples = dist.rvs(size=1000) # Random variates # Calculate mean and variance mean, var = dist.stats(moments='mv') print(f"Mean: {mean}, Variance: {var}") # Plot the probability mass function k_values = np.arange(0, 10) probabilities = dist.pmf(k_values) plt.bar(k_values, probabilities) plt.xlabel('Number of Events') plt.ylabel('Probability') plt.title('Poisson Distribution PMF') plt.show() 

This script:

  1. Sets up a Poisson distribution with an average rate (lambda) of 3 events per interval.
  2. Calculates the probability of exactly 5 events occurring.
  3. Calculates the cumulative probability of up to 5 events occurring.
  4. Generates 1000 random variates from this distribution and prints out the sample mean and variance.
  5. Finally, plots the probability mass function (PMF) of the distribution for a range of values.

This distribution is widely used in various fields such as telecommunications, astronomy, and biology to model random events.


More Tags

compiled infinite selecteditem sample android-textureview android-theme dart-http alphabetical three.js elasticsearch

More Programming Guides

Other Guides

More Programming Examples