Add test comparing to streaming-stats
This commit is contained in:
parent
a5aa7df51d
commit
53b44d1a13
@ -6,6 +6,7 @@ extern crate stats;
|
||||
|
||||
use bencher::Bencher;
|
||||
|
||||
/// Create a random vector by sampling from a normal distribution.
|
||||
fn initialize_vec() -> Vec<f64> {
|
||||
use rand::distributions::{Normal, IndependentSample};
|
||||
use rand::{XorShiftRng, SeedableRng};
|
||||
|
@ -140,13 +140,15 @@ impl core::iter::FromIterator<f64> for Average {
|
||||
///
|
||||
/// On panic, this macro will print the values of the expressions with their
|
||||
/// debug representations.
|
||||
#[macro_export]
|
||||
macro_rules! assert_almost_eq {
|
||||
($a:expr, $b:expr, $prec:expr) => (
|
||||
if ($a - $b).abs() > $prec {
|
||||
let diff = ($a - $b).abs();
|
||||
if diff > $prec {
|
||||
panic!(format!(
|
||||
"assertion failed: `abs(left - right) < {:e}`, \
|
||||
"assertion failed: `abs(left - right) = {:.1e} < {:e}`, \
|
||||
(left: `{}`, right: `{}`)",
|
||||
$prec, $a, $b));
|
||||
diff, $prec, $a, $b));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
35
tests/average.rs
Normal file
35
tests/average.rs
Normal file
@ -0,0 +1,35 @@
|
||||
#[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> {
|
||||
use rand::distributions::{Normal, IndependentSample};
|
||||
use rand::{XorShiftRng, SeedableRng};
|
||||
let normal = Normal::new(2.0, 3.0);
|
||||
let mut values = Vec::with_capacity(size);
|
||||
let mut rng = XorShiftRng::from_seed([1, 2, 3, 4]);
|
||||
for _ in 0..size {
|
||||
values.push(normal.ind_sample(&mut rng));
|
||||
}
|
||||
values
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn average_vs_streaming_stats_small() {
|
||||
let values = initialize_vec(100);
|
||||
let a: average::Average = values.iter().map(|x| *x).collect();
|
||||
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-16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn average_vs_streaming_stats_large() {
|
||||
let values = initialize_vec(1_000_000);
|
||||
let a: average::Average = values.iter().map(|x| *x).collect();
|
||||
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-13);
|
||||
}
|
Loading…
Reference in New Issue
Block a user