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

Fix simple clippy warnings #216

Merged
merged 7 commits into from
Apr 21, 2024
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
105 changes: 52 additions & 53 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::function::{beta, gamma};
use crate::is_zero;
use crate::statistics::*;
use crate::{Result, StatsError};
use core::f64::INFINITY as INF;
use rand::Rng;

/// Implements the [Beta](https://en.wikipedia.org/wiki/Beta_distribution)
Expand Down Expand Up @@ -326,13 +325,13 @@ impl Continuous<f64, f64> for Beta {
0.0
} else if self.shape_a.is_infinite() {
if ulps_eq!(x, 1.0) {
INF
f64::INFINITY
} else {
0.0
}
} else if self.shape_b.is_infinite() {
if is_zero(x) {
INF
f64::INFINITY
} else {
0.0
}
Expand Down Expand Up @@ -361,18 +360,18 @@ impl Continuous<f64, f64> for Beta {
/// where `α` is shapeA, `β` is shapeB, and `Γ` is the gamma function
fn ln_pdf(&self, x: f64) -> f64 {
if !(0.0..=1.0).contains(&x) {
-INF
f64::NEG_INFINITY
} else if self.shape_a.is_infinite() {
if ulps_eq!(x, 1.0) {
INF
f64::INFINITY
} else {
-INF
f64::NEG_INFINITY
}
} else if self.shape_b.is_infinite() {
if is_zero(x) {
INF
f64::INFINITY
} else {
-INF
f64::NEG_INFINITY
}
} else if ulps_eq!(self.shape_a, 1.0) && ulps_eq!(self.shape_b, 1.0) {
0.0
Expand All @@ -383,14 +382,14 @@ impl Continuous<f64, f64> for Beta {
let bb = if ulps_eq!(self.shape_a, 1.0) && is_zero(x) {
0.0
} else if is_zero(x) {
-INF
f64::NEG_INFINITY
} else {
(self.shape_a - 1.0) * x.ln()
};
let cc = if ulps_eq!(self.shape_b, 1.0) && ulps_eq!(x, 1.0) {
0.0
} else if ulps_eq!(x, 1.0) {
-INF
f64::NEG_INFINITY
} else {
(self.shape_b - 1.0) * (1.0 - x).ln()
};
Expand All @@ -412,7 +411,7 @@ mod tests {

#[test]
fn test_create() {
let valid = [(1.0, 1.0), (9.0, 1.0), (5.0, 100.0), (1.0, INF), (INF, 1.0)];
let valid = [(1.0, 1.0), (9.0, 1.0), (5.0, 100.0), (1.0, f64::INFINITY), (f64::INFINITY, 1.0)];
for &arg in valid.iter() {
try_create(arg);
}
Expand All @@ -424,15 +423,15 @@ mod tests {
(0.0, 0.0),
(0.0, 0.1),
(1.0, 0.0),
(0.0, INF),
(INF, 0.0),
(0.0, f64::INFINITY),
(f64::INFINITY, 0.0),
(f64::NAN, 1.0),
(1.0, f64::NAN),
(f64::NAN, f64::NAN),
(1.0, -1.0),
(-1.0, 1.0),
(-1.0, -1.0),
(INF, INF),
(f64::INFINITY, f64::INFINITY),
];
for &arg in invalid.iter() {
bad_create_case(arg);
Expand All @@ -446,8 +445,8 @@ mod tests {
((1.0, 1.0), 0.5),
((9.0, 1.0), 0.9),
((5.0, 100.0), 0.047619047619047619047616),
((1.0, INF), 0.0),
((INF, 1.0), 1.0),
((1.0, f64::INFINITY), 0.0),
((f64::INFINITY, 1.0), 1.0),
];
for &(arg, res) in test.iter() {
test_case(arg, res, f);
Expand All @@ -461,8 +460,8 @@ mod tests {
((1.0, 1.0), 1.0 / 12.0),
((9.0, 1.0), 9.0 / 1100.0),
((5.0, 100.0), 500.0 / 1168650.0),
((1.0, INF), 0.0),
((INF, 1.0), 0.0),
((1.0, f64::INFINITY), 0.0),
((f64::INFINITY, 1.0), 0.0),
];
for &(arg, res) in test.iter() {
test_case(arg, res, f);
Expand All @@ -481,8 +480,8 @@ mod tests {
}
test_case_special((1.0, 1.0), 0.0, 1e-14, f);
let entropy = |x: Beta| x.entropy();
test_none((1.0, INF), entropy);
test_none((INF, 1.0), entropy);
test_none((1.0, f64::INFINITY), entropy);
test_none((f64::INFINITY, 1.0), entropy);
}

#[test]
Expand All @@ -491,16 +490,16 @@ mod tests {
test_case((1.0, 1.0), 0.0, skewness);
test_case((9.0, 1.0), -1.4740554623801777107177478829, skewness);
test_case((5.0, 100.0), 0.817594109275534303545831591, skewness);
test_case((1.0, INF), 2.0, skewness);
test_case((INF, 1.0), -2.0, skewness);
test_case((1.0, f64::INFINITY), 2.0, skewness);
test_case((f64::INFINITY, 1.0), -2.0, skewness);
}

#[test]
fn test_mode() {
let mode = |x: Beta| x.mode().unwrap();
test_case((5.0, 100.0), 0.038834951456310676243255386, mode);
test_case((92.0, INF), 0.0, mode);
test_case((INF, 2.0), 1.0, mode);
test_case((92.0, f64::INFINITY), 0.0, mode);
test_case((f64::INFINITY, 2.0), 1.0, mode);
}

#[test]
Expand Down Expand Up @@ -539,12 +538,12 @@ mod tests {
((5.0, 100.0), 0.5, 4.534102298350337661e-23),
((5.0, 100.0), 1.0, 0.0),
((5.0, 100.0), 1.0, 0.0),
((1.0, INF), 0.0, INF),
((1.0, INF), 0.5, 0.0),
((1.0, INF), 1.0, 0.0),
((INF, 1.0), 0.0, 0.0),
((INF, 1.0), 0.5, 0.0),
((INF, 1.0), 1.0, INF),
((1.0, f64::INFINITY), 0.0, f64::INFINITY),
((1.0, f64::INFINITY), 0.5, 0.0),
((1.0, f64::INFINITY), 1.0, 0.0),
((f64::INFINITY, 1.0), 0.0, 0.0),
((f64::INFINITY, 1.0), 0.5, 0.0),
((f64::INFINITY, 1.0), 1.0, f64::INFINITY),
];
for &(arg, x, expect) in test.iter() {
test_case(arg, expect, f(x));
Expand All @@ -570,18 +569,18 @@ mod tests {
((1.0, 1.0), 0.0, 0.0),
((1.0, 1.0), 0.5, 0.0),
((1.0, 1.0), 1.0, 0.0),
((9.0, 1.0), 0.0, -INF),
((9.0, 1.0), 0.0, f64::NEG_INFINITY),
((9.0, 1.0), 0.5, -3.347952867143343092547366497),
((9.0, 1.0), 1.0, 2.1972245773362193827904904738),
((5.0, 100.0), 0.0, -INF),
((5.0, 100.0), 0.0, f64::NEG_INFINITY),
((5.0, 100.0), 0.5, -51.447830024537682154565870),
((5.0, 100.0), 1.0, -INF),
((1.0, INF), 0.0, INF),
((1.0, INF), 0.5, -INF),
((1.0, INF), 1.0, -INF),
((INF, 1.0), 0.0, -INF),
((INF, 1.0), 0.5, -INF),
((INF, 1.0), 1.0, INF),
((5.0, 100.0), 1.0, f64::NEG_INFINITY),
((1.0, f64::INFINITY), 0.0, f64::INFINITY),
((1.0, f64::INFINITY), 0.5, f64::NEG_INFINITY),
((1.0, f64::INFINITY), 1.0, f64::NEG_INFINITY),
((f64::INFINITY, 1.0), 0.0, f64::NEG_INFINITY),
((f64::INFINITY, 1.0), 0.5, f64::NEG_INFINITY),
((f64::INFINITY, 1.0), 1.0, f64::INFINITY),
];
for &(arg, x, expect) in test.iter() {
test_case(arg, expect, f(x));
Expand All @@ -591,13 +590,13 @@ mod tests {
#[test]
fn test_ln_pdf_input_lt_0() {
let ln_pdf = |arg: f64| move |x: Beta| x.ln_pdf(arg);
test_case((1.0, 1.0), -INF, ln_pdf(-1.0));
test_case((1.0, 1.0), f64::NEG_INFINITY, ln_pdf(-1.0));
}

#[test]
fn test_ln_pdf_input_gt_1() {
let ln_pdf = |arg: f64| move |x: Beta| x.ln_pdf(arg);
test_case((1.0, 1.0), -INF, ln_pdf(2.0));
test_case((1.0, 1.0), f64::NEG_INFINITY, ln_pdf(2.0));
}

#[test]
Expand All @@ -613,12 +612,12 @@ mod tests {
((5.0, 100.0), 0.0, 0.0),
((5.0, 100.0), 0.5, 1.0),
((5.0, 100.0), 1.0, 1.0),
((1.0, INF), 0.0, 1.0),
((1.0, INF), 0.5, 1.0),
((1.0, INF), 1.0, 1.0),
((INF, 1.0), 0.0, 0.0),
((INF, 1.0), 0.5, 0.0),
((INF, 1.0), 1.0, 1.0),
((1.0, f64::INFINITY), 0.0, 1.0),
((1.0, f64::INFINITY), 0.5, 1.0),
((1.0, f64::INFINITY), 1.0, 1.0),
((f64::INFINITY, 1.0), 0.0, 0.0),
((f64::INFINITY, 1.0), 0.5, 0.0),
((f64::INFINITY, 1.0), 1.0, 1.0),
];
for &(arg, x, expect) in test.iter() {
test_case(arg, expect, cdf(x));
Expand All @@ -638,12 +637,12 @@ mod tests {
((5.0, 100.0), 0.0, 1.0),
((5.0, 100.0), 0.5, 0.0),
((5.0, 100.0), 1.0, 0.0),
((1.0, INF), 0.0, 0.0),
((1.0, INF), 0.5, 0.0),
((1.0, INF), 1.0, 0.0),
((INF, 1.0), 0.0, 1.0),
((INF, 1.0), 0.5, 1.0),
((INF, 1.0), 1.0, 0.0),
((1.0, f64::INFINITY), 0.0, 0.0),
((1.0, f64::INFINITY), 0.5, 0.0),
((1.0, f64::INFINITY), 1.0, 0.0),
((f64::INFINITY, 1.0), 0.0, 1.0),
((f64::INFINITY, 1.0), 0.5, 1.0),
((f64::INFINITY, 1.0), 1.0, 0.0),
];
for &(arg, x, expect) in test.iter() {
test_case(arg, expect, sf(x));
Expand Down
6 changes: 3 additions & 3 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Binomial {
/// assert!(result.is_err());
/// ```
pub fn new(p: f64, n: u64) -> Result<Binomial> {
if p.is_nan() || p < 0.0 || p > 1.0 {
if p.is_nan() || !(0.0..=1.0).contains(&p) {
Err(StatsError::BadParams)
} else {
Ok(Binomial { p, n })
Expand Down Expand Up @@ -279,7 +279,7 @@ impl Discrete<u64, f64> for Binomial {
0.0
}
} else {
(factorial::ln_binomial(self.n as u64, x as u64)
(factorial::ln_binomial(self.n, x)
+ x as f64 * self.p.ln()
+ (self.n - x) as f64 * (1.0 - self.p).ln())
.exp()
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Discrete<u64, f64> for Binomial {
f64::NEG_INFINITY
}
} else {
factorial::ln_binomial(self.n as u64, x as u64)
factorial::ln_binomial(self.n, x)
+ x as f64 * self.p.ln()
+ (self.n - x) as f64 * (1.0 - self.p).ln()
}
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Max<f64> for Cauchy {
/// # Formula
///
/// ```text
/// INF
/// f64::INFINITY
/// ```
fn max(&self) -> f64 {
f64::INFINITY
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/chi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Max<f64> for Chi {
/// # Formula
///
/// ```text
/// INF
/// f64::INFINITY
/// ```
fn max(&self) -> f64 {
f64::INFINITY
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Max<f64> for ChiSquared {
/// # Formula
///
/// ```text
/// INF
/// f64::INFINITY
/// ```
fn max(&self) -> f64 {
f64::INFINITY
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ impl ::rand::distributions::Distribution<f64> for Empirical {
/// Panics if number of samples is zero
impl Max<f64> for Empirical {
fn max(&self) -> f64 {
self.data.iter().rev().map(|(key, _)| key.0).next().unwrap()
self.data.keys().rev().map(|key| key.0) .next().unwrap()
}
}

/// Panics if number of samples is zero
impl Min<f64> for Empirical {
fn min(&self) -> f64 {
self.data.iter().map(|(key, _)| key.0).next().unwrap()
self.data.keys().map(|key| key.0).next().unwrap()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/distribution/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Max<f64> for Erlang {
/// # Formula
///
/// ```text
/// INF
/// f64::INFINITY
/// ```
fn max(&self) -> f64 {
self.g.max()
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Max<f64> for Exp {
/// # Formula
///
/// ```text
/// INF
/// f64::INFINITY
/// ```
fn max(&self) -> f64 {
f64::INFINITY
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/fisher_snedecor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl Max<f64> for FisherSnedecor {
/// # Formula
///
/// ```text
/// INF
/// f64::INFINITY
/// ```
fn max(&self) -> f64 {
f64::INFINITY
Expand Down
Loading
Loading