Skip to main content

probability_rs/dist/
mod.rs

1//! Collection of probability distributions.
2//! This module groups all distribution implementations under `dist`.
3use crate::rng;
4
5pub mod bernoulli;
6pub mod beta;
7pub mod binomial;
8pub mod chisquared;
9pub mod exponential;
10pub mod gamma;
11pub mod geometric;
12pub mod lognormal;
13pub mod normal;
14pub mod poisson;
15pub mod uniform;
16/// Basic moments available for a distribution.
17pub trait Moments {
18    fn mean(&self) -> f64;
19    fn variance(&self) -> f64;
20    fn std_dev(&self) -> f64 {
21        self.variance().sqrt()
22    }
23    /// Skewness (standardized 3rd central moment).
24    fn skewness(&self) -> f64;
25    /// Excess kurtosis (kurtosis − 3).
26    fn kurtosis(&self) -> f64;
27    /// Full kurtosis (i.e., excess kurtosis + 3).
28    fn kurtosis_full(&self) -> f64 { self.kurtosis() + 3.0 }
29    /// Entropy: Shannon (discrete) or differential entropy (continuous), in nats.
30    fn entropy(&self) -> f64;
31    
32}
33 
34/// Basic trait for distributions.
35pub trait Distribution {
36    type Value;
37    fn cdf(&self, x: Self::Value) -> f64;
38    fn sample<R: rng::RngCore>(&self, rng: &mut R) -> Self::Value;
39    fn in_support(&self, x: Self::Value) -> bool;
40}
41
42/// Trait for continuous real-valued distributions.
43pub trait Continuous: Distribution<Value = f64> {
44    /// Returns f(x) (density / pdf).
45    fn pdf(&self, x: f64) -> f64;
46    /// Quantile: F^{-1}(p) for p in (0,1).
47    fn inv_cdf(&self, p: f64) -> f64;
48}
49
50/// Trait for discrete distributions over {0,1} or small integers.
51pub trait Discrete: Distribution<Value = i64> {
52    /// pmf(x)
53    fn pmf(&self, x: Self::Value) -> f64;
54    fn inv_cdf(&self, p: f64) -> Self::Value;
55}
56
57/// Error returned when constructing distributions with invalid parameters.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum DistError {
60    InvalidParameter,
61}