pub fn correlation(x: &[f64], y: &[f64]) -> f64Expand description
Calculates the correlation coefficient (Pearson’s r) between two slices.
This implementation uses a numerically stable single-pass algorithm that avoids catastrophic cancellation, similar to Welford’s method. It computes mean, variance, and covariance in one pass.
§Arguments
x- First slice of f64 valuesy- Second slice of f64 values (must be same length as x)
§Returns
The correlation coefficient as f64, or NaN if inputs are invalid
§Examples
use linreg_core::stats::correlation;
let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let y = vec![2.0, 4.0, 5.0, 4.0, 5.0];
let r = correlation(&x, &y);
assert!((r - 0.7746).abs() < 1e-4);