Year-End Discount: 10% OFF 1-year and 20% OFF 2-year subscriptions!

Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to model the gamma distribution in Python

Joy Kareko

Tired of LeetCode? 😩

Learn the 24 patterns to solve any coding interview question without getting lost in a maze of LeetCode-style practice problems. Practice your skills in a hands-on, setup-free coding environment. 💪

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

  • It’s continuous.
  • Its parameter λ>0\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 α>0\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)={λαxα1eλxΓ(α),x>0f(x) = \{\frac{\lambda ^\alpha x^{\alpha-1}e^{-\lambda x}\\}{\Gamma(\alpha)} ,x >0 and 00 elsewhere.

It’s important to note that when α=1\alpha = 1, f(x)=λeλxf(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 np
import matplotlib.pyplot as plt
num = 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 plt
import numpy as np
from scipy.stats import gamma
a = 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
Copyright ©2022 Educative, Inc. All rights reserved

Tired of LeetCode? 😩

Learn the 24 patterns to solve any coding interview question without getting lost in a maze of LeetCode-style practice problems. Practice your skills in a hands-on, setup-free coding environment. 💪

Keep Exploring
Related Courses