2018-01-11 18:29:43 +01:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
|
|
|
|
|
|
|
|
#[macro_use] extern crate average;
|
|
|
|
|
|
|
|
extern crate core;
|
2019-01-22 14:29:29 +01:00
|
|
|
#[cfg(feature = "serde1")]
|
2018-01-11 18:29:43 +01:00
|
|
|
extern crate serde_json;
|
|
|
|
|
|
|
|
use core::iter::Iterator;
|
|
|
|
|
2018-07-10 17:19:57 +02:00
|
|
|
use average::{Moments4, Merge};
|
2018-01-11 18:29:43 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trivial() {
|
2018-07-10 17:19:57 +02:00
|
|
|
let mut a = Moments4::new();
|
2018-01-11 18:29:43 +01:00
|
|
|
assert_eq!(a.len(), 0);
|
2018-07-11 12:37:06 +02:00
|
|
|
assert_eq!(a.central_moment(1), 0.0);
|
|
|
|
assert_eq!(a.standardized_moment(2), 1.0);
|
2018-01-11 18:29:43 +01:00
|
|
|
a.add(1.0);
|
|
|
|
assert_eq!(a.len(), 1);
|
|
|
|
assert_eq!(a.mean(), 1.0);
|
|
|
|
assert_eq!(a.central_moment(0), 1.0);
|
|
|
|
assert_eq!(a.central_moment(1), 0.0);
|
|
|
|
assert_eq!(a.central_moment(2), 0.0);
|
|
|
|
assert_eq!(a.central_moment(3), 0.0);
|
|
|
|
a.add(1.0);
|
|
|
|
assert_eq!(a.len(), 2);
|
|
|
|
assert_eq!(a.mean(), 1.0);
|
|
|
|
assert_eq!(a.central_moment(0), 1.0);
|
|
|
|
assert_eq!(a.central_moment(1), 0.0);
|
|
|
|
assert_eq!(a.central_moment(2), 0.0);
|
|
|
|
assert_eq!(a.central_moment(3), 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
2018-07-10 17:19:57 +02:00
|
|
|
let mut a: Moments4 = (1..6).map(f64::from).collect();
|
2018-01-11 18:29:43 +01:00
|
|
|
assert_eq!(a.len(), 5);
|
|
|
|
assert_eq!(a.mean(), 3.0);
|
|
|
|
assert_eq!(a.central_moment(0), 1.0);
|
|
|
|
assert_eq!(a.central_moment(1), 0.0);
|
|
|
|
// variance
|
|
|
|
assert_eq!(a.central_moment(2), 2.0);
|
|
|
|
assert_eq!(a.standardized_moment(0), 5.0);
|
|
|
|
assert_eq!(a.standardized_moment(1), 0.0);
|
|
|
|
assert_eq!(a.standardized_moment(2), 1.0);
|
|
|
|
assert_almost_eq!(a.sample_skewness(), 0.0, 1e-15);
|
2018-01-11 19:25:25 +01:00
|
|
|
assert_almost_eq!(a.standardized_moment(3), 0.0, 1e-15);
|
2018-01-11 18:29:43 +01:00
|
|
|
a.add(1.0);
|
|
|
|
// skewness
|
|
|
|
assert_almost_eq!(a.standardized_moment(3), 0.2795084971874741, 1e-15);
|
|
|
|
// kurtosis
|
|
|
|
assert_almost_eq!(a.standardized_moment(4), -1.365 + 3.0, 1e-14);
|
|
|
|
}
|
|
|
|
|
2019-01-22 14:29:29 +01:00
|
|
|
#[cfg(feature = "serde1")]
|
2018-01-11 18:29:43 +01:00
|
|
|
#[test]
|
|
|
|
fn simple_serde() {
|
2018-07-10 17:19:57 +02:00
|
|
|
let a: Moments4 = (1..6).map(f64::from).collect();
|
2018-01-11 18:29:43 +01:00
|
|
|
let b = serde_json::to_string(&a).unwrap();
|
2019-01-22 14:29:29 +01:00
|
|
|
assert_eq!(&b, "{\"n\":5,\"avg\":3.0,\"m\":[10.0,1.7763568394002505e-15,34.00000000000001]}");
|
2018-07-11 12:54:37 +02:00
|
|
|
let mut c: Moments4 = serde_json::from_str(&b).unwrap();
|
2018-01-11 18:29:43 +01:00
|
|
|
assert_eq!(c.len(), 5);
|
2018-01-11 19:42:41 +01:00
|
|
|
assert_eq!(c.mean(), 3.0);
|
|
|
|
assert_eq!(c.central_moment(0), 1.0);
|
|
|
|
assert_eq!(c.central_moment(1), 0.0);
|
|
|
|
// variance
|
|
|
|
assert_eq!(c.central_moment(2), 2.0);
|
|
|
|
assert_eq!(c.standardized_moment(0), 5.0);
|
|
|
|
assert_eq!(c.standardized_moment(1), 0.0);
|
|
|
|
assert_eq!(c.standardized_moment(2), 1.0);
|
|
|
|
assert_almost_eq!(c.sample_skewness(), 0.0, 1e-15);
|
|
|
|
assert_almost_eq!(c.standardized_moment(3), 0.0, 1e-15);
|
2018-01-11 18:29:43 +01:00
|
|
|
c.add(1.0);
|
2018-01-11 19:42:41 +01:00
|
|
|
// skewness
|
|
|
|
assert_almost_eq!(c.standardized_moment(3), 0.2795084971874741, 1e-15);
|
|
|
|
// kurtosis
|
|
|
|
assert_almost_eq!(c.standardized_moment(4), -1.365 + 3.0, 1e-14);
|
2018-01-11 18:29:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge() {
|
|
|
|
let sequence: &[f64] = &[1., 2., 3., -4., 5.1, 6.3, 7.3, -8., 9., 1.];
|
|
|
|
for mid in 0..sequence.len() {
|
|
|
|
let (left, right) = sequence.split_at(mid);
|
2018-07-10 17:19:57 +02:00
|
|
|
let avg_total: Moments4 = sequence.iter().collect();
|
|
|
|
let mut avg_left: Moments4 = left.iter().collect();
|
|
|
|
let avg_right: Moments4 = right.iter().collect();
|
2018-01-11 18:29:43 +01:00
|
|
|
avg_left.merge(&avg_right);
|
|
|
|
assert_eq!(avg_total.len(), avg_left.len());
|
|
|
|
assert_almost_eq!(avg_total.mean(), avg_left.mean(), 1e-14);
|
2018-01-11 19:16:11 +01:00
|
|
|
assert_almost_eq!(avg_total.central_moment(2), avg_left.central_moment(2), 1e-14);
|
|
|
|
assert_almost_eq!(avg_total.central_moment(3), avg_left.central_moment(3), 1e-13);
|
|
|
|
assert_almost_eq!(avg_total.central_moment(4), avg_left.central_moment(4), 1e-12);
|
2018-01-11 18:29:43 +01:00
|
|
|
}
|
|
|
|
}
|