Fix a typo and a numerically unstable test

This commit is contained in:
Vinzent Steinberg 2017-05-05 15:41:41 +02:00
parent 3178edcde6
commit c740a60bf4
1 changed files with 10 additions and 1 deletions

View File

@ -102,7 +102,7 @@ impl Average {
//
// self.avg += delta * len_other / len_total;
//
// instead but this results in cancellation if the number of samples are similar.
// instead but this results in cancelation if the number of samples are similar.
self.v += other.v + delta*delta * len_self * len_other / len_total;
}
}
@ -163,6 +163,15 @@ mod tests {
assert_almost_eq!(a.err(), f64::sqrt(0.5), 1e-16);
}
#[test]
fn average_numerically_unstable() {
// The naive algorithm fails for this example due to cancelation.
let big = 1e9;
let sample = &[big + 4., big + 7., big + 13., big + 16.];
let a: Average = sample.iter().map(|x| *x).collect();
assert_eq!(a.sample_variance(), 30.);
}
#[test]
fn average_normal_distribution() {
use rand::distributions::{Normal, IndependentSample};