rust-average/tests/streaming_stats.rs

36 lines
1.2 KiB
Rust
Raw Normal View History

2019-07-08 17:32:18 +02:00
#![cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp, map_clone))]
2017-05-30 10:54:14 +02:00
2019-07-08 16:17:04 +02:00
use average::assert_almost_eq;
2017-05-19 15:54:13 +02:00
/// Create a random vector by sampling from a normal distribution.
fn initialize_vec(size: usize) -> Vec<f64> {
2019-07-08 16:01:13 +02:00
use rand_distr::{Normal, Distribution};
use rand_xoshiro::Xoshiro256StarStar;
2019-01-18 15:55:37 +01:00
use rand::SeedableRng;
2019-07-08 16:01:13 +02:00
let normal = Normal::new(2.0, 3.0).unwrap();
2017-05-19 15:54:13 +02:00
let mut values = Vec::with_capacity(size);
2019-07-08 16:01:13 +02:00
let mut rng = Xoshiro256StarStar::seed_from_u64(42);
2017-05-19 15:54:13 +02:00
for _ in 0..size {
2018-07-06 10:31:26 +02:00
values.push(normal.sample(&mut rng));
2017-05-19 15:54:13 +02:00
}
values
}
#[test]
fn average_vs_streaming_stats_small() {
let values = initialize_vec(100);
let a: average::MeanWithError = values.iter().collect();
2017-05-19 15:54:13 +02:00
let b: stats::OnlineStats = values.iter().map(|x| *x).collect();
assert_almost_eq!(a.mean(), b.mean(), 1e-16);
assert_almost_eq!(a.population_variance(), b.variance(), 1e-14);
2017-05-19 15:54:13 +02:00
}
#[test]
fn average_vs_streaming_stats_large() {
let values = initialize_vec(1_000_000);
let a: average::MeanWithError = values.iter().collect();
2017-05-19 15:54:13 +02:00
let b: stats::OnlineStats = values.iter().map(|x| *x).collect();
assert_almost_eq!(a.mean(), b.mean(), 1e-16);
2018-07-06 10:31:26 +02:00
assert_almost_eq!(a.population_variance(), b.variance(), 1e-12);
2017-05-19 15:54:13 +02:00
}