1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
//! Defines common interfaces for interacting with statistical distributions
//! and provides
//! concrete implementations for a variety of distributions.
use super::statistics::{Max, Min};
use ::num_traits::{float::Float, Bounded, Num};
pub use self::bernoulli::Bernoulli;
pub use self::beta::Beta;
pub use self::binomial::Binomial;
pub use self::categorical::Categorical;
pub use self::cauchy::Cauchy;
pub use self::chi::Chi;
pub use self::chi_squared::ChiSquared;
pub use self::dirac::Dirac;
pub use self::dirichlet::Dirichlet;
pub use self::discrete_uniform::DiscreteUniform;
pub use self::empirical::Empirical;
pub use self::erlang::Erlang;
pub use self::exponential::Exp;
pub use self::fisher_snedecor::FisherSnedecor;
pub use self::gamma::Gamma;
pub use self::geometric::Geometric;
pub use self::hypergeometric::Hypergeometric;
pub use self::inverse_gamma::InverseGamma;
pub use self::laplace::Laplace;
pub use self::log_normal::LogNormal;
pub use self::multinomial::Multinomial;
pub use self::multivariate_normal::MultivariateNormal;
pub use self::negative_binomial::NegativeBinomial;
pub use self::normal::Normal;
pub use self::pareto::Pareto;
pub use self::poisson::Poisson;
pub use self::students_t::StudentsT;
pub use self::triangular::Triangular;
pub use self::uniform::Uniform;
pub use self::weibull::Weibull;
mod bernoulli;
mod beta;
mod binomial;
mod categorical;
mod cauchy;
mod chi;
mod chi_squared;
mod dirac;
mod dirichlet;
mod discrete_uniform;
mod empirical;
mod erlang;
mod exponential;
mod fisher_snedecor;
mod gamma;
mod geometric;
mod hypergeometric;
mod internal;
mod inverse_gamma;
mod laplace;
mod log_normal;
mod multinomial;
mod multivariate_normal;
mod negative_binomial;
mod normal;
mod pareto;
mod poisson;
mod students_t;
mod triangular;
mod uniform;
mod weibull;
mod ziggurat;
mod ziggurat_tables;
use crate::Result;
/// The `ContinuousCDF` trait is used to specify an interface for univariate
/// distributions for which cdf float arguments are sensible.
pub trait ContinuousCDF<K: Float, T: Float>: Min<K> + Max<K> {
/// Returns the cumulative distribution function calculated
/// at `x` for a given distribution. May panic depending
/// on the implementor.
///
/// # Examples
///
/// ```
/// use statrs::distribution::{ContinuousCDF, Uniform};
///
/// let n = Uniform::new(0.0, 1.0).unwrap();
/// assert_eq!(0.5, n.cdf(0.5));
/// ```
fn cdf(&self, x: K) -> T;
/// Due to issues with rounding and floating-point accuracy the default
/// implementation may be ill-behaved.
/// Specialized inverse cdfs should be used whenever possible.
/// Performs a binary search on the domain of `cdf` to obtain an approximation
/// of `F^-1(p) := inf { x | F(x) >= p }`. Needless to say, performance may
/// may be lacking.
fn inverse_cdf(&self, p: T) -> K {
if p == T::zero() {
return self.min();
};
if p == T::one() {
return self.max();
};
let two = K::one() + K::one();
let mut high = two;
let mut low = -high;
while self.cdf(low) > p {
low = low + low;
}
while self.cdf(high) < p {
high = high + high;
}
let mut i = 16;
while i != 0 {
let mid = (high + low) / two;
if self.cdf(mid) >= p {
high = mid;
} else {
low = mid;
}
i -= 1;
}
(high + low) / two
}
}
/// The `DiscreteCDF` trait is used to specify an interface for univariate
/// discrete distributions.
pub trait DiscreteCDF<K: Bounded + Clone + Num, T: Float>: Min<K> + Max<K> {
/// Returns the cumulative distribution function calculated
/// at `x` for a given distribution. May panic depending
/// on the implementor.
///
/// # Examples
///
/// ```
/// use statrs::distribution::{ContinuousCDF, Uniform};
///
/// let n = Uniform::new(0.0, 1.0).unwrap();
/// assert_eq!(0.5, n.cdf(0.5));
/// ```
fn cdf(&self, x: K) -> T;
/// Due to issues with rounding and floating-point accuracy the default implementation may be ill-behaved
/// Specialized inverse cdfs should be used whenever possible.
fn inverse_cdf(&self, p: T) -> K {
// TODO: fix integer implementation
if p == T::zero() {
return self.min();
};
if p == T::one() {
return self.max();
};
let two = K::one() + K::one();
let mut high = two.clone();
let mut low = K::min_value();
while self.cdf(high.clone()) < p {
high = high.clone() + high.clone();
}
while high != low {
let mid = (high.clone() + low.clone()) / two.clone();
if self.cdf(mid.clone()) >= p {
high = mid;
} else {
low = mid;
}
}
high
}
}
/// The `Continuous` trait provides an interface for interacting with
/// continuous statistical distributions
///
/// # Remarks
///
/// All methods provided by the `Continuous` trait are unchecked, meaning
/// they can panic if in an invalid state or encountering invalid input
/// depending on the implementing distribution.
pub trait Continuous<K, T> {
/// Returns the probability density function calculated at `x` for a given
/// distribution.
/// May panic depending on the implementor.
///
/// # Examples
///
/// ```
/// use statrs::distribution::{Continuous, Uniform};
///
/// let n = Uniform::new(0.0, 1.0).unwrap();
/// assert_eq!(1.0, n.pdf(0.5));
/// ```
fn pdf(&self, x: K) -> T;
/// Returns the log of the probability density function calculated at `x`
/// for a given distribution.
/// May panic depending on the implementor.
///
/// # Examples
///
/// ```
/// use statrs::distribution::{Continuous, Uniform};
///
/// let n = Uniform::new(0.0, 1.0).unwrap();
/// assert_eq!(0.0, n.ln_pdf(0.5));
/// ```
fn ln_pdf(&self, x: K) -> T;
}
/// The `Discrete` trait provides an interface for interacting with discrete
/// statistical distributions
///
/// # Remarks
///
/// All methods provided by the `Discrete` trait are unchecked, meaning
/// they can panic if in an invalid state or encountering invalid input
/// depending on the implementing distribution.
pub trait Discrete<K, T> {
/// Returns the probability mass function calculated at `x` for a given
/// distribution.
/// May panic depending on the implementor.
///
/// # Examples
///
/// ```
/// use statrs::distribution::{Discrete, Binomial};
/// use statrs::prec;
///
/// let n = Binomial::new(0.5, 10).unwrap();
/// assert!(prec::almost_eq(n.pmf(5), 0.24609375, 1e-15));
/// ```
fn pmf(&self, x: K) -> T;
/// Returns the log of the probability mass function calculated at `x` for
/// a given distribution.
/// May panic depending on the implementor.
///
/// # Examples
///
/// ```
/// use statrs::distribution::{Discrete, Binomial};
/// use statrs::prec;
///
/// let n = Binomial::new(0.5, 10).unwrap();
/// assert!(prec::almost_eq(n.ln_pmf(5), (0.24609375f64).ln(), 1e-15));
/// ```
fn ln_pmf(&self, x: K) -> T;
}