Plot the poisson distribution function for cumulative probability in python
In this article we will explore Poisson distribution and Poisson process in Python. Show
Table of contents
IntroductionTo continue following this tutorial we will need the following Python libraries: scipy, numpy, and matplotlib. If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:
What is a Poisson processA Poisson point process (or simply, Poisson process) is a collection of points randomly located in mathematical space. Due to its several properties, the Poisson process is often defined on a real line, where it can be considered a random (stochastic) process in one dimension. This further allows to build mathematical systems and study certain events that appear in a random manner. One of its important properties is that each point of the process is stochastically independent from other points in the process. As an example we can think of an example where such process can be observed in real life. Suppose you are studying the historical frequencies of hurricanes. This indeed is a random process, since the number of hurricanes this year is independent of the number of hurricanes las year and so on. However, over time you may be observing some trends, average frequency, and more. Mathematically speaking, in this case, the point process depends on something that might be some constant, such as average rate (average number of customers calling, for example). A Poisson process is defined by a Poisson distribution. What is a Poisson distribution?A Poisson distribution is a discrete probability distribution of a number of events occurring in a fixed interval of time given two conditions:
To put this in some context, consider our example of frequencies of hurricanes from the previous section. Assume that when we have data on observing hurricanes over a period of 20 years. We find that the average number of hurricanes per year is 7. Each year is independent of previous years, which means that if we observed 8 hurricanes this year, it doesn’t mean we will observe 8 next year. The PMF (probability mass function) of a Poisson distribution is given by: $$p(k, \lambda) = \frac{\lambda^{k}e^{-\lambda}}{k!}$$ where:
The \(Pr(X=k)\) can be read as: Poisson probability of k events in an interval. And the CDF (cumulative distribution function) of a Poisson distribution is given by: $$F(k, \lambda) = \sum^{k}_{i=0} \frac{\lambda^{i}e^{-\lambda}}{i!}$$ Poisson distribution exampleNow that we know some formulas to work with, let’s go through an example in detail. Recall the hurricanes data we mentioned in the previous sections. We know that the historical frequency of hurricanes is 7 per year (which is the rate, \(\mu\), and this forms our \(\lambda\) value (since \(\lambda=\mu\)): $$\lambda = 7$$ The question we can have is what is the probability of observing exactly 5 hurricanes this year? And this forms our \(k\) value: $$k = 5$$ Using the formula from the previous section, we can calculate the Poisson probability: $$p(5, 7) = \frac{(7^{5})(e^{-7})}{5!} = 0.12772 \approx 12.77\%$$ Therefore, the probability of observing exactly 5 hurricanes next year is equal to 12.77%. Naturally, we are curious about the probabilities of other frequencies. Poisson PMF (probability mass function)Consider the table below which shows the Poisson probability of hurricane frequencies (0-15):
Using the above table we can create the following visualization of the Poisson probability mass function for this example: Poisson PMFPoisson CDF (cumulative distribution function)Consider the table below which shows the Poisson cumulative probability of hurricane frequencies (0-15):
Using the above table we can create the following visualization of the Poisson cumulative distribution function for this example: Poisson CDFThe table also allows us to answer some interesting questions. For example, what if we wanted to find out the probability of seeing up to 5 hurricanes (mathematically: \(k\leq5\)), we can see that it’s \(0.30071\) or \(30.07\%\). On the other hand, we can be interested in probability of observing more than 5 hurricanes (mathematically: \(k>5\)), which would be \(1-p(5,7) = 1-0.30071 = 0.69929\) or \(69.93\%\). Poisson distribution example in PythonIn the previous section we computed probability mass function and cumulative distribution function by hand. In this section, we will reproduce the same results using Python. We will begin with importing the required dependencies:
Next we will need an array of the \(k\) values for which we will compute the Poisson PMF. In the previous section, we calculated it for 16 values of \(k\) from 0 to 16, so let’s create an array with these values:
You should get:
In the following sections we will focus on calculating the PMF and CDF using Python. Poisson PMF (probability mass function) in PythonIn order to calculate the Poisson PMF using Python, we will use the .pmf() method of the scipy.poisson generator. It will need two parameters:
And now we can create an array with Poisson probability values:
And you should get:
Note: If you want to print it in a nicer way with each \(k\) value and the corresponding probability:
And you should get:
which is exactly the same as we saw in the example where we calculated probabilities by hand. Plot Poisson PMF using PythonWe will need the k values array that we created earlier as well as the pmf values array in this step. Using matplotlib library, we can easily plot the Poisson PMF using Python:
And you should get: Poisson PMF using PythonPoisson CDF (cumulative distribution function) in PythonIn order to calculate the Poisson CDF using Python, we will use the .cdf() method of the scipy.poisson generator. It will need two parameters:
And now we can create an array with Poisson cumulative probability values:
And you should get:
Note: If you want to print it in a nicer way with each \(k\) value and the corresponding cumulative probability:
And you should get:
which is exactly the same as we saw in the example where we calculated cumulative probabilities by hand. Plot Poisson CDF using PythonWe will need the k values array that we created earlier as well as the pmf values array in this step. Using matplotlib library, we can easily plot the Poisson PMF using Python:
And you should get: Poisson CDF using PythonConclusionIn this article we explored Poisson distribution and Poisson process, as well as how to create and plot Poisson distribution in Python. Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Statistics articles. How do you plot a Poisson distribution in Python?Poisson distribution example in Python. import numpy as np import matplotlib. pyplot as plt from scipy. ... . k = np. ... . pmf = poisson. ... . for val, prob in zip(k,pmf): print(f"k-value {val} has probability = {prob}") ... . plt. ... . cdf = poisson. ... . for val, prob in zip(k,cdf): print(f"k-value {val} has probability = {prob}") ... . How do you find the Poisson cumulative probability?The Poisson cumulative distribution function lets you obtain the probability of an event occurring within a given time or space interval less than or equal to x times if on average the event occurs λ times within that interval. p = F ( x | λ ) = e − λ ∑ i = 0 f l o o r ( x ) λ i i ! .
How do you calculate cumulative probability in Python?The easiest way to calculate normal CDF probabilities in Python is to use the norm. cdf() function from the SciPy library. What is this? The probability that a random variables takes on a value less than 1.96 in a standard normal distribution is roughly 0.975.
|