about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/correlations.rs18
-rw-r--r--tests/in_memory.rs73
2 files changed, 89 insertions, 2 deletions
diff --git a/src/correlations.rs b/src/correlations.rs
index 356bfcc..2b9c0f1 100644
--- a/src/correlations.rs
+++ b/src/correlations.rs
@@ -20,8 +20,16 @@ pub struct Pearson {
     degrees_of_freedom: f64,
 }
 
+/// Spearman's rank correlation, on the same footing as [`Pearson`].
+///
+/// Public for the same reason `Pearson` is: a caller that already holds the
+/// two value vectors (a request handler, an enrichment step, a test) should be
+/// able to compute the rank correlation without going through
+/// [`Compute`] -- which means writing a dataset to a file and reading the
+/// result back out of a CSV.  `Compute` stays the streaming path for a whole
+/// dataset; this is the in-memory one, and the p-value is the same code.
 #[derive(PartialEq, Debug)]
-struct Spearman {
+pub struct Spearman {
     n: usize,
     degrees_of_freedom: f64,
 }
@@ -127,8 +135,14 @@ impl<'a> Compute<'a> {
         }
     }
 
+    /// Sort by the correction coefficient, descending.
+    ///
+    /// NOTE: this orders by *signed* r, while [`crate::sorter::custom_float_sorter`]
+    /// -- the one `compute` actually uses -- orders by *magnitude*, with NaNs
+    /// last. The two disagree for negative correlations, so prefer the latter
+    /// when "same as the file path" is what a caller wants. Kept as it is
+    /// because it is public API, not because the difference is intended.
     pub fn sorter(results: &mut [(f64, f64)]) {
-        //naive sorter
         results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
     }
 
diff --git a/tests/in_memory.rs b/tests/in_memory.rs
new file mode 100644
index 0000000..bd0637e
--- /dev/null
+++ b/tests/in_memory.rs
@@ -0,0 +1,73 @@
+//! The in-memory path: both correlation methods, the row parser and the
+//! ordering, usable from outside the crate.
+//!
+//! These are the pieces a caller that already holds two value vectors needs --
+//! no dataset file, no JSON, no CSV round trip. They exist as an integration
+//! test (rather than a unit test inside `src/`) because that is what proves the
+//! public surface: `Spearman` used to be a private struct, so only `Pearson`
+//! could be constructed from outside.
+
+use correlation_rust::correlations::{Correlation, Pearson, Spearman};
+use correlation_rust::parser::parse_rows_with_names;
+use correlation_rust::sorter::custom_float_sorter;
+
+#[test]
+fn both_methods_correlate_in_memory() {
+    // A monotone relationship: both coefficients are 1.
+    let x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
+    let y = [2.0, 4.0, 6.0, 8.0, 10.0, 12.0];
+    let (pearson_r, pearson_p) = Pearson::new(x.len()).correlate(&x, &y);
+    let (spearman_rho, spearman_p) = Spearman::new(x.len()).correlate(&x, &y);
+    assert!((pearson_r - 1.0).abs() < 1e-12, "{pearson_r}");
+    assert!((spearman_rho - 1.0).abs() < 1e-12, "{spearman_rho}");
+    assert!(pearson_p.abs() < 1e-12, "{pearson_p}");
+    assert!(spearman_p.abs() < 1e-12, "{spearman_p}");
+
+    // Reversed: both -1.
+    let reversed: Vec<f64> = y.iter().rev().copied().collect();
+    assert!((Pearson::new(x.len()).correlate(&x, &reversed).0 + 1.0).abs() < 1e-12);
+    assert!((Spearman::new(x.len()).correlate(&x, &reversed).0 + 1.0).abs() < 1e-12);
+
+    // A real (non-monotone) pair: the two methods differ, and both p-values
+    // are in range. Pearson is sensitive to the outlier, Spearman is not.
+    let x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
+    let y = [1.0, 4.0, 9.0, 16.0, 25.0, 400.0];
+    let (pearson_r, pearson_p) = Pearson::new(x.len()).correlate(&x, &y);
+    let (spearman_rho, spearman_p) = Spearman::new(x.len()).correlate(&x, &y);
+    assert!(pearson_r.is_finite() && pearson_r.abs() <= 1.0, "{pearson_r}");
+    assert!(spearman_rho.is_finite() && spearman_rho.abs() <= 1.0, "{spearman_rho}");
+    assert!((0.0..=1.0).contains(&pearson_p), "{pearson_p}");
+    assert!((0.0..=1.0).contains(&spearman_p), "{spearman_p}");
+    assert!(spearman_rho > pearson_r, "rank is robust to the outlier: {spearman_rho} vs {pearson_r}");
+}
+
+#[test]
+fn a_row_is_parsed_positionally_and_bad_values_drop_their_x() {
+    // The row's first field is the trait name; the rest align positionally
+    // with x, and a value that does not parse takes its x with it.
+    let x = [10.0, 20.0, 30.0, 40.0];
+    let row = ["trait_1", "1.5", "NA", "3.5", ""];
+    let parsed = parse_rows_with_names(&x, &row);
+    assert_eq!(parsed.row_name, "trait_1");
+    assert_eq!(parsed.x_vals, vec![10.0, 30.0]);
+    assert_eq!(parsed.y_vals, vec![1.5, 3.5]);
+
+    // A short row keeps only as many pairs as it has values.
+    let parsed = parse_rows_with_names(&x, &["trait_2", "1.0", "2.0"]);
+    assert_eq!(parsed.x_vals, vec![10.0, 20.0]);
+    assert_eq!(parsed.y_vals, vec![1.0, 2.0]);
+}
+
+#[test]
+fn results_sort_by_magnitude_with_nans_last() {
+    let mut rows = vec![
+        ("weak".to_string(), 0.1, 0.9, 30),
+        ("negative".to_string(), -0.9, 0.001, 30),
+        ("nan".to_string(), f64::NAN, f64::NAN, 30),
+        ("strong".to_string(), 0.95, 0.0001, 30),
+        ("middle".to_string(), -0.5, 0.2, 30),
+    ];
+    custom_float_sorter(&mut rows);
+    let names: Vec<&str> = rows.iter().map(|row| row.0.as_str()).collect();
+    assert_eq!(names, vec!["strong", "negative", "middle", "weak", "nan"]);
+}