Python Scipy Stats Poisson – Useful Guide

In this Python tutorial, we will learn about the “Python Scipy Stats Poisson” to compute the CDF, PDF, Logpdf, etc of the Poisson distribution. And additionally, we will cover the following topics.

  • What is a Poisson Distribution
  • Python Scipy Stats Poisson CDF
  • Python Scipy Stats kstest Poisson
  • Python Scipy Stats Zero Inflated Poisson
  • Python Scipy Stats Poisson Pmf
  • Python Scipy Stats Poisson Rvs
  • Python Scipy Stats Poisson Logcdf
  • Python Scipy Stats Poisson Logpmf
  • Python Scipy Stats Poisson Logsf

What is Poisson Distribution

A Poisson distribution is a type of probability distribution used in statistics to determine how frequently an event will occur over a certain time period. It is a distribution of counts, in other words.

Poisson distributions are frequently used to comprehend independent occurrences that happen at a steady rate throughout a certain period. It bears Siméon Denis Poisson’s name, a French mathematician.

Given that the Poisson distribution is a discrete function, only certain values in a (possibly endless) list are possible for the variable. In other words, the variable is not capable of accepting all values across any continuous range.

The variable cannot take any fractional or decimal values for the Poisson distribution; it can only take whole integer values (0, 1, 2, 3, etc.).

Read: Python Scipy Kdtree

Python Scipy Stats Poisson

The scipy.stats.poisson represents the discrete random variable. It has different kinds of functions of distribution like CDF, median, etc.

It has one important parameter loc for the mean for shifting the distribution using these parameters.

The syntax is given below.

scipy.stats.poisson.method_name(mu,k,loc,moments)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • k: It is the data.
  • loc: It is used to specify the mean, by default it is 0.
  • moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.

The above parameters are the common parameter of all the methods in the object scipy.stats.poisson(). The methods are given below.

  • scipy.stats.poisson.cdf(): It is used for the cumulative distribution function.
  • scipy.stats.poisson.rvs(): To get the random variates.
  • scipy.stats.poisson.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
  • scipy.stats.poisson.logPDF(): It is used to get the log related to the probability density function.
  • scipy.stats.poisson.logCDF(): It is used to find the log related to the cumulative distribution function.
  • scipy.stats.poisson.sf(): It is used to get the values of the survival function.
  • scipy.stats.poisson.isf(): It is used to get the values of the inverse survival function.
  • scipy.stats.poisson.logsf(): It is used to find the log related to the survival function.
  • scipy.stats.poisson.mean(): It is used to find the mean related to the normal distribution.
  • scipy.stats.poisson.medain(): It is used to find the median related to the normal distribution.
  • scipy.stats.poisson.var(): It is used to find the variance related to the distribution.
  • scipy.stats.poisson.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

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

Code creates a variable for the shape parameters and assigns some values.

mu = 0.5

Create an array of data using the method ppf() of an object poisson using the below code.

array_data = np.linspace(poisson.ppf(0.01, mu),
                poisson.ppf(0.90, mu))
array_data
Scipy Poisson Example
Scipy Stats Poisson Example

Now plot the probability mass function by accessing the method pdf() of an object poisson of the module scipy.stats using the below code.

fig, ax = plt.subplots(1, 1)
ax.plot(array_data, poisson.pmf(array_data, mu), 'bo',ms=8,label='poisson pmf')
ax.vlines(array_data, 0,poisson.pmf(array_data, mu),colors='b', lw=4, alpha=0.5,)
Scipy Poisson
Scipy Stats Poisson

This is how to use the object poission to create a distribution with the help of Python SciPy.

Also, check: Python Scipy Stats Kurtosis 

Python Scipy Stats Poisson CDF

The object poisson has a method cdf() to compute the cumulative distribution of the Poisson distribution.

The syntax is given below.

scipy.stats.poisson.cdf(mu,k,loc)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • k: It is the data.
  • loc: It is used to specify the mean, by default it is 0.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

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

Code creates a variable for the shape parameters and assigns some values.

mu = 5

Create an array of data using the method ppf() of an object poisson using the below code.

array_data = np.linspace(poisson.ppf(0.01, mu),
                poisson.ppf(0.90, mu))

Now graph the cumulative distribution by accessing the method cdf() of an object poisson of the module scipy.stats using the below code.

fig, ax = plt.subplots(1, 1)
ax.plot(array_data, poisson.cdf(array_data, mu), 'bo',ms=8,label='poisson cdf')
ax.vlines(array_data, 0,poisson.pmf(array_data, mu),colors='b', lw=4, alpha=0.5,)
Scipy Stats Poisson CDF
Scipy Stats Poisson CDF

Read: Python Scipy Minimize

Python Scipy Stats Poisson Pmf

To determine a particular Poisson Distribution’s probability mass function value for a random variable. The Python Scipy has a method pmf() in module scipy.stats.

The syntax is given below.

scipy.stats.poisson.pmf(mu,k,loc)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • loc: It is used to specify the mean, by default it is 0.
  • size: It is the sample size.

Let’s take an example by following the below steps:

Import the required libraries using the below code.

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

Code creates a variable for the shape parameters and assigns some values.

mu = 15

Create an array of data using the method ppf() of an object poisson using the below code.

array_data = np.linspace(poisson.ppf(0.01, mu),
                poisson.ppf(0.90, mu))

Now plot the probability mass function by accessing the method pdf() of an object poisson of the module scipy.stats using the below code.

fig, ax = plt.subplots(1, 1)
ax.plot(array_data, poisson.pmf(array_data, mu), 'bo',ms=8,label='poisson pmf')
ax.vlines(array_data, 0,poisson.pmf(array_data, mu),colors='b', lw=4, alpha=0.5,)
Python Scipy Stats Poisson Pmf
Python Scipy Stats Poisson Pmf

Read: Python Scipy Confidence Interval

Python Scipy Stats Poisson Rvs

The method rvs() of Python Scipy of object poisson generate random numbers or samples from the Poisson distribution.

The syntax is given below.

scipy.stats.poisson.cdf(mu,loc,size)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • loc: It is used to specify the mean, by default it is 0.
  • size: It is the sample size.

Let’s understand with an example how to generate random numbers using the method rvs() of object Poisson by following the below steps:

Import the required libraries or methods using the below python code.

from scipy.stats import poisson

Define the mu and sample size equal to 5000 using the below code.

mu = 6
random_num = poisson.rvs(mu,size = 500)

Display the generated random numbers using the below code.

random_num
Python Scipy Stats Poisson Rvs
Python Scipy Stats Poisson Rvs

This is how to generate the random numbers using the method rvs() of Python Scipy.

Read: Python Scipy Chi-Square Test

Python Scipy Stats Poisson Logcdf

The method logcdf() in a module scipy.stats.poisson of Python Scipy computes the log of the cumulative distribution of Poisson distribution.

The syntax is given below.

scipy.stats.poisson.logcdf(mu,k,loc)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • k: It is the data.
  • loc: It is used to specify the mean, by default it is 0.

Let’s take an example by following the below steps:

Import the required libraries using the below code.

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

Creates a variable for the shape parameters and assigns some values.

mu = 20

Create an array of data using the method ppf() of an object poisson using the below code.

array_data = np.linspace(poisson.ppf(0.01, mu),
                poisson.ppf(0.90, mu))

Now plot the log of CDF of Poisson distribution by accessing the method logcdf() of an object poisson of the module scipy.stats using the below code.

fig, ax = plt.subplots(1, 1)
ax.plot(array_data, poisson.logcdf(array_data, mu), 'bo',ms=8,label='poisson logcdf')
ax.vlines(array_data, 0,poisson.logcdf(array_data, mu),colors='b', lw=5, alpha=0.5,)
Pyton Scipy Stats Poisson Logcdf
Pyton Scipy Stats Poisson Logcdf

Read: Python Scipy FFT

Python Scipy Stats Poisson Logpmf

The method logpmf() in a module scipy.stats.poisson of Python Scipy computes the log of the probability of Poisson distribution.

The syntax is given below.

scipy.stats.poisson.logpmf(mu,k,loc)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • k: It is the data.
  • loc: It is used to specify the mean, by default it is 0.

Let’s take an example by following the below steps:

Import the required libraries using the below code.

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

Creates a variable for the shape parameters and assigns some values.

mu = 5

Create an array of data using the method ppf() of an object poisson using the below code.

array_data = np.linspace(poisson.ppf(0.01, mu),
                poisson.ppf(0.90, mu))

Now plot the log of pmf of Poisson distribution by accessing the method logpmf() of an object poisson of the module scipy.stats using the below code.

fig, ax = plt.subplots(1, 1)
ax.plot(array_data, poisson.logpmf(array_data, mu), 'bo',ms=8,label='poisson logpmf')
ax.vlines(array_data, 0,poisson.logpmf(array_data, mu),colors='b', lw=5, alpha=0.5,)
Python Scipy Stats Poisson Logpmf
Python Scipy Stats Logpmf

Read: Python Scipy Matrix + Examples

Python Scipy Stats Poisson Logsf

The method logsf() in a module scipy.stats.poisson of Python Scipy computes the log of the survival function of the Poisson distribution.

The syntax is given below.

scipy.stats.poisson.logsf(mu,k,loc)

Where parameters are:

  • mu: It is used to define the shape parameter.
  • k: It is the data.
  • loc: It is used to specify the mean, by default it is 0.

Let’s take an example by following the below steps:

Import the required libraries using the below code.

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

Creates a variable for the shape parameters and assigns some values.

mu = 8

Create an array of data using the method ppf() of an object poisson using the below code.

array_data = np.linspace(poisson.ppf(0.01, mu),
                poisson.ppf(0.90, mu))

Now plot the log of sf of Poisson distribution by accessing the method logsf() of an object poisson of the module scipy.stats using the below code.

fig, ax = plt.subplots(1, 1)
ax.plot(array_data, poisson.logsf(array_data, mu), 'bo',ms=8,label='poisson logpmf')
ax.vlines(array_data, 0,poisson.logsf(array_data, mu),colors='b', lw=5, alpha=0.5,)
Python Scipy Stats Poisson Logsf
Python Scipy Stats Logsf

Read: Scipy Ndimage Rotate

Python Scipy Stats kstest Poisson

The method kstest() of Python Scipy in a module scipy.stats that performs the Kolmogorov-Smirnov test for goodness of fit with one or two samples.

A sample’s underlying distribution F(x) is compared to a given distribution G using the one-sample test (x). The underlying distributions of two distinct samples are compared using the two-sample test. Only continuous distributions can be used for either test’s validity.

The syntax is given below.

scipy.stats.kstest(rvs, cdf, args=(), N=10, alternative='less', mode='exact')

Where parameters are:

  • rvs(str, array_data): If an array, it should be a 1-D array of random variable observations. A function that produces random variables should be called, and it must have a keyword argument size. If a string, it has to be the name of the distribution in scipy.stats that will be used to produce random numbers.
  • cdf(str, array_data): The two-sample test is run (and the rvs must be array_like) if the data is array_like, which should be a 1-D array of observations of random variables. If there is a callable, the cdf is computed using that callable. If a string, it needs to be the name of distribution in scipy.stats since that will be the cdf function.
  • args(sequence, tuple): When rvs or cdf are callables or strings, distribution parameters are used.
  • N(int): If rvs is a callable or string, the sample size. By default, 20.
  • alternative(less, greater, two-sided): Explains the alternative and null hypotheses. “Two-sided” is the default.
  • mode(): Specifies the distribution that will be utilized to determine the p-value. There are the following choices (‘auto’ is the default): ‘exact’, ‘approx’, ‘auto’ and ‘asymp’.

The method kstest() returns statistic and p-value of type float.

Let’s create a Poisson distribution to produce a random distribution of numbers. To ascertain whether it resembles a Poisson distribution, use the KS test by following the below steps:

Import the required libraries using the below python code.

from scipy import stats

Create a Poisson distribution using the code below to create a random distribution of numbers.

num_size = 500
lambda_Poisson = 10

data = stats.poisson.rvs(size = num_size, mu = lambda_Poisson)

Now see if it resembles a Poisson distribution, and perform the KS test using the below code.

kstest_statistic, p_value = stats.kstest(data, 'poisson', args=(lambda_Poisson,0))
print(kstest_statistic, p_value)
Python Scipy Stats kstest Poisson
Python Scipy Stats kstest Poisson

Also, take a look at some more Python SciPy tutorials.

So, in this tutorial, we have learned about the “Python Scipy Stats Poisson” and also covered the following topics.

  • What is a Poisson Distribution
  • Python Scipy Stats Poisson CDF
  • Python Scipy Stats kstest Poisson
  • Python Scipy Stats Zero Inflated Poisson
  • Python Scipy Stats Poisson Pmf
  • Python Scipy Stats Poisson Rvs
  • Python Scipy Stats Logcdf
  • Python Scipy Stats Logpmf
  • Python Scipy Stats Logsf