Skip to content

Commit

Permalink
More precision lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Dec 13, 2020
1 parent e47e825 commit 378cad7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions rand_distr/tests/value_stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use core::{fmt::Debug, cmp::PartialEq};
use core::fmt::{Debug, Display};
use core::cmp::PartialEq;
use num_traits::Float;
use rand::Rng;
use rand_distr::*;

Expand All @@ -26,6 +28,16 @@ fn test_samples<F: Debug + Copy + PartialEq, D: Distribution<F>>(
}
}

fn test_samples_approx<F: Float + Display + Copy + PartialEq, D: Distribution<F>>(
seed: u64, distr: D, thresh: F, expected: &[F],
) {
let mut rng = get_rng(seed);
for &val in expected {
let x = rng.sample(&distr);
assert!((x - val).abs() < thresh, "not approx eq: {}, {}", x, val);
}
}

#[test]
fn binominal_stability() {
// We have multiple code paths: np < 10, p > 0.5
Expand Down Expand Up @@ -95,7 +107,7 @@ fn pareto_stability() {
test_samples(213, Pareto::new(1.0, 1.0).unwrap(), &[
1.0423688f32, 2.1235929, 4.132709, 1.4679428,
]);
test_samples(213, Pareto::new(2.0, 0.5).unwrap(), &[
test_samples_approx(213, Pareto::new(2.0, 0.5).unwrap(), 1e-14, &[
9.019295276219136f64,
4.3097126018270595,
6.837815045397157,
Expand Down Expand Up @@ -184,7 +196,7 @@ fn gamma_stability() {
0.5013580219361969,
0.1457735613733489,
]);
test_samples(223, ChiSquared::new(0.1).unwrap(), &[
test_samples_approx(223, ChiSquared::new(0.1).unwrap(), 1e-15, &[
0.014824404726978617f64,
0.021602123937134326,
0.0000003431429746851693,
Expand Down Expand Up @@ -285,7 +297,7 @@ fn normal_stability() {
test_samples(213, LogNormal::new(0.0, 1.0).unwrap(), &[
0.88830346f32, 2.1844804, 1.0678421, 0.30322206,
]);
test_samples(213, LogNormal::new(2.0, 0.5).unwrap(), &[
test_samples_approx(213, LogNormal::new(2.0, 0.5).unwrap(), 1e-14, &[
6.964174338639032f64,
10.921015733601452,
7.6355881556915906,
Expand Down

0 comments on commit 378cad7

Please sign in to comment.