spearmanCorrelation

Computes the Spearman rank correlation coefficient between two arrays.

The Spearman correlation is a non-parametric measure of the monotonic relationship between two variables. Unlike Pearson, it does not assume linearity — it detects whether the variables tend to increase or decrease together, regardless of the rate.

Computed by applying the Pearson correlation to the average-method ranks of the input arrays. Tied values receive the average of the ranks they would occupy.

Example:

val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
val y = doubleArrayOf(1.0, 4.0, 9.0, 16.0, 25.0)
val result = spearmanCorrelation(x, y)
result.coefficient // 1.0 (perfect monotonic relationship)

Returns Double.NaN for both coefficient and p-value when either array contains NaN, or when either array has zero variance (all values identical after ranking).

Return

a CorrelationResult containing the Spearman rho, two-sided p-value, and sample size.

Parameters

x

the first array of observations.

y

the second array of observations, must have the same size as x.

Throws

if x and y have different sizes.

if there are fewer than 3 observations.