diff --git a/rand_distr/tests/cdf.rs b/rand_distr/tests/cdf.rs index b55914ef76..19d1b3caaf 100644 --- a/rand_distr/tests/cdf.rs +++ b/rand_distr/tests/cdf.rs @@ -158,6 +158,27 @@ fn normal() { } } +#[test] +fn cauchy() { + fn cdf(x: f64, median: f64, scale: f64) -> f64 { + (1.0 / f64::consts::PI) * ((x - median) / scale).atan() + 0.5 + } + + let parameters = [ + (0.0, 1.0), + (0.0, 0.1), + (1.0, 10.0), + (1.0, 100.0), + (-1.0, 0.00001), + (-1.0, 0.0000001), + ]; + + for (seed, (median, scale)) in parameters.into_iter().enumerate() { + let dist = rand_distr::Cauchy::new(median, scale).unwrap(); + test_continuous(seed as u64, dist, |x| cdf(x, median, scale)); + } +} + #[test] fn uniform() { fn cdf(x: f64, a: f64, b: f64) -> f64 { @@ -178,6 +199,33 @@ fn uniform() { } } +#[test] +fn log_normal() { + fn cdf(x: f64, mean: f64, std_dev: f64) -> f64 { + if x <= 0.0 { + 0.0 + } else if x.is_infinite() { + 1.0 + } else { + 0.5 * (mean - x.ln() / (std_dev * f64::consts::SQRT_2)).erfc() + } + } + + let parameters = [ + (0.0, 1.0), + (0.0, 0.1), + (1.0, 10.0), + (1.0, 100.0), + (-1.0, 0.00001), + (-1.0, 0.0000001), + ]; + + for (seed, (mean, std_dev)) in parameters.into_iter().enumerate() { + let dist = rand_distr::LogNormal::new(mean, std_dev).unwrap(); + test_continuous(seed as u64, dist, |x| cdf(x, mean, std_dev)); + } +} + #[test] fn exp() { fn cdf(x: f64, lambda: f64) -> f64 { @@ -192,6 +240,31 @@ fn exp() { } } +#[test] +fn weibull() { + fn cdf(x: f64, lambda: f64, k: f64) -> f64 { + if x < 0.0 { + return 0.0; + } + + 1.0 - (-(x / lambda).powf(k)).exp() + } + + let parameters = [ + (0.5, 1.0), + (1.0, 1.0), + (10.0, 0.1), + (0.1, 10.0), + (15.0, 20.0), + (1000.0, 1.0), + ]; + + for (seed, (lambda, k)) in parameters.into_iter().enumerate() { + let dist = rand_distr::Weibull::new(lambda, k).unwrap(); + test_continuous(seed as u64, dist, |x| cdf(x, lambda, k)); + } +} + #[test] fn gamma() { fn cdf(x: f64, shape: f64, scale: f64) -> f64 {