From 12c2a37c3ef3a09f1ab0c5ea69b31b433b15f9f6 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sat, 14 Nov 2026 23:33:37 +0000 Subject: Expose Spearman, so both methods work without a dataset file `Spearman` was a private struct, so only `Pearson` could be constructed from outside the crate: a caller that already holds two value vectors -- a request handler, an enrichment step, a test -- could not compute the rank correlation without going through `Compute`, which means writing the dataset to a file and reading the result back out of a CSV. That is why `Pearson` was public and `Spearman` was not, and it is not a distinction worth keeping: the p-value code is the same either way. `Compute` remains the streaming path for a whole dataset. tests/in_memory.rs is an integration test (outside the crate, so it can only use the public surface) covering the three pieces that path needs: both methods in memory, the positional row parser dropping unparseable values together with their x, and the |r|-descending/NaN-last ordering. Also documents `Compute::sorter`, which orders by *signed* r while `custom_float_sorter` -- the one `compute` uses -- orders by magnitude. The two disagree for negative correlations; left as is because it is public API, but no longer silent. --- src/correlations.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src') 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()); } -- cgit 1.4.1