fix: ridge regression, post-review changes

This commit is contained in:
Volodymyr Orlov
2020-11-11 12:00:58 -08:00
parent 83048dbe94
commit ca3a3a101c
2 changed files with 48 additions and 13 deletions
+15 -10
View File
@@ -168,16 +168,10 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
/// Computes the arithmetic mean.
fn mean(&self) -> T {
let n = self.len();
let mut mean = T::zero();
for i in 0..n {
mean += self.get(i);
}
mean / T::from_usize(n).unwrap()
self.sum() / T::from_usize(self.len()).unwrap()
}
/// Computes the standard deviation.
fn std(&self) -> T {
/// Computes variance.
fn var(&self) -> T {
let n = self.len();
let mut mu = T::zero();
@@ -189,7 +183,11 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
sum += xi * xi;
}
mu /= div;
(sum / div - mu * mu).sqrt()
sum / div - mu * mu
}
/// Computes the standard deviation.
fn std(&self) -> T {
self.var().sqrt()
}
}
@@ -592,4 +590,11 @@ mod tests {
assert!((m.std() - 0.81f64).abs() < 1e-2);
}
#[test]
fn var() {
let m = vec![1., 2., 3., 4.];
assert!((m.var() - 1.25f64).abs() < std::f64::EPSILON);
}
}