Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions rand_distr/src/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,22 +339,4 @@ mod test {
fn test_binomial_invalid_lambda_neg() {
Binomial::new(20, -10.0).unwrap();
}

#[test]
fn value_stability() {
fn test_samples(n: u64, p: f64, expected: &[u64]) {
let distr = Binomial::new(n, p).unwrap();
let mut rng = crate::test::rng(353);
let mut buf = [0; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

// We have multiple code paths: np < 10, p > 0.5
test_samples(2, 0.7, &[1, 1, 2, 1]);
test_samples(20, 0.3, &[7, 7, 5, 7]);
test_samples(2000, 0.6, &[1194, 1208, 1192, 1210]);
}
}
2 changes: 2 additions & 0 deletions rand_distr/src/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ mod test {

#[test]
fn value_stability() {


fn gen_samples<F: Float + FloatConst + core::fmt::Debug>(m: F, s: F, buf: &mut [F])
Comment thread
newpavlov marked this conversation as resolved.
where Standard: Distribution<F> {
let distr = Cauchy::new(m, s).unwrap();
Expand Down
16 changes: 0 additions & 16 deletions rand_distr/src/dirichlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,4 @@ mod test {
fn test_dirichlet_invalid_alpha() {
Dirichlet::new_with_size(0.0f64, 2).unwrap();
}

#[test]
fn value_stability() {
let mut rng = crate::test::rng(223);
assert_eq!(
rng.sample(Dirichlet::new(vec![1.0, 2.0, 3.0]).unwrap()),
vec![0.12941567177708177, 0.4702121891675036, 0.4003721390554146]
);
assert_eq!(rng.sample(Dirichlet::new_with_size(8.0, 5).unwrap()), vec![
0.17684200044809556,
0.29915953935953055,
0.1832858056608014,
0.1425623503573967,
0.19815030417417595
]);
}
}
32 changes: 0 additions & 32 deletions rand_distr/src/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,36 +162,4 @@ mod test {
fn test_exp_invalid_lambda_neg() {
Exp::new(-10.0).unwrap();
}

#[test]
fn value_stability() {
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
) {
let mut rng = crate::test::rng(223);
let mut buf = [zero; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

test_samples(Exp1, 0f32, &[1.079617, 1.8325565, 0.04601166, 0.34471703]);
test_samples(Exp1, 0f64, &[
1.0796170642388276,
1.8325565304274,
0.04601166186842716,
0.3447170217100157,
]);

test_samples(Exp::new(2.0).unwrap(), 0f32, &[
0.5398085, 0.91627824, 0.02300583, 0.17235851,
]);
test_samples(Exp::new(1.0).unwrap(), 0f64, &[
1.0796170642388276,
1.8325565304274,
0.04601166186842716,
0.3447170217100157,
]);
}
}
87 changes: 0 additions & 87 deletions rand_distr/src/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,91 +636,4 @@ mod test {
fn test_beta_invalid_dof() {
Beta::new(0., 0.).unwrap();
}

#[test]
fn value_stability() {
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
) {
let mut rng = crate::test::rng(223);
let mut buf = [zero; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

// Gamma has 3 cases: shape == 1, shape < 1, shape > 1
test_samples(Gamma::new(1.0, 5.0).unwrap(), 0f32, &[
5.398085, 9.162783, 0.2300583, 1.7235851,
]);
test_samples(Gamma::new(0.8, 5.0).unwrap(), 0f32, &[
0.5051203, 0.9048302, 3.095812, 1.8566116,
]);
test_samples(Gamma::new(1.1, 5.0).unwrap(), 0f64, &[
7.783878094584059,
1.4939528171618057,
8.638017638857592,
3.0949337228829004,
]);

// ChiSquared has 2 cases: k == 1, k != 1
test_samples(ChiSquared::new(1.0).unwrap(), 0f64, &[
0.4893526200348249,
1.635249736808788,
0.5013580219361969,
0.1457735613733489,
]);
test_samples(ChiSquared::new(0.1).unwrap(), 0f64, &[
0.014824404726978617,
0.021602123937134326,
0.0000003431429746851693,
0.00000002291755769542258,
]);
test_samples(ChiSquared::new(10.0).unwrap(), 0f32, &[
12.693656, 6.812016, 11.082001, 12.436167,
]);

// FisherF has same special cases as ChiSquared on each param
test_samples(FisherF::new(1.0, 13.5).unwrap(), 0f32, &[
0.32283646,
0.048049655,
0.0788893,
1.817178,
]);
test_samples(FisherF::new(1.0, 1.0).unwrap(), 0f32, &[
0.29925257, 3.4392934, 9.567652, 0.020074,
]);
test_samples(FisherF::new(0.7, 13.5).unwrap(), 0f64, &[
3.3196593155045124,
0.3409169916262829,
0.03377989856426519,
0.00004041672861036937,
]);

// StudentT has same special cases as ChiSquared
test_samples(StudentT::new(1.0).unwrap(), 0f32, &[
0.54703987,
-1.8545331,
3.093162,
-0.14168274,
]);
test_samples(StudentT::new(1.1).unwrap(), 0f64, &[
0.7729195887949754,
1.2606210611616204,
-1.7553606501113175,
-2.377641221169782,
]);

// Beta has same special cases as Gamma on each param
test_samples(Beta::new(1.0, 0.8).unwrap(), 0f32, &[
0.6444564, 0.357635, 0.4110078, 0.7347192,
]);
test_samples(Beta::new(0.7, 1.2).unwrap(), 0f64, &[
0.6433129944095513,
0.5373371199711573,
0.10313293199269491,
0.002472280249144378,
]);
}
}
24 changes: 0 additions & 24 deletions rand_distr/src/inverse_gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,4 @@ mod tests {
assert!(InverseGaussian::new(1.0, -1.0).is_err());
assert!(InverseGaussian::new(1.0, 1.0).is_ok());
}

#[test]
fn value_stability() {
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
) {
let mut rng = crate::test::rng(213);
let mut buf = [zero; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

test_samples(InverseGaussian::new(1.0, 3.0).unwrap(), 0f32, &[
0.9339157, 1.108113, 0.50864697, 0.39849377,
]);
test_samples(InverseGaussian::new(1.0, 3.0).unwrap(), 0f64, &[
1.0707604954722476,
0.9628140605340697,
0.4069687656468226,
0.660283852985818,
]);
}
}
50 changes: 0 additions & 50 deletions rand_distr/src/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,54 +239,4 @@ mod tests {
fn test_log_normal_invalid_sd() {
LogNormal::new(10.0, -1.0).unwrap();
}

#[test]
fn value_stability() {
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
) {
let mut rng = crate::test::rng(213);
let mut buf = [zero; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

test_samples(StandardNormal, 0f32, &[
-0.11844189,
0.781378,
0.06563994,
-1.1932899,
]);
test_samples(StandardNormal, 0f64, &[
-0.11844188827977231,
0.7813779637772346,
0.06563993969580051,
-1.1932899004186373,
]);

test_samples(Normal::new(0.0, 1.0).unwrap(), 0f32, &[
-0.11844189,
0.781378,
0.06563994,
-1.1932899,
]);
test_samples(Normal::new(2.0, 0.5).unwrap(), 0f64, &[
1.940779055860114,
2.3906889818886174,
2.0328199698479,
1.4033550497906813,
]);

test_samples(LogNormal::new(0.0, 1.0).unwrap(), 0f32, &[
0.88830346, 2.1844804, 1.0678421, 0.30322206,
]);
test_samples(LogNormal::new(2.0, 0.5).unwrap(), 0f64, &[
6.964174338639032,
10.921015733601452,
7.6355881556915906,
4.068828213584092,
]);
}
}
25 changes: 0 additions & 25 deletions rand_distr/src/normal_inverse_gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,4 @@ mod tests {
assert!(NormalInverseGaussian::new(1.0, 2.0).is_err());
assert!(NormalInverseGaussian::new(2.0, 1.0).is_ok());
}


#[test]
fn value_stability() {
fn test_samples<F: Float + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
) {
let mut rng = crate::test::rng(213);
let mut buf = [zero; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

test_samples(NormalInverseGaussian::new(2.0, 1.0).unwrap(), 0f32, &[
0.6568966, 1.3744819, 2.216063, 0.11488572,
]);
test_samples(NormalInverseGaussian::new(2.0, 1.0).unwrap(), 0f64, &[
0.6838707059642927,
2.4447306460569784,
0.2361045023235968,
1.7774534624785319,
]);
}
}
12 changes: 0 additions & 12 deletions rand_distr/src/pert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,4 @@ mod test {
assert!(Pert::new(min, max, mode).is_err());
}
}

#[test]
fn value_stability() {
let rng = crate::test::rng(860);
let distr = Pert::new(2., 10., 3.).unwrap(); // mean = 4, var = 12/7
let mut seq = distr.sample_iter(rng);
assert_eq!(seq.next(), Some(4.631484136029422f64));
assert_eq!(seq.next(), Some(3.307201472321789f64));
assert_eq!(seq.next(), Some(3.29995019556348f64));
assert_eq!(seq.next(), Some(3.66835483991721f64));
assert_eq!(seq.next(), Some(3.514246139933899f64));
}
}
19 changes: 0 additions & 19 deletions rand_distr/src/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,4 @@ mod test {
fn test_poisson_invalid_lambda_neg() {
Poisson::new(-10.0).unwrap();
}

#[test]
fn value_stability() {
fn test_samples<F: Float + FloatConst + core::fmt::Debug, D: Distribution<F>>(
distr: D, zero: F, expected: &[F],
) {
let mut rng = crate::test::rng(223);
let mut buf = [zero; 4];
for x in &mut buf {
*x = rng.sample(&distr);
}
assert_eq!(buf, expected);
}

// Special cases: < 12, >= 12
test_samples(Poisson::new(7.0).unwrap(), 0f32, &[5.0, 11.0, 6.0, 5.0]);
test_samples(Poisson::new(7.0).unwrap(), 0f64, &[9.0, 5.0, 7.0, 6.0]);
test_samples(Poisson::new(27.0).unwrap(), 0f32, &[28.0, 32.0, 36.0, 36.0]);
}
}
13 changes: 0 additions & 13 deletions rand_distr/src/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,4 @@ mod test {
assert!(Triangular::new(min, max, mode).is_err());
}
}

#[test]
fn value_stability() {
let rng = crate::test::rng(860);
let distr = Triangular::new(2., 10., 3.).unwrap();
let mut seq = distr.sample_iter(rng);

assert_eq!(seq.next(), Some(5.74373257511361f64));
assert_eq!(seq.next(), Some(7.890059162791258f64));
assert_eq!(seq.next(), Some(4.7256280652553455f64));
assert_eq!(seq.next(), Some(2.9474808121184077f64));
assert_eq!(seq.next(), Some(3.058301946314053f64));
}
}
22 changes: 0 additions & 22 deletions rand_distr/src/unit_ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,3 @@ impl<F: Float + SampleUniform> Distribution<[F; 3]> for UnitBall {
[x1, x2, x3]
}
}

#[cfg(test)]
mod tests {
use super::UnitBall;
use crate::Distribution;

#[test]
fn value_stability() {
let mut rng = crate::test::rng(2);
let expected = [
[0.018035709265959987, -0.4348771383120438, -0.07982762085055706],
[0.10588569388223945, -0.4734350111375454, -0.7392104908825501],
[0.11060237642041049, -0.16065642822852677, -0.8444043930440075]
];
let samples: [[f64; 3]; 3] = [
UnitBall.sample(&mut rng),
UnitBall.sample(&mut rng),
UnitBall.sample(&mut rng),
];
assert_eq!(samples, expected);
}
}
Loading