@@ -417,8 +417,7 @@ where StandardNormal: Distribution<N>, Exp1: Distribution<N>, Open01: Distributi
417
417
418
418
#[ cfg( test) ]
419
419
mod test {
420
- use crate :: Distribution ;
421
- use super :: { Beta , ChiSquared , StudentT , FisherF } ;
420
+ use super :: * ;
422
421
423
422
#[ test]
424
423
fn test_chi_squared_one ( ) {
@@ -482,4 +481,60 @@ mod test {
482
481
fn test_beta_invalid_dof ( ) {
483
482
Beta :: new ( 0. , 0. ) . unwrap ( ) ;
484
483
}
484
+
485
+ #[ test]
486
+ fn value_stability ( ) {
487
+ fn test_samples < N : Float + core:: fmt:: Debug , D : Distribution < N > >
488
+ ( distr : D , zero : N , expected : & [ N ] )
489
+ {
490
+ let mut rng = crate :: test:: rng ( 223 ) ;
491
+ let mut buf = [ zero; 4 ] ;
492
+ for x in & mut buf {
493
+ * x = rng. sample ( & distr) ;
494
+ }
495
+ assert_eq ! ( buf, expected) ;
496
+ }
497
+
498
+ // Gamma has 3 cases: shape == 1, shape < 1, shape > 1
499
+ test_samples ( Gamma :: new ( 1.0 , 5.0 ) . unwrap ( ) , 0f32 ,
500
+ & [ 5.398085 , 9.162783 , 0.2300583 , 1.7235851 ] ) ;
501
+ test_samples ( Gamma :: new ( 0.8 , 5.0 ) . unwrap ( ) , 0f32 ,
502
+ & [ 0.5051203 , 0.9048302 , 3.095812 , 1.8566116 ] ) ;
503
+ test_samples ( Gamma :: new ( 1.1 , 5.0 ) . unwrap ( ) , 0f64 , & [
504
+ 7.783878094584059 , 1.4939528171618057 ,
505
+ 8.638017638857592 , 3.0949337228829004 ] ) ;
506
+
507
+ // ChiSquared has 2 cases: k == 1, k != 1
508
+ test_samples ( ChiSquared :: new ( 1.0 ) . unwrap ( ) , 0f64 , & [
509
+ 0.4893526200348249 , 1.635249736808788 ,
510
+ 0.5013580219361969 , 0.1457735613733489 ] ) ;
511
+ test_samples ( ChiSquared :: new ( 0.1 ) . unwrap ( ) , 0f64 , & [
512
+ 0.014824404726978617 , 0.021602123937134326 ,
513
+ 0.0000003431429746851693 , 0.00000002291755769542258 ] ) ;
514
+ test_samples ( ChiSquared :: new ( 10.0 ) . unwrap ( ) , 0f32 ,
515
+ & [ 12.693656 , 6.812016 , 11.082001 , 12.436167 ] ) ;
516
+
517
+ // FisherF has same special cases as ChiSquared on each param
518
+ test_samples ( FisherF :: new ( 1.0 , 13.5 ) . unwrap ( ) , 0f32 ,
519
+ & [ 0.32283646 , 0.048049655 , 0.0788893 , 1.817178 ] ) ;
520
+ test_samples ( FisherF :: new ( 1.0 , 1.0 ) . unwrap ( ) , 0f32 ,
521
+ & [ 0.29925257 , 3.4392934 , 9.567652 , 0.020074 ] ) ;
522
+ test_samples ( FisherF :: new ( 0.7 , 13.5 ) . unwrap ( ) , 0f64 , & [
523
+ 3.3196593155045124 , 0.3409169916262829 ,
524
+ 0.03377989856426519 , 0.00004041672861036937 ] ) ;
525
+
526
+ // StudentT has same special cases as ChiSquared
527
+ test_samples ( StudentT :: new ( 1.0 ) . unwrap ( ) , 0f32 ,
528
+ & [ 0.54703987 , -1.8545331 , 3.093162 , -0.14168274 ] ) ;
529
+ test_samples ( StudentT :: new ( 1.1 ) . unwrap ( ) , 0f64 , & [
530
+ 0.7729195887949754 , 1.2606210611616204 ,
531
+ -1.7553606501113175 , -2.377641221169782 ] ) ;
532
+
533
+ // Beta has same special cases as Gamma on each param
534
+ test_samples ( Beta :: new ( 1.0 , 0.8 ) . unwrap ( ) , 0f32 ,
535
+ & [ 0.6444564 , 0.357635 , 0.4110078 , 0.7347192 ] ) ;
536
+ test_samples ( Beta :: new ( 0.7 , 1.2 ) . unwrap ( ) , 0f64 , & [
537
+ 0.6433129944095513 , 0.5373371199711573 ,
538
+ 0.10313293199269491 , 0.002472280249144378 ] ) ;
539
+ }
485
540
}
0 commit comments