Skip to content

Commit 3844e92

Browse files
committed
doc: updates doc tests to use new traits
1 parent 01bb6d5 commit 3844e92

32 files changed

+209
-163
lines changed

src/distribution/bernoulli.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ use crate::statistics::*;
1010
/// # Examples
1111
///
1212
/// ```
13-
/// use statrs::distribution::{Bernoulli, Discrete};
14-
/// use statrs::statistics::Distribution;
13+
/// use statrs::distribution::{Bernoulli, BinomialError, Discrete};
14+
/// use statrs::statistics::*;
1515
///
16-
/// let n = Bernoulli::new(0.5).unwrap();
17-
/// assert_eq!(n.mean().unwrap(), 0.5);
16+
/// let n = Bernoulli::new(0.5)?;
17+
/// assert_eq!(n.mean(), 0.5);
1818
/// assert_eq!(n.pmf(0), 0.5);
1919
/// assert_eq!(n.pmf(1), 0.5);
20+
/// # Ok::<(), BinomialError>(())
2021
/// ```
2122
#[derive(Copy, Clone, PartialEq, Debug)]
2223
pub struct Bernoulli {
@@ -53,10 +54,11 @@ impl Bernoulli {
5354
/// # Examples
5455
///
5556
/// ```
56-
/// use statrs::distribution::Bernoulli;
57+
/// use statrs::distribution::{Bernoulli, BinomialError};
5758
///
58-
/// let n = Bernoulli::new(0.5).unwrap();
59+
/// let n = Bernoulli::new(0.5)?;
5960
/// assert_eq!(n.p(), 0.5);
61+
/// # Ok::<(), BinomialError>(())
6062
/// ```
6163
pub fn p(&self) -> f64 {
6264
self.b.p()
@@ -68,10 +70,11 @@ impl Bernoulli {
6870
/// # Examples
6971
///
7072
/// ```
71-
/// use statrs::distribution::Bernoulli;
73+
/// use statrs::distribution::{Bernoulli, BinomialError};
7274
///
73-
/// let n = Bernoulli::new(0.5).unwrap();
75+
/// let n = Bernoulli::new(0.5)?;
7476
/// assert_eq!(n.n(), 1);
77+
/// # Ok::<(), BinomialError>(())
7578
/// ```
7679
pub fn n(&self) -> u64 {
7780
1

src/distribution/beta.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ use crate::statistics::*;
88
/// # Examples
99
///
1010
/// ```
11-
/// use statrs::distribution::{Beta, Continuous};
11+
/// use statrs::distribution::{Beta, Continuous, BetaError};
1212
/// use statrs::statistics::*;
1313
/// use statrs::prec;
1414
///
15-
/// let n = Beta::new(2.0, 2.0).unwrap();
16-
/// assert_eq!(n.mean().unwrap(), 0.5);
15+
/// let n = Beta::new(2.0, 2.0)?;
16+
/// assert_eq!(n.mean(), 0.5);
1717
/// assert!(prec::almost_eq(n.pdf(0.5), 1.5, 1e-14));
18+
/// # Ok::<(), BetaError>(())
1819
/// ```
1920
#[derive(Copy, Clone, PartialEq, Debug)]
2021
pub struct Beta {
@@ -82,10 +83,11 @@ impl Beta {
8283
/// # Examples
8384
///
8485
/// ```
85-
/// use statrs::distribution::Beta;
86+
/// use statrs::distribution::{Beta, BetaError};
8687
///
87-
/// let n = Beta::new(1.0, 2.0).unwrap();
88+
/// let n = Beta::new(1.0, 2.0)?;
8889
/// assert_eq!(n.shape_a(), 1.0);
90+
/// # Ok::<(), BetaError>(())
8991
/// ```
9092
pub fn shape_a(&self) -> f64 {
9193
self.shape_a
@@ -96,10 +98,11 @@ impl Beta {
9698
/// # Examples
9799
///
98100
/// ```
99-
/// use statrs::distribution::Beta;
101+
/// use statrs::distribution::{Beta, BetaError};
100102
///
101-
/// let n = Beta::new(1.0, 2.0).unwrap();
103+
/// let n = Beta::new(1.0, 2.0)?;
102104
/// assert_eq!(n.shape_b(), 2.0);
105+
/// # Ok::<(), BetaError>(())
103106
/// ```
104107
pub fn shape_b(&self) -> f64 {
105108
self.shape_b

src/distribution/binomial.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ use std::f64;
1010
/// # Examples
1111
///
1212
/// ```
13-
/// use statrs::distribution::{Binomial, Discrete};
14-
/// use statrs::statistics::Distribution;
13+
/// use statrs::distribution::{Binomial, BinomialError, Discrete};
14+
/// use statrs::statistics::*;
1515
///
16-
/// let n = Binomial::new(0.5, 5).unwrap();
17-
/// assert_eq!(n.mean().unwrap(), 2.5);
16+
/// let n = Binomial::new(0.5, 5)?;
17+
/// assert_eq!(n.mean(), 2.5);
1818
/// assert_eq!(n.pmf(0), 0.03125);
1919
/// assert_eq!(n.pmf(3), 0.3125);
20+
/// # Ok::<(), BinomialError>(())
2021
/// ```
2122
#[derive(Copy, Clone, PartialEq, Debug)]
2223
pub struct Binomial {
@@ -78,10 +79,11 @@ impl Binomial {
7879
/// # Examples
7980
///
8081
/// ```
81-
/// use statrs::distribution::Binomial;
82+
/// use statrs::distribution::{Binomial, BinomialError};
8283
///
83-
/// let n = Binomial::new(0.5, 5).unwrap();
84+
/// let n = Binomial::new(0.5, 5)?;
8485
/// assert_eq!(n.p(), 0.5);
86+
/// # Ok::<(), BinomialError>(())
8587
/// ```
8688
pub fn p(&self) -> f64 {
8789
self.p
@@ -93,10 +95,11 @@ impl Binomial {
9395
/// # Examples
9496
///
9597
/// ```
96-
/// use statrs::distribution::Binomial;
98+
/// use statrs::distribution::{Binomial, BinomialError};
9799
///
98-
/// let n = Binomial::new(0.5, 5).unwrap();
100+
/// let n = Binomial::new(0.5, 5)?;
99101
/// assert_eq!(n.n(), 5);
102+
/// # Ok::<(), BinomialError>(())
100103
/// ```
101104
pub fn n(&self) -> u64 {
102105
self.n

src/distribution/categorical.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ use std::f64;
1010
/// # Examples
1111
///
1212
/// ```
13-
/// use statrs::distribution::{Categorical, Discrete};
14-
/// use statrs::statistics::Distribution;
13+
/// use statrs::distribution::{Categorical, CategoricalError, Discrete};
14+
/// use statrs::statistics::*;
1515
/// use statrs::prec;
1616
///
17-
/// let n = Categorical::new(&[0.0, 1.0, 2.0]).unwrap();
18-
/// assert!(prec::almost_eq(n.mean().unwrap(), 5.0 / 3.0, 1e-15));
17+
/// let n = Categorical::new(&[0.0, 1.0, 2.0])?;
18+
/// assert!(prec::almost_eq(n.mean(), 5.0 / 3.0, 1e-15));
1919
/// assert_eq!(n.pmf(1), 1.0 / 3.0);
20+
/// # Ok::<(), CategoricalError>(())
2021
/// ```
2122
#[derive(Clone, PartialEq, Debug)]
2223
pub struct Categorical {

src/distribution/cauchy.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use std::f64;
88
/// # Examples
99
///
1010
/// ```
11-
/// use statrs::distribution::{Cauchy, Continuous};
12-
/// use statrs::statistics::Mode;
11+
/// use statrs::distribution::{Cauchy, Continuous, CauchyError};
12+
/// use statrs::statistics::*;
1313
///
14-
/// let n = Cauchy::new(0.0, 1.0).unwrap();
14+
/// let n = Cauchy::new(0.0, 1.0)?;
1515
/// assert_eq!(n.mode().unwrap(), 0.0);
1616
/// assert_eq!(n.pdf(1.0), 0.1591549430918953357689);
17+
/// # Ok::<(), CauchyError>(())
1718
/// ```
1819
#[derive(Copy, Clone, PartialEq, Debug)]
1920
pub struct Cauchy {
@@ -80,10 +81,11 @@ impl Cauchy {
8081
/// # Examples
8182
///
8283
/// ```
83-
/// use statrs::distribution::Cauchy;
84+
/// use statrs::distribution::{Cauchy, CauchyError};
8485
///
85-
/// let n = Cauchy::new(0.0, 1.0).unwrap();
86+
/// let n = Cauchy::new(0.0, 1.0)?;
8687
/// assert_eq!(n.location(), 0.0);
88+
/// # Ok::<(), CauchyError>(())
8789
/// ```
8890
pub fn location(&self) -> f64 {
8991
self.location
@@ -94,10 +96,11 @@ impl Cauchy {
9496
/// # Examples
9597
///
9698
/// ```
97-
/// use statrs::distribution::Cauchy;
99+
/// use statrs::distribution::{Cauchy, CauchyError};
98100
///
99-
/// let n = Cauchy::new(0.0, 1.0).unwrap();
101+
/// let n = Cauchy::new(0.0, 1.0)?;
100102
/// assert_eq!(n.scale(), 1.0);
103+
/// # Ok::<(), CauchyError>(())
101104
/// ```
102105
pub fn scale(&self) -> f64 {
103106
self.scale

src/distribution/chi.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ use std::f64;
99
/// # Examples
1010
///
1111
/// ```
12-
/// use statrs::distribution::{Chi, Continuous};
13-
/// use statrs::statistics::Distribution;
12+
/// use statrs::distribution::{Chi, Continuous, ChiError};
13+
/// use statrs::statistics::*;
1414
/// use statrs::prec;
1515
///
16-
/// let n = Chi::new(2.0).unwrap();
16+
/// let n = Chi::new(2.0)?;
1717
/// assert!(prec::almost_eq(n.mean().unwrap(), 1.25331413731550025121, 1e-14));
1818
/// assert!(prec::almost_eq(n.pdf(1.0), 0.60653065971263342360, 1e-15));
19+
/// # Ok::<(), ChiError>(())
1920
/// ```
2021
#[derive(Copy, Clone, PartialEq, Debug)]
2122
pub struct Chi {
@@ -77,10 +78,12 @@ impl Chi {
7778
/// # Examples
7879
///
7980
/// ```
80-
/// use statrs::distribution::Chi;
81+
/// use statrs::distribution::{Chi, ChiError};
82+
///
8183
///
82-
/// let n = Chi::new(2.0).unwrap();
84+
/// let n = Chi::new(2.0)?;
8385
/// assert_eq!(n.freedom(), 2.0);
86+
/// # Ok::<(), ChiError>(())
8487
/// ```
8588
pub fn freedom(&self) -> f64 {
8689
self.freedom

src/distribution/chi_squared.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ use std::f64;
1111
/// # Examples
1212
///
1313
/// ```
14-
/// use statrs::distribution::{ChiSquared, Continuous};
15-
/// use statrs::statistics::Distribution;
14+
/// use statrs::distribution::{ChiSquared, Continuous, GammaError};
15+
/// use statrs::statistics::*;
1616
/// use statrs::prec;
1717
///
18-
/// let n = ChiSquared::new(3.0).unwrap();
19-
/// assert_eq!(n.mean().unwrap(), 3.0);
18+
/// let n = ChiSquared::new(3.0)?;
19+
/// assert_eq!(n.mean(), 3.0);
2020
/// assert!(prec::almost_eq(n.pdf(4.0), 0.107981933026376103901, 1e-15));
21+
/// # Ok::<(), GammaError>(())
2122
/// ```
2223
#[derive(Copy, Clone, PartialEq, Debug)]
2324
pub struct ChiSquared {
@@ -56,10 +57,11 @@ impl ChiSquared {
5657
/// # Examples
5758
///
5859
/// ```
59-
/// use statrs::distribution::ChiSquared;
60+
/// use statrs::distribution::{ChiSquared, GammaError};
6061
///
61-
/// let n = ChiSquared::new(3.0).unwrap();
62+
/// let n = ChiSquared::new(3.0)?;
6263
/// assert_eq!(n.freedom(), 3.0);
64+
/// # Ok::<(), GammaError>(())
6365
/// ```
6466
pub fn freedom(&self) -> f64 {
6567
self.freedom
@@ -70,10 +72,11 @@ impl ChiSquared {
7072
/// # Examples
7173
///
7274
/// ```
73-
/// use statrs::distribution::ChiSquared;
75+
/// use statrs::distribution::{ChiSquared, GammaError};
7476
///
75-
/// let n = ChiSquared::new(3.0).unwrap();
77+
/// let n = ChiSquared::new(3.0)?;
7678
/// assert_eq!(n.shape(), 3.0 / 2.0);
79+
/// # Ok::<(), GammaError>(())
7780
/// ```
7881
pub fn shape(&self) -> f64 {
7982
self.g.shape()
@@ -84,10 +87,11 @@ impl ChiSquared {
8487
/// # Examples
8588
///
8689
/// ```
87-
/// use statrs::distribution::ChiSquared;
90+
/// use statrs::distribution::{ChiSquared, GammaError};
8891
///
89-
/// let n = ChiSquared::new(3.0).unwrap();
92+
/// let n = ChiSquared::new(3.0)?;
9093
/// assert_eq!(n.rate(), 0.5);
94+
/// # Ok::<(), GammaError>(())
9195
/// ```
9296
pub fn rate(&self) -> f64 {
9397
self.g.rate()

src/distribution/dirac.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ use crate::statistics::*;
77
/// # Examples
88
///
99
/// ```
10-
/// use statrs::distribution::{Dirac, Continuous};
11-
/// use statrs::statistics::Distribution;
10+
/// use statrs::distribution::{Dirac, Continuous, DiracError};
11+
/// use statrs::statistics::*;
1212
///
13-
/// let n = Dirac::new(3.0).unwrap();
14-
/// assert_eq!(n.mean().unwrap(), 3.0);
13+
/// let n = Dirac::new(3.0)?;
14+
/// assert_eq!(n.mean(), 3.0);
15+
/// # Ok::<(), DiracError>(())
1516
/// ```
1617
#[derive(Debug, Copy, Clone, PartialEq)]
1718
pub struct Dirac(f64);

src/distribution/dirichlet.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use std::f64;
1212
/// # Examples
1313
///
1414
/// ```
15-
/// use statrs::distribution::{Dirichlet, Continuous};
16-
/// use statrs::statistics::Distribution;
15+
/// use statrs::distribution::{Dirichlet, Continuous, DirichletError};
16+
/// use statrs::statistics::*;
1717
/// use nalgebra::DVector;
18-
/// use statrs::statistics::MeanN;
1918
///
20-
/// let n = Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap();
21-
/// assert_eq!(n.mean().unwrap(), DVector::from_vec(vec![1.0 / 6.0, 1.0 / 3.0, 0.5]));
19+
/// let n = Dirichlet::new(vec![1.0, 2.0, 3.0])?;
20+
/// assert_eq!(n.mean(), DVector::from_vec(vec![1.0 / 6.0, 1.0 / 3.0, 0.5]));
2221
/// assert_eq!(n.pdf(&DVector::from_vec(vec![0.33333, 0.33333, 0.33333])), 2.222155556222205);
22+
/// # Ok::<(), DirichletError>(())
2323
/// ```
2424
#[derive(Clone, PartialEq, Debug)]
2525
pub struct Dirichlet<D>
@@ -138,11 +138,12 @@ where
138138
/// # Examples
139139
///
140140
/// ```
141-
/// use statrs::distribution::Dirichlet;
141+
/// use statrs::distribution::{Dirichlet, DirichletError};
142142
/// use nalgebra::DVector;
143143
///
144-
/// let n = Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap();
144+
/// let n = Dirichlet::new(vec![1.0, 2.0, 3.0])?;
145145
/// assert_eq!(n.alpha(), &DVector::from_vec(vec![1.0, 2.0, 3.0]));
146+
/// # Ok::<(), DirichletError>(())
146147
/// ```
147148
pub fn alpha(&self) -> &nalgebra::OVector<f64, D> {
148149
&self.alpha

src/distribution/discrete_uniform.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use crate::statistics::*;
88
/// # Examples
99
///
1010
/// ```
11-
/// use statrs::distribution::{DiscreteUniform, Discrete};
12-
/// use statrs::statistics::Distribution;
11+
/// use statrs::distribution::{DiscreteUniform, Discrete, DiscreteUniformError};
12+
/// use statrs::statistics::*;
1313
///
14-
/// let n = DiscreteUniform::new(0, 5).unwrap();
15-
/// assert_eq!(n.mean().unwrap(), 2.5);
14+
/// let n = DiscreteUniform::new(0, 5)?;
15+
/// assert_eq!(n.mean(), 2.5);
1616
/// assert_eq!(n.pmf(3), 1.0 / 6.0);
17+
/// # Ok::<(), DiscreteUniformError>(())
1718
/// ```
1819
#[derive(Copy, Clone, PartialEq, Debug)]
1920
pub struct DiscreteUniform {
@@ -66,6 +67,11 @@ impl DiscreteUniform {
6667
Ok(DiscreteUniform { min, max })
6768
}
6869
}
70+
71+
pub fn std_dev(&self) -> f64 {
72+
let dist = self.max - self.min + 1;
73+
(dist * dist) as f64 / 12.
74+
}
6975
}
7076

7177
impl std::fmt::Display for DiscreteUniform {

0 commit comments

Comments
 (0)