Skip to main content

correlation

Function correlation 

Source
pub fn correlation(x: &[f64], y: &[f64]) -> FinanceResult<f64>
Expand description

Pearson correlation of two equal-length series.

Uses the algebraically equivalent form Σ(dx·dy) / sqrt(Σdx² · Σdy²) (same as sample correlation; n−1 cancels). Errors if either series has zero variance.

§Examples

use finance_solution::correlation;
let x = [1.0, 2.0, 3.0, 4.0];
let y = [2.0, 4.0, 6.0, 8.0];
assert!((correlation(&x, &y).unwrap() - 1.0).abs() < 1e-12);