Skip to main content

correlation_matrix

Function correlation_matrix 

Source
pub fn correlation_matrix(variables: &[&[f64]]) -> Option<Matrix>
Expand description

Computes a pairwise Pearson correlation matrix.

§Arguments

  • variables — Slice of variable data. Each inner slice is one variable’s observations. All must have the same length.

§Returns

A symmetric n×n Matrix where entry (i,j) is the Pearson r between variables i and j. Diagonal is 1.0. Returns None if fewer than 2 variables, observations < 3, or variable lengths differ.

§Examples

use u_analytics::correlation::correlation_matrix;

let x = [1.0, 2.0, 3.0, 4.0, 5.0];
let y = [2.0, 4.0, 6.0, 8.0, 10.0];
let z = [5.0, 4.0, 3.0, 2.0, 1.0];
let mat = correlation_matrix(&[&x, &y, &z]).unwrap();
assert!((mat.get(0, 1) - 1.0).abs() < 1e-10);   // x,y perfectly correlated
assert!((mat.get(0, 2) + 1.0).abs() < 1e-10);   // x,z perfectly anti-correlated