Trait rustml::math::var::Var [] [src]

pub trait Var<T> {
    fn var(&self, dim: Dimension, nrm: Normalization) -> T;
}

Trait to compute the variance of values.

Required Methods

fn var(&self, dim: Dimension, nrm: Normalization) -> T

If the implementation for a vector is used this method computes the variance of the values of that vectors. For vectors only the dimension Dimension::Row is valid.

If the implementation for a matrix is used this methods computes the variance of all values of each row if the dimension is equal to Dimension::Row or each column if the dimension is Dimension::Column.

For the parameter Normalization::N the following formula is used to compute the variance: $$\frac{1}{n} \sum_{i=1}^n (x_i - \mu)^2$$

For the parameter Normalization::MinusOne the following formula is used to compute the variance: $$\frac{1}{n-1} \sum_{i=1}^n (x_i - \mu)^2$$ If n is equal to 1 the parameter Normalization::MinusOne behaves like Normalization::N.

Compute the variance of the values in a vector.

use rustml::*;

let a = vec![1.0, 2.0, 1.5, 1.8, 2.2];

let mut r = a.var(Dimension::Row, Normalization::N);
assert!(num::abs(r - 0.176) < 0.001);

r = a.var(Dimension::Row, Normalization::MinusOne);
assert!(num::abs(r - 0.22) < 0.001);

Compute the variance for rows and columns of a matrix.


Let M be a matrix with the rows $r_1, r_2, \dots$, i.e.

$ M = \begin{bmatrix} -\ r_1 - \\ -\ r_2 - \\ \vdots \\ \end{bmatrix} $

Then, the variance of the matrix with the dimension parameter Dimension::Row and the normalization parameter x returns the following vector: vec![r1.var(Row, x), r2.var(Row, x), ...]

use rustml::*;

let m = mat![
    1.0f32, 5.0, 3.0, 6.0, 2.0;
    2.0, 2.5, 2.2, 1.8, 2.1;
    4.0, 9.0, 2.7, 13.0, 1.9
];

let mut r = m.var(Dimension::Row, Normalization::N);
let mut e = vec![3.44, 0.05, 17.93];
// check that r is "equal" to e
assert!(r.iter().zip(e.iter()).all(|(&a, &b)| num::abs(a - b) < 0.01));

r = m.var(Dimension::Column, Normalization::N);
e = vec![1.56, 7.17, 0.11, 21.34, 0.007];
// check that r is "equal" to e
assert!(r.iter().zip(e.iter()).all(|(&a, &b)| num::abs(a - b) < 0.01));

Implementors