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 Chi Squared Distribution

From Leeroopedia


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

Overview

Concrete implementation of the Chi-Squared probability distribution provided by the RustQuant library.

Description

The ChiSquared struct models the Chi-Squared distribution, denoted X ~ ChiSq(k). This continuous probability distribution arises as the sum of squares of k independent standard normal random variables. It is widely used in statistical hypothesis testing and confidence interval estimation.

The struct contains a single field:

  • k (usize) -- the degrees of freedom, which must be a positive integer.

The implementation provides the full Distribution trait interface. The PDF and CDF make use of the gamma and gamma_li (lower incomplete gamma) functions from the statrs crate. The entropy computation utilizes the digamma function. The inverse CDF is computed via a bisection search algorithm. The characteristic function is given by (1 - 2it)^(-k/2). The moment generating function requires t < 0.5.

A Default implementation sets k = 1, which is equivalent to the exponential distribution. Random sampling delegates to the rand_distr crate.

Usage

Use this distribution for goodness-of-fit tests, testing independence in contingency tables, and variance estimation. In quantitative finance, it appears in volatility modeling and in the degrees-of-freedom parameter of Student-t distributions used for heavy-tailed return modeling.

Code Reference

Source Location

Signature

pub struct ChiSquared {
    k: usize,
}

impl ChiSquared {
    pub fn new(k: usize) -> Self
}

impl Distribution for ChiSquared {
    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;
    fn mean(&self) -> f64;
    fn median(&self) -> f64;
    fn mode(&self) -> f64;
    fn variance(&self) -> f64;
    fn skewness(&self) -> f64;
    fn kurtosis(&self) -> f64;
    fn entropy(&self) -> f64;
    fn mgf(&self, t: f64) -> f64;
    fn sample(&self, n: usize) -> Result<Vec<f64>, RustQuantError>;
}

Import

use RustQuant::math::distributions::ChiSquared;
use RustQuant::math::distributions::Distribution;

I/O Contract

Inputs

Name Type Required Description
k usize Yes Degrees of freedom. Must be greater than 0.

Outputs

Name Type Description
ChiSquared struct A new Chi-Squared distribution instance.
mean() f64 Returns k (the degrees of freedom).
variance() f64 Returns 2k.
sample(n) Result<Vec<f64>, RustQuantError> A vector of n random variates from the Chi-Squared distribution.

Usage Examples

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

// Create a Chi-Squared distribution with 1 degree of freedom
let chi = ChiSquared::new(1);

// Statistical moments
assert_eq!(chi.mean(), 1.0);
assert_eq!(chi.variance(), 2.0);
assert_eq!(chi.mode(), 0.0);
assert_eq!(chi.kurtosis(), 12.0);

// Probability density function
let pdf = chi.pdf(1.0); // approximately 0.2420

// Cumulative distribution function
let cdf = chi.cdf(1.0); // approximately 0.6827

// Moment generating function (requires t < 0.5)
let mgf = chi.mgf(0.25); // approximately 1.4142

// Generate 100 random samples
let sample = chi.sample(100).expect("Chi-Squared sampled.");

Related Pages

Page Connections

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