Skip to content

Commit

Permalink
add ks test for cauchy, log_normal, weibull
Browse files Browse the repository at this point in the history
  • Loading branch information
JamboChen committed Oct 8, 2024
1 parent 2244c8d commit d4f9226
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions rand_distr/tests/cdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit d4f9226

Please sign in to comment.