Moments: Test merge
This commit is contained in:
parent
49ce6c5256
commit
9f2a8ee44f
@ -140,16 +140,16 @@ impl Moments {
|
||||
binom.next().unwrap(); // Skip k = 0.
|
||||
for k in 1..=(p - 2) {
|
||||
coeff *= factor_coeff;
|
||||
println!("writing m[{}], accessing m[{}]", p - 2, p - 2 - k);
|
||||
self.m[p - 2] += f64::approx_from(binom.next().unwrap()).unwrap() *
|
||||
prev_m[p - 2 - k] * coeff;
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
impl Merge for Moments {
|
||||
#[inline]
|
||||
pub fn merge(&mut self, other: &Moments) {
|
||||
fn merge(&mut self, other: &Moments) {
|
||||
let mut result = Moments::new();
|
||||
result.n = self.n + other.n;
|
||||
if result.n == 0 {
|
||||
|
@ -8,7 +8,7 @@ extern crate serde_json;
|
||||
|
||||
use core::iter::Iterator;
|
||||
|
||||
use average::{Moments};
|
||||
use average::{Moments, Merge};
|
||||
|
||||
#[test]
|
||||
fn trivial() {
|
||||
@ -67,21 +67,21 @@ fn simple_serde() {
|
||||
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.];
|
||||
for mid in 0..sequence.len() {
|
||||
let (left, right) = sequence.split_at(mid);
|
||||
let avg_total: Kurtosis = sequence.iter().map(|x| *x).collect();
|
||||
let mut avg_left: Kurtosis = left.iter().map(|x| *x).collect();
|
||||
let avg_right: Kurtosis = right.iter().map(|x| *x).collect();
|
||||
let avg_total: Moments = sequence.iter().map(|x| *x).collect();
|
||||
let mut avg_left: Moments = left.iter().map(|x| *x).collect();
|
||||
let avg_right: Moments = right.iter().map(|x| *x).collect();
|
||||
avg_left.merge(&avg_right);
|
||||
assert_eq!(avg_total.len(), avg_left.len());
|
||||
assert_almost_eq!(avg_total.mean(), avg_left.mean(), 1e-14);
|
||||
assert_almost_eq!(avg_total.sample_variance(), avg_left.sample_variance(), 1e-14);
|
||||
assert_almost_eq!(avg_total.skewness(), avg_left.skewness(), 1e-14);
|
||||
assert_almost_eq!(avg_total.kurtosis(), avg_left.kurtosis(), 1e-14);
|
||||
assert_almost_eq!(avg_total.central_moment(2), avg_left.central_moment(2), 1e-14);
|
||||
assert_almost_eq!(avg_total.central_moment(3), avg_left.central_moment(3), 1e-13);
|
||||
assert_almost_eq!(avg_total.central_moment(4), avg_left.central_moment(4), 1e-12);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user