Inverse Gamma Distribution in Python


Introduction

A continuous probability distribution known as the inverse gamma appears in many disciplines, such as Bayesian statistics, economics, and physics. It serves as the conjugate prior for the accuracy parameter in the normal distribution and is frequently employed to represent variables with positive skewness. The Inverse Gamma distribution, its description, syntax in Python, and examples with working code to show how to use it are all covered in this article.

Inverse Gamma Distribution

Definition

A probability distribution defined on the positive real line is the inverse gamma distribution. Shape (alpha) and scale (beta) are two characteristics that describe it. The Inverse Gamma distribution's probability density function (PDF) is given by −

f(x|alpha, beta) = (betaalpha / math.gamma(alpha)) * x(-alpha-1) * exp(-beta / x)

where x > 0, alpha > 0, beta > 0, and math.gamma denotes the gamma function.

Syntax

The scipy.stats module in Python, which offers a large selection of probability distributions, can be used to work with the Inverse Gamma distribution. The following is the syntax for constructing an Inverse Gamma distribution object −

from scipy.stats import invgamma
# Creating an Inverse Gamma distribution object
inverse_gamma_dist = invgamma(alpha, scale=beta)

You can replicate the above code snippet and change alpha and beta to the desired values for the Inverse Gamma distribution's shape and scale parameters. Please be aware that using the Inverse Gamma distribution requires that the scipy.stats module be installed in your Python environment.

Explanation of Syntax

  • ‘from scipy.stats import invgamma’: This line imports the scipy.stats module's invgamma class.

  • ‘inverse_gamma_dist = invgamma(alpha, scale=beta)’: In this line, alpha serves as the shape parameter and beta as the scale parameter to produce an instance of the Inverse Gamma distribution.

Algorithm

  • Step 1 − Import the required libraries

  • Step 2 − Define the shape and scale parameters

  • Step 3 − Create an Inverse Gamma distribution object

  • Step 4 − Generate random numbers from the Inverse Gamma distribution

  • Step 5 − Plot the probability density function (PDF) and histogram of the generated numbers

Approach

  • Approach 1 − Generating Random Numbers

  • Approach 2 − Probability Density Function (PDF)

Approach 1: Generating Random Numbers

Using the rvs method of the Inverse Gamma distribution object, we produce random numbers in this technique from the Inverse Gamma distribution. We can regulate the quantity of generated random numbers by selecting the size parameter.

Example

import numpy as np
from scipy.stats import invgamma

# Define the shape and scale parameters
alpha = 3
beta = 2

# Create an Inverse Gamma distribution object
inverse_gamma_dist = invgamma(alpha, scale=beta)

# Generate random numbers from the Inverse Gamma distribution
random_numbers = inverse_gamma_dist.rvs(size=1000)

print(random_numbers)

Output

[0.50492148 0.99333748 1.75490027 ... 0.65061424 0.70721182 0.78544613]

The primary goal of Approach 1 is to produce random numbers using the Inverse Gamma distribution. The Inverse Gamma distribution object's rvs method can be used to quickly produce the required number of random samples.In this method, we first specify the Inverse Gamma distribution's shape (alpha) and scale (beta) parameters. We next use the invgamma class from the scipy.stats module to generate an instance of the Inverse Gamma distribution. The Inverse Gamma distribution is used to produce a set of random numbers by using the rvs method on the distribution object and supplying the required size.With this method, random numbers can be generated to simulate data or run Monte Carlo simulations for a variety of purposes.

Please take note that because the generated random numbers are generated at random, they will vary every time the code is run.

Approach 2: Probability Density Function (PDF)

In this method, we use the Inverse Gamma distribution object's pdf method to visualise the probability density function (PDF) of the distribution. This enables us to see the distribution's form and comprehend its properties.

Example

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

# Define the shape and scale parameters
alpha = 2.5
beta = 1.2

# Create an Inverse Gamma distribution object
inverse_gamma_dist = invgamma(alpha, scale=beta)

# Generate an array of x values
x = np.linspace(0.001, 10, 100)

# Calculate the PDF values for the x values
pdf_values = inverse_gamma_dist.pdf(x)

# Plot the PDF of the Inverse Gamma distribution
plt.plot(x, pdf_values, 'r-', label='PDF')
plt.xlabel('x')
plt.ylabel('Probability Density')
plt.title('PDF of Inverse Gamma Distribution')
plt.legend()
plt.show()

Output

The np.linspace function, which produces 100 evenly spaced values between 0.001 and 10, is used in this method to first build an array of x values. The range over which we want to assess the PDF is represented by these x values. The probability density function (PDF) values for the generated x values are then calculated using the pdf method of the Inverse Gamma distribution object. As a result, we are given an array of PDF values for each x value.

Finally, we use the matplotlib.pyplot module's plt.plot method to plot the PDF of the inverse gamma distribution. With the x values on the x-axis and the PDF values on the y-axis, the resulting plot depicts the distribution's shape. We can see important details like the distribution's position, spread, and skewness by visualizing the PDF. The Inverse Gamma distribution and its behavior are better understood using this method.

Please note that the generated plot will be displayed in a separate window when you run the code.

Conclusion

A flexible probability distribution, the inverse gamma distribution is used in a number of different fields. We discussed its description, Python syntax, and examples with working code to show how to use it in this article. You may use the Inverse Gamma distribution to model and analyze data in Python by grasping the fundamentals and utilizing the scipy.stats module. There are several applications for the Inverse Gamma distribution, including Bayesian analysis, finance, and physics. For the modelling of variables with positive skewness, it offers a versatile and effective technique. By making use of the ideas and illustrations provided in this article, you may securely add the Inverse Gamma distribution to your Python data analysis projects.

Python and the scipy.stats module offer all the tools required for dealing with the Inverse Gamma distribution proficiently, whether you need to create random numbers or examine the distribution's features.

Updated on: 11-Oct-2023

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements