Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove StatsError type & replace Result<T, StatsError> with Option<T> #258

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Binomial, Discrete, DiscreteCDF};
use crate::statistics::*;
use crate::Result;
use rand::Rng;

/// Implements the
Expand Down Expand Up @@ -40,12 +39,12 @@ impl Bernoulli {
/// use statrs::distribution::Bernoulli;
///
/// let mut result = Bernoulli::new(0.5);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Bernoulli::new(-0.5);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(p: f64) -> Result<Bernoulli> {
pub fn new(p: f64) -> Option<Bernoulli> {
Binomial::new(p, 1).map(|b| Bernoulli { b })
}

Expand Down Expand Up @@ -271,7 +270,7 @@ mod testing {

fn try_create(p: f64) -> Bernoulli {
let n = Bernoulli::new(p);
assert!(n.is_ok());
assert!(n.is_some());
n.unwrap()
}

Expand All @@ -282,7 +281,7 @@ mod testing {

fn bad_create_case(p: f64) {
let n = Bernoulli::new(p);
assert!(n.is_err());
assert!(n.is_none());
}

fn get_value<T, F>(p: f64, eval: F) -> T
Expand Down
11 changes: 5 additions & 6 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::{beta, gamma};
use crate::is_zero;
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;

/// Implements the [Beta](https://en.wikipedia.org/wiki/Beta_distribution)
Expand Down Expand Up @@ -40,21 +39,21 @@ impl Beta {
/// use statrs::distribution::Beta;
///
/// let mut result = Beta::new(2.0, 2.0);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Beta::new(0.0, 0.0);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(shape_a: f64, shape_b: f64) -> Result<Beta> {
pub fn new(shape_a: f64, shape_b: f64) -> Option<Beta> {
if shape_a.is_nan()
|| shape_b.is_nan()
|| shape_a.is_infinite() && shape_b.is_infinite()
|| shape_a <= 0.0
|| shape_b <= 0.0
{
return Err(StatsError::BadParams);
return None;
};
Ok(Beta { shape_a, shape_b })
Some(Beta { shape_a, shape_b })
}

/// Returns the shapeA (α) of the beta distribution
Expand Down
15 changes: 7 additions & 8 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::distribution::{Discrete, DiscreteCDF};
use crate::function::{beta, factorial};
use crate::is_zero;
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
use std::f64;

Expand Down Expand Up @@ -43,16 +42,16 @@ impl Binomial {
/// use statrs::distribution::Binomial;
///
/// let mut result = Binomial::new(0.5, 5);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Binomial::new(-0.5, 5);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(p: f64, n: u64) -> Result<Binomial> {
pub fn new(p: f64, n: u64) -> Option<Binomial> {
if p.is_nan() || !(0.0..=1.0).contains(&p) {
Err(StatsError::BadParams)
None
} else {
Ok(Binomial { p, n })
Some(Binomial { p, n })
}
}

Expand Down Expand Up @@ -336,7 +335,7 @@ mod tests {

fn try_create(p: f64, n: u64) -> Binomial {
let n = Binomial::new(p, n);
assert!(n.is_ok());
assert!(n.is_some());
n.unwrap()
}

Expand All @@ -348,7 +347,7 @@ mod tests {

fn bad_create_case(p: f64, n: u64) {
let n = Binomial::new(p, n);
assert!(n.is_err());
assert!(n.is_none());
}

fn get_value<T, F>(p: f64, n: u64, eval: F) -> T
Expand Down
15 changes: 7 additions & 8 deletions src/distribution/categorical.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
use std::f64;

Expand Down Expand Up @@ -48,14 +47,14 @@ impl Categorical {
/// use statrs::distribution::Categorical;
///
/// let mut result = Categorical::new(&[0.0, 1.0, 2.0]);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Categorical::new(&[0.0, -1.0, 2.0]);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(prob_mass: &[f64]) -> Result<Categorical> {
pub fn new(prob_mass: &[f64]) -> Option<Categorical> {
if !super::internal::is_valid_multinomial(prob_mass, true) {
Err(StatsError::BadParams)
None
} else {
// extract un-normalized cdf
let cdf = prob_mass_to_cdf(prob_mass);
Expand All @@ -68,7 +67,7 @@ impl Categorical {
.iter_mut()
.zip(prob_mass.iter())
.for_each(|(np, pm)| *np = *pm / sum);
Ok(Categorical { norm_pmf, cdf, sf })
Some(Categorical { norm_pmf, cdf, sf })
}
}

Expand Down Expand Up @@ -359,7 +358,7 @@ mod tests {

fn try_create(prob_mass: &[f64]) -> Categorical {
let n = Categorical::new(prob_mass);
assert!(n.is_ok());
assert!(n.is_some());
n.unwrap()
}

Expand All @@ -369,7 +368,7 @@ mod tests {

fn bad_create_case(prob_mass: &[f64]) {
let n = Categorical::new(prob_mass);
assert!(n.is_err());
assert!(n.is_none());
}

fn get_value<T, F>(prob_mass: &[f64], eval: F) -> T
Expand Down
15 changes: 7 additions & 8 deletions src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
use std::f64;

Expand Down Expand Up @@ -37,16 +36,16 @@ impl Cauchy {
/// use statrs::distribution::Cauchy;
///
/// let mut result = Cauchy::new(0.0, 1.0);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Cauchy::new(0.0, -1.0);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(location: f64, scale: f64) -> Result<Cauchy> {
pub fn new(location: f64, scale: f64) -> Option<Cauchy> {
if location.is_nan() || scale.is_nan() || scale <= 0.0 {
Err(StatsError::BadParams)
None
} else {
Ok(Cauchy { location, scale })
Some(Cauchy { location, scale })
}
}

Expand Down Expand Up @@ -240,7 +239,7 @@ mod tests {

fn try_create(location: f64, scale: f64) -> Cauchy {
let n = Cauchy::new(location, scale);
assert!(n.is_ok());
assert!(n.is_some());
n.unwrap()
}

Expand All @@ -252,7 +251,7 @@ mod tests {

fn bad_create_case(location: f64, scale: f64) {
let n = Cauchy::new(location, scale);
assert!(n.is_err());
assert!(n.is_none());
}

fn test_case<F>(location: f64, scale: f64, expected: f64, eval: F)
Expand Down
15 changes: 7 additions & 8 deletions src/distribution/chi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::gamma;
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
use std::f64;

Expand Down Expand Up @@ -39,16 +38,16 @@ impl Chi {
/// use statrs::distribution::Chi;
///
/// let mut result = Chi::new(2.0);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Chi::new(0.0);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(freedom: f64) -> Result<Chi> {
pub fn new(freedom: f64) -> Option<Chi> {
if freedom.is_nan() || freedom <= 0.0 {
Err(StatsError::BadParams)
None
} else {
Ok(Chi { freedom })
Some(Chi { freedom })
}
}

Expand Down Expand Up @@ -332,7 +331,7 @@ mod tests {

fn try_create(freedom: f64) -> Chi {
let n = Chi::new(freedom);
assert!(n.is_ok());
assert!(n.is_some());
n.unwrap()
}

Expand All @@ -343,7 +342,7 @@ mod tests {

fn bad_create_case(freedom: f64) {
let n = Chi::new(freedom);
assert!(n.is_err());
assert!(n.is_none());
}

fn get_value<F>(freedom: f64, eval: F) -> f64
Expand Down
9 changes: 4 additions & 5 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF, Gamma};
use crate::statistics::*;
use crate::Result;
use rand::Rng;
use std::f64;

Expand Down Expand Up @@ -43,12 +42,12 @@ impl ChiSquared {
/// use statrs::distribution::ChiSquared;
///
/// let mut result = ChiSquared::new(3.0);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = ChiSquared::new(0.0);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(freedom: f64) -> Result<ChiSquared> {
pub fn new(freedom: f64) -> Option<ChiSquared> {
Gamma::new(freedom / 2.0, 0.5).map(|g| ChiSquared { freedom, g })
}

Expand Down Expand Up @@ -297,7 +296,7 @@ mod tests {

fn try_create(freedom: f64) -> ChiSquared {
let n = ChiSquared::new(freedom);
assert!(n.is_ok());
assert!(n.is_some());
n.unwrap()
}

Expand Down
15 changes: 7 additions & 8 deletions src/distribution/dirac.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;

/// Implements the [Dirac Delta](https://en.wikipedia.org/wiki/Dirac_delta_function#As_a_distribution)
Expand Down Expand Up @@ -31,16 +30,16 @@ impl Dirac {
/// use statrs::distribution::Dirac;
///
/// let mut result = Dirac::new(0.0);
/// assert!(result.is_ok());
/// assert!(result.is_some());
///
/// result = Dirac::new(f64::NAN);
/// assert!(result.is_err());
/// assert!(result.is_none());
/// ```
pub fn new(v: f64) -> Result<Self> {
pub fn new(v: f64) -> Option<Self> {
if v.is_nan() {
Err(StatsError::BadParams)
None
} else {
Ok(Dirac(v))
Some(Dirac(v))
}
}
}
Expand Down Expand Up @@ -198,7 +197,7 @@ mod tests {

fn try_create(v: f64) -> Dirac {
let d = Dirac::new(v);
assert!(d.is_ok());
assert!(d.is_some());
d.unwrap()
}

Expand All @@ -209,7 +208,7 @@ mod tests {

fn bad_create_case(v: f64) {
let d = Dirac::new(v);
assert!(d.is_err());
assert!(d.is_none());
}

fn test_case<F>(v: f64, expected: f64, eval: F)
Expand Down
Loading