7 Statistics
7.1 Statistics
trait
- To make generic code, there is
Statistics
traitmean
: just meanvar
: variancesd
: standard deviation (R-like notation)cov
: covariancecor
: correlation coefficient
pub trait Statistics { type Array; type Value; fn mean(&self) -> Self::Value; fn var(&self) -> Self::Value; fn sd(&self) -> Self::Value; fn cov(&self) -> Self::Array; fn cor(&self) -> Self::Array; }
7.1.1 For Vec<f64>
But there are other functions to calculate cov
& cor
fn main() {
let v1 = c!(1,2,3);
let v2 = c!(3,2,1);
cov(&v1, &v2).print(); // -0.9999999999999998
cor(&v1, &v2).print(); // -0.9999999999999993
}
7.1.2 For Matrix
For
Matrix
,mean, var, sd
means column operationscov
means covariance matrix &cor
means also correlation coefficient matrixfn main() { let m = matrix(c!(1,2,3,3,2,1), 3, 2, Col); m.mean().print(); // [2, 2] m.var().print(); // [1.0000, 1.0000] m.sd().print(); // [1.0000, 1.0000] m.cov().print(); // c[0] c[1] // r[0] 1.0000 -1.0000 // r[1] -1.0000 1.0000 m.cor().print(); // c[0] c[1] // r[0] 1 -1.0000 // r[1] -1.0000 1 }