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

Keep calm and carry on #56

Merged
merged 4 commits into from
Jun 2, 2017
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v0.8.0
- `cdf(x)`, `pdf(x)` and `pmf(x)` now return the correct value instead of panicking when `x` is outside the range of values that the distribution can attain.

v0.7.0
- Implemented `Categorical` distribution
- Implemented `Erlang` distribution
Expand Down
12 changes: 0 additions & 12 deletions src/distribution/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ impl Univariate<u64, f64> for Bernoulli {
/// Calculates the cumulative distribution
/// function for the bernoulli distribution at `x`.
///
/// # Panics
///
/// If `x < 0.0` or `x > 1.0`
///
/// # Formula
///
/// ```ignore
Expand Down Expand Up @@ -277,10 +273,6 @@ impl Discrete<u64, f64> for Bernoulli {
/// Calculates the probability mass function for the
/// bernoulli distribution at `x`.
///
/// # Panics
///
/// If `x > 1`
///
/// # Formula
///
/// ```ignore
Expand All @@ -294,10 +286,6 @@ impl Discrete<u64, f64> for Bernoulli {
/// Calculates the log probability mass function for the
/// bernoulli distribution at `x`.
///
/// # Panics
///
/// If `x > 1`
///
/// # Formula
///
/// ```ignore
Expand Down
55 changes: 22 additions & 33 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ impl Univariate<f64, f64> for Beta {
/// Calculates the cumulative distribution function for the beta distribution
/// at `x`
///
/// # Panics
///
/// If `x < 0.0` or `x > 1.0`
///
/// # Formula
///
/// ```ignore
Expand All @@ -149,9 +145,9 @@ impl Univariate<f64, f64> for Beta {
/// where `α` is shapeA, `β` is shapeB, and `I_x` is the regularized
/// lower incomplete beta function
fn cdf(&self, x: f64) -> f64 {
assert!(x >= 0.0 && x <= 1.0,
format!("{}", StatsError::ArgIntervalIncl("x", 0.0, 1.0)));
if x == 1.0 {
if x < 0.0 {
0.0
} else if x >= 1.0 {
1.0
} else if self.shape_a == f64::INFINITY && self.shape_b == f64::INFINITY {
if x < 0.5 { 0.0 } else { 1.0 }
Expand Down Expand Up @@ -348,10 +344,6 @@ impl Mode<f64> for Beta {
impl Continuous<f64, f64> for Beta {
/// Calculates the probability density function for the beta distribution at `x`.
///
/// # Panics
///
/// If `x < 0.0` or `x > 1.0`
///
/// # Formula
///
/// ```ignore
Expand All @@ -362,9 +354,9 @@ impl Continuous<f64, f64> for Beta {
///
/// where `α` is shapeA, `β` is shapeB, and `Γ` is the gamma function
fn pdf(&self, x: f64) -> f64 {
assert!(x >= 0.0 && x <= 1.0,
format!("{}", StatsError::ArgIntervalIncl("x", 0.0, 1.0)));
if self.shape_a == f64::INFINITY && self.shape_b == f64::INFINITY {
if x < 0.0 || x > 1.0 {
0.0
} else if self.shape_a == f64::INFINITY && self.shape_b == f64::INFINITY {
if x == 0.5 { f64::INFINITY } else { 0.0 }
} else if self.shape_a == f64::INFINITY {
if x == 1.0 { f64::INFINITY } else { 0.0 }
Expand All @@ -383,10 +375,6 @@ impl Continuous<f64, f64> for Beta {

/// Calculates the log probability density function for the beta distribution at `x`.
///
/// # Panics
///
/// If `x < 0.0` or `x > 1.0`
///
/// # Formula
///
/// ```ignore
Expand All @@ -397,9 +385,9 @@ impl Continuous<f64, f64> for Beta {
///
/// where `α` is shapeA, `β` is shapeB, and `Γ` is the gamma function
fn ln_pdf(&self, x: f64) -> f64 {
assert!(x >= 0.0 && x <= 1.0,
format!("{}", StatsError::ArgIntervalIncl("x", 0.0, 1.0)));
if self.shape_a == f64::INFINITY && self.shape_b == f64::INFINITY {
if x < 0.0 || x > 1.0 {
f64::NEG_INFINITY
} else if self.shape_a == f64::INFINITY && self.shape_b == f64::INFINITY {
if x == 0.5 {
f64::INFINITY
} else {
Expand Down Expand Up @@ -443,6 +431,7 @@ mod test {
use std::f64;
use statistics::*;
use distribution::{Univariate, Continuous, Beta};
use distribution::internal::*;

fn try_create(shape_a: f64, shape_b: f64) -> Beta {
let n = Beta::new(shape_a, shape_b);
Expand Down Expand Up @@ -612,15 +601,13 @@ mod test {
}

#[test]
#[should_panic]
fn test_pdf_input_lt_zero() {
get_value(1.0, 1.0, |x| x.pdf(-1.0));
test_case(1.0, 1.0, 0.0, |x| x.pdf(-1.0));
}

#[test]
#[should_panic]
fn test_pdf_input_gt_one() {
get_value(1.0, 1.0, |x| x.pdf(2.0));
test_case(1.0, 1.0, 0.0, |x| x.pdf(2.0));
}

#[test]
Expand All @@ -646,15 +633,13 @@ mod test {
}

#[test]
#[should_panic]
fn test_ln_pdf_input_lt_zero() {
get_value(1.0, 1.0, |x| x.ln_pdf(-1.0));
test_case(1.0, 1.0, f64::NEG_INFINITY, |x| x.ln_pdf(-1.0));
}

#[test]
#[should_panic]
fn test_ln_pdf_input_gt_one() {
get_value(1.0, 1.0, |x| x.ln_pdf(2.0));
test_case(1.0, 1.0, f64::NEG_INFINITY, |x| x.ln_pdf(2.0));
}

#[test]
Expand All @@ -680,14 +665,18 @@ mod test {
}

#[test]
#[should_panic]
fn test_cdf_input_lt_zero() {
get_value(1.0, 1.0, |x| x.cdf(-1.0));
test_case(1.0, 1.0, 0.0, |x| x.cdf(-1.0));
}

#[test]
#[should_panic]
fn test_cdf_input_gt_zero() {
get_value(1.0, 1.0, |x| x.cdf(2.0));
test_case(1.0, 1.0, 1.0, |x| x.cdf(2.0));
}

#[test]
fn test_continuous() {
test::check_continuous_distribution(&try_create(1.2, 3.4), 0.0, 1.0);
test::check_continuous_distribution(&try_create(4.5, 6.7), 0.0, 1.0);
}
}
41 changes: 18 additions & 23 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ impl Univariate<u64, f64> for Binomial {
/// Calulcates the cumulative distribution function for the
/// binomial distribution at `x`
///
/// # Panics
///
/// If `x < 0.0` or `x > n`
///
/// # Formula
///
/// ```ignore
Expand All @@ -147,9 +143,9 @@ impl Univariate<u64, f64> for Binomial {
///
/// where `I_(x)(a, b)` is the regularized incomplete beta function
fn cdf(&self, x: f64) -> f64 {
assert!(x >= 0.0 && x <= self.n as f64,
format!("{}", StatsError::ArgIntervalIncl("x", 0.0, self.n as f64)));
if x == self.n as f64 {
if x < 0.0 {
0.0
} else if x >= self.n as f64 {
1.0
} else {
let k = x.floor();
Expand Down Expand Up @@ -293,18 +289,15 @@ impl Discrete<u64, f64> for Binomial {
/// Calculates the probability mass function for the binomial
/// distribution at `x`
///
/// # Panics
///
/// If `x > n`
///
/// # Formula
///
/// ```ignore
/// (n choose k) * p^k * (1 - p)^(n - k)
/// ```
fn pmf(&self, x: u64) -> f64 {
assert!(x <= self.n,
format!("{}", StatsError::ArgLte("x", 1.0)));
if x > self.n {
return 0.0;
}
match self.p {
0.0 if x == 0 => 1.0,
0.0 => 0.0,
Expand All @@ -321,18 +314,15 @@ impl Discrete<u64, f64> for Binomial {
/// Calculates the log probability mass function for the binomial
/// distribution at `x`
///
/// # Panics
///
/// If `x > n`
///
/// # Formula
///
/// ```ignore
/// ln((n choose k) * p^k * (1 - p)^(n - k))
/// ```
fn ln_pmf(&self, x: u64) -> f64 {
assert!(x <= self.n,
format!("{}", StatsError::ArgLte("x", 1.0)));
if x > self.n {
return f64::NEG_INFINITY;
}
match self.p {
0.0 if x == 0 => 0.0,
0.0 => f64::NEG_INFINITY,
Expand All @@ -353,6 +343,7 @@ mod test {
use std::f64;
use statistics::*;
use distribution::{Univariate, Discrete, Binomial};
use distribution::internal::*;

fn try_create(p: f64, n: u64) -> Binomial {
let n = Binomial::new(p, n);
Expand Down Expand Up @@ -548,14 +539,18 @@ mod test {
}

#[test]
#[should_panic]
fn test_cdf_lower_bound() {
get_value(0.5, 3, |x| x.cdf(-1.0));
test_case(0.5, 3, 0.0, |x| x.cdf(-1.0));
}

#[test]
#[should_panic]
fn test_cdf_upper_bound() {
get_value(0.5, 3, |x| x.cdf(5.0));
test_case(0.5, 3, 1.0, |x| x.cdf(5.0));
}

#[test]
fn test_discrete() {
test::check_discrete_distribution(&try_create(0.3, 5), 5);
test::check_discrete_distribution(&try_create(0.7, 10), 10);
}
}
Loading