2022-12-01

Gamma distribution

What is gamma distribution

The gamma distribution is the probability distribution that X follows for an event that occurs on average once per \beta over a period of time until it occurs \alpha times. The following examples are said to follow the gamma distribution

  • Person's weight
  • Virus incubation period
  • Latency to system downtime
  • Lifetime of an electronic component

The probability density function of the gamma distribution is expressed by the following equation:

P(X)={\begin{cases}{\frac{1}{\beta^\alpha \Gamma(\alpha)}x^{\alpha-1}e^{-\frac{x}{\beta}}}&x\geq 0,\\0&x<0.\end{cases}}
\Gamma(\alpha) = \int_{0}^{\infty}x^{\alpha-1}e^{-x}dx \quad \alpha > 0

The gamma distribution has \alpha and \beta as parameters as shown in the figure below.

Gamma distribution

Relationship with exponential distribution

The exponential distribution coincides with the gamma distribution when \alpha=1. In other words, the gamma distribution is a probability distribution extended from the exponential distribution.

Substituting \alpha=1 for the gamma distribution, the probability density function becomes

P(X)=\frac{1}{\beta^\alpha \Gamma(\alpha)}x^{\alpha-1}e^{-\frac{x}{\beta}} = \frac{1}{\beta0!}e^{-\frac{x}{\beta}}=\frac{1}{\beta}e^{-\frac{x}{\beta}}

The above equation is consistent with the probability density function of an exponential distribution whose expected value is \beta. An event that occurs on average \frac{1}{\beta} times per unit time can be thought of as an exponential distribution whose distribution is the one followed by time x until the next event occurs.

Expected value and variance of gamma distribution

The expected value and variance of the gamma distribution are respectively:

E(X)=\frac{\alpha}{\beta}
V(X)=\frac{\alpha}{\beta^2}

Effect of parameters on gamma distribution

Visualize the effect of the parameters \alpha and \beta on the gamma distribution.

Gamma distribution params

We can see that the larger \alpha is, the more the peak of probability density moves to the right. Also, the larger \beta is, the smaller the distribution width (variance) is.

Reproductive property of gamma distribution

Suppose that the random variables X and Y follow the gamma distribution respectively and are independent of each other as follows.

X \sim Ga(\alpha_1, \beta),\quad Y \sim Ga(\alpha_1, \beta)

Then, from the reproductive property of the gamma distribution, X + Y follows the following gamma distribution:

X + Y \sim Ga(\alpha_1 + \alpha_2, \beta)

Python Code

The Python code used in this project is as follows.

Draw gamma distribution

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

plt.style.use('ggplot')
fig, ax = plt.subplots(facecolor="w", figsize=(10, 5))

# x axis
x = np.linspace(0, 8, 100)

# draw graph
plt.plot(x, gamma.pdf(x, 1, 0, scale=1/1), label='alpha=1, beta=1')
plt.plot(x, gamma.pdf(x, 1, 0, scale=1/2), label='alpha=1, beta=2')
plt.plot(x, gamma.pdf(x, 2, 0, scale=1/2), label='alpha=2, beta=2')
plt.plot(x, gamma.pdf(x, 3, 0, scale=1/2), label='alpha=3, beta=2')
plt.plot(x, gamma.pdf(x, 5, 0, scale=1/2), label='alpha=5, beta=2')
plt.plot(x, gamma.pdf(x, 10, 0, scale=1/1), label='alpha=5, beta=1')
plt.legend()
plt.xlabel("x")
plt.ylabel("Probability density")
plt.show()

Gamma distribution

Draw the effect of parameters on gamma distribution

import numpy as np
from scipy.stats import gamma
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from matplotlib.animation import FuncAnimation

rc('animation', html='html5')
np.random.seed(5)

# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

prob_vals = np.arange(start=0.1, stop=10.01, step=0.2)

plt.style.use('ggplot')
fig, ax = plt.subplots(facecolor="w", figsize=(15, 5))

# x axis
x = np.linspace(0, 10, 100)

def update(i):
    p = prob_vals[i]

    # alpha graph
    plt.subplot(1, 2, 1)
    plt.cla()
    plt.plot(x, gamma.pdf(x, round(p, 1), 0, scale=1/2))
    # plt.plot(x, gamma.pdf(x, 2, 0, scale=1/round(p, 1)))
    plt.title(f'$alpha={str(round(p, 1))}, beta=2$', loc='left')
    plt.xlabel("x")
    plt.ylabel("Probability density")
    plt.ylim(0, 4.1)
    plt.xticks(ticks=[0, 10]) # x axis ticks

    # beta graph
    plt.subplot(1, 2, 2)
    plt.cla()
    plt.plot(x, gamma.pdf(x, 2, 0, scale=1/round(p, 1)))
    plt.title(f'$alpha=2, beta={str(round(p, 1))}$', loc='left')
    plt.xlabel("x")
    plt.ylabel("Probability density")
    plt.ylim(0, 4.1)
    plt.xticks(ticks=[0, 10]) # x axis ticks

anime_prob = FuncAnimation(fig, update, frames=len(prob_vals), interval=1000)
anime_prob.save('gamma_dist.gif', writer='pillow', fps=10)

Gamma distribution params

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!