Implementation:Avhz RustQuant Binomial Distribution
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Statistics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete implementation of the Binomial probability distribution provided by the RustQuant library.
Description
The Binomial struct models the Binomial distribution, denoted X ~ Bin(n, p). This discrete probability distribution describes the number of successes in a fixed number of independent Bernoulli trials, each with the same probability of success.
The struct contains two fields:
- n (usize) -- the number of trials.
- p (f64) -- the probability of success on each trial, constrained to [0, 1].
The implementation provides the full Distribution trait interface. The probability mass function is computed using the binomial coefficient formula: C(n,k) * p^k * (1-p)^(n-k). The CDF is implemented via the regularized incomplete beta function from the statrs crate. The inverse CDF uses a sequential search algorithm. Entropy is approximated using the normal approximation formula: 0.5 * ln(2 * pi * e * variance).
A Default implementation creates a Binomial(1, 0.5) distribution. Random sampling delegates to the rand_distr crate.
Usage
Use this distribution when modeling the count of successes across a fixed number of independent trials. In quantitative finance, it is used in binomial tree pricing models, modeling the number of defaults in a credit portfolio, and counting events over discrete time steps.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_math/src/distributions/binomial.rs
- Lines: 1-358
Signature
pub struct Binomial {
n: usize,
p: f64,
}
impl Binomial {
pub fn new(trials: usize, probability: f64) -> Self
}
impl Distribution for Binomial {
fn cf(&self, t: f64) -> Complex<f64>;
fn pdf(&self, x: f64) -> f64;
fn pmf(&self, k: f64) -> f64;
fn cdf(&self, k: 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::Binomial;
use RustQuant::math::distributions::Distribution;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| trials | usize | Yes | Number of independent Bernoulli trials. |
| probability | f64 | Yes | Probability of success on each trial. Must be in [0, 1]. |
Outputs
| Name | Type | Description |
|---|---|---|
| Binomial | struct | A new Binomial distribution instance. |
| mean() | f64 | Returns n * p. |
| variance() | f64 | Returns n * p * (1 - p). |
| sample(n) | Result<Vec<f64>, RustQuantError> | A vector of n random variates representing success counts. |
Usage Examples
use RustQuant::math::distributions::{Binomial, Distribution};
// Create a Binomial distribution with 5 trials and p = 0.4
let binomial = Binomial::new(5, 0.4);
// Statistical moments
assert_eq!(binomial.mean(), 2.0);
assert_eq!(binomial.variance(), 1.2);
assert_eq!(binomial.median(), 2.0);
assert_eq!(binomial.mode(), 2.0);
// Probability mass function: P(X = 3)
let pmf = binomial.pmf(3.0); // approximately 0.2304
// Cumulative distribution function: P(X <= 3)
let cdf = binomial.cdf(3.0); // approximately 0.9130
// Moment generating function
let mgf = binomial.mgf(1.0); // approximately 13.6766
// Generate 100 random samples
let sample = binomial.sample(100).unwrap();