Ensure math::variance() function does not divide by zero
This commit is contained in:
parent
f0745386cf
commit
37871ceb81
1 changed files with 10 additions and 5 deletions
|
@ -9,10 +9,15 @@ pub trait Variance {
|
|||
|
||||
impl Variance for Vec<Number> {
|
||||
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::<Number>() / 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::<Number>() / len;
|
||||
out
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue