about summary refs log tree commit diff
path: root/src/correlations.rs
blob: 2b9c0f1155868ae3f3e8d82693ad539d69303330 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::cmp::Ordering;

use crate::parser::parse_rows_with_names;
use crate::reader::BufferReader;
use crate::sorter::sort_write_to_file;

//way to parse the args
use rgsl::{
    randist::t_distribution::{tdist_P, tdist_Q},
    statistics::{correlation, spearman},
};

pub trait Correlation {
    fn correlate(&self, x: &[f64], y: &[f64]) -> (f64, f64);
}

#[derive(PartialEq, Debug)]
pub struct Pearson {
    n: usize,
    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)]
pub struct Spearman {
    n: usize,
    degrees_of_freedom: f64,
}

impl Pearson {
    pub fn new(n: usize) -> Self {
        Self {
            n,
            degrees_of_freedom: (n - 2) as f64,
        }
    }
}

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) {
        let corr_coeff = correlation(x, 1, y, 1, self.n);

        //compute p_val two sided

        // P-value (two-sided)
        // Based on R's cor.test method (https://github.com/SurajGupta/r-source/blob/a28e609e72ed7c47f6ddfbb86c85279a0750f0b7/src/library/stats/R/cor.test.R#L21

        let statistic =
            self.degrees_of_freedom.sqrt() * corr_coeff / (1.0 - corr_coeff.powi(2)).sqrt();

        let p_val = 2.0
            * tdist_P(statistic, self.degrees_of_freedom)
                .min(tdist_Q(statistic, self.degrees_of_freedom));

        (corr_coeff, p_val)
    }
}

impl Correlation for Spearman {
    fn correlate(&self, x: &[f64], y: &[f64]) -> (f64, f64) {
        match x.len() == y.len() {
            true => (),
            false => panic!("array should be of the same size"),
        };

        //dditional workspace of size 2*n is required in work
        let mut vec = Vec::with_capacity(2 * self.n);
        let workspace: &mut [f64] = vec.as_mut_slice();
        let rho = spearman(x, 1, y, 1, self.n, workspace);

        //p-val two sided

        let t = rho * (self.degrees_of_freedom / ((rho + 1.0) * (1.0 - rho))).sqrt();
        let p_val = 2.0 * (tdist_Q(t.abs(), self.degrees_of_freedom));

        (rho, p_val)
    }
}

#[allow(dead_code)]
#[derive(PartialEq, Debug)]
pub struct Compute<'a> {
    x_vals: &'a [f64],
    dataset_path: &'a str,
    method: CorrelationMethod,
    file_delimiter: char,
    output_file: &'a str,
}
#[allow(dead_code)]
impl<'a> Compute<'a> {
    pub fn new(
        file_delimeter: char,
        method: &str,
        dataset_path: &'a str,
        x_vals: &'a [f64],
        output_file: &'a str,
    ) -> Self {
        //capitalize method/same cases

        let method = match method.to_lowercase().as_str() {
            "pearson" => CorrelationMethod::Pearson,
            "spearman" => CorrelationMethod::Spearman,
            _ => panic!("method cannot be found"),
        };

        Self {
            x_vals,
            dataset_path,
            method,
            output_file,
            file_delimiter: file_delimeter,
        }
    }

    pub fn filter_top(result: Vec<(f64, f64)>, n: Option<usize>) -> Vec<(f64, f64)> {
        match n {
            Some(n_top) => result[0..n_top].to_vec(),

            None => result,
        }
    }

    /// 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)]) {
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
    }

    pub fn compute(&self) -> std::io::Result<String> {
        let mut corr_results: Vec<(String, f64, f64, i32)> = Vec::new();

        let reader = BufferReader::new(self.dataset_path);

        match reader {
            Ok(mut buffer_read) => {
                let mut n_string = String::new();

                while let Some(val) = buffer_read.read_line(&mut n_string) {
                    if let Ok(array_new_val) = val {
                        let ty = parse_rows_with_names(
                            self.x_vals,
                            &array_new_val
                                .split(self.file_delimiter)
                                .collect::<Vec<&str>>(),
                        );
                        if ty.x_vals.len() < 4 || ty.y_vals.len() < 4 {
                            // minimum number of acceptable trait values for
                            // computing the correlations
                            continue;
                        }

                        let (key_name, parsed_x_val, parsed_y_val) =
                            (ty.row_name, ty.x_vals, ty.y_vals);

                        if self.method == CorrelationMethod::Pearson {
                            let (rho, p_val) = Pearson::new(parsed_x_val.len())
                                .correlate(&parsed_x_val, &parsed_y_val);

                            corr_results.push((key_name, rho, p_val, parsed_x_val.len() as i32));
                        } else {
                            let (rho, p_val) = Spearman::new(parsed_x_val.len())
                                .correlate(&parsed_x_val, &parsed_y_val);

                            corr_results.push((key_name, rho, p_val, parsed_x_val.len() as i32));
                        }
                    }
                }
            }

            Err(err) => panic!("an error ocurrexxxxxxxxxxxxxxd {:?}", err),
        }

        sort_write_to_file(String::from(self.output_file), corr_results)
    }
}
#[derive(PartialEq, Debug)]
enum CorrelationMethod {
    Pearson,
    Spearman,
}

#[cfg(test)]

mod tests {

    use std::vec;

    use super::*;
    use assert_approx_eq::assert_approx_eq;

    #[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_pearson_negative_rho() {
        let new_pearson = Pearson::new(3);
        let (corr_coeff, p_val) =
            new_pearson.correlate(&[1., 2., 3., 4., 5.], &[5., 4., 3., 2., 1.]);

        assert_eq!(format!("{:.2}", corr_coeff), "-1.00");
    }

    #[test]
    fn test_pearson_negative_rho_signs() {
        // let new_pearson = Pearson::new(3);

        // Each pair should produce a negative correlation
        let test_cases = vec![
            (vec![1., 2., 3., 4., 5.], vec![5., 4., 3., 2., 1.]), // perfect -1.0
            (vec![1., 2., 3., 4., 5.], vec![5., 4.5, 3.5, 3.0, 2.5]), // roughly -0.9
            (vec![1., 2., 3., 4., 5.], vec![4.5, 4.0, 3.8, 3.5, 3.0]), // small negative ~ -0.3
        ];

        for (x, y) in test_cases {
            let new_pearson = Pearson::new(3);
            let new_spearman = Spearman::new(3);
            let (corr_coeff, _p_val) = new_pearson.correlate(&x, &y); // pearson correlation
            let (s_rho, p_val) = new_spearman.correlate(&x, &y);
            let formatted = format!("{:.2}", corr_coeff);
            let s_formatted = format!("{:.2}", s_rho);

            // Assert the sign is actually negative in the formatted string
            assert!(
                formatted.starts_with('-'),
                "Expected negative correlation, got formatted value `{}` (raw {})",
                formatted,
                corr_coeff
            );

            assert!(
                formatted.starts_with('-'),
                "Expected negative correlation, got formatted value `{}` (raw {})",
                s_formatted,
                s_rho
            );
        }
    }

    #[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)
    }

    #[test]
    fn test_compute_obj() {
        let x_vals = [1., 3., 4., 5., 6., 7., 7.];
        let new_compute = [
            Compute::new(',', "spearman", "./notes.txt", &x_vals, "/output.txt"),
            Compute::new(',', "pearson", "./note2.txt", &x_vals, "/output.txt"),
        ];

        assert_eq!(
            [
                Compute {
                    file_delimiter: ',',
                    x_vals: &[1., 3., 4., 5., 6., 7., 7.],
                    dataset_path: "./notes.txt",
                    output_file: "/output.txt",
                    method: CorrelationMethod::Spearman
                },
                Compute {
                    file_delimiter: ',',
                    x_vals: &[1., 3., 4., 5., 6., 7., 7.],
                    dataset_path: "./note2.txt",
                    output_file: "/output.txt",
                    method: CorrelationMethod::Pearson
                }
            ],
            new_compute
        )
    }

    #[test]
    #[should_panic]
    fn test_compute_panics() {
        let new_compute = Compute::new(
            ',',
            "unknown",
            "/notes.txt",
            &[1.2, 1.4, 1.5],
            "/output.txt",
        );
        assert_eq!(
            new_compute,
            Compute {
                file_delimiter: ',',
                x_vals: &[1.2, 1.4, 1.5],
                dataset_path: "/notes.txt",
                method: CorrelationMethod::Pearson,
                output_file: "/output.txt"
            }
        )
    }

    #[test]
    fn test_compute() {
        let compute_obj = Compute::new(
            ',',
            "pearson",
            "tests/data/mock_dataset.txt",
            &[12., 15., 11., 11., 16., 11., 8., 7.],
            "./output.txt",
        );

        let corr_results = compute_obj.compute();

        assert!(corr_results.is_ok())
    }

    #[test]
    fn test_parse_f64() {
        let data = "9. ,5. ,0. ,7. ,6. ,1. ,5. ,0.\n";

        let y = "25.08439 ,72.02225 ,47.56293 ,22.87893 ,14.28721 ,71.84655 ,87.81991 ,84.86824 ,6.72478 ,5.72373 ,73.47078 ,63.74703";

        let expected_results = vec![9., 5., 0., 7., 6., 1., 5., 0.];

        let results: Vec<f64> = data
            .split(',')
            .map(|f_str| f_str.trim().parse::<f64>().expect("parse failed"))
            .collect();

        let u: Vec<f64> = y
            .split(',')
            .map(|f_str| f_str.trim().parse::<f64>().expect("parse failed"))
            .collect();

        assert_eq!(results, expected_results);
        assert_eq!(
            u,
            vec![
                25.08439, 72.02225, 47.56293, 22.87893, 14.28721, 71.84655, 87.81991, 84.86824,
                6.72478, 5.72373, 73.47078, 63.74703
            ]
        )
    }

    #[test]
    fn test_sorter() {
        let mut a = vec![(1.2, 1.0), (1.3, 0.5), (1.3, 7.)];
        Compute::sorter(&mut a);

        let mut f = vec![
            (1.2, 11.1),
            (1.1, 9.7),
            (11.1, 7.8),
            (9.3, 11.1),
            (1.3, 7.0),
            (1.2, 1.0),
            (1.3, 0.5),
        ];
        Compute::sorter(&mut f);

        assert_eq!(a, vec![(1.3, 7.0), (1.2, 1.0), (1.3, 0.5)]);
        assert_eq!(
            f,
            vec![
                (1.2, 11.1),
                (9.3, 11.1),
                (1.1, 9.7),
                (11.1, 7.8),
                (1.3, 7.0),
                (1.2, 1.0),
                (1.3, 0.5)
            ]
        );
    }

    #[test]
    fn test_n_top() {
        let a1 = vec![(1.2, 1.0), (1.3, 0.5), (1.3, 7.)];

        let new_vec = Compute::filter_top(a1, Some(2));

        assert_eq!(new_vec, vec![(1.2, 1.0), (1.3, 0.5)])
    }
}