# Python Scipy Gamma [10 Useful Examples]

In this Python tutorial, we will learn about the “Python Scipy Gamma“. In addition to learning how to generate and use gamma distribution, we will also cover the following subjects.

• What is Gamma
• Python Scipy Gamma Distribution
• Python Scipy Gamma
• Python Scipy Gamma Ppf
• Python Scipy Gamma Loc
• Python Scipy Gamma Quantile
• Python Scipy Gamma Sample
• Python Scipy Gamma Logpdf
• Python Scipy Gamma Cdf
• Python Scipy Gamma Fit
• Python Scipy Gamma Pdf

## What is Gamma

One of the distributions that are frequently used in the fields of business, science, and engineering is the gamma distribution, which is used to describe continuous variables that should have a positive and skewed distribution.

The beta distribution is connected to the gamma distribution, a type of statistical distribution. This distribution, in which the waiting intervals between Poisson distributed events are significant to one another, develops spontaneously.

Most often, the phrase “gamma distribution” refers to a distribution with continuous probability distributions and two parameters: shape parameter and inverse scale parameter. It has connections to the Erlang distribution, chi-squared distribution, exponential distribution, and normal distribution. “Γ” stands for the gamma function.

Alpha(α) and beta(β) are two free parameters in gamma distributions, where:

• Alpha(α): It is the shape parameter.
• Beta(β) = It is rate parameter.

Also, check: Python Scipy Stats Norm

## Python Scipy Gamma Distribution

The Python Scipy has a method `gamma()` within the module `scipy.special` that calculates the gamma of the given array. The generalized factorial function is what the gamma function is known as.

The syntax is given below.

``scipy.special.gamma(z)``

Where a parameter `z` is an argument with a real or complex value of type array.

The method `gamma()` returns the gamma function’s values of type ndarray or scalar.

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

Import the libraries using the below python code.

``from scipy import special``

Create an array of data and pass the array to a method `gamma()` as shown below the code.

``special.gamma([5,1,0.5,1])``

Define a complex number and compute the gamma of that number using the below code.

``special.gamma(1.6 + 2j)``

As we can see in the above output, we have calculated the gamma values of the array and complex numbers.

This is how to compute the gamma value of the given array or complex number using the method `gamma()` of Python SciPy.

## Python Scipy Gamma

The `scipy.stats.gamma` represents the continuous random variable that is gamma. It has different kinds of functions for normal distribution like CDF, PDF, median, etc.

It has two important parameters `loc` for the mean and `scale` for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

``scipy.stats.gamma.method_name(data,loc,size,moments,scale)``

Where parameters are:

• data: It is a set of points or values that represent evenly sampled data in the form of array data.
• a: It is the shape parameter of the gamma.
• 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.
• scale: It is used to specify the standard deviation, by default it is 1.

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

• scipy.stats.gamma.cdf(): It is used for the cumulative distribution function.
• scipy.stats.gamma.pdf(): It is used for the probability density function.
• scipy.stats.gamma.rvs(): To get the random variates.
• scipy.stats.gamma.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
• scipy.stats.gamma.logpdf(): It is used to get the log related to the probability density function.
• scipy.stats.gamma.logcdf(): It is used to find the log related to the cumulative distribution function.
• scipy.stats.gamma.sf(): It is used to get the values of the survival function.
• scipy.stats.gamma.isf(): It is used to get the values of the inverse survival function.
• scipy.stats.gamma.logsf(): It is used to find the log related to the survival function.
• scipy.stats.gamma.mean(): It is used to find the mean related to the normal distribution.
• scipy.stats.gamma.median(): It is used to find the median related to the normal distribution.
• scipy.stats.gamma.var(): It is used to find the variance related to the distribution.
• scipy.stats.gamma.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.

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

Create observation data values and calculate the `probability density function` from these data values with `mean = 0` and `standard deviation = 1`.

``````a=1.5
observatin_x = np.linspace(-4,4,200)
pdf_gamma = stats.gamma.pdf(observatin_x,a,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,pdf_gamma)
plt.xlabel('x-values')
plt.ylabel('PDF_gamma_values')
plt.title("Probability density funciton of gamma distribution")
plt.show()``````

This is how to generate a Gamma distribution using the method `gamma()` of Python Scipy.

## Python Scipy Gamma Ppf

The object `gamma()` has a method `ppf()` that calculate the Percent point function of gamma. In other words, The method `norm.ppf()` accepts a percentage and returns a standard deviation multiplier for the value that percentage occurs at.

The syntax is given below.

``scipy.stats.gamma.ppf(q,loc,size,scale)``

Where parameters are:

• q: It is a percentage.
• a: Shape parameter
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to specify the standard deviation, by default it is 1.

Let’s understand with an example by following the below code.

``````from scipy.stats import gamma
gamma.ppf(0.99, 1.5, loc=0, scale=1)``````

The above code gives a one-tail test result with a 99% confidence interval for a gamma distribution.

## Python Scipy Gamma Loc

The Python Scipy method `gamma()` accept the parameter `loc` which is the mean of the distribution. Using the `loc` of method `gamma()`, we can shift the distribution.

Let’s see with an example to shift the distribution at a different location by following the below steps:

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

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

Create observation data values and calculate the `probability density function` from these data values with `loc` or `mean = 0` and `standard deviation = 1`.

``````a=1.5
observatin_x = np.linspace(-1,1,300)
pdf_gamma = stats.gamma.pdf(observatin_x,a,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,pdf_gamma)
plt.xlabel('x-values')
plt.ylabel('PDF_gamma_values')
plt.title("Probability density funciton of gamma distribution")
plt.show()``````

Now change the mean or loc value to a different value or as equal to 0.5 using the blow code.

``````a=1.5
observatin_x = np.linspace(-1,1,300)
pdf_gamma = stats.gamma.pdf(observatin_x,a,loc=1,scale=1)``````

Again plot the distribution with mean or loc equal to 0.5 in the above code using the below code to see the change in the location of the distribution.

``````plt.plot(observatin_x,pdf_gamma)
plt.xlabel('x-values')
plt.ylabel('PDF_gamma_values')
plt.title("Probability density funciton of gamma distribution")
plt.show()``````

From the output, we can see that the distribution is shifted towards the right side when we use the parameter loc equal to 0.5.

## Python Scipy Gamma Quantile

The set of values or points called quantiles is used to partition the dataset into equal-sized groups. Here in this section, we will generate a sample from gamma dist and pass this sample to the method `numpy.quantile()` to compute the quantile of the sample.

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

``````from scipy import stats
import numpy as np``````

Generate data from gamma dist using the below code.

``````a=0.5
gamma_dist = stats.gamma(a)
data = gamma_dist.rvs(1000)``````

Now compute the quantile of the above data using the below code.

``np.quantile(data,0.25)``

The above code returns the first quartile of the sample or data.

This is how to compute the quantile of the data from gamma dist.

## Python Scipy Gamma Sample

The method `rvs()` of Python Scipy of the object `gamma` is random variates that generate random numbers or samples from a gamma distribution.

The syntax is given below

``scipy.stats.gamma.rvs(loc=0, scale=1, size=1, random_state=None)``

Where parameters are:

• loc: It is a mean.
• scale: The distribution’s matrix of covariance.
• size(int): It is the sample size.
• random_state(int): If the seed is None, the NumPy.random method is utilized (or np.random). It uses a single instance of RandomState. If the seed is an integer, a new RandomState object is made using the seed. If the seed already has a Generator or RandomState instance, that instance is used.

Let’s draw a random sample from a multivariate normal distribution by following the below steps:

Import the required libraries using the below python code.

``from scipy import stats``

Create a gamma distribution using the below code.

``````a=0.5
gamma_dist = stats.gamma(a)``````

Generate random numbers using normal distribution using the below code.

``````samp_size = 100000
gamma_dist.rvs(samp_size)``````

This is how to generate a sample using the method `gamma.rvs()` of Python Scipy.

## Python Scipy Gamma Cdf

The method `pdf()` of Python Scipy of object `gamma` compute the cumulative distribution of gamma.

The syntax is given below.

``scipy.stats.gamma(x, a, loc, scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• a: It is a shape parameter for the distribution.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to specify the standard deviation, by default it is 1.

Import the required libraries using the below python code.

``````import numpy as np
from scipy.stats import gamma
import matplotlib.pyplot as plt
%matplotlib inline``````

Create an array containing the values between -2 to 2 with a difference of 0.3 with shape parameters = 1.5 using the below code.

``````a = 1.0
x_array = np.arange(-2, 2, 0.3)``````

Comput the pdf by providing the created array of data to a method `gamma.cdf()` with parameters value loc = 0 and scale = 1 using the below code.

``y_cdf = gamma.cdf(x_array,a,0,1)``

Now plot the distribution using the below code.

``````plt.plot(x_array, y_cdf)
plt.show()``````

This is how to use the method `gamma.cdf()` of Python Scipy to compute the cumulative distribution of gamma.

## Python Scipy Gamma Fit

The method `fit()` of Python Scipy of object `gamma` that provides approximations for scale and location.

The syntax is given below.

``scipy.stats.gamma.fit(data)``

Where parameter data is the data for which we need the location and scale.

Let’s understand with an example by following steps:

Import the required libraries or methods using the below code.

``from scipy.stats import gamma``

Generate random numbers using the method `gamma.rvs()`.

``x_data = gamma.rvs(1., 2., size=500, random_state=123)``

Now fit the above data using the below code.

``loc_, scale_ = gamma.fit(x_data)``

Check the estimated parameter values using the below code.

``````print("loc is ",res)
print("Scale is ",res)``````

This is how to get the approximation for the parameter location and scale using the method `gamma.fit()` of Python Scipy.

## Python Scipy Gamma Pdf

As we have learned in the above Python Scipy subsection the object `gamma` has many methods like CDF, PDF, ISF, etc, to generate a different kind of gamma distribution. Here we will use one of the methods that are `scipy.stats.gamma.pdf()` to compute the `Probability Density Funtion` a given distribution.

The syntax is given below.

``scipy.stats.gamma.pdf(x,a,loc,scale)``

Where parameters are:

• x: It is a set of points or values that represent sampled data in the form of array data.
• a: It is the shape parameter of the gamma distribution.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to specify the standard deviation, by default it is 1.

Let’s take an example of how to compute the pdf of a given distribution by following the below steps:

Import the required libraries using the below python code.

``````import numpy as np
from scipy.stats import gamma
import matplotlib.pyplot as plt
%matplotlib inline``````

Create an array containing the values between -5 to 5 with a difference of 0.3 with shape parameters = 1.5 using the below code.

``````a = 1.5
x_array = np.arange(-5, 5, 0.3)``````

Comput the pdf by providing the created array of data to a method `gamma.pdf()` with parameters value loc = 0 and scale = 1 using the below code.

``y_pdf = gamma.pdf(x_array,a,0,1)``

Now plot the distribution using the below code.

``````plt.plot(x_array, y_pdf)
plt.show()``````

This is how to compute the pdf of the gamma distribution using the method `gamma.pdf()` of Python Scipy.

## Python Scipy Gamma Logpdf

The object `gamm()` has a method `logpdf()` that calculates the log probability density of the gamma.

The syntax is given below.

``scipy.stats.gamma.logpdf(x,a,loc,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• a: It is the shape parameter of gamma.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

The above parameters are the standard parameter of all the methods in the object `scipy.stats.gamma()`. The methods are given below.

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.

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

Create observation data values and calculate the log probability from these data values with `mean = 0` and `standard deviation = 1`.

``````a=1.5
observatin_x = np.linspace(-2,2,200)
logpdf_norm = stats.gamma.logpdf(observatin_x,a,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,logpdf_norm)
plt.xlabel('x-values')
plt.ylabel('logpdf_gamma_values')
plt.title("Log probability of gamma distribution")
plt.show()``````

This is how to compute the logpdf of gamma distribution using the method `gamma.logpdf()` of Python Scipy.

## Python Scipy Gamma Logcdf

The object `gamm()` has a method `logcdf()` that calculates the cumulative distribution of the gamma.

The syntax is given below.

``scipy.stats.gamma.logcdf(x,a,loc,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• a: It is the shape parameter of gamma.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

The above parameters are the standard parameter of all the methods in the object `scipy.stats.gamma()`. The methods are given below.

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.

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

Create observation data values and calculate the log cumulative from these data values with `mean = 0` and `standard deviation = 1`.

``````a=1.5
observatin_x = np.linspace(-10,10,200)
logcdf_norm = stats.gamma.logcdf(observatin_x,a,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,logcdf_norm)
plt.xlabel('x-values')
plt.ylabel('logcdf_gamm_values')
plt.title("Log cumulative of gamma distribution")
plt.show()``````

This is how to compute the logcdf of the gamma distribution using the method `gamma.logcdf()` of Python Scipy.

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

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

• What is Gamma
• Python Scipy Gamma Distribution
• Python Scipy Gamma
• Python Scipy Gamma Ppf
• Python Scipy Gamma Loc
• Python Scipy Gamma Quantile
• Python Scipy Gamma Sample
• Python Scipy Gamma Logpdf
• Python Scipy Gamma Cdf
• Python Scipy Gamma Fit
• Python Scipy Gamma Pdf