probability/distribution/
mod.rs

1//! Probability distributions.
2
3use alloc::vec::Vec;
4#[allow(unused_imports)]
5use special::Primitive;
6
7use source::Source;
8
9/// A continuous distribution.
10pub trait Continuous: Distribution {
11    /// Compute the probability density function.
12    fn density(&self, x: f64) -> f64;
13}
14
15/// A discrete distribution.
16pub trait Discrete: Distribution {
17    /// Compute the probability mass function.
18    fn mass(&self, x: Self::Value) -> f64;
19}
20
21/// A distribution.
22pub trait Distribution {
23    /// The type of outcomes.
24    type Value;
25
26    /// Compute the cumulative distribution function.
27    fn distribution(&self, x: f64) -> f64;
28}
29
30/// A distribution capable of computing the differential entropy.
31pub trait Entropy: Distribution {
32    /// Compute the differential entropy.
33    ///
34    /// The entropy is computed in nats.
35    fn entropy(&self) -> f64;
36}
37
38/// A distribution capable of inverting the distribution function.
39pub trait Inverse: Distribution {
40    /// Compute the inverse of the cumulative distribution function.
41    fn inverse(&self, p: f64) -> Self::Value;
42}
43
44/// A distribution capable of computing the excess kurtosis.
45pub trait Kurtosis: Skewness {
46    /// Compute the excess kurtosis.
47    fn kurtosis(&self) -> f64;
48}
49
50/// A distribution capable of computing the expected value.
51///
52/// The trait is applicable when the expected value exists, that is, finite.
53pub trait Mean: Distribution {
54    /// Compute the expected value.
55    fn mean(&self) -> f64;
56}
57
58/// A distribution capable of computing the median.
59///
60/// The trait is applicable when exactly one median exists.
61pub trait Median: Distribution {
62    /// Compute the median.
63    fn median(&self) -> f64;
64}
65
66/// A distribution capable of computing the modes.
67///
68/// The trait is applicable when the number of modes is finite.
69pub trait Modes: Distribution {
70    /// Compute the modes.
71    fn modes(&self) -> Vec<Self::Value>;
72}
73
74/// A distribution capable of drawing samples.
75pub trait Sample: Distribution {
76    /// Draw a sample.
77    fn sample<S>(&self, source: &mut S) -> Self::Value
78    where
79        S: Source;
80}
81
82/// A distribution capable of computing the skewness.
83pub trait Skewness: Variance {
84    /// Compute the skewness.
85    fn skewness(&self) -> f64;
86}
87
88/// A distribution capable of computing the variance.
89///
90/// The trait is applicable when the variance exists, that is, finite.
91pub trait Variance: Mean {
92    /// Compute the variance.
93    fn variance(&self) -> f64;
94
95    /// Compute the standard deviation.
96    #[inline(always)]
97    fn deviation(&self) -> f64 {
98        self.variance().sqrt()
99    }
100}
101
102mod bernoulli;
103mod beta;
104mod binomial;
105mod categorical;
106mod cauchy;
107mod exponential;
108mod gamma;
109mod gaussian;
110mod laplace;
111mod logistic;
112mod lognormal;
113mod pert;
114mod triangular;
115mod uniform;
116
117pub use self::bernoulli::Bernoulli;
118pub use self::beta::Beta;
119pub use self::binomial::Binomial;
120pub use self::categorical::Categorical;
121pub use self::cauchy::Cauchy;
122pub use self::exponential::Exponential;
123pub use self::gamma::Gamma;
124pub use self::gaussian::Gaussian;
125pub use self::laplace::Laplace;
126pub use self::logistic::Logistic;
127pub use self::lognormal::Lognormal;
128pub use self::pert::Pert;
129pub use self::triangular::Triangular;
130pub use self::uniform::Uniform;