Module peroxide::statistics::stat

source ·
Expand description

Basic statistics

§Statistics trait

  • To make generic code, there is Statistics trait
    • mean: just mean
    • var : variance
    • sd : standard deviation (R-like notation)
    • cov : covariance
    • cor : 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;
    }

§For Vec<f64>

  • Caution: For Vec<f64>, cov & cor are unimplemented (those for Matrix)

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    fn main() {
        let a = c!(1,2,3,4,5);
        a.mean().print(); // 3
        a.var().print();  // 2.5
        a.sd().print();   // 1.5811388300841898
    }
  • But there are other functions to calculate cov & cor

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    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
    }

§For Matrix

  • For Matrix, mean, var, sd means column operations

  • cov means covariance matrix & cor means also correlation coefficient matrix

    #[macro_use]
    extern crate peroxide;
    use peroxide::fuga::*;
    
    fn 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
    }

§For DataFrame

  • Similar to Matrix but, Value is DataFrame
  • cov means covariance matrix.
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

fn main() {
    #[cfg(feature = "dataframe")]
    {
        let mut m = DataFrame::with_header(vec!["x", "y"]);
        m["x"] = c!(1,2,3);
        m["y"] = c!(3,2,1);

        m.cov().print();
        //         c[0]    c[1]
        // r[0]  1.0000 -1.0000
        // r[1] -1.0000  1.0000
    }
}

§Confusion Matrix

  • ConfusionMatrix is a struct to calculate confusion matrix
  • The reference is here

§Example

use peroxide::fuga::*;

fn main() {
    let y     = vec![1usize, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0];
    let y_hat = vec![0usize, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0];
    let true_val = 1usize;

    let cm = ConfusionMatrix::new(&y, &y_hat, true_val);
    cm.print();
    //         c[0]    c[1]
    // r[0]       6       2
    // r[1]       1       3

    // to matrix
    let cm_mat = cm.to_matrix();
     
    // Calculate accuracy
    cm.ACC().print(); // 0.75

    // Calculate TPR (Sensitivity or Recall)
    cm.TPR().print(); // 0.6666....

    // Calculate some metrics
    let metrics = cm.calc_metrics(&[ACC, TPR, TNR, F1]);

    // Print some metrics
    cm.summary(&[ACC, TPR, TNR, F1]);
}

Structs§

Enums§

Traits§

Functions§

  • Pearson’s correlation coefficient
  • Covariance (to Value)
  • R like linear regression