Kendall's Tau
Background
Kendall's tau is a rank-correlation coefficient that measures the strength and direction of association between two variables using concordant minus discordant pairs, normalised by the total number of pairs. Unlike Pearson, it depends only on the ordering of the data — robust to outliers and non-linear monotone transforms. It is the canonical secondary measure used alongside Spearman when LLM-as-judge scores are calibrated against human gold sets.
Problem statement
Implement kendalls_tau(x, y) returning the tau-a statistic:
where is the number of concordant pairs ( and same sign), is the number of discordant pairs (opposite sign), and the denominator is the total number of pairs.
Input
x— array-like of floats, length .y— array-like of floats, same length asx.
Output
Returns a Python float in .
Examples
Example 1 — perfect concordance
Input: x = [1, 2, 3, 4], y = [10, 20, 30, 40]
Output: 1.0
Example 2 — perfect discordance
Input: x = [1, 2, 3, 4], y = [40, 30, 20, 10]
Output: -1.0
Example 3 — hand-computed mixed case
Input: x = [1, 2, 3], y = [2, 1, 3]
Output: 1/3 ≈ 0.3333
Explanation: 3 pairs. (1,2): → discordant. (1,3): both → concordant. (2,3): both → concordant. .
Constraints
- Length mismatch or raises
ValueError. - Tied pairs (sign ) contribute neither to nor — they're skipped, matching the tau-a convention.
- time. For use the merge-sort variant (out of scope here).
Notes
- vs Spearman. Spearman is Pearson on ranks; Kendall counts inversions directly. They agree on direction but Kendall has tighter confidence intervals at small and is more robust to outliers.
- tau-a vs tau-b. This is tau-a (no tie correction in the denominator). Tau-b adjusts for tied groups; for large datasets the two differ by < 1%.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Perfect concordance example
- •Mixed reference case -> 1/3
- •Sample with reordered y