kendallTau

Computes Kendall's tau-b rank correlation coefficient between two arrays.

Kendall's tau-b measures the ordinal association between two variables. It counts the number of concordant pairs (both values increase together) versus discordant pairs (one increases while the other decreases), with an adjustment for ties. Values range from -1.0 (all pairs discordant) to 1.0 (all pairs concordant).

Uses an O(n log n) merge-sort algorithm (Knight, 1966) for counting discordant pairs, rather than the naive O(n²) approach. The p-value is computed using a normal approximation with the ties-adjusted variance formula (Kendall, 1970).

Returns Double.NaN for both coefficient and p-value when all values in either array are identical (denominator becomes zero).

Example:

val x = doubleArrayOf(1.0, 2.0, 3.0, 4.0, 5.0)
val y = doubleArrayOf(1.0, 3.0, 2.0, 5.0, 4.0)
val result = kendallTau(x, y)
result.coefficient // 0.6
result.pValue // p-value from normal approximation

Return

a CorrelationResult containing tau-b, 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.