Move random tests to separate file

This commit is contained in:
Vinzent Steinberg 2017-05-28 23:46:15 +02:00
parent b8e980d6ce
commit bea79374e3
4 changed files with 38 additions and 53 deletions

View File

@ -2,8 +2,6 @@
extern crate core;
extern crate rand;
use core::iter::Iterator;
use average::Kurtosis;
@ -57,20 +55,3 @@ fn merge() {
assert_almost_eq!(avg_total.kurtosis(), avg_left.kurtosis(), 1e-14);
}
}
#[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, 4e-2);
}

View File

@ -2,8 +2,6 @@
extern crate core;
extern crate rand;
use core::iter::Iterator;
use average::MeanWithError;
@ -58,17 +56,3 @@ fn merge() {
assert_eq!(avg_total.sample_variance(), avg_left.sample_variance());
}
}
#[test]
fn normal_distribution() {
use rand::distributions::{Normal, IndependentSample};
let normal = Normal::new(2.0, 3.0);
let mut a = MeanWithError::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(), 0.0, 1e-2);
}

38
tests/random.rs Normal file
View File

@ -0,0 +1,38 @@
#[macro_use] extern crate average;
extern crate rand;
use average::Kurtosis;
#[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);
}

View File

@ -2,8 +2,6 @@
extern crate core;
extern crate rand;
use core::iter::Iterator;
use average::Skewness;
@ -55,19 +53,3 @@ fn merge() {
assert_almost_eq!(avg_total.skewness(), avg_left.skewness(), 1e-14);
}
}
#[test]
fn exponential_distribution() {
use rand::distributions::{Exp, IndependentSample};
let lambda = 2.0;
let normal = Exp::new(lambda);
let mut a = Skewness::new();
for _ in 0..2_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);
}