about summary refs log tree commit diff
path: root/tests/in_memory.rs
blob: bd0637eef680c8048a712177eef67b0ab5d5b086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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"]);
}