pearsonCorrelation
Computes the Pearson product-moment correlation coefficient between two arrays.
The Pearson correlation measures the strength and direction of the linear relationship between two variables. A value of 1.0 indicates a perfect positive linear relationship, -1.0 indicates a perfect negative linear relationship, and 0.0 indicates no linear relationship.
The p-value is computed using a two-sided t-test for the null hypothesis that the true correlation is zero. Uses the numerically stable form (1-r)(1+r) instead of (1-r²) to avoid catastrophic cancellation when r is close to ±1.
Returns Double.NaN for both coefficient and p-value when either array has zero variance (all values identical).
Example:
val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
val y = doubleArrayOf(2.0, 4.0, 6.0, 8.0, 10.0)
val result = pearsonCorrelation(x, y)
result.coefficient // 1.0
result.pValue // 0.0
result.n // 5Return
a CorrelationResult containing the Pearson r, two-sided p-value, and sample size.
Parameters
the first array of observations.
the second array of observations, must have the same size as x.