Skip to content
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
2 changes: 1 addition & 1 deletion src/distribution/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ where
mod tests {
use super::*;

use std::fmt::{Debug, Display};
use core::fmt::{Debug, Display};

use nalgebra::{dmatrix, dvector, vector, DimMin, OVector};

Expand Down
25 changes: 9 additions & 16 deletions src/distribution/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn integral_bisection_search<K: Num + Clone, T: Num + PartialOrd>(
#[cfg(test)]
pub mod test {
use crate::distribution::{Continuous, ContinuousCDF, Discrete, DiscreteCDF};
use crate::prec;

#[macro_export]
macro_rules! testing_boiler {
Expand Down Expand Up @@ -377,7 +378,7 @@ pub mod test {
sum += (prev_density + density) * step / 2.0;

let cdf = dist.cdf(x);
if (sum - cdf).abs() > 1e-3 {
if !prec::almost_eq(sum, cdf, 1e-3) {
panic!(
"Integral of pdf doesn't equal cdf!\n\
Integration from {x_min} by {step} to {x} = {sum}\n\
Expand All @@ -393,8 +394,10 @@ pub mod test {
}
}

assert!(sum > 0.99);
assert!(sum <= 1.001);
assert!(
sum > 0.99 && sum <= 1.001,
"sum should be close to 1, but is {sum}"
);
}

/// cdf should be the sum of the pmf
Expand Down Expand Up @@ -457,9 +460,7 @@ pub mod test {
/// Does a series of checks that all continuous distributions must obey.
/// 99% of the probability mass should be between x_min and x_max or the finite
/// difference of cdf should be near to the pdf for much of the support.
pub fn check_continuous_distribution<
D: ContinuousCDF<f64, f64> + Continuous<f64, f64> + std::panic::RefUnwindSafe,
>(
pub fn check_continuous_distribution<D: ContinuousCDF<f64, f64> + Continuous<f64, f64>>(
dist: &D,
x_min: f64,
x_max: f64,
Expand All @@ -471,16 +472,8 @@ pub mod test {
assert_eq!(dist.cdf(f64::NEG_INFINITY), 0.0);
assert_eq!(dist.cdf(f64::INFINITY), 1.0);

if std::panic::catch_unwind(|| {
check_integrate_pdf_is_cdf(dist, x_min, x_max, (x_max - x_min) / 100000.0);
})
.or(std::panic::catch_unwind(|| {
check_derivative_of_cdf_is_pdf(dist, x_min, x_max, (x_max - x_min) / 100000.0);
}))
.is_err()
{
panic!("Integration of pdf doesn't equal cdf and derivative of cdf doesn't equal pdf!");
}
check_integrate_pdf_is_cdf(dist, x_min, x_max, (x_max - x_min) / 100000.0);
check_derivative_of_cdf_is_pdf(dist, x_min, x_max, (x_max - x_min) / 100000.0);
}

/// Does a series of checks that all positive discrete distributions must
Expand Down
9 changes: 5 additions & 4 deletions src/distribution/levy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ pub enum LevyError {
ScaleInvalid,
}

impl std::fmt::Display for LevyError {
impl core::fmt::Display for LevyError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
LevyError::LocationInvalid => write!(f, "location is NaN or infinite"),
LevyError::ScaleInvalid => write!(f, "scale is NaN, infinite or nonpositive"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for LevyError {}

impl Levy {
Expand Down Expand Up @@ -100,8 +101,8 @@ impl Levy {
}
}

impl std::fmt::Display for Levy {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
impl core::fmt::Display for Levy {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "Levy(mu = {}, c = {})", self.mu, self.c)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/negative_binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ mod tests {
}

#[test]
#[cfg(feature = "rand")]
#[cfg(all(feature = "rand", feature = "std"))]
fn test_sample() {
use crate::prec;
use rand::{distributions::Distribution, SeedableRng, rngs::StdRng};
Expand Down
5 changes: 3 additions & 2 deletions src/stats_tests/chisquare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub enum ChiSquareTestError {
DdofInvalid,
}

impl std::fmt::Display for ChiSquareTestError {
impl core::fmt::Display for ChiSquareTestError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
ChiSquareTestError::FObsInvalid => {
write!(f, "`f_obs` must have a length greater than 1")
Expand All @@ -32,6 +32,7 @@ impl std::fmt::Display for ChiSquareTestError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for ChiSquareTestError {}

/// Perform a Pearson's chi-square test
Expand Down
4 changes: 2 additions & 2 deletions src/stats_tests/f_oneway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub enum FOneWayTestError {
SampleContainsNaN,
}

impl std::fmt::Display for FOneWayTestError {
impl core::fmt::Display for FOneWayTestError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
FOneWayTestError::NotEnoughSamples => write!(f, "must be at least two samples"),
FOneWayTestError::SampleTooSmall => {
Expand Down
4 changes: 2 additions & 2 deletions src/stats_tests/ks_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub enum KSTestError {
ExactAndTooLarge,
}

impl std::fmt::Display for KSTestError {
impl core::fmt::Display for KSTestError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
KSTestError::SampleTooSmall => write!(f, "sample must be len > 1"),
KSTestError::SampleContainsNaN => {
Expand Down
4 changes: 2 additions & 2 deletions src/stats_tests/mannwhitneyu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub enum MannWhitneyUError {
ExactMethodWithTiesInData,
}

impl std::fmt::Display for MannWhitneyUError {
impl core::fmt::Display for MannWhitneyUError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
MannWhitneyUError::UncomparableData => {
write!(f, "elements in the data are not comparable")
Expand Down
6 changes: 6 additions & 0 deletions src/stats_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#[cfg(feature = "std")]
pub mod chisquare;
#[cfg(feature = "std")]
pub mod f_oneway;
pub mod fisher;
#[cfg(feature = "std")]
pub mod ks_test;
#[cfg(feature = "std")]
pub mod mannwhitneyu;
#[cfg(feature = "std")]
pub mod skewtest;
#[cfg(feature = "std")]
pub mod ttest_onesample;

/// Specifies an [alternative hypothesis](https://en.wikipedia.org/wiki/Alternative_hypothesis)
Expand Down
5 changes: 3 additions & 2 deletions src/stats_tests/skewtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub enum SkewTestError {
SampleContainsNaN,
}

impl std::fmt::Display for SkewTestError {
impl core::fmt::Display for SkewTestError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
SkewTestError::SampleTooSmall => {
write!(f, "sample must contain at least 8 observations")
Expand All @@ -31,6 +31,7 @@ impl std::fmt::Display for SkewTestError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for SkewTestError {}

fn calc_root_b1(data: &[f64]) -> f64 {
Expand Down
5 changes: 3 additions & 2 deletions src/stats_tests/ttest_onesample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub enum TTestOneSampleError {
SampleContainsNaN,
}

impl std::fmt::Display for TTestOneSampleError {
impl core::fmt::Display for TTestOneSampleError {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
TTestOneSampleError::SampleTooSmall => write!(f, "sample must be len > 1"),
TTestOneSampleError::SampleContainsNaN => {
Expand All @@ -29,6 +29,7 @@ impl std::fmt::Display for TTestOneSampleError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for TTestOneSampleError {}

/// Perform a one sample t-test
Expand Down
2 changes: 1 addition & 1 deletion tests/nist_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct TestCase {
values: Vec<f64>,
}

impl std::fmt::Debug for TestCase {
impl core::fmt::Debug for TestCase {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "TestCase({:?}, [...]", self.certified)
}
Expand Down
Loading