about summary refs log tree commit diff
path: root/src/correlations.rs
diff options
context:
space:
mode:
authorAlexander <alexanderkabua@gmail.com>2022-04-26 14:56:53 +0300
committerAlexander <alexanderkabua@gmail.com>2022-04-26 14:56:53 +0300
commit1bb2f52839b75b9c2ee9aa08a11d7d3823dbe647 (patch)
tree1ade7d6a93500faf2dd45b6405bd8ca1531d2e21 /src/correlations.rs
parentd3849a57f854bb2bde329f18e0478968adb4f842 (diff)
downloadcorrelation_rust-1bb2f52839b75b9c2ee9aa08a11d7d3823dbe647.tar.gz
modularize code and tests
Diffstat (limited to 'src/correlations.rs')
-rw-r--r--src/correlations.rs57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/correlations.rs b/src/correlations.rs
index f84bb65..596240e 100644
--- a/src/correlations.rs
+++ b/src/correlations.rs
@@ -5,7 +5,8 @@ use rgsl::{
 
 pub trait Correlation {
     
-    fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64);   
+    fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64);
+
 }
 
 #[derive(PartialEq, Debug)]
@@ -29,6 +30,16 @@ impl Pearson {
         }
     }
 }
+
+impl Spearman {
+    pub fn new(n:usize) -> Self {
+
+        Self {
+            n,
+            degrees_of_freedom:(n - 2) as f64,
+        }
+    }
+}
 impl Correlation for Pearson {
     fn correlate(&self,x:&[f64],y:&[f64]) ->(f64,f64){
 
@@ -72,3 +83,47 @@ impl Correlation for Spearman {
         return (rho,p_val);
     }
 }
+
+
+#[cfg(test)]
+
+mod test{
+    use assert_approx_eq::assert_approx_eq;
+    use super::*; 
+
+
+
+    #[test]
+    fn test_pearson_obj(){
+        let new_pearson = Pearson::new(5);
+        assert_eq!(new_pearson,Pearson{n:5, degrees_of_freedom:3.0});
+    }
+    #[test]
+    fn test_pearson_correlation(){
+        let new_pearson = Pearson::new(3);
+        let (corr_coeff,p_val) = new_pearson.correlate(&[1.,2.,3.,4.,5.] ,&[1.,2.,3.,4.,5.]);
+
+        assert_eq!(format!("{:.2}", corr_coeff),"1.00");
+        assert_approx_eq!(p_val,1.341575855,2f64);
+    }
+
+    #[test]
+    fn test_spearman_obj(){
+        let new_spearman = Spearman::new(5);
+
+        assert_eq!(new_spearman,Spearman{n:5, degrees_of_freedom:3.0});
+
+    }
+    #[test]
+    fn test_spearman_correlation(){
+        let new_spearman = Spearman::new(5);
+
+        let (s_rho,p_val) = new_spearman.correlate(&[1.,2.,3.,4.,5.], &[5.,6.,7.,8.,7.]);
+
+
+        assert_approx_eq!(s_rho,0.079603960396039,2f64);
+        assert_approx_eq!(p_val,0.43111687,2f64)
+
+    }
+
+}
\ No newline at end of file