correlationMatrix

Computes the Pearson correlation matrix for multiple variables.

Returns a symmetric k×k matrix where element (i, j) is the Pearson correlation between variables i and j. Diagonal elements are always 1.0 (a variable is perfectly correlated with itself).

Example:

val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0)
val y = doubleArrayOf(2.0, 3.0, 5.0, 7.0)
val matrix = correlationMatrix(x, y)
matrix[0][0] // 1.0 (x with x)
matrix[0][1] // Pearson r between x and y
matrix[1][0] // same as matrix[0][1] (symmetric)

Note: requires at least 3 observations (unlike covarianceMatrix which requires 2), because the underlying Pearson computation needs n ≥ 3 for a defined t-statistic.

Return

a k×k array of Pearson correlation coefficients.

Parameters

variables

two or more arrays of observations, all with the same size.

Throws

if the arrays have different sizes.

if there are fewer than 2 variables or fewer than 3 observations.