add simple test, remove comment from old variance impl
This commit is contained in:
parent
032002dce9
commit
fc56abe481
|
@ -335,25 +335,14 @@ impl<T: Scalar, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S> {
|
||||||
if self.is_empty() {
|
if self.is_empty() {
|
||||||
T::zero()
|
T::zero()
|
||||||
} else {
|
} 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::<T>();
|
|
||||||
//
|
|
||||||
// let x: Vec<T> = (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
|
// 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 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| {
|
let variance = self.iter().cloned().fold(T::zero(), |acc, x| {
|
||||||
acc + (x.clone() - mean.clone()) * (x.clone() - mean.clone())
|
acc + (x.clone() - mean.clone()) * (x.clone() - mean.clone())
|
||||||
}) / n_elements.clone();
|
}) / n_elements;
|
||||||
|
|
||||||
variance
|
variance
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
use crate::DVector;
|
use crate::DVector;
|
||||||
#[test]
|
#[test]
|
||||||
fn test_variance_new() {
|
fn test_variance_new() {
|
||||||
let v = DVector::repeat(10_000, 100000000.0);
|
let long_repeating_vector = DVector::repeat(10_000, 100000000.0);
|
||||||
assert_eq!(v.variance(), 0.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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue