Related Tags

python

# How to model the gamma distribution in Python

Joy Kareko

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

A random variable $X$ is said to have a gamma distribution, that is, $X\sim Gamma(\alpha,\lambda)$ if:

• It’s continuous.
• Its parameter $\lambda >0$. $\lambda$ is known as the rate or scale parameter. This parameter is the mean rate of an event’s occurrence during one unit.
• Its parameter $\alpha >0$. $\alpha$ is known as the shape parameter. This parameter specifies the number of events being modeled.
• Its PDF (probability density function) is given by $f(x) = \{\frac{\lambda ^\alpha x^{\alpha-1}e^{-\lambda x}\\}{\Gamma(\alpha)} ,x >0$ and $0$ elsewhere.

It’s important to note that when $\alpha = 1$, $f(x) = {\lambda}e^{-\lambda{x}}$ which is the exponential distribution. Further, when the shape parameter, $\alpha$, 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 $\lambda$, 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)
Use NumPy to model gamma distribution

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()
Use SciPy to model the gamma distribution

The code above uses SciPy to plot a gamma distribution with a scale of 100, replicating a normal distribution as earlier explained.

RELATED TAGS

python

CONTRIBUTOR

Joy Kareko