Implementation:Avhz RustQuant Gaussian Distribution
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Statistics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete implementation of the Gaussian (Normal) probability distribution provided by the RustQuant library.
Description
The Gaussian struct models the normal distribution, denoted X ~ N(mu, sigma^2). This is the most fundamental continuous probability distribution in statistics and quantitative finance, characterized by its bell-shaped curve.
The struct contains two fields:
- mean (f64) -- the mean (location parameter), mu.
- variance (f64) -- the variance (squared scale parameter), sigma^2. Must be positive.
The implementation provides the full Distribution trait interface. The CDF uses the complementary error function (erfc) from the errorfunctions crate to avoid subtractive cancellation that leads to inaccuracy in the tails. The inverse CDF uses the inverse error function (erf_inv) from the statrs crate. The new constructor is const, allowing compile-time construction.
A convenience constant N is provided representing the standard normal distribution N(0, 1), primarily for use in option pricing modules. A Default implementation also creates a standard normal N(0, 1). The pmf method delegates to pdf since the Gaussian is a continuous distribution. Random sampling delegates to the rand_distr crate using the Normal distribution.
Usage
Use this distribution for modeling asset returns, computing option prices (Black-Scholes), risk metrics (VaR, Expected Shortfall), and any scenario requiring normal distribution computations. The standard normal constant N is particularly useful for Black-Scholes option pricing formulas.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_math/src/distributions/gaussian.rs
- Lines: 1-414
Signature
pub struct Gaussian {
mean: f64,
variance: f64,
}
pub const N: Gaussian = Gaussian::new(0.0, 1.0);
impl Gaussian {
pub const fn new(mean: f64, variance: f64) -> Self
}
impl Distribution for Gaussian {
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::Gaussian;
use RustQuant::math::distributions::Distribution;
use RustQuant::math::distributions::N; // Standard normal constant
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| mean | f64 | Yes | The mean (location parameter) of the distribution. |
| variance | f64 | Yes | The variance (squared scale) of the distribution. Must be > 0. |
Outputs
| Name | Type | Description |
|---|---|---|
| Gaussian | struct | A new Gaussian distribution instance. |
| mean() | f64 | Returns the mean parameter. |
| variance() | f64 | Returns the variance parameter. |
| skewness() | f64 | Always returns 0.0. |
| kurtosis() | f64 | Always returns 0.0 (excess kurtosis). |
| sample(n) | Result<Vec<f64>, RustQuantError> | A vector of n random variates from the Gaussian distribution. |
Usage Examples
use RustQuant::math::distributions::{Gaussian, Distribution, N};
// Create a standard normal distribution
let gaussian = Gaussian::new(0.0, 1.0);
// Or use the convenience constant
// N is equivalent to Gaussian::new(0.0, 1.0)
// Statistical moments
assert_eq!(gaussian.mean(), 0.0);
assert_eq!(gaussian.variance(), 1.0);
assert_eq!(gaussian.skewness(), 0.0);
assert_eq!(gaussian.kurtosis(), 0.0);
// PDF at x = 0 (peak of standard normal)
let pdf = gaussian.pdf(0.0); // approximately 0.3989
// CDF
let cdf = gaussian.cdf(1.0); // approximately 0.8413
let cdf_neg = gaussian.cdf(-1.0); // approximately 0.1587
// Inverse CDF (quantile function)
assert_eq!(gaussian.inv_cdf(0.5), 0.0);
// Generate 1000 random samples
let sample = gaussian.sample(1000).expect("Gaussian sampled");