This commit is contained in:
Eh2406 2017-12-20 16:46:50 -05:00
parent 5963cbb032
commit cac986d87e
8 changed files with 119 additions and 0 deletions

View File

@ -19,6 +19,7 @@ serde = { version = "1", optional = true, features = ["derive"]}
bencher = "0.1"
rand = "0.3"
streaming-stats = "0.1"
serde_json = "1"
[[bench]]
name = "mean"

View File

@ -3,6 +3,8 @@
#[macro_use] extern crate average;
extern crate core;
#[cfg(feature = "serde")]
extern crate serde_json;
use core::iter::Iterator;
@ -41,6 +43,23 @@ fn simple() {
assert_almost_eq!(a.kurtosis(), -1.365, 1e-15);
}
#[cfg(feature = "serde")]
#[test]
fn simple_serde() {
let a: Kurtosis = (1..6).map(f64::from).collect();
let b = serde_json::to_string(&a).unwrap();
assert_eq!(&b, "{\"avg\":{\"avg\":{\"avg\":{\"avg\":3.0,\"n\":5},\"sum_2\":10.0},\"sum_3\":0.0},\"sum_4\":34.0}");
let mut c: Kurtosis = serde_json::from_str(&b).unwrap();
assert_eq!(c.mean(), 3.0);
assert_eq!(c.len(), 5);
assert_eq!(c.sample_variance(), 2.5);
assert_almost_eq!(c.error_mean(), f64::sqrt(0.5), 1e-16);
assert_eq!(c.skewness(), 0.0);
c.add(1.0);
assert_almost_eq!(c.skewness(), 0.2795084971874741, 1e-15);
assert_almost_eq!(c.kurtosis(), -1.365, 1e-15);
}
#[test]
fn merge() {
let sequence: &[f64] = &[1., 2., 3., -4., 5.1, 6.3, 7.3, -8., 9., 1.];

View File

@ -3,6 +3,8 @@
extern crate average;
extern crate core;
#[cfg(feature = "serde")]
extern crate serde_json;
use core::iter::Iterator;
@ -19,6 +21,20 @@ fn trivial() {
assert_eq!(m.max(), 3.)
}
#[cfg(feature = "serde")]
#[test]
fn trivial_serde() {
let mut m = Max::new();
m.add(2.);
m.add(1.);
m.add(3.);
m.add(1.);
let b = serde_json::to_string(&m).unwrap();
assert_eq!(&b, "{\"x\":3.0}");
let c: Max = serde_json::from_str(&b).unwrap();
assert_eq!(c.max(), 3.)
}
#[test]
fn merge() {
let sequence: &[f64] = &[1., 2., 3., 4., 5., 6., 7., 8., 9.];

View File

@ -3,6 +3,8 @@
#[macro_use] extern crate average;
extern crate core;
#[cfg(feature = "serde")]
extern crate serde_json;
use core::iter::Iterator;
@ -35,6 +37,19 @@ fn simple() {
assert_almost_eq!(a.error(), f64::sqrt(0.5), 1e-16);
}
#[cfg(feature = "serde")]
#[test]
fn simple_serde() {
let a: MeanWithError = (1..6).map(f64::from).collect();
let b = serde_json::to_string(&a).unwrap();
assert_eq!(&b, "{\"avg\":{\"avg\":3.0,\"n\":5},\"sum_2\":10.0}");
let c: MeanWithError = serde_json::from_str(&b).unwrap();
assert_eq!(c.mean(), 3.0);
assert_eq!(c.len(), 5);
assert_eq!(c.sample_variance(), 2.5);
assert_almost_eq!(c.error(), f64::sqrt(0.5), 1e-16);
}
#[test]
fn numerically_unstable() {
// The naive algorithm fails for this example due to cancelation.

View File

@ -3,6 +3,8 @@
extern crate average;
extern crate core;
#[cfg(feature = "serde")]
extern crate serde_json;
use core::iter::Iterator;
@ -19,6 +21,20 @@ fn trivial() {
assert_eq!(m.min(), -1.)
}
#[cfg(feature = "serde")]
#[test]
fn trivial_serde() {
let mut m = Min::new();
m.add(1.);
m.add(2.);
m.add(-1.);
m.add(1.);
let b = serde_json::to_string(&m).unwrap();
assert_eq!(&b, "{\"x\":-1.0}");
let c: Min = serde_json::from_str(&b).unwrap();
assert_eq!(c.min(), -1.)
}
#[test]
fn merge() {
let sequence: &[f64] = &[1., 2., 3., 4., 5., 6., 7., 8., 9.];

View File

@ -1,4 +1,6 @@
extern crate average;
#[cfg(feature = "serde")]
extern crate serde_json;
use average::{Estimate, Quantile};
@ -21,6 +23,21 @@ fn few_observations() {
assert_eq!(q.quantile(), 2.5);
}
#[cfg(feature = "serde")]
#[test]
fn few_observations_serde() {
let mut q = Quantile::new(0.5);
q.add(1.);
q.add(2.);
q.add(3.);
q.add(4.);
let b = serde_json::to_string(&q).unwrap();
assert_eq!(&b, "{\"q\":[1.0,2.0,3.0,4.0,0.0],\"n\":[1,2,3,4,4],\"m\":[1.0,2.0,3.0,4.0,5.0],\"dm\":[0.0,0.25,0.5,0.75,1.0]}");
let c: Quantile = serde_json::from_str(&b).unwrap();
assert_eq!(c.len(), 4);
assert_eq!(c.quantile(), 2.5);
}
#[test]
fn percentile_99_9() {
let observations = [

View File

@ -3,6 +3,8 @@
#[macro_use] extern crate average;
extern crate core;
#[cfg(feature = "serde")]
extern crate serde_json;
use core::iter::Iterator;
@ -40,6 +42,22 @@ fn simple() {
assert_almost_eq!(a.skewness(), 0.2795084971874741, 1e-15);
}
#[cfg(feature = "serde")]
#[test]
fn simple_serde() {
let mut a: Skewness = (1..6).map(f64::from).collect();
let b = serde_json::to_string(&a).unwrap();
assert_eq!(&b, "{\"avg\":{\"avg\":{\"avg\":3.0,\"n\":5},\"sum_2\":10.0},\"sum_3\":0.0}");
let mut c: Skewness = serde_json::from_str(&b).unwrap();
assert_eq!(c.mean(), 3.0);
assert_eq!(c.len(), 5);
assert_eq!(c.sample_variance(), 2.5);
assert_almost_eq!(c.error_mean(), f64::sqrt(0.5), 1e-16);
assert_eq!(c.skewness(), 0.0);
c.add(1.0);
assert_almost_eq!(c.skewness(), 0.2795084971874741, 1e-15);
}
#[test]
fn merge() {
let sequence: &[f64] = &[1., 2., 3., -4., 5., 6., 7., 8., 9., 1.];

View File

@ -3,6 +3,8 @@
#[macro_use] extern crate average;
extern crate core;
#[cfg(feature = "serde")]
extern crate serde_json;
use core::iter::Iterator;
@ -43,6 +45,21 @@ fn simple() {
assert_almost_eq!(a.error(), f64::sqrt(0.5), 1e-16);
}
#[cfg(feature = "serde")]
#[test]
fn simple_serde() {
let a: WeightedMeanWithError = (1..6).map(|x| (f64::from(x), 1.0)).collect();
let b = serde_json::to_string(&a).unwrap();
assert_eq!(&b, "{\"weight_sum_sq\":5.0,\"weighted_avg\":{\"weight_sum\":5.0,\"weighted_avg\":3.0},\"unweighted_avg\":{\"avg\":{\"avg\":3.0,\"n\":5},\"sum_2\":10.0}}");
let c: WeightedMeanWithError = serde_json::from_str(&b).unwrap();
assert_eq!(c.len(), 5);
assert_eq!(c.weighted_mean(), 3.0);
assert_eq!(c.unweighted_mean(), 3.0);
assert_eq!(c.sum_weights(), 5.0);
assert_eq!(c.sample_variance(), 2.5);
assert_almost_eq!(c.error(), f64::sqrt(0.5), 1e-16);
}
#[test]
fn reference() {
// Example from http://www.analyticalgroup.com/download/WEIGHTED_MEAN.pdf.