Skip to content

Commit

Permalink
Fix test_sample_counts_match_probabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed May 8, 2024
1 parent 161134a commit 62c9560
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions rand_distr/src/weighted_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ mod test {
}

#[test]
#[allow(clippy::needless_range_loop)]
fn test_sample_counts_match_probabilities() {
let start = 1;
let end = 3;
Expand All @@ -371,22 +372,24 @@ mod test {
let mut tree = WeightedTreeIndex::new(weights).unwrap();
let mut total_weight = 0.0;
let mut weights = alloc::vec![0.0; end];
for (i, w) in weights.iter_mut().enumerate() {
for i in 0..end {
tree.update(i, i as f64).unwrap();
*w = i as f64;
weights[i] = i as f64;
total_weight += i as f64;
}
for (i, w) in weights.iter_mut().enumerate() {
for i in 0..start {
tree.update(i, 0.0).unwrap();
*w = 0.0;
weights[i] = 0.0;
total_weight -= i as f64;
}
let mut counts = alloc::vec![0_usize; end];
for _ in 0..samples {
let i = tree.sample(&mut rng);
counts[i] += 1;
}
counts.iter().for_each(|&c| assert_eq!(c, 0));
for i in 0..start {
assert_eq!(counts[i], 0);
}
for i in start..end {
let diff = counts[i] as f64 / samples as f64 - weights[i] / total_weight;
assert!(diff.abs() < 0.05);
Expand Down

0 comments on commit 62c9560

Please sign in to comment.