rust-average/tests/random.rs
Vinzent Steinberg 5d6d67bac9 Move add and merge to trait
This should make it possible to write more generic code.
2017-06-25 15:39:12 +02:00

41 lines
1.3 KiB
Rust

#![cfg_attr(feature = "cargo-clippy", allow(float_cmp, map_clone))]
#[macro_use] extern crate average;
extern crate rand;
use average::{Kurtosis, Estimate};
#[test]
fn normal_distribution() {
use rand::distributions::{Normal, IndependentSample};
let normal = Normal::new(2.0, 3.0);
let mut a = Kurtosis::new();
for _ in 0..1_000_000 {
a.add(normal.ind_sample(&mut ::rand::thread_rng()));
}
assert_almost_eq!(a.mean(), 2.0, 1e-2);
assert_almost_eq!(a.sample_variance().sqrt(), 3.0, 1e-2);
assert_almost_eq!(a.population_variance().sqrt(), 3.0, 1e-2);
assert_almost_eq!(a.error_mean(), 0.0, 1e-2);
assert_almost_eq!(a.skewness(), 0.0, 1e-2);
assert_almost_eq!(a.kurtosis(), 0.0, 4e-2);
}
#[test]
fn exponential_distribution() {
use rand::distributions::{Exp, IndependentSample};
let lambda = 2.0;
let normal = Exp::new(lambda);
let mut a = Kurtosis::new();
for _ in 0..6_000_000 {
a.add(normal.ind_sample(&mut ::rand::thread_rng()));
}
assert_almost_eq!(a.mean(), 1./lambda, 1e-2);
assert_almost_eq!(a.sample_variance().sqrt(), 1./lambda, 1e-2);
assert_almost_eq!(a.population_variance().sqrt(), 1./lambda, 1e-2);
assert_almost_eq!(a.error_mean(), 0.0, 1e-2);
assert_almost_eq!(a.skewness(), 2.0, 1e-2);
assert_almost_eq!(a.kurtosis(), 6.0, 1e-1);
}