Debian Codegraph @

debian/rust-vendor/statrs/src/distribution/mod.rs

raw
1 //! Defines common interfaces for interacting with statistical distributions
2 //! and provides
3 //! concrete implementations for a variety of distributions.
4 use super::statistics::{Max, Min};
5 use ::num_traits::{Float, Num};
6 use num_traits::NumAssignOps;
7
8 pub use self::bernoulli::Bernoulli;
9 pub use self::beta::{Beta, BetaError};
10 pub use self::binomial::{Binomial, BinomialError};
11 pub use self::categorical::{Categorical, CategoricalError};
12 pub use self::cauchy::{Cauchy, CauchyError};
13 pub use self::chi::{Chi, ChiError};
14 pub use self::chi_squared::ChiSquared;
15 pub use self::dirac::{Dirac, DiracError};
16 #[cfg(feature = "nalgebra")]
17 pub use self::dirichlet::{Dirichlet, DirichletError};
18 pub use self::discrete_uniform::{DiscreteUniform, DiscreteUniformError};
19 pub use self::empirical::Empirical;
20 pub use self::erlang::Erlang;
21 pub use self::exponential::{Exp, ExpError};
22 pub use self::fisher_snedecor::{FisherSnedecor, FisherSnedecorError};
23 pub use self::gamma::{Gamma, GammaError};
24 pub use self::geometric::{Geometric, GeometricError};
25 pub use self::gumbel::{Gumbel, GumbelError};
26 pub use self::hypergeometric::{Hypergeometric, HypergeometricError};
27 pub use self::inverse_gamma::{InverseGamma, InverseGammaError};
28 pub use self::laplace::{Laplace, LaplaceError};
29 pub use self::log_normal::{LogNormal, LogNormalError};
30 #[cfg(feature = "nalgebra")]
31 pub use self::multinomial::{Multinomial, MultinomialError};
32 #[cfg(feature = "nalgebra")]
33 pub use self::multivariate_normal::{MultivariateNormal, MultivariateNormalError};
34 #[cfg(feature = "nalgebra")]
35 pub use self::multivariate_students_t::{MultivariateStudent, MultivariateStudentError};
36 pub use self::negative_binomial::{NegativeBinomial, NegativeBinomialError};
37 pub use self::normal::{Normal, NormalError};
38 pub use self::pareto::{Pareto, ParetoError};
39 pub use self::poisson::{Poisson, PoissonError};
40 pub use self::students_t::{StudentsT, StudentsTError};
41 pub use self::triangular::{Triangular, TriangularError};
42 pub use self::uniform::{Uniform, UniformError};
43 pub use self::weibull::{Weibull, WeibullError};
44
45 mod bernoulli;
46 mod beta;
47 mod binomial;
48 mod categorical;
49 mod cauchy;
50 mod chi;
51 mod chi_squared;
52 mod dirac;
53 #[cfg(feature = "nalgebra")]
54 #[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))]
55 mod dirichlet;
56 mod discrete_uniform;
57 mod empirical;
58 mod erlang;
59 mod exponential;
60 mod fisher_snedecor;
61 mod gamma;
62 mod geometric;
63 mod gumbel;
64 mod hypergeometric;
65 #[macro_use]
66 mod internal;
67 mod inverse_gamma;
68 mod laplace;
69 mod log_normal;
70 #[cfg(feature = "nalgebra")]
71 #[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))]
72 mod multinomial;
73 #[cfg(feature = "nalgebra")]
74 #[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))]
75 mod multivariate_normal;
76 #[cfg(feature = "nalgebra")]
77 #[cfg_attr(docsrs, doc(cfg(feature = "nalgebra")))]
78 mod multivariate_students_t;
79 mod negative_binomial;
80 mod normal;
81 mod pareto;
82 mod poisson;
83 mod students_t;
84 mod triangular;
85 mod uniform;
86 mod weibull;
87 #[cfg(feature = "rand")]
88 #[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
89 mod ziggurat;
90 #[cfg(feature = "rand")]
91 #[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
92 mod ziggurat_tables;
93
94 /// The `ContinuousCDF` trait is used to specify an interface for univariate
95 /// distributions for which cdf float arguments are sensible.
96 pub trait ContinuousCDF<K: Float, T: Float>: Min<K> + Max<K> {
97 /// Returns the cumulative distribution function calculated
98 /// at `x` for a given distribution. May panic depending
99 /// on the implementor.
100 ///
101 /// # Examples
102 ///
103 /// ```
104 /// use statrs::distribution::{ContinuousCDF, Uniform};
105 ///
106 /// let n = Uniform::new(0.0, 1.0).unwrap();
107 /// assert_eq!(0.5, n.cdf(0.5));
108 /// ```
109 fn cdf(&self, x: K) -> T;
110
111 /// Returns the survival function calculated
112 /// at `x` for a given distribution. May panic depending
113 /// on the implementor.
114 ///
115 /// # Examples
116 ///
117 /// ```
118 /// use statrs::distribution::{ContinuousCDF, Uniform};
119 ///
120 /// let n = Uniform::new(0.0, 1.0).unwrap();
121 /// assert_eq!(0.5, n.sf(0.5));
122 /// ```
123 fn sf(&self, x: K) -> T {
124 T::one() - self.cdf(x)
125 }
126
127 /// Due to issues with rounding and floating-point accuracy the default
128 /// implementation may be ill-behaved.
129 /// Specialized inverse cdfs should be used whenever possible.
130 /// Performs a binary search on the domain of `cdf` to obtain an approximation
131 /// of `F^-1(p) := inf { x | F(x) >= p }`. Needless to say, performance may
132 /// may be lacking.
133 #[doc(alias = "quantile function")]
134 #[doc(alias = "quantile")]
135 fn inverse_cdf(&self, p: T) -> K {
136 if p == T::zero() {
137 return self.min();
138 };
139 if p == T::one() {
140 return self.max();
141 };
142 let two = K::one() + K::one();
143 let mut high = two;
144 let mut low = -high;
145 while self.cdf(low) > p {
146 low = low + low;
147 }
148 while self.cdf(high) < p {
149 high = high + high;
150 }
151 let mut i = 16;
152 while i != 0 {
153 let mid = (high + low) / two;
154 if self.cdf(mid) >= p {
155 high = mid;
156 } else {
157 low = mid;
158 }
159 i -= 1;
160 }
161 (high + low) / two
162 }
163 }
164
165 /// The `DiscreteCDF` trait is used to specify an interface for univariate
166 /// discrete distributions.
167 pub trait DiscreteCDF<K: Sized + Num + Ord + Clone + NumAssignOps, T: Float>:
168 Min<K> + Max<K>
169 {
170 /// Returns the cumulative distribution function calculated
171 /// at `x` for a given distribution. May panic depending
172 /// on the implementor.
173 ///
174 /// # Examples
175 ///
176 /// ```
177 /// use statrs::distribution::{DiscreteCDF, DiscreteUniform};
178 ///
179 /// let n = DiscreteUniform::new(1, 10).unwrap();
180 /// assert_eq!(0.6, n.cdf(6));
181 /// ```
182 fn cdf(&self, x: K) -> T;
183
184 /// Returns the survival function calculated at `x` for
185 /// a given distribution. May panic depending on the implementor.
186 ///
187 /// # Examples
188 ///
189 /// ```
190 /// use statrs::distribution::{DiscreteCDF, DiscreteUniform};
191 ///
192 /// let n = DiscreteUniform::new(1, 10).unwrap();
193 /// assert_eq!(0.4, n.sf(6));
194 /// ```
195 fn sf(&self, x: K) -> T {
196 T::one() - self.cdf(x)
197 }
198
199 /// Due to issues with rounding and floating-point accuracy the default implementation may be ill-behaved
200 /// Specialized inverse cdfs should be used whenever possible.
201 ///
202 /// # Panics
203 /// this default impl panics if provided `p` not on interval [0.0, 1.0]
204 fn inverse_cdf(&self, p: T) -> K {
205 if p == T::zero() {
206 return self.min();
207 } else if p == T::one() {
208 return self.max();
209 } else if !(T::zero()..=T::one()).contains(&p) {
210 panic!("p must be on [0, 1]")
211 }
212
213 let two = K::one() + K::one();
214 let mut ub = two.clone();
215 let lb = self.min();
216 while self.cdf(ub.clone()) < p {
217 ub *= two.clone();
218 }
219
220 internal::integral_bisection_search(|p| self.cdf(p.clone()), p, lb, ub).unwrap()
221 }
222 }
223
224 /// The `Continuous` trait provides an interface for interacting with
225 /// continuous statistical distributions
226 ///
227 /// # Remarks
228 ///
229 /// All methods provided by the `Continuous` trait are unchecked, meaning
230 /// they can panic if in an invalid state or encountering invalid input
231 /// depending on the implementing distribution.
232 pub trait Continuous<K, T> {
233 /// Returns the probability density function calculated at `x` for a given
234 /// distribution.
235 /// May panic depending on the implementor.
236 ///
237 /// # Examples
238 ///
239 /// ```
240 /// use statrs::distribution::{Continuous, Uniform};
241 ///
242 /// let n = Uniform::new(0.0, 1.0).unwrap();
243 /// assert_eq!(1.0, n.pdf(0.5));
244 /// ```
245 fn pdf(&self, x: K) -> T;
246
247 /// Returns the log of the probability density function calculated at `x`
248 /// for a given distribution.
249 /// May panic depending on the implementor.
250 ///
251 /// # Examples
252 ///
253 /// ```
254 /// use statrs::distribution::{Continuous, Uniform};
255 ///
256 /// let n = Uniform::new(0.0, 1.0).unwrap();
257 /// assert_eq!(0.0, n.ln_pdf(0.5));
258 /// ```
259 fn ln_pdf(&self, x: K) -> T;
260 }
261
262 /// The `Discrete` trait provides an interface for interacting with discrete
263 /// statistical distributions
264 ///
265 /// # Remarks
266 ///
267 /// All methods provided by the `Discrete` trait are unchecked, meaning
268 /// they can panic if in an invalid state or encountering invalid input
269 /// depending on the implementing distribution.
270 pub trait Discrete<K, T> {
271 /// Returns the probability mass function calculated at `x` for a given
272 /// distribution.
273 /// May panic depending on the implementor.
274 ///
275 /// # Examples
276 ///
277 /// ```
278 /// use statrs::distribution::{Discrete, Binomial};
279 /// use statrs::prec;
280 ///
281 /// let n = Binomial::new(0.5, 10).unwrap();
282 /// assert!(prec::almost_eq(n.pmf(5), 0.24609375, 1e-15));
283 /// ```
284 fn pmf(&self, x: K) -> T;
285
286 /// Returns the log of the probability mass function calculated at `x` for
287 /// a given distribution.
288 /// May panic depending on the implementor.
289 ///
290 /// # Examples
291 ///
292 /// ```
293 /// use statrs::distribution::{Discrete, Binomial};
294 /// use statrs::prec;
295 ///
296 /// let n = Binomial::new(0.5, 10).unwrap();
297 /// assert!(prec::almost_eq(n.ln_pmf(5), (0.24609375f64).ln(), 1e-15));
298 /// ```
299 fn ln_pmf(&self, x: K) -> T;
300 }