Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python random.gammavariate() Method



The random.gammavariate() method in Python generates random numbers that follows the Gamma distribution. The gamma distribution is a two-parameter family of continuous probability distributions, depends on the values of two positive parameters, alpha () and beta (), both of which must be greater than 0.

The probability density function (PDF) of the Gamma distribution is given by −

         x ** (alpha - 1) * math.exp(-x / beta)
pdf(x) =  --------------------------------------
            math.gamma(alpha) * beta ** alpha

The parameter alpha () defines the shape of the distribution. A larger value of results in a distribution that is more spread out. Where as the parameter beta () defines the scales the distribution. It affects the spread of the data points around the mean.

Syntax

Following is the syntax of the gammavariate() method −

random.gammavariate(alpha, beta)

Parameters

This method accepts the following parameters −

  • alpha: This is the shape parameter of the Gamma distribution, and it must be greater than 0.

  • beta: This is the scale parameter of the Gamma distribution, and it also must be greater than 0.

Return Value

This method returns a random number that follows the gamma distribution.

Example 1

Let's see a basic example of using the random.gammavariate() method for generating a random number from a Gamma distribution with shape parameter 2 and scale parameter 3.

import random

# Parameters for the gamma distribution
alpha = 2
beta = 3

# Generate a gamma-distributed random number
random_number = random.gammavariate(alpha, beta)

print("Generated random number:", random_number)

Following is the output −

Generated random number from gamma distribution: 7.80586421115812

Note: The Output generated will vary each time you run the program due to its random nature.

Example 2

This example generates a list of 10 random numbers that follows the Gamma distribution using this method.

import random

# Parameters for the gamma distribution
alpha = 3
beta = 1.5

result = []
# Generate a random numbers from the gamma distribution
for i in range(10):
    result.append(random.gammavariate(alpha, beta))

print("List of random numbers from Gamma distribution:", result)

While executing the above code you will get the similar output like below −

List of random numbers from Gamma distribution: [1.8459995636263633, 1.5884068672272527, 2.472844073811172, 5.9912332880010375, 5.710796196794566, 7.0073286403252535, 0.6174810186947404, 2.3729043573117172, 3.5488507756541923, 4.851207589108078]

Example 3

Here is another example that uses the random.gammavariate() method to generate and display a histogram showing the distribution of data from a Gamma distribution.

import random
import numpy as np
import matplotlib.pyplot as plt

# Parameters for the gamma distribution
alpha = 1
beta = 2

# Generate gamma-distributed data
d = [random.gammavariate(alpha, beta) for _ in range(10000)]

# Create a histogram from the generated data, with bins up to the maximum value in d
h, b = np.histogram(d, bins=np.arange(0, max(d)+1))

# Plot the histogram to visualize the distribution
plt.bar(b[:-1], h, width=1, edgecolor='none') 
plt.title('Histogram of Gamma Distributed Data')

# Display the plot
plt.show() 

The output of the above code is as follows −

 random gammavariate method
python_modules.htm
Advertisements