diff options
| author | Alexander Kabui <Alexanderkabua@gmail.com> | 2025-11-13 18:28:09 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-13 18:28:09 +0300 |
| commit | 48604d86471df965f189e46580578b24191add0e (patch) | |
| tree | b1a561cb108c17cfbfc1ef28e706f75b010c8b6d /src/correlations.rs | |
| parent | d7b1fd65814511890a4e606e6e610379b6d89393 (diff) | |
| parent | 3f91fb1fb84255e2ec2bbfdc5a096f800de5fde6 (diff) | |
| download | correlation_rust-48604d86471df965f189e46580578b24191add0e.tar.gz | |
Merge pull request #5 from Alexanderlacuna/fixes/rho-magnitude-handling
Sort correlation (ρ) values by absolute magnitude
Diffstat (limited to 'src/correlations.rs')
| -rw-r--r-- | src/correlations.rs | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/src/correlations.rs b/src/correlations.rs index 29b9064..356bfcc 100644 --- a/src/correlations.rs +++ b/src/correlations.rs @@ -149,11 +149,11 @@ impl<'a> Compute<'a> { .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; - } + 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); @@ -216,6 +216,51 @@ mod tests { } #[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); @@ -304,7 +349,6 @@ mod tests { } #[test] - fn test_parse_f64() { let data = "9. ,5. ,0. ,7. ,6. ,1. ,5. ,0.\n"; |
