From 80f50c5a1cdc66e900cbfabe07ba60e8d83f5f57 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 25 Apr 2022 13:24:57 +0300 Subject: add spearman correlation --- src/lib.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index c00657a..f78fc0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ use assert_approx_eq::assert_approx_eq; trait Correlation { fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64); + + } @@ -21,6 +23,8 @@ struct Pearson { degrees_of_freedom:f64, } + + impl Pearson { fn new(n:usize) -> Self { @@ -32,6 +36,8 @@ impl Pearson { } + + impl Correlation for Pearson { fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64){ @@ -53,10 +59,29 @@ impl Correlation for Pearson { } } -fn main() { +#[derive(PartialEq, Debug)] +struct Spearman { + n: usize, + degrees_of_freedom:f64, +} + + +impl Spearman { + fn new(n:usize) ->Spearman { + Spearman { n: n, degrees_of_freedom: (n - 2) as f64 } + } } +impl Correlation for Spearman { + + fn correlate(&self,x:&[f64],y:&[f64]) -> (f64,f64){ + (1.2,2.1) + } +} + + + #[cfg(test)] mod tests { use super::*; @@ -78,4 +103,12 @@ mod tests { assert_eq!(format!("{:.2}", corr_coeff),"1.00"); assert_approx_eq!(p_val,1.341575855,2f64); } + + #[test] + fn test_spearman(){ + let new_spearman = Spearman::new(5); + + assert_eq!(new_spearman,Spearman{n:5, degrees_of_freedom:3.0}); + + } } \ No newline at end of file -- cgit 1.4.1