66// option. This file may not be copied, modified, or distributed
77// except according to those terms.
88
9- use core:: { fmt:: Debug , cmp:: PartialEq } ;
9+ use core:: fmt:: { Debug , Display } ;
10+ use core:: cmp:: PartialEq ;
11+ use num_traits:: Float ;
1012use rand:: Rng ;
1113use rand_distr:: * ;
1214
@@ -26,6 +28,16 @@ fn test_samples<F: Debug + Copy + PartialEq, D: Distribution<F>>(
2628 }
2729}
2830
31+ fn test_samples_approx < F : Float + Display + Copy + PartialEq , D : Distribution < F > > (
32+ seed : u64 , distr : D , thresh : F , expected : & [ F ] ,
33+ ) {
34+ let mut rng = get_rng ( seed) ;
35+ for & val in expected {
36+ let x = rng. sample ( & distr) ;
37+ assert ! ( ( x - val) . abs( ) < thresh, "not approx eq: {}, {}" , x, val) ;
38+ }
39+ }
40+
2941#[ test]
3042fn binominal_stability ( ) {
3143 // We have multiple code paths: np < 10, p > 0.5
@@ -95,7 +107,7 @@ fn pareto_stability() {
95107 test_samples ( 213 , Pareto :: new ( 1.0 , 1.0 ) . unwrap ( ) , & [
96108 1.0423688f32 , 2.1235929 , 4.132709 , 1.4679428 ,
97109 ] ) ;
98- test_samples ( 213 , Pareto :: new ( 2.0 , 0.5 ) . unwrap ( ) , & [
110+ test_samples_approx ( 213 , Pareto :: new ( 2.0 , 0.5 ) . unwrap ( ) , 1e-14 , & [
99111 9.019295276219136f64 ,
100112 4.3097126018270595 ,
101113 6.837815045397157 ,
@@ -184,7 +196,7 @@ fn gamma_stability() {
184196 0.5013580219361969 ,
185197 0.1457735613733489 ,
186198 ] ) ;
187- test_samples ( 223 , ChiSquared :: new ( 0.1 ) . unwrap ( ) , & [
199+ test_samples_approx ( 223 , ChiSquared :: new ( 0.1 ) . unwrap ( ) , 1e-15 , & [
188200 0.014824404726978617f64 ,
189201 0.021602123937134326 ,
190202 0.0000003431429746851693 ,
@@ -285,7 +297,7 @@ fn normal_stability() {
285297 test_samples ( 213 , LogNormal :: new ( 0.0 , 1.0 ) . unwrap ( ) , & [
286298 0.88830346f32 , 2.1844804 , 1.0678421 , 0.30322206 ,
287299 ] ) ;
288- test_samples ( 213 , LogNormal :: new ( 2.0 , 0.5 ) . unwrap ( ) , & [
300+ test_samples_approx ( 213 , LogNormal :: new ( 2.0 , 0.5 ) . unwrap ( ) , 1e-14 , & [
289301 6.964174338639032f64 ,
290302 10.921015733601452 ,
291303 7.6355881556915906 ,
@@ -339,6 +351,6 @@ fn cauchy_stability() {
339351 let expected = [ 15.023088 , -5.446413 , 3.7092876 , 3.112482 ] ;
340352 for & a in expected. iter ( ) {
341353 let b = rng. sample ( & distr) ;
342- assert ! ( ( a - b) . abs( ) < 1e-6 , "expected: {} = {}" , a, b) ;
354+ assert ! ( ( a - b) . abs( ) < 1e-5 , "expected: {} = {}" , a, b) ;
343355 }
344356}
0 commit comments