GammaDistribution

class GammaDistribution(val shape: Double, val rate: Double = 1.0) : ContinuousDistribution(source)

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

See also

Constructors

Link copied to clipboard
constructor(shape: Double, rate: Double = 1.0)

Properties

Link copied to clipboard
open override val entropy: Double

The differential entropy of this distribution.

Link copied to clipboard
open override val kurtosis: Double

The excess kurtosis of this distribution, which decreases as shape increases.

Link copied to clipboard
open override val mean: Double

The mean of this distribution, equal to shape / rate.

Link copied to clipboard

the rate parameter (inverse of scale). Defaults to 1.0, meaning the scale is 1. Must be positive.

Link copied to clipboard

the shape parameter (sometimes called k or alpha). Must be positive.

Link copied to clipboard
open override val skewness: Double

The skewness of this distribution, which decreases as shape increases.

Link copied to clipboard
open override val variance: Double

The variance of this distribution, equal to shape / rate squared.

Functions

Link copied to clipboard
open override fun cdf(x: Double): Double

Computes the cumulative distribution function at x.

Link copied to clipboard
open override fun logPdf(x: Double): Double

Computes the natural logarithm of the probability density at x.

Link copied to clipboard
open override fun pdf(x: Double): Double

Computes the probability density at x.

Link copied to clipboard
open override fun quantile(p: Double): Double

Computes the quantile (inverse CDF) for the given probability p.

Link copied to clipboard
open override fun sample(random: Random): Double

Draws a single random value from this Gamma distribution.

Link copied to clipboard
open override fun sf(x: Double): Double

Computes the survival function (one minus the CDF) at x.