Implementation:Avhz RustQuant Distribution Trait
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Statistics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete trait definition for all probability distributions provided by the RustQuant library.
Description
The Distribution trait is the base trait that all probability distributions in RustQuant must implement. It defines a unified interface for working with any probability distribution, ensuring consistent access to density functions, statistical moments, and random sampling.
The module also defines:
- DistributionClass -- an enum with variants Discrete and Continuous, used by distributions that support both forms (such as the Uniform distribution).
- i -- a convenience constant for the imaginary unit as a Complex<f64>.
The trait requires 14 methods:
- cf(t) -- Characteristic function, E[e^{itX}], returning a Complex<f64>.
- pdf(x) -- Probability density function (panics if the distribution is discrete).
- pmf(x) -- Probability mass function (panics if the distribution is continuous).
- cdf(x) -- Cumulative distribution function, P(X <= x).
- inv_cdf(p) -- Inverse CDF (quantile function), the value x such that cdf(x) = p.
- mean() -- Expected value.
- median() -- Median value.
- mode() -- Most likely value.
- variance() -- Variance, a measure of spread.
- skewness() -- Skewness, a measure of asymmetry.
- kurtosis() -- Kurtosis, a measure of tail heaviness.
- entropy() -- Shannon entropy.
- mgf(t) -- Moment generating function, E[e^{tX}].
- sample(n) -- Generate n random samples, returning Result<Vec<f64>, RustQuantError>.
Usage
Implement this trait on any new probability distribution struct to integrate it into the RustQuant distribution framework. Use the trait as a bound in generic functions that need to work with any distribution type (e.g., computing moments, generating samples, or evaluating density functions polymorphically).
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_math/src/distributions/distribution.rs
- Lines: 1-95
Signature
pub const i: Complex<f64> = Complex { re: 0.0, im: 1.0 };
pub enum DistributionClass {
Discrete,
Continuous,
}
pub trait Distribution {
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::Distribution;
use RustQuant::math::distributions::DistributionClass;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| t | f64 | Yes (for cf, mgf) | Evaluation point for characteristic/moment generating functions. |
| x | f64 | Yes (for pdf, pmf, cdf) | Evaluation point for density/mass/distribution functions. |
| p | f64 | Yes (for inv_cdf) | Probability value for the quantile function. Must be in [0, 1]. |
| n | usize | Yes (for sample) | Number of random samples to generate. |
Outputs
| Name | Type | Description |
|---|---|---|
| cf() | Complex<f64> | Value of the characteristic function at t. |
| pdf() / pmf() | f64 | Probability density or mass at x. |
| cdf() | f64 | Cumulative probability up to x. |
| inv_cdf() | f64 | Quantile value for given probability p. |
| mean(), median(), mode() | f64 | Location summary statistics. |
| variance(), skewness(), kurtosis() | f64 | Shape summary statistics. |
| entropy() | f64 | Shannon entropy of the distribution. |
| mgf() | f64 | Value of the moment generating function at t. |
| sample() | Result<Vec<f64>, RustQuantError> | Vector of random variates. |
Usage Examples
use RustQuant::math::distributions::{Distribution, Gaussian};
// Any struct implementing Distribution can be used generically
fn print_summary<D: Distribution>(dist: &D) {
println!("Mean: {}", dist.mean());
println!("Variance: {}", dist.variance());
println!("Skewness: {}", dist.skewness());
println!("Kurtosis: {}", dist.kurtosis());
}
let gaussian = Gaussian::new(0.0, 1.0);
print_summary(&gaussian);
// Generate samples from any distribution
let samples = gaussian.sample(1000).expect("sampled");