GammaDistribution
Represents the Gamma distribution, a continuous probability distribution defined on the interval [0, +infinity).
The Gamma distribution generalizes the exponential distribution to allow for a variable number of waiting periods. It is commonly used to model waiting times, rainfall amounts, insurance claims, and other non-negative continuous quantities. When the shape parameter is 1, the distribution reduces to an exponential distribution with the given rate.
The CDF is computed via the regularized incomplete gamma function. Quantiles are found using Newton's method with a Wilson-Hilferty normal approximation as the initial guess. Random samples are generated using the Marsaglia-Tsang method for shape >= 1, with a transformation trick for shape < 1.
Example:
val dist = GammaDistribution(shape = 3.0, rate = 2.0)
dist.mean // 1.5 (shape / rate)
dist.variance // 0.75 (shape / rate^2)
dist.pdf(1.0) // 0.5413... (density at x = 1)
dist.cdf(2.0) // 0.8008... (probability of being at most 2)
// Exponential distribution as a special case
val expo = GammaDistribution(shape = 1.0, rate = 0.5)
expo.mean // 2.0