How to model the gamma distribution in Python
A random variable is said to have a gamma distribution, that is, if:
- It’s continuous.
- Its parameter . is known as the rate or scale parameter. This parameter is the mean rate of an event’s occurrence during one unit.
- Its parameter . is known as the shape parameter. This parameter specifies the number of events being modeled.
- Its PDF (probability density function) is given by and elsewhere.
It’s important to note that when , which is the exponential distribution. Further, when the shape parameter, , is extensive, the distribution becomes bell-shaped, mimicking the normal distribution. In these 2 cases, the gamma distribution becomes a special exponential and normal distribution.
Practical applications of the gamma distribution
Gamma distribution is used to model the time until an event occurs, given a constant rate , which is essentially the average rate of occurrence of that event. The events need to be independent of each other.
Thus, the gamma distribution is ideal for modeling situations such as the time until an accident occurs or rain falls, etc.
Code implementation in Python
There are two ways to model the gamma distribution in Python.
Use NumPy
import numpy as npimport matplotlib.pyplot as pltnum = np.random.gamma(shape = 2, scale = 2, size = 1000)plt.hist(num, bins = 50, density = True)
The code above uses NumPy to plot a gamma distribution of shape and scale of 2 and 1000 random variables of a gamma distribution.
Use SciPy
import matplotlib.pyplot as pltimport numpy as npfrom scipy.stats import gammaa = 100 #scale parameter (alpha)x = np.linspace(gamma.ppf(0.01, a),gamma.ppf(0.99, a), 100)plt.plot(x, gamma.pdf(x, a),'r-', lw=5, alpha=0.6, label='gamma pdf')plt.show()
The code above uses SciPy to plot a gamma distribution with a scale of 100, replicating a normal distribution as earlier explained.
Free Resources