-
Notifications
You must be signed in to change notification settings - Fork 11
/
random.rs
34 lines (31 loc) · 1.19 KB
/
random.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use rand_distr::Distribution;
use average::{assert_almost_eq, Estimate, Kurtosis};
#[test]
fn normal_distribution() {
let normal = rand_distr::Normal::new(2.0, 3.0).unwrap();
let mut a = Kurtosis::new();
for _ in 0..1_000_000 {
a.add(normal.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() {
let lambda = 2.0;
let normal = rand_distr::Exp::new(lambda).unwrap();
let mut a = Kurtosis::new();
for _ in 0..6_000_000 {
a.add(normal.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-1);
assert_almost_eq!(a.kurtosis(), 6.0, 1.5e-1);
}