generateGamma(alpha, beta)

Overview

The generateGamma(alpha, beta) function generates a random number that follows a Gamma distribution with the given shape parameter \alpha and scale parameter \beta . The function uses the Marsaglia and Tsang method for this purpose.

Parameters

Parameter Type Description Default
alpha Number The shape parameter of the Gamma distribution. -
beta Number The scale parameter of the Gamma distribution. -

Returns

Return Type Description
x Number A random number from a Gamma distribution with parameters \alpha and \beta .

Example

local alpha = 2
local beta = 1
local x = StatBook.generateGamma(alpha, beta)
print(x)  -- Output will vary

Mathematical Background

The function utilizes the Marsaglia and Tsang method to generate a random number x that follows a Gamma distribution with shape parameter \alpha and scale parameter \beta .

For \alpha < 1 , the function uses a recursive approach to find x such that:

[ x = \text{GenerateGamma}(\alpha + 1, \beta) \times u ^ {\frac{1}{\alpha}} ] Where u is a uniformly distributed random number between 0 and 1.

For \alpha \geq 1 , the function uses a while loop to generate x as:

[ x = \frac{d \times v}{\beta} ] Where d = \alpha - \frac{1}{3} and v is calculated using a standard normally distributed random number Z generated by Box-Muller transform as:

[ v = (1 + c \times Z)^3 ] And c = \frac{1}{\sqrt{9 \times d}} .

The function repeats this process until a suitable x is found.