How to Plot a Gamma Distribution in Python

Spread the love

The Gamma distribution is a two-parameter family of continuous probability distributions. While it’s not as famous as the Normal distribution, it is quite a bit more flexible, due to the shape and scale parameters.

In this tutorial, we will go through how to plot a Gamma distribution using Python. The libraries we will be using are numpy for numerical computation, matplotlib for plotting, and scipy, which has numerous functions for scientific computing.

Firstly, import the necessary libraries:

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

Step 1: Understanding the Gamma Distribution

The Gamma distribution is a continuous probability distribution that has two parameters, often described as shape and scale. The shape parameter determines the shape of the distribution. The scale parameter is a multiplier that stretches or shrinks the distribution.

Step 2: Generate Data for Gamma Distribution

We will generate data for a Gamma distribution using numpy and scipy.

The numpy.linspace function generates evenly spaced numbers over a specified interval. The scipy.stats.gamma.pdf function gives the Probability Density Function (PDF) for the gamma distribution.

Let’s generate a Gamma distribution with a shape parameter of 3 and a scale parameter of 2:

shape = 3  # shape parameter
scale = 2  # scale parameter
x = np.linspace(gamma.ppf(0.01, shape), gamma.ppf(0.99, shape), 100)
y = gamma.pdf(x, shape, scale=scale)

In this example, we’re generating 100 points between the 1st percentile and the 99th percentile of the Gamma distribution.

Step 3: Plotting the Gamma Distribution

Now that we have our data, we can plot it using the matplotlib library. The matplotlib.pyplot.plot function is used to draw lines from one point to other points.

plt.figure(figsize=(10,6))  
plt.plot(x, y, 'r-', lw=5, alpha=0.6, label='gamma pdf')  
plt.title('Gamma Distribution')  
plt.xlabel('Value')  
plt.ylabel('Density')  
plt.legend(loc='best')  
plt.grid(True)  
plt.show() 

This code will produce a plot of a Gamma distribution with a shape parameter of 3 and a scale parameter of 2. The x-axis represents the value of the variable and the y-axis represents the density of each value.

Step 4: Experimenting with Different Shape and Scale Parameters

The shape and scale of the Gamma distribution can significantly change its form. You can experiment with different shape and scale parameters to see how they alter the distribution.

# List of shape parameters to try
shapes = [1, 2, 3, 5]

# Create a new figure
plt.figure(figsize=(10,6))

# Generate and plot a Gamma distribution for each shape parameter
for shape in shapes:
    x = np.linspace(gamma.ppf(0.01, shape), gamma.ppf(0.99, shape), 100)
    y = gamma.pdf(x, shape, scale=scale)
    plt.plot(x, y, lw=5, alpha=0.6, label=f'shape={shape}, scale={scale}')

# Set the title and labels
plt.title('Gamma Distributions with Different Shape Parameters')
plt.xlabel('Value')
plt.ylabel('Density')

# Add a legend and grid
plt.legend(loc='best')
plt.grid(True)

# Display the plot
plt.show()

In this code, we’re plotting Gamma distributions with different shape parameters while keeping the scale parameter constant. The result should show four different curves, each representing a Gamma distribution with a different shape parameter. You can see how the shape parameter changes the skewness and kurtosis of the distribution.

By using Python and its extensive libraries such as numpy, scipy, and matplotlib, we can generate, manipulate, and visualize different statistical distributions like the Gamma distribution. Understanding these distributions and their parameters allows for more nuanced analysis and interpretation of data.

Leave a Reply