2017-05-30 10:54:14 +02:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
|
|
|
|
|
2017-05-19 15:54:13 +02:00
|
|
|
#[macro_use] extern crate average;
|
|
|
|
|
|
|
|
extern crate rand;
|
|
|
|
extern crate stats;
|
|
|
|
|
|
|
|
/// Create a random vector by sampling from a normal distribution.
|
|
|
|
fn initialize_vec(size: usize) -> Vec<f64> {
|
2018-07-06 10:31:26 +02:00
|
|
|
use rand::distributions::{Normal, Distribution};
|
2017-05-19 15:54:13 +02:00
|
|
|
use rand::{XorShiftRng, SeedableRng};
|
|
|
|
let normal = Normal::new(2.0, 3.0);
|
|
|
|
let mut values = Vec::with_capacity(size);
|
2018-07-06 10:31:26 +02:00
|
|
|
let mut rng = XorShiftRng::from_seed(
|
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
|
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);
|
2018-02-27 01:38:45 +01:00
|
|
|
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);
|
2017-05-28 20:30:05 +02:00
|
|
|
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);
|
2018-02-27 01:38:45 +01:00
|
|
|
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
|
|
|
}
|