Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant Gamma Distribution

From Leeroopedia


Knowledge Sources
Domains Mathematics, Statistics
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete implementation of the Gamma probability distribution provided by the RustQuant library.

Description

The Gamma struct models the Gamma distribution using the shape-rate parametrization, denoted X ~ Gamma(alpha, beta). There are two common parametrizations: (shape, rate) and (shape, scale). This implementation uses (shape, rate) where scale = 1/rate.

The struct contains two fields:

  • alpha (f64) -- the shape parameter. Must be positive.
  • beta (f64) -- the rate parameter (inverse scale). Must be positive.

The implementation provides the full Distribution trait interface. The PDF and CDF rely on the gamma and gamma_li (lower incomplete gamma) functions from the statrs crate. Key formulas:

  • PDF: beta^alpha * x^(alpha-1) * e^(-beta*x) / Gamma(alpha).
  • CDF: gamma_li(alpha, beta*x) / Gamma(alpha).
  • Mean: alpha / beta.
  • Variance: alpha / beta^2.
  • Mode: (alpha - 1) / beta for alpha >= 1, otherwise 0.

The inv_cdf, median, and entropy methods are currently unimplemented. The MGF requires t < beta. Random sampling delegates to the rand_distr::Gamma distribution (note: rand_distr uses the shape-scale parametrization, so beta is inverted).

Usage

Use this distribution for modeling positive-valued continuous quantities such as waiting times, rainfall amounts, or insurance claims. In quantitative finance, the Gamma distribution is used in stochastic volatility models, interest rate modeling, and as a mixing distribution. The Exponential and Chi-Squared distributions are special cases of the Gamma.

Code Reference

Source Location

Signature

pub struct Gamma {
    alpha: f64,
    beta: f64,
}

impl Gamma {
    pub fn new(alpha: f64, beta: f64) -> Self
}

impl Distribution for Gamma {
    fn cf(&self, t: f64) -> Complex<f64>;
    fn pdf(&self, x: f64) -> f64;
    fn pmf(&self, x: f64) -> f64;
    fn cdf(&self, x: f64) -> f64;
    fn inv_cdf(&self, _p: f64) -> f64; // unimplemented!()
    fn mean(&self) -> f64;
    fn median(&self) -> f64; // unimplemented!()
    fn mode(&self) -> f64;
    fn variance(&self) -> f64;
    fn skewness(&self) -> f64;
    fn kurtosis(&self) -> f64;
    fn entropy(&self) -> f64; // todo!()
    fn mgf(&self, t: f64) -> f64;
    fn sample(&self, n: usize) -> Result<Vec<f64>, RustQuantError>;
}

Import

use RustQuant::math::distributions::Gamma;
use RustQuant::math::distributions::Distribution;

I/O Contract

Inputs

Name Type Required Description
alpha f64 Yes Shape parameter. Must be > 0.
beta f64 Yes Rate parameter (inverse scale). Must be > 0.

Outputs

Name Type Description
Gamma struct A new Gamma distribution instance.
mean() f64 Returns alpha / beta.
variance() f64 Returns alpha / beta^2.
skewness() f64 Returns 2 / sqrt(alpha).
kurtosis() f64 Returns 6 / alpha.
sample(n) Result<Vec<f64>, RustQuantError> A vector of n random variates from the Gamma distribution.

Usage Examples

use RustQuant::math::distributions::{Gamma, Distribution};

// Create a Gamma(1, 1) distribution (equivalent to Exp(1))
let gamma = Gamma::new(1.0, 1.0);

// Statistical moments
assert_eq!(gamma.mean(), 1.0);
assert_eq!(gamma.variance(), 1.0);
assert_eq!(gamma.skewness(), 2.0);
assert_eq!(gamma.kurtosis(), 6.0);

// PDF at x = 1
let pdf = gamma.pdf(1.0); // approximately 0.3679

// CDF at x = 1
let cdf = gamma.cdf(1.0); // approximately 0.6321

// MGF (requires t < beta)
let mgf = gamma.mgf(0.5); // (1 - 0.5)^(-1) = 2.0

// Generate random samples
let sample = gamma.sample(1000).unwrap();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment