From 3844e92a3265852548ca05ac9e8c71447108f414 Mon Sep 17 00:00:00 2001 From: Orion Yeung <11580988+orionyeung001@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:43:19 -0600 Subject: [PATCH] doc: updates doc tests to use new traits --- src/distribution/bernoulli.rs | 19 +++++++++------- src/distribution/beta.rs | 17 +++++++++------ src/distribution/binomial.rs | 19 +++++++++------- src/distribution/categorical.rs | 9 ++++---- src/distribution/cauchy.rs | 17 +++++++++------ src/distribution/chi.rs | 13 ++++++----- src/distribution/chi_squared.rs | 24 ++++++++++++--------- src/distribution/dirac.rs | 9 ++++---- src/distribution/dirichlet.rs | 15 +++++++------ src/distribution/discrete_uniform.rs | 14 ++++++++---- src/distribution/empirical.rs | 2 +- src/distribution/erlang.rs | 17 +++++++++------ src/distribution/exponential.rs | 15 +++++++------ src/distribution/fisher_snedecor.rs | 15 +++++++------ src/distribution/gamma.rs | 17 +++++++++------ src/distribution/geometric.rs | 13 ++++++----- src/distribution/gumbel.rs | 23 +++++++++++++------- src/distribution/hypergeometric.rs | 8 +++---- src/distribution/inverse_gamma.rs | 6 +++--- src/distribution/log_normal.rs | 8 +++---- src/distribution/multinomial.rs | 4 ++-- src/distribution/multivariate_normal.rs | 6 +++--- src/distribution/multivariate_students_t.rs | 6 +++--- src/distribution/negative_binomial.rs | 10 ++++----- src/distribution/normal.rs | 7 +++--- src/distribution/pareto.rs | 14 +++++------- src/distribution/poisson.rs | 8 +++---- src/distribution/students_t.rs | 8 +++---- src/distribution/triangular.rs | 4 ++-- src/distribution/uniform.rs | 4 ++-- src/distribution/weibull.rs | 9 ++++---- src/lib.rs | 12 +++++------ 32 files changed, 209 insertions(+), 163 deletions(-) diff --git a/src/distribution/bernoulli.rs b/src/distribution/bernoulli.rs index 066c15c2..c4b413c6 100644 --- a/src/distribution/bernoulli.rs +++ b/src/distribution/bernoulli.rs @@ -10,13 +10,14 @@ use crate::statistics::*; /// # Examples /// /// ``` -/// use statrs::distribution::{Bernoulli, Discrete}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Bernoulli, BinomialError, Discrete}; +/// use statrs::statistics::*; /// -/// let n = Bernoulli::new(0.5).unwrap(); -/// assert_eq!(n.mean().unwrap(), 0.5); +/// let n = Bernoulli::new(0.5)?; +/// assert_eq!(n.mean(), 0.5); /// assert_eq!(n.pmf(0), 0.5); /// assert_eq!(n.pmf(1), 0.5); +/// # Ok::<(), BinomialError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Bernoulli { @@ -53,10 +54,11 @@ impl Bernoulli { /// # Examples /// /// ``` - /// use statrs::distribution::Bernoulli; + /// use statrs::distribution::{Bernoulli, BinomialError}; /// - /// let n = Bernoulli::new(0.5).unwrap(); + /// let n = Bernoulli::new(0.5)?; /// assert_eq!(n.p(), 0.5); + /// # Ok::<(), BinomialError>(()) /// ``` pub fn p(&self) -> f64 { self.b.p() @@ -68,10 +70,11 @@ impl Bernoulli { /// # Examples /// /// ``` - /// use statrs::distribution::Bernoulli; + /// use statrs::distribution::{Bernoulli, BinomialError}; /// - /// let n = Bernoulli::new(0.5).unwrap(); + /// let n = Bernoulli::new(0.5)?; /// assert_eq!(n.n(), 1); + /// # Ok::<(), BinomialError>(()) /// ``` pub fn n(&self) -> u64 { 1 diff --git a/src/distribution/beta.rs b/src/distribution/beta.rs index b00be4a3..dd0d1cee 100644 --- a/src/distribution/beta.rs +++ b/src/distribution/beta.rs @@ -8,13 +8,14 @@ use crate::statistics::*; /// # Examples /// /// ``` -/// use statrs::distribution::{Beta, Continuous}; +/// use statrs::distribution::{Beta, Continuous, BetaError}; /// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = Beta::new(2.0, 2.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 0.5); +/// let n = Beta::new(2.0, 2.0)?; +/// assert_eq!(n.mean(), 0.5); /// assert!(prec::almost_eq(n.pdf(0.5), 1.5, 1e-14)); +/// # Ok::<(), BetaError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Beta { @@ -82,10 +83,11 @@ impl Beta { /// # Examples /// /// ``` - /// use statrs::distribution::Beta; + /// use statrs::distribution::{Beta, BetaError}; /// - /// let n = Beta::new(1.0, 2.0).unwrap(); + /// let n = Beta::new(1.0, 2.0)?; /// assert_eq!(n.shape_a(), 1.0); + /// # Ok::<(), BetaError>(()) /// ``` pub fn shape_a(&self) -> f64 { self.shape_a @@ -96,10 +98,11 @@ impl Beta { /// # Examples /// /// ``` - /// use statrs::distribution::Beta; + /// use statrs::distribution::{Beta, BetaError}; /// - /// let n = Beta::new(1.0, 2.0).unwrap(); + /// let n = Beta::new(1.0, 2.0)?; /// assert_eq!(n.shape_b(), 2.0); + /// # Ok::<(), BetaError>(()) /// ``` pub fn shape_b(&self) -> f64 { self.shape_b diff --git a/src/distribution/binomial.rs b/src/distribution/binomial.rs index f441e2b9..4b8a3d66 100644 --- a/src/distribution/binomial.rs +++ b/src/distribution/binomial.rs @@ -10,13 +10,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Binomial, Discrete}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Binomial, BinomialError, Discrete}; +/// use statrs::statistics::*; /// -/// let n = Binomial::new(0.5, 5).unwrap(); -/// assert_eq!(n.mean().unwrap(), 2.5); +/// let n = Binomial::new(0.5, 5)?; +/// assert_eq!(n.mean(), 2.5); /// assert_eq!(n.pmf(0), 0.03125); /// assert_eq!(n.pmf(3), 0.3125); +/// # Ok::<(), BinomialError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Binomial { @@ -78,10 +79,11 @@ impl Binomial { /// # Examples /// /// ``` - /// use statrs::distribution::Binomial; + /// use statrs::distribution::{Binomial, BinomialError}; /// - /// let n = Binomial::new(0.5, 5).unwrap(); + /// let n = Binomial::new(0.5, 5)?; /// assert_eq!(n.p(), 0.5); + /// # Ok::<(), BinomialError>(()) /// ``` pub fn p(&self) -> f64 { self.p @@ -93,10 +95,11 @@ impl Binomial { /// # Examples /// /// ``` - /// use statrs::distribution::Binomial; + /// use statrs::distribution::{Binomial, BinomialError}; /// - /// let n = Binomial::new(0.5, 5).unwrap(); + /// let n = Binomial::new(0.5, 5)?; /// assert_eq!(n.n(), 5); + /// # Ok::<(), BinomialError>(()) /// ``` pub fn n(&self) -> u64 { self.n diff --git a/src/distribution/categorical.rs b/src/distribution/categorical.rs index e46d0faa..e26cf626 100644 --- a/src/distribution/categorical.rs +++ b/src/distribution/categorical.rs @@ -10,13 +10,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Categorical, Discrete}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Categorical, CategoricalError, Discrete}; +/// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = Categorical::new(&[0.0, 1.0, 2.0]).unwrap(); -/// assert!(prec::almost_eq(n.mean().unwrap(), 5.0 / 3.0, 1e-15)); +/// let n = Categorical::new(&[0.0, 1.0, 2.0])?; +/// assert!(prec::almost_eq(n.mean(), 5.0 / 3.0, 1e-15)); /// assert_eq!(n.pmf(1), 1.0 / 3.0); +/// # Ok::<(), CategoricalError>(()) /// ``` #[derive(Clone, PartialEq, Debug)] pub struct Categorical { diff --git a/src/distribution/cauchy.rs b/src/distribution/cauchy.rs index 0e2d6768..32e5b37d 100644 --- a/src/distribution/cauchy.rs +++ b/src/distribution/cauchy.rs @@ -8,12 +8,13 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Cauchy, Continuous}; -/// use statrs::statistics::Mode; +/// use statrs::distribution::{Cauchy, Continuous, CauchyError}; +/// use statrs::statistics::*; /// -/// let n = Cauchy::new(0.0, 1.0).unwrap(); +/// let n = Cauchy::new(0.0, 1.0)?; /// assert_eq!(n.mode().unwrap(), 0.0); /// assert_eq!(n.pdf(1.0), 0.1591549430918953357689); +/// # Ok::<(), CauchyError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Cauchy { @@ -80,10 +81,11 @@ impl Cauchy { /// # Examples /// /// ``` - /// use statrs::distribution::Cauchy; + /// use statrs::distribution::{Cauchy, CauchyError}; /// - /// let n = Cauchy::new(0.0, 1.0).unwrap(); + /// let n = Cauchy::new(0.0, 1.0)?; /// assert_eq!(n.location(), 0.0); + /// # Ok::<(), CauchyError>(()) /// ``` pub fn location(&self) -> f64 { self.location @@ -94,10 +96,11 @@ impl Cauchy { /// # Examples /// /// ``` - /// use statrs::distribution::Cauchy; + /// use statrs::distribution::{Cauchy, CauchyError}; /// - /// let n = Cauchy::new(0.0, 1.0).unwrap(); + /// let n = Cauchy::new(0.0, 1.0)?; /// assert_eq!(n.scale(), 1.0); + /// # Ok::<(), CauchyError>(()) /// ``` pub fn scale(&self) -> f64 { self.scale diff --git a/src/distribution/chi.rs b/src/distribution/chi.rs index 072c2356..75729b3e 100644 --- a/src/distribution/chi.rs +++ b/src/distribution/chi.rs @@ -9,13 +9,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Chi, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Chi, Continuous, ChiError}; +/// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = Chi::new(2.0).unwrap(); +/// let n = Chi::new(2.0)?; /// assert!(prec::almost_eq(n.mean().unwrap(), 1.25331413731550025121, 1e-14)); /// assert!(prec::almost_eq(n.pdf(1.0), 0.60653065971263342360, 1e-15)); +/// # Ok::<(), ChiError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Chi { @@ -77,10 +78,12 @@ impl Chi { /// # Examples /// /// ``` - /// use statrs::distribution::Chi; + /// use statrs::distribution::{Chi, ChiError}; + /// /// - /// let n = Chi::new(2.0).unwrap(); + /// let n = Chi::new(2.0)?; /// assert_eq!(n.freedom(), 2.0); + /// # Ok::<(), ChiError>(()) /// ``` pub fn freedom(&self) -> f64 { self.freedom diff --git a/src/distribution/chi_squared.rs b/src/distribution/chi_squared.rs index 524df2b5..d0cb939d 100644 --- a/src/distribution/chi_squared.rs +++ b/src/distribution/chi_squared.rs @@ -11,13 +11,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{ChiSquared, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{ChiSquared, Continuous, GammaError}; +/// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = ChiSquared::new(3.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 3.0); +/// let n = ChiSquared::new(3.0)?; +/// assert_eq!(n.mean(), 3.0); /// assert!(prec::almost_eq(n.pdf(4.0), 0.107981933026376103901, 1e-15)); +/// # Ok::<(), GammaError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct ChiSquared { @@ -56,10 +57,11 @@ impl ChiSquared { /// # Examples /// /// ``` - /// use statrs::distribution::ChiSquared; + /// use statrs::distribution::{ChiSquared, GammaError}; /// - /// let n = ChiSquared::new(3.0).unwrap(); + /// let n = ChiSquared::new(3.0)?; /// assert_eq!(n.freedom(), 3.0); + /// # Ok::<(), GammaError>(()) /// ``` pub fn freedom(&self) -> f64 { self.freedom @@ -70,10 +72,11 @@ impl ChiSquared { /// # Examples /// /// ``` - /// use statrs::distribution::ChiSquared; + /// use statrs::distribution::{ChiSquared, GammaError}; /// - /// let n = ChiSquared::new(3.0).unwrap(); + /// let n = ChiSquared::new(3.0)?; /// assert_eq!(n.shape(), 3.0 / 2.0); + /// # Ok::<(), GammaError>(()) /// ``` pub fn shape(&self) -> f64 { self.g.shape() @@ -84,10 +87,11 @@ impl ChiSquared { /// # Examples /// /// ``` - /// use statrs::distribution::ChiSquared; + /// use statrs::distribution::{ChiSquared, GammaError}; /// - /// let n = ChiSquared::new(3.0).unwrap(); + /// let n = ChiSquared::new(3.0)?; /// assert_eq!(n.rate(), 0.5); + /// # Ok::<(), GammaError>(()) /// ``` pub fn rate(&self) -> f64 { self.g.rate() diff --git a/src/distribution/dirac.rs b/src/distribution/dirac.rs index 0420cdf1..056213b4 100644 --- a/src/distribution/dirac.rs +++ b/src/distribution/dirac.rs @@ -7,11 +7,12 @@ use crate::statistics::*; /// # Examples /// /// ``` -/// use statrs::distribution::{Dirac, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Dirac, Continuous, DiracError}; +/// use statrs::statistics::*; /// -/// let n = Dirac::new(3.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 3.0); +/// let n = Dirac::new(3.0)?; +/// assert_eq!(n.mean(), 3.0); +/// # Ok::<(), DiracError>(()) /// ``` #[derive(Debug, Copy, Clone, PartialEq)] pub struct Dirac(f64); diff --git a/src/distribution/dirichlet.rs b/src/distribution/dirichlet.rs index 5e902b63..69a424ef 100644 --- a/src/distribution/dirichlet.rs +++ b/src/distribution/dirichlet.rs @@ -12,14 +12,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Dirichlet, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Dirichlet, Continuous, DirichletError}; +/// use statrs::statistics::*; /// use nalgebra::DVector; -/// use statrs::statistics::MeanN; /// -/// let n = Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap(); -/// assert_eq!(n.mean().unwrap(), DVector::from_vec(vec![1.0 / 6.0, 1.0 / 3.0, 0.5])); +/// let n = Dirichlet::new(vec![1.0, 2.0, 3.0])?; +/// assert_eq!(n.mean(), DVector::from_vec(vec![1.0 / 6.0, 1.0 / 3.0, 0.5])); /// assert_eq!(n.pdf(&DVector::from_vec(vec![0.33333, 0.33333, 0.33333])), 2.222155556222205); +/// # Ok::<(), DirichletError>(()) /// ``` #[derive(Clone, PartialEq, Debug)] pub struct Dirichlet @@ -138,11 +138,12 @@ where /// # Examples /// /// ``` - /// use statrs::distribution::Dirichlet; + /// use statrs::distribution::{Dirichlet, DirichletError}; /// use nalgebra::DVector; /// - /// let n = Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap(); + /// let n = Dirichlet::new(vec![1.0, 2.0, 3.0])?; /// assert_eq!(n.alpha(), &DVector::from_vec(vec![1.0, 2.0, 3.0])); + /// # Ok::<(), DirichletError>(()) /// ``` pub fn alpha(&self) -> &nalgebra::OVector { &self.alpha diff --git a/src/distribution/discrete_uniform.rs b/src/distribution/discrete_uniform.rs index 447c15a3..382c5104 100644 --- a/src/distribution/discrete_uniform.rs +++ b/src/distribution/discrete_uniform.rs @@ -8,12 +8,13 @@ use crate::statistics::*; /// # Examples /// /// ``` -/// use statrs::distribution::{DiscreteUniform, Discrete}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{DiscreteUniform, Discrete, DiscreteUniformError}; +/// use statrs::statistics::*; /// -/// let n = DiscreteUniform::new(0, 5).unwrap(); -/// assert_eq!(n.mean().unwrap(), 2.5); +/// let n = DiscreteUniform::new(0, 5)?; +/// assert_eq!(n.mean(), 2.5); /// assert_eq!(n.pmf(3), 1.0 / 6.0); +/// # Ok::<(), DiscreteUniformError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct DiscreteUniform { @@ -66,6 +67,11 @@ impl DiscreteUniform { Ok(DiscreteUniform { min, max }) } } + + pub fn std_dev(&self) -> f64 { + let dist = self.max - self.min + 1; + (dist * dist) as f64 / 12. + } } impl std::fmt::Display for DiscreteUniform { diff --git a/src/distribution/empirical.rs b/src/distribution/empirical.rs index 88805120..9545e637 100644 --- a/src/distribution/empirical.rs +++ b/src/distribution/empirical.rs @@ -27,7 +27,7 @@ impl Ord for NonNan { /// /// ``` /// use statrs::distribution::{Continuous, Empirical}; -/// use statrs::statistics::Distribution; +/// use statrs::statistics::*; /// /// let samples = vec![0.0, 5.0, 10.0]; /// diff --git a/src/distribution/erlang.rs b/src/distribution/erlang.rs index 333800aa..97d1b264 100644 --- a/src/distribution/erlang.rs +++ b/src/distribution/erlang.rs @@ -10,13 +10,14 @@ use crate::statistics::*; /// # Examples /// /// ``` -/// use statrs::distribution::{Erlang, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Erlang, Continuous, GammaError}; +/// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = Erlang::new(3, 1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 3.0); +/// let n = Erlang::new(3, 1.0)?; +/// assert_eq!(n.mean(), 3.0); /// assert!(prec::almost_eq(n.pdf(2.0), 0.270670566473225383788, 1e-15)); +/// # Ok::<(), GammaError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Erlang { @@ -52,10 +53,11 @@ impl Erlang { /// # Examples /// /// ``` - /// use statrs::distribution::Erlang; + /// use statrs::distribution::{Erlang, GammaError}; /// - /// let n = Erlang::new(3, 1.0).unwrap(); + /// let n = Erlang::new(3, 1.0)?; /// assert_eq!(n.shape(), 3); + /// # Ok::<(), GammaError>(()) /// ``` pub fn shape(&self) -> u64 { self.g.shape() as u64 @@ -66,10 +68,11 @@ impl Erlang { /// # Examples /// /// ``` - /// use statrs::distribution::Erlang; + /// use statrs::distribution::{Erlang, GammaError}; /// /// let n = Erlang::new(3, 1.0).unwrap(); /// assert_eq!(n.rate(), 1.0); + /// # Ok::<(), GammaError>(()) /// ``` pub fn rate(&self) -> f64 { self.g.rate() diff --git a/src/distribution/exponential.rs b/src/distribution/exponential.rs index 28274662..18364a93 100644 --- a/src/distribution/exponential.rs +++ b/src/distribution/exponential.rs @@ -11,13 +11,15 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Exp, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Exp, Continuous, ExpError}; +/// use statrs::statistics::*; /// -/// let n = Exp::new(1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 1.0); +/// let n = Exp::new(1.0)?; +/// assert_eq!(n.mean(), 1.0); /// assert_eq!(n.pdf(1.0), 0.3678794411714423215955); +/// # Ok::<(), ExpError>(()) /// ``` +#[doc(alias = "Exponential")] #[derive(Copy, Clone, PartialEq, Debug)] pub struct Exp { rate: f64, @@ -74,10 +76,11 @@ impl Exp { /// # Examples /// /// ``` - /// use statrs::distribution::Exp; + /// use statrs::distribution::{Exp, ExpError}; /// - /// let n = Exp::new(1.0).unwrap(); + /// let n = Exp::new(1.0)?; /// assert_eq!(n.rate(), 1.0); + /// # Ok::<(), ExpError>(()) /// ``` pub fn rate(&self) -> f64 { self.rate diff --git a/src/distribution/fisher_snedecor.rs b/src/distribution/fisher_snedecor.rs index ded4f654..12d1c3ca 100644 --- a/src/distribution/fisher_snedecor.rs +++ b/src/distribution/fisher_snedecor.rs @@ -10,13 +10,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{FisherSnedecor, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{FisherSnedecor, Continuous, FisherSnedecorError}; +/// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = FisherSnedecor::new(3.0, 3.0).unwrap(); +/// let n = FisherSnedecor::new(3.0, 3.0)?; /// assert_eq!(n.mean().unwrap(), 3.0); /// assert!(prec::almost_eq(n.pdf(1.0), 0.318309886183790671538, 1e-15)); +/// # Ok::<(), FisherSnedecorError>(()) /// ``` #[derive(Debug, Copy, Clone, PartialEq)] pub struct FisherSnedecor { @@ -92,10 +93,11 @@ impl FisherSnedecor { /// # Examples /// /// ``` - /// use statrs::distribution::FisherSnedecor; + /// use statrs::distribution::{FisherSnedecor, FisherSnedecorError}; /// - /// let n = FisherSnedecor::new(2.0, 3.0).unwrap(); + /// let n = FisherSnedecor::new(2.0, 3.0)?; /// assert_eq!(n.freedom_1(), 2.0); + /// # Ok::<(), FisherSnedecorError>(()) /// ``` pub fn freedom_1(&self) -> f64 { self.freedom_1 @@ -107,10 +109,11 @@ impl FisherSnedecor { /// # Examples /// /// ``` - /// use statrs::distribution::FisherSnedecor; + /// use statrs::distribution::{FisherSnedecor, FisherSnedecorError}; /// /// let n = FisherSnedecor::new(2.0, 3.0).unwrap(); /// assert_eq!(n.freedom_2(), 3.0); + /// # Ok::<(), FisherSnedecorError>(()) /// ``` pub fn freedom_2(&self) -> f64 { self.freedom_2 diff --git a/src/distribution/gamma.rs b/src/distribution/gamma.rs index 190d4034..7542b1ec 100644 --- a/src/distribution/gamma.rs +++ b/src/distribution/gamma.rs @@ -9,13 +9,14 @@ use crate::statistics::*; /// # Examples /// /// ``` -/// use statrs::distribution::{Gamma, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Gamma, Continuous, GammaError}; +/// use statrs::statistics::*; /// use statrs::prec; /// -/// let n = Gamma::new(3.0, 1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 3.0); +/// let n = Gamma::new(3.0, 1.0)?; +/// assert_eq!(n.mean(), 3.0); /// assert!(prec::almost_eq(n.pdf(2.0), 0.270670566473225383788, 1e-15)); +/// # Ok::<(), GammaError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Gamma { @@ -91,10 +92,11 @@ impl Gamma { /// # Examples /// /// ``` - /// use statrs::distribution::Gamma; + /// use statrs::distribution::{Gamma, GammaError}; /// - /// let n = Gamma::new(3.0, 1.0).unwrap(); + /// let n = Gamma::new(3.0, 1.0)?; /// assert_eq!(n.shape(), 3.0); + /// # Ok::<(), GammaError>(()) /// ``` pub fn shape(&self) -> f64 { self.shape @@ -105,10 +107,11 @@ impl Gamma { /// # Examples /// /// ``` - /// use statrs::distribution::Gamma; + /// use statrs::distribution::{Gamma, GammaError}; /// /// let n = Gamma::new(3.0, 1.0).unwrap(); /// assert_eq!(n.rate(), 1.0); + /// # Ok::<(), GammaError>(()) /// ``` pub fn rate(&self) -> f64 { self.rate diff --git a/src/distribution/geometric.rs b/src/distribution/geometric.rs index 995fa5ff..e1477101 100644 --- a/src/distribution/geometric.rs +++ b/src/distribution/geometric.rs @@ -9,13 +9,14 @@ use std::f64; /// # Examples /// /// ``` -/// use statrs::distribution::{Geometric, Discrete}; -/// use statrs::statistics::Distribution; +/// use statrs::distribution::{Geometric, GeometricError, Discrete}; +/// use statrs::statistics::*; /// -/// let n = Geometric::new(0.3).unwrap(); -/// assert_eq!(n.mean().unwrap(), 1.0 / 0.3); +/// let n = Geometric::new(0.3)?; +/// assert_eq!(n.mean(), 1.0 / 0.3); /// assert_eq!(n.pmf(1), 0.3); /// assert_eq!(n.pmf(2), 0.21); +/// # Ok::<(), GeometricError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Geometric { @@ -75,9 +76,11 @@ impl Geometric { /// /// ``` /// use statrs::distribution::Geometric; + /// # use statrs::distribution::GeometricError; /// - /// let n = Geometric::new(0.5).unwrap(); + /// let n = Geometric::new(0.5)?; /// assert_eq!(n.p(), 0.5); + /// # Ok::<(), GeometricError>(()) /// ``` pub fn p(&self) -> f64 { self.p diff --git a/src/distribution/gumbel.rs b/src/distribution/gumbel.rs index c7e9c244..42a8dc45 100644 --- a/src/distribution/gumbel.rs +++ b/src/distribution/gumbel.rs @@ -9,13 +9,14 @@ use std::f64::{self, consts::PI}; /// # Examples /// /// ``` -/// use statrs::distribution::{Gumbel, Continuous}; -/// use statrs::{consts::EULER_MASCHERONI, statistics::Distribution}; +/// use statrs::distribution::{Gumbel, Continuous, GumbelError}; +/// use statrs::{consts::EULER_MASCHERONI, statistics::*}; /// -/// let n = Gumbel::new(0.0, 1.0).unwrap(); +/// let n = Gumbel::new(0.0, 1.0)?; /// assert_eq!(n.location(), 0.0); -/// assert_eq!(n.skewness().unwrap(), 1.13955); +/// assert_eq!(n.mean(), 0.5772156649015329); /// assert_eq!(n.pdf(0.0), 0.36787944117144233); +/// # Ok::<(), GumbelError>(()) /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Gumbel { @@ -82,10 +83,11 @@ impl Gumbel { /// # Examples /// /// ``` - /// use statrs::distribution::Gumbel; + /// use statrs::distribution::{Gumbel, GumbelError}; /// - /// let n = Gumbel::new(0.0, 1.0).unwrap(); + /// let n = Gumbel::new(0.0, 1.0)?; /// assert_eq!(n.location(), 0.0); + /// # Ok::<(), GumbelError>(()) /// ``` pub fn location(&self) -> f64 { self.location @@ -96,14 +98,19 @@ impl Gumbel { /// # Examples /// /// ``` - /// use statrs::distribution::Gumbel; + /// use statrs::distribution::{Gumbel, GumbelError}; /// - /// let n = Gumbel::new(0.0, 1.0).unwrap(); + /// let n = Gumbel::new(0.0, 1.0)?; /// assert_eq!(n.scale(), 1.0); + /// # Ok::<(), GumbelError>(()) /// ``` pub fn scale(&self) -> f64 { self.scale } + + pub fn std_dev(&self) -> f64 { + (PI * self.scale) / 6_f64.sqrt() + } } impl std::fmt::Display for Gumbel { diff --git a/src/distribution/hypergeometric.rs b/src/distribution/hypergeometric.rs index ba6d3456..409ee548 100644 --- a/src/distribution/hypergeometric.rs +++ b/src/distribution/hypergeometric.rs @@ -12,13 +12,13 @@ use std::f64; /// /// ``` /// use statrs::distribution::{Hypergeometric, Discrete}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = Hypergeometric::new(500, 50, 100).unwrap(); /// assert_eq!(n.mean().unwrap(), 10.); -/// assert!(prec::almost_eq(n.pmf(10), 0.14736784, 1e-8)); -/// assert!(prec::almost_eq(n.pmf(25), 3.537e-7, 1e-10)); +/// assert_relative_eq!(n.pmf(10), 0.14736784, epsilon=1e-8); +/// assert_relative_eq!(n.pmf(25), 3.537e-7, epsilon=1e-10); /// ``` #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct Hypergeometric { diff --git a/src/distribution/inverse_gamma.rs b/src/distribution/inverse_gamma.rs index 88abf91f..0cb40da7 100644 --- a/src/distribution/inverse_gamma.rs +++ b/src/distribution/inverse_gamma.rs @@ -11,11 +11,11 @@ use std::f64; /// /// ``` /// use statrs::distribution::{InverseGamma, Continuous}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = InverseGamma::new(1.1, 0.1).unwrap(); -/// assert!(prec::almost_eq(n.mean().unwrap(), 1.0, 1e-14)); +/// assert_relative_eq!(n.mean().unwrap(), 1.0, epsilon=1e-14); /// assert_eq!(n.pdf(1.0), 0.07554920138253064); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] diff --git a/src/distribution/log_normal.rs b/src/distribution/log_normal.rs index da377705..2bcb5960 100644 --- a/src/distribution/log_normal.rs +++ b/src/distribution/log_normal.rs @@ -12,12 +12,12 @@ use std::f64; /// /// ``` /// use statrs::distribution::{LogNormal, Continuous}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = LogNormal::new(0.0, 1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), (0.5f64).exp()); -/// assert!(prec::almost_eq(n.pdf(1.0), 0.3989422804014326779399, 1e-16)); +/// assert_eq!(n.mean(), (0.5f64).exp()); +/// assert_relative_eq!(n.pdf(1.0), 0.3989422804014326779399, epsilon=1e-16); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct LogNormal { diff --git a/src/distribution/multinomial.rs b/src/distribution/multinomial.rs index b62003c5..40532919 100644 --- a/src/distribution/multinomial.rs +++ b/src/distribution/multinomial.rs @@ -13,11 +13,11 @@ use nalgebra::{Cholesky, Dim, DimMin, Dyn, OMatrix, OVector}; /// /// ``` /// use statrs::distribution::Multinomial; -/// use statrs::statistics::MeanN; +/// use statrs::statistics::*; /// use nalgebra::vector; /// /// let n = Multinomial::new_from_nalgebra(vector![0.3, 0.7], 5).unwrap(); -/// assert_eq!(n.mean().unwrap(), (vector![1.5, 3.5])); +/// assert_eq!(n.mean(), (vector![1.5, 3.5])); /// ``` #[derive(Debug, Clone, PartialEq)] pub struct Multinomial diff --git a/src/distribution/multivariate_normal.rs b/src/distribution/multivariate_normal.rs index 9754a96d..1c52d375 100644 --- a/src/distribution/multivariate_normal.rs +++ b/src/distribution/multivariate_normal.rs @@ -78,11 +78,11 @@ where /// ``` /// use statrs::distribution::{MultivariateNormal, Continuous}; /// use nalgebra::{matrix, vector}; -/// use statrs::statistics::{MeanN, VarianceN}; +/// use statrs::statistics::*; /// /// let mvn = MultivariateNormal::new_from_nalgebra(vector![0., 0.], matrix![1., 0.; 0., 1.]).unwrap(); -/// assert_eq!(mvn.mean().unwrap(), vector![0., 0.]); -/// assert_eq!(mvn.variance().unwrap(), matrix![1., 0.; 0., 1.]); +/// assert_eq!(mvn.mean(), vector![0., 0.]); +/// assert_eq!(mvn.variance(), matrix![1., 0.; 0., 1.]); /// assert_eq!(mvn.pdf(&vector![1., 1.]), 0.05854983152431917); /// ``` #[derive(Clone, PartialEq, Debug)] diff --git a/src/distribution/multivariate_students_t.rs b/src/distribution/multivariate_students_t.rs index bc034397..3f169428 100644 --- a/src/distribution/multivariate_students_t.rs +++ b/src/distribution/multivariate_students_t.rs @@ -14,11 +14,11 @@ use std::f64::consts::PI; /// ``` /// use statrs::distribution::{MultivariateStudent, Continuous}; /// use nalgebra::{DVector, DMatrix}; -/// use statrs::statistics::{MeanN, VarianceN}; +/// use statrs::statistics::*; /// /// let mvs = MultivariateStudent::new(vec![0., 0.], vec![1., 0., 0., 1.], 4.).unwrap(); -/// assert_eq!(mvs.mean().unwrap(), DVector::from_vec(vec![0., 0.])); -/// assert_eq!(mvs.variance().unwrap(), DMatrix::from_vec(2, 2, vec![2., 0., 0., 2.])); +/// assert_eq!(mvs.mean(), Some(DVector::from_vec(vec![0., 0.]))); +/// assert_eq!(mvs.variance(), Some(DMatrix::from_vec(2, 2, vec![2., 0., 0., 2.]))); /// assert_eq!(mvs.pdf(&DVector::from_vec(vec![1., 1.])), 0.04715702017537655); /// ``` #[derive(Debug, Clone, PartialEq)] diff --git a/src/distribution/negative_binomial.rs b/src/distribution/negative_binomial.rs index b56ff268..f42252dd 100644 --- a/src/distribution/negative_binomial.rs +++ b/src/distribution/negative_binomial.rs @@ -25,13 +25,13 @@ use std::f64; /// /// ``` /// use statrs::distribution::{NegativeBinomial, Discrete}; -/// use statrs::statistics::DiscreteDistribution; -/// use statrs::prec::almost_eq; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let r = NegativeBinomial::new(4.0, 0.5).unwrap(); -/// assert_eq!(r.mean().unwrap(), 4.0); -/// assert!(almost_eq(r.pmf(0), 0.0625, 1e-8)); -/// assert!(almost_eq(r.pmf(3), 0.15625, 1e-8)); +/// assert_eq!(r.mean(), 4.0); +/// assert_relative_eq!(r.pmf(0), 0.0625, epsilon=1e-8); +/// assert_relative_eq!(r.pmf(3), 0.15625, epsilon=1e-8); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct NegativeBinomial { diff --git a/src/distribution/normal.rs b/src/distribution/normal.rs index cfce032f..0407ec7f 100644 --- a/src/distribution/normal.rs +++ b/src/distribution/normal.rs @@ -11,11 +11,12 @@ use std::f64; /// /// ``` /// use statrs::distribution::{Normal, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = Normal::new(0.0, 1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 0.0); -/// assert_eq!(n.pdf(1.0), 0.2419707245191433497978); +/// assert_eq!(n.mean(), 0.0); +/// assert_relative_eq!(n.pdf(1.0), 0.2419707245191433497978); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Normal { diff --git a/src/distribution/pareto.rs b/src/distribution/pareto.rs index 22a8de86..e056d8f9 100644 --- a/src/distribution/pareto.rs +++ b/src/distribution/pareto.rs @@ -9,12 +9,12 @@ use std::f64; /// /// ``` /// use statrs::distribution::{Pareto, Continuous}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let p = Pareto::new(1.0, 2.0).unwrap(); -/// assert_eq!(p.mean().unwrap(), 2.0); -/// assert!(prec::almost_eq(p.pdf(2.0), 0.25, 1e-15)); +/// assert_eq!(p.mean(), Some(2.0)); +/// assert_relative_eq!(p.pdf(2.0), 0.25); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Pareto { @@ -223,11 +223,7 @@ impl Max for Pareto { /// # Formula /// /// ```text -/// if α <= 1 { -/// f64::INFINITY -/// } else { -/// (α * x_m)/(α - 1) -/// } +/// (α * x_m)/(α - 1), where α > 1 /// ``` /// /// where `x_m` is the scale and `α` is the shape diff --git a/src/distribution/poisson.rs b/src/distribution/poisson.rs index b8ed6c77..36f2c877 100644 --- a/src/distribution/poisson.rs +++ b/src/distribution/poisson.rs @@ -10,12 +10,12 @@ use std::f64; /// /// ``` /// use statrs::distribution::{Poisson, Discrete}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = Poisson::new(1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 1.0); -/// assert!(prec::almost_eq(n.pmf(1), 0.367879441171442, 1e-15)); +/// assert_eq!(n.mean(), 1.0); +/// assert_relative_eq!(n.pmf(1), 0.367879441171442, epsilon=1e-14); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Poisson { diff --git a/src/distribution/students_t.rs b/src/distribution/students_t.rs index 99991b72..f11e6e8a 100644 --- a/src/distribution/students_t.rs +++ b/src/distribution/students_t.rs @@ -10,12 +10,12 @@ use std::f64; /// /// ``` /// use statrs::distribution::{StudentsT, Continuous}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = StudentsT::new(0.0, 1.0, 2.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 0.0); -/// assert!(prec::almost_eq(n.pdf(0.0), 0.353553390593274, 1e-15)); +/// assert_eq!(n.mean(), Some(0.0)); +/// assert_relative_eq!(n.pdf(0.0), 0.353553390593274, epsilon=1e-14); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct StudentsT { diff --git a/src/distribution/triangular.rs b/src/distribution/triangular.rs index ba306807..268c9969 100644 --- a/src/distribution/triangular.rs +++ b/src/distribution/triangular.rs @@ -10,10 +10,10 @@ use std::f64; /// /// ``` /// use statrs::distribution::{Triangular, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::statistics::*; /// /// let n = Triangular::new(0.0, 5.0, 2.5).unwrap(); -/// assert_eq!(n.mean().unwrap(), 7.5 / 3.0); +/// assert_eq!(n.mean(), 7.5 / 3.0); /// assert_eq!(n.pdf(2.5), 5.0 / 12.5); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] diff --git a/src/distribution/uniform.rs b/src/distribution/uniform.rs index 10df74bb..6289948f 100644 --- a/src/distribution/uniform.rs +++ b/src/distribution/uniform.rs @@ -11,10 +11,10 @@ use std::fmt::Debug; /// /// ``` /// use statrs::distribution::{Uniform, Continuous}; -/// use statrs::statistics::Distribution; +/// use statrs::statistics::*; /// /// let n = Uniform::new(0.0, 1.0).unwrap(); -/// assert_eq!(n.mean().unwrap(), 0.5); +/// assert_eq!(n.mean(), 0.5); /// assert_eq!(n.pdf(0.5), 1.0); /// ``` #[derive(Debug, Copy, Clone, PartialEq)] diff --git a/src/distribution/weibull.rs b/src/distribution/weibull.rs index 6a70abd9..ed2f29a6 100644 --- a/src/distribution/weibull.rs +++ b/src/distribution/weibull.rs @@ -11,13 +11,12 @@ use std::f64; /// /// ``` /// use statrs::distribution::{Weibull, Continuous}; -/// use statrs::statistics::Distribution; -/// use statrs::prec; +/// use statrs::statistics::*; +/// use approx::assert_relative_eq; /// /// let n = Weibull::new(10.0, 1.0).unwrap(); -/// assert!(prec::almost_eq(n.mean().unwrap(), -/// 0.95135076986687318362924871772654021925505786260884, 1e-15)); -/// assert_eq!(n.pdf(1.0), 3.6787944117144232159552377016146086744581113103177); +/// assert_relative_eq!(n.mean(), 0.951350769866873183629, epsilon = 1e-14); +/// assert_relative_eq!(n.pdf(1.0), 3.67879441171442321595, epsilon = 1e-14); /// ``` #[derive(Copy, Clone, PartialEq, Debug)] pub struct Weibull { diff --git a/src/lib.rs b/src/lib.rs index d4d80ebc..59942837 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,13 +20,13 @@ //! Statrs also comes with a number of useful utility traits for more detailed introspection of distributions. //! ``` //! use statrs::distribution::{Exp, Continuous, ContinuousCDF}; // `cdf` and `pdf` -//! use statrs::statistics::Distribution; // statistical moments and entropy +//! use statrs::statistics::*; // statistical moments and entropy //! //! let n = Exp::new(1.0).unwrap(); -//! assert_eq!(n.mean(), Some(1.0)); -//! assert_eq!(n.variance(), Some(1.0)); -//! assert_eq!(n.entropy(), Some(1.0)); -//! assert_eq!(n.skewness(), Some(2.0)); +//! assert_eq!(n.mean(), 1.0); +//! assert_eq!(n.variance(), 1.0); +//! assert_eq!(n.entropy(), 1.0); +//! assert_eq!(n.skewness(), 2.0); //! assert_eq!(n.cdf(1.0), 0.6321205588285576784045); //! assert_eq!(n.pdf(1.0), 0.3678794411714423215955); //! ``` @@ -36,7 +36,7 @@ //! //! ``` //! use statrs::distribution::FisherSnedecor; -//! use statrs::statistics::Distribution; +//! use statrs::statistics::*; //! //! let n = FisherSnedecor::new(1.0, 1.0).unwrap(); //! assert!(n.variance().is_none());