diff --git a/rand_distr/src/binomial.rs b/rand_distr/src/binomial.rs index 6dbf7ab7494..a856bcd76d1 100644 --- a/rand_distr/src/binomial.rs +++ b/rand_distr/src/binomial.rs @@ -73,6 +73,16 @@ impl Binomial { } Ok(Binomial { n, p }) } + + /// Returns the number of trials (`n`) of the distribution. + pub fn n(&self) -> u64 { + self.n + } + + /// Returns the probability of success (`p`) of the distribution. + pub fn p(&self) -> f64 { + self.p + } } /// Convert a `f64` to an `i64`, panicking on overflow. diff --git a/rand_distr/src/cauchy.rs b/rand_distr/src/cauchy.rs index 9aff7e625f4..3852e20108b 100644 --- a/rand_distr/src/cauchy.rs +++ b/rand_distr/src/cauchy.rs @@ -70,6 +70,16 @@ where F: Float + FloatConst, Standard: Distribution } Ok(Cauchy { median, scale }) } + + /// Returns the median (`median`) of the distribution. + pub fn median(&self) -> F { + self.median + } + + /// Returns the scale (`scale`) of the distribution. + pub fn scale(&self) -> F { + self.scale + } } impl Distribution for Cauchy diff --git a/rand_distr/src/dirichlet.rs b/rand_distr/src/dirichlet.rs index 786cbccd0cc..4f242e9fe1d 100644 --- a/rand_distr/src/dirichlet.rs +++ b/rand_distr/src/dirichlet.rs @@ -111,6 +111,11 @@ where alpha: vec![alpha; size].into_boxed_slice(), }) } + + /// Returns the shape parameter (`alpha`) of the distribution. + pub fn alpha(&self) -> &[F] { + &self.alpha + } } impl Distribution> for Dirichlet diff --git a/rand_distr/src/exponential.rs b/rand_distr/src/exponential.rs index e3d2a8d1cf6..199c9439e47 100644 --- a/rand_distr/src/exponential.rs +++ b/rand_distr/src/exponential.rs @@ -140,6 +140,11 @@ where F: Float, Exp1: Distribution lambda_inverse: F::one() / lambda, }) } + + /// Returns the (inverse of the) shape parameter (`lambda`) of the distribution. + pub fn lambda_inverse(&self) -> F { + self.lambda_inverse + } } impl Distribution for Exp diff --git a/rand_distr/src/frechet.rs b/rand_distr/src/frechet.rs index 63205b40cbd..435d6551ca4 100644 --- a/rand_distr/src/frechet.rs +++ b/rand_distr/src/frechet.rs @@ -86,6 +86,21 @@ where shape, }) } + + /// Returns the location (`location`) of the distribution. + pub fn location(&self) -> F { + self.location + } + + /// Returns the scale (`scale`) of the distribution. + pub fn scale(&self) -> F { + self.scale + } + + /// Returns the shape (`shape`) of the distribution. + pub fn shape(&self) -> F { + self.shape + } } impl Distribution for Frechet diff --git a/rand_distr/src/gamma.rs b/rand_distr/src/gamma.rs index 1a575bd6a9f..3aec9d5e8c6 100644 --- a/rand_distr/src/gamma.rs +++ b/rand_distr/src/gamma.rs @@ -175,6 +175,30 @@ where }; Ok(Gamma { repr }) } + + /// Returns the shape parameter (`shape`) of the distribution. + pub fn shape(&self) -> F { + match self.repr { + // By definition of `one`. + One(_) => F::one(), + // By definition of `small shape`. + Small(gamma) => gamma.inv_shape.recip(), + // By definition of `large shape`. + Large(gamma) => gamma.d + F::from(1. / 3.).unwrap(), + } + } + + /// Returns the scale parameter (`scale`) of the distribution. + pub fn scale(&self) -> F { + match self.repr { + // By definition of `one`. + One(exp) => exp.lambda_inverse().recip(), + // By definition of `small shape`. + Small(gamma) => gamma.large_shape.scale, + // By definition of `large shape`. + Large(gamma) => gamma.scale, + } + } } impl GammaSmallShape @@ -350,6 +374,16 @@ where }; Ok(ChiSquared { repr }) } + + /// Returns the degrees-of-freedom (`k`) of the distribution. + pub fn k(&self) -> F { + match self.repr { + DoFExactlyOne => F::one(), + // Since `DoFAnythingElse` is computed using Gamma(shape = 0.5 * k, ...), + // we revert the operation k = (1. / 0.5) * shape = 2. * shape. + DoFAnythingElse(gamma) => F::from(2.).unwrap() * gamma.shape(), + } + } } impl Distribution for ChiSquared where @@ -447,6 +481,16 @@ where dof_ratio: n / m, }) } + + /// Returns the m parameter (`m`) of the distribution. + pub fn m(&self) -> F { + self.numer.k() + } + + /// Returns the n parameter (`n`) of the distribution. + pub fn n(&self) -> F { + self.denom.k() + } } impl Distribution for FisherF where @@ -500,6 +544,11 @@ where dof: n, }) } + + /// Return the degrees-of-freedom (`n`) of the distribution. + pub fn n(&self) -> F { + self.chi.k() + } } impl Distribution for StudentT where @@ -650,6 +699,24 @@ where }) } } + + /// Returns the alpha shape (`alpha`) of the distribution. + pub fn alpha(&self) -> F { + if self.switched_params { + self.b + } else { + self.a + } + } + + /// Returns the beta shape (`beta`) of the distribution. + pub fn beta(&self) -> F { + if self.switched_params { + self.a + } else { + self.b + } + } } impl Distribution for Beta diff --git a/rand_distr/src/geometric.rs b/rand_distr/src/geometric.rs index 3ea8b8f3e13..227dbac1ed3 100644 --- a/rand_distr/src/geometric.rs +++ b/rand_distr/src/geometric.rs @@ -78,6 +78,11 @@ impl Geometric { Ok(Geometric { p, pi, k }) } } + + /// Returns the probability of success on each trial (`p`) of the distribution. + pub fn p(&self) -> f64 { + self.p + } } impl Distribution for Geometric diff --git a/rand_distr/src/gumbel.rs b/rand_distr/src/gumbel.rs index b254919f3b8..76948f563f1 100644 --- a/rand_distr/src/gumbel.rs +++ b/rand_distr/src/gumbel.rs @@ -75,6 +75,16 @@ where } Ok(Gumbel { location, scale }) } + + /// Returns the location (`location`) of the distribution. + pub fn location(&self) -> F { + self.location + } + + /// Returns the scale (`scale`) of the distribution. + pub fn scale(&self) -> F { + self.scale + } } impl Distribution for Gumbel diff --git a/rand_distr/src/inverse_gaussian.rs b/rand_distr/src/inverse_gaussian.rs index ba845fd1505..38a78fc5051 100644 --- a/rand_distr/src/inverse_gaussian.rs +++ b/rand_distr/src/inverse_gaussian.rs @@ -58,6 +58,16 @@ where Ok(Self { mean, shape }) } + + /// Returns the mean (`μ`) of the distribution. + pub fn mean(&self) -> F { + self.mean + } + + /// Returns the shape (`shape`) of the distribution. + pub fn shape(&self) -> F { + self.shape + } } impl Distribution for InverseGaussian diff --git a/rand_distr/src/normal.rs b/rand_distr/src/normal.rs index b3b801dfed9..3b7dd3fb4b3 100644 --- a/rand_distr/src/normal.rs +++ b/rand_distr/src/normal.rs @@ -304,6 +304,16 @@ where F: Float, StandardNormal: Distribution pub fn from_zscore(&self, zscore: F) -> F { self.norm.from_zscore(zscore).exp() } + + /// Returns the mean (`μ`) of the distribution. + pub fn mean(&self) -> F { + self.norm.mean() + } + + /// Returns the standard deviation (`σ`) of the distribution. + pub fn std_dev(&self) -> F { + self.norm.std_dev() + } } impl Distribution for LogNormal diff --git a/rand_distr/src/normal_inverse_gaussian.rs b/rand_distr/src/normal_inverse_gaussian.rs index b1ba588ac8d..6edbfc4f84b 100644 --- a/rand_distr/src/normal_inverse_gaussian.rs +++ b/rand_distr/src/normal_inverse_gaussian.rs @@ -66,6 +66,25 @@ where inverse_gaussian, }) } + + /// Returns the alpha parameter (`alpha`) of the distribution. + pub fn alpha(&self) -> F { + // By definition: + // gamma = sqrt(alpha^2 - beta^2) with alpha > 0 + // mu = 1 / gamma + // Therefore: + // mu = 1 / sqrt(alpha^2 - beta^2) + // mu^2 = 1 / (alpha^2 - beta^2) + // 1 / mu^2 = alpha^2 - beta^2 + // 1 / mu^2 + beta^2 = alpha^2 + // sqrt(1 / mu^2 + beta^2) = alpha with alpha > 0 + F::sqrt(self.inverse_gaussian.mean().powi(2).recip() + self.beta.powi(2)) + } + + /// Returns the beta parameter (`beta`) of the distribution. + pub fn beta(&self) -> F { + self.beta + } } impl Distribution for NormalInverseGaussian diff --git a/rand_distr/src/pareto.rs b/rand_distr/src/pareto.rs index 25c8e0537dd..4369aba805d 100644 --- a/rand_distr/src/pareto.rs +++ b/rand_distr/src/pareto.rs @@ -75,6 +75,16 @@ where F: Float, OpenClosed01: Distribution inv_neg_shape: F::from(-1.0).unwrap() / shape, }) } + + /// Returns the scale (`scale`) of the distribution. + pub fn scale(&self) -> F { + self.scale + } + + /// Returns the shape (`shape`) of the distribution. + pub fn shape(&self) -> F { + self.inv_neg_shape.recip().neg() + } } impl Distribution for Pareto diff --git a/rand_distr/src/poisson.rs b/rand_distr/src/poisson.rs index 8b9bffd020e..8b52613e14c 100644 --- a/rand_distr/src/poisson.rs +++ b/rand_distr/src/poisson.rs @@ -78,6 +78,11 @@ where F: Float + FloatConst, Standard: Distribution magic_val: lambda * log_lambda - crate::utils::log_gamma(F::one() + lambda), }) } + + /// Returns the lambda shape (`lambda`) of the distribution. + pub fn lambda(&self) -> F { + self.lambda + } } impl Distribution for Poisson diff --git a/rand_distr/src/triangular.rs b/rand_distr/src/triangular.rs index eef7d190133..601dd89d4f9 100644 --- a/rand_distr/src/triangular.rs +++ b/rand_distr/src/triangular.rs @@ -79,6 +79,21 @@ where F: Float, Standard: Distribution } Ok(Triangular { min, max, mode }) } + + /// Return the min shape (`min`) of the distribution. + pub fn min(&self) -> F { + self.min + } + + /// Return the min shape (`max`) of the distribution. + pub fn max(&self) -> F { + self.max + } + + /// Return the min shape (`mode`) of the distribution. + pub fn mode(&self) -> F { + self.mode + } } impl Distribution for Triangular diff --git a/rand_distr/src/weibull.rs b/rand_distr/src/weibull.rs index fe45eff6613..bd34bdce2a3 100644 --- a/rand_distr/src/weibull.rs +++ b/rand_distr/src/weibull.rs @@ -70,6 +70,16 @@ where F: Float, OpenClosed01: Distribution scale, }) } + + /// Returns the scale parameter (`scale`) of the distribution. + pub fn scale(&self) -> F { + self.scale + } + + /// Returns the shape parameter (`shape`) of the distribution. + pub fn shape(&self) -> F { + self.inv_shape.recip() + } } impl Distribution for Weibull diff --git a/rand_distr/src/zipf.rs b/rand_distr/src/zipf.rs index e15b6cdd197..9118583a2ba 100644 --- a/rand_distr/src/zipf.rs +++ b/rand_distr/src/zipf.rs @@ -89,6 +89,11 @@ where F: Float, Standard: Distribution, OpenClosed01: Distribution b: two.powf(a_minus_1), }) } + + /// Returns the a parameter (`a`) of the distribution. + pub fn a(&self) -> F { + self.a_minus_1 + F::one() + } } impl Distribution for Zeta @@ -205,6 +210,35 @@ where F: Float, Standard: Distribution { }) } + /// Returns the cardinality of the set of elements (`n`) of the distribution. + pub fn n(&self) -> F { + match self.s != F::one() { + true => { + // By definition: + // t = (n^(1 - s) - s) * q + // Therefore: + // t / q = n^(1 - s) - s + // t / q + s = n^(1 - s) + // (t / q + s)^(1/(1 - s)) = n + F::powf(self.t / self.q + self.s, F::recip(F::one() - self.s)) + }, + false => { + // By definition: + // t = 1 - ln(n) + // Therefore: + // t - 1 = - ln(n) + // 1 - t = ln(n) + // e^(1 - t) = n + F::exp(F::one() - self.t) + }, + } + } + + /// Returns the frequency rank exponent (`s`) of the distribution. + pub fn s(&self) -> F { + self.s + } + /// Inverse cumulative density function #[inline] fn inv_cdf(&self, p: F) -> F {