# Mastering Probability with the Poisson Distribution in Python

Probability is a concept that deals with the likelihood of events and outcomes happening. When it comes to analyzing data and making informed decisions, having a good understanding of probability is essential.

One of the key components of probability is the Poisson distribution, which is a mathematical model that describes the probability of a certain number of events occurring in a fixed period of time. In this article, we will explore the basics of the Poisson distribution, including how to generate it and calculate probabilities, with a practical example.

## Poisson Distribution Basics:

The Poisson distribution is named after the French mathematician Simon Denis Poisson, who first introduced it in the early 19th century. It is a discrete probability distribution that predicts the probability of a certain number of independent events occurring during a fixed period of time or space, given the average rate at which they occur.

The Poisson distribution is often used in a variety of fields, including physics, biology, economics, and finance.

### Generating a Poisson Distribution:

To generate a Poisson distribution, we need to know two things – the mean rate or average number of events per unit of time/space (usually denoted by λ), and the sample size or number of units of time/space we want to examine.

One way to generate a Poisson distribution is to use the poisson.rvs() function in Python. This function generates random numbers from a Poisson distribution, based on the given mean and sample size.

For example, let’s say we want to generate a Poisson distribution with a mean of 3 and a sample size of 10. We can use the following code:

import numpy as np
from scipy.stats import poisson

mean = 3
size = 10

X = poisson.rvs(mu=mean, size=size)

print(X)

This code will output a list of 10 random numbers generated from a Poisson distribution with a mean of 3.

The output may vary each time we run the code, but they will be centered around the mean of 3.

### Calculating Probabilities with Poisson Distribution:

Once we have generated a Poisson distribution, we can use it to calculate probabilities of certain events occurring.

There are two main functions used for this – the probability mass function (pmf) and the cumulative distribution function (cdf). The pmf gives the probability of a specific number of events occurring, while the cdf gives the probability of getting up to a certain number of events.

For example, let’s say we have a store that sells apples and that it sells an average of 5 apples per day. We want to find the probability of the store selling exactly 3 apples in a day, given this average rate.

To do this, we can use the poisson.pmf() function in Python. The code would be:

mean = 5
k = 3

p = poisson.pmf(k=k, mu=mean)

print(p)

This code will output the probability of the store selling exactly 3 apples in a day, given that the average rate of sales is 5.

Depending on the values of the mean and k, the output may vary, but it will always be a value between 0 and 1.

### Example 1: Probability Equal to Some Value:

Using the store and apple sales example, let’s say we now want to find the probability that the store sells exactly 7 apples in a day.

Using the same formula as above, but replacing k with 7, we get:

mean = 5
k = 7

p = poisson.pmf(k=k, mu=mean)

print(p)

The output of this code will be a very small probability value, since 7 apples is quite far from the average rate of 5. This means that it is highly unlikely for the store to sell exactly 7 apples in a day.

## Conclusion:

Probability and the Poisson distribution are important concepts that can help us make informed decisions based on data. By understanding how to generate a Poisson distribution and use it to calculate probabilities, we can gain valuable insights into different scenarios.

Whether it’s predicting sales at a store or analyzing patterns in radioactive decay, the Poisson distribution can provide a useful tool for analysis. By utilizing the functions and methods outlined in this article, we can make more informed decisions and better understand the world around us.

### Example 2: Probability Less than Some Value

Let’s continue with our store examples, but this time with a different product. Imagine that the store sells footballs and on average, it sells 2 footballs per day.

We want to find the probability that the store sells less than 3 footballs in a day, given this average rate. To find this probability, we can use the poisson.cdf() function in Python, which gives us the cumulative distribution function up to a certain number of events.

### The code would be:

mean = 2
k = 2

p = poisson.cdf(k=k, mu=mean)

print(p)

This code will output the probability that the store sells less than 3 footballs in a day, which in this case is approximately 0.72. This means that there is a relatively high probability of the store selling less than 3 footballs in a day, given the average rate of 2.

### Example 3: Probability Greater than Some Value

Lastly, let’s consider a scenario where the store sells a less common item – tuna. On average, the store sells 1 tuna per day.

We want to find the probability that the store sells more than 3 tuna in a day, given this average rate. To find this probability, we can use the complementary cumulative distribution function (ccdf) which is simply 1 minus the cdf, or we can subtract the probability of getting 3 or fewer tuna from 1.

In Python, this would look like:

mean = 1
k = 3

p = 1 - poisson.cdf(k=k-1, mu=mean)

print(p)

Alternatively,

mean = 1
k = 3

p = 1 - poisson.cdf(k=k, mu=mean)

print(p)

This code will output the probability that the stores sells more than 3 tuna in a day, which in this case is approximately 0.061. This means that it is relatively unlikely for the store to sell more than 3 tuna in a day, given the low average rate of 1.

## Conclusion:

In conclusion, the Poisson distribution is a powerful tool for analyzing events that happen over a fixed period of time or space. The ability to generate random samples and calculate probabilities allows us to make data-driven decisions in a variety of fields.

By using the poisson.rvs() function in Python, we can generate random samples that follow the Poisson distribution given a mean and sample size. Using the poisson.pmf() function, we can calculate the probability of a specific number of events occurring.

And by using the poisson.cdf() function, we can calculate the probability of getting up to a certain number of events, less than a certain number of events, or greater than a certain number of events. The Poisson distribution may be applied to many industries including insurance where it can be used to accurately predict the number of future claims.

By understanding how to generate and analyze data using the Poisson distribution, we can gain useful insights and make more informed decisions.

## 5) Plotting a Poisson Distribution

In addition to generating and calculating probabilities from a Poisson distribution, it is also useful to visualize the distribution using a plot. In Python, we can use the matplotlib library to create a Poisson distribution plot.

To create the plot, we need to define the mean rate or average number of events per unit of time/space and the range of values for which we want to plot probabilities. We can use the poisson.pmf() function to create a list of probabilities for each value in the range, and then use matplotlib to create a plot.

### The syntax for creating a Poisson distribution plot is as follows:

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

mean = 5
X = np.arange(0, 15)
poisson_pmf = poisson.pmf(X, mean)
plt.plot(X, poisson_pmf)
plt.show()

This code will create a Poisson distribution plot with a mean of 5, where the X-axis represents the number of events and the Y-axis represents the probability. The plot will be generated using the list of probabilities generated by the poisson.pmf() function.

The Poisson distribution is a widely used probability distribution that is useful for analyzing events that happen over a fixed period of time or space. In addition to the functions and techniques described above, Python offers a range of libraries that allow us to work with the Poisson distribution, including NumPy, SciPy, and Matplotlib.

These libraries provide a range of tools and methods for working with data and probability distributions. Further resources for learning about the Poisson distribution and using Python to analyze data include:

By utilizing these resources, we can gain a deeper understanding of the Poisson distribution and how to apply it to real-world scenarios using Python.

In conclusion, the Poisson distribution is a powerful tool for analyzing events that happen over a fixed period of time or space. By using Python with libraries such as NumPy, SciPy, and Matplotlib, we can generate random samples, calculate probabilities, and visualize the distribution using plots.

Understanding the Poisson distribution can be useful in a variety of fields such as insurance, finance, and nuclear physics. The main takeaways are that the Poisson distribution depends on the mean rate or average number of events per unit of time/space, and that its probability density function describes the likelihood of a certain number of events occurring.

By using the methods and resources described in this article, we can make data-driven decisions and gain valuable insights into real-world scenarios.