diff --git a/lib/src/fnc/util/math/variance.rs b/lib/src/fnc/util/math/variance.rs index 3a7fd92d..51f4b748 100644 --- a/lib/src/fnc/util/math/variance.rs +++ b/lib/src/fnc/util/math/variance.rs @@ -9,10 +9,15 @@ pub trait Variance { impl Variance for Vec { fn variance(self, sample: bool) -> Number { - let mean = self.mean(); - let len = Number::from(self.len() - sample as usize); - let out = self.iter().map(|x| (x - &mean) * (x - &mean)).sum::() / len; - - out + match self.len() { + 0 => Number::NAN, + 1 => Number::from(0), + len => { + let mean = self.mean(); + let len = Number::from(len - sample as usize); + let out = self.iter().map(|x| (x - &mean) * (x - &mean)).sum::() / len; + out + } + } } }