A random variable is said to have a gamma distribution, that is, if:
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.
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.
There are two ways to model the gamma distribution in Python.
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.
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