From fc56abe4816d6d00a9db4914c568698b4ed5754b Mon Sep 17 00:00:00 2001 From: vasil Date: Mon, 24 Apr 2023 23:22:32 +0300 Subject: [PATCH] add simple test, remove comment from old variance impl --- src/base/statistics.rs | 17 +++-------------- src/base/variance_test.rs | 7 +++++-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/base/statistics.rs b/src/base/statistics.rs index bf2c40d8..ebefb49d 100644 --- a/src/base/statistics.rs +++ b/src/base/statistics.rs @@ -335,25 +335,14 @@ impl> Matrix { if self.is_empty() { T::zero() } else { - // let val = self.iter().cloned().fold((T::zero(), T::zero()), |a, b| { - // (a.0 + b.clone() * b.clone(), a.1 + b) - // }); - // let denom = T::one() / crate::convert::<_, T>(self.len() as f64); - // let vd = val.1 * denom.clone(); - // val.0 * denom - vd.clone() * vd - // let mean: T = self.iter().map(|&entry| entry).sum::(); - // - // let x: Vec = (0..1000).map(|_| T::zero()).collect(); - // let s: T = x.iter().cloned().fold(T::zero(), |a, b| a + b); - // cannot use sum since `T` is not `Sum` by trait bounds - let total_sum = self.iter().cloned().fold(T::zero(), |a, b| a + b); + let sum_of_elements = self.iter().cloned().fold(T::zero(), |a, b| a + b); let n_elements = crate::convert::<_, T>(self.len() as f64); - let mean = total_sum / n_elements.clone(); + let mean = sum_of_elements / n_elements.clone(); let variance = self.iter().cloned().fold(T::zero(), |acc, x| { acc + (x.clone() - mean.clone()) * (x.clone() - mean.clone()) - }) / n_elements.clone(); + }) / n_elements; variance } diff --git a/src/base/variance_test.rs b/src/base/variance_test.rs index af5d22af..4319e156 100644 --- a/src/base/variance_test.rs +++ b/src/base/variance_test.rs @@ -2,6 +2,9 @@ use crate::DVector; #[test] fn test_variance_new() { - let v = DVector::repeat(10_000, 100000000.0); - assert_eq!(v.variance(), 0.0) + let long_repeating_vector = DVector::repeat(10_000, 100000000.0); + assert_eq!(long_repeating_vector.variance(), 0.0); + + let short_vec = DVector::from_vec(vec![1., 2., 3.]); + assert_eq!(short_vec.variance(), 2.0 / 3.0) }