Skip to content

Commit

Permalink
doc: updates doc tests to use new traits
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion committed Nov 5, 2024
1 parent 01bb6d5 commit 3844e92
Show file tree
Hide file tree
Showing 32 changed files with 209 additions and 163 deletions.
19 changes: 11 additions & 8 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/distribution/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 10 additions & 7 deletions src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D>
Expand Down Expand Up @@ -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<f64, D> {
&self.alpha
Expand Down
14 changes: 10 additions & 4 deletions src/distribution/discrete_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
}

Check warning on line 74 in src/distribution/discrete_uniform.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/discrete_uniform.rs#L71-L74

Added lines #L71 - L74 were not covered by tests
}

impl std::fmt::Display for DiscreteUniform {
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<T: PartialOrd> Ord for NonNan<T> {
///
/// ```
/// use statrs::distribution::{Continuous, Empirical};
/// use statrs::statistics::Distribution;
/// use statrs::statistics::*;
///
/// let samples = vec![0.0, 5.0, 10.0];
///
Expand Down
Loading

0 comments on commit 3844e92

Please sign in to comment.