Spearman Rank Correlation and Kendall Tau
Install and import#
npm install fintech-algorithmsimport { spearmanRankCorrelationAndKendallTau } from "fintech-algorithms/foundations/dependence-regression-and-model-foundations/spearman-rank-correlation-and-kendall-tau";Signature#
spearmanRankCorrelationAndKendallTau(input)Replaces input.x and input.y with their ranks, averaging ranks across ties, then reports the Spearman correlation of those ranks and Kendall's tau-a from the concordant and discordant pair counts.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | One record holding the two aligned series x and y, one observation per position.x: non-empty list of finite numbers, at least two, not all identical · y: finite numbers, same length as x |
Returns#
D00Output
An object with spearman (Pearson correlation of the two rank vectors), kendallTauA (concordant minus discordant pairs over the total pair count, with no tie correction) and tiesPresent (true when either series repeats a value).
Errors#
- When x or y is missing, empty, or contains a non-finite number — throws RangeError
- When x and y have different lengths, or fewer than two observations — the least-squares fit is computed for every topic in the family before the topic branch is taken — throws RangeError
- When x is constant, which leaves the regression slope undefined — throws RangeError
- When either rank vector is constant, which leaves the Spearman correlation undefined — throws RangeError
Complexity: time O(n^2),
space O(n).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
{
"x": [1, 2, 3, 4, 5, 6],
"y": [2, 3, 5, 4, 6, 7],
"groups": ["A", "A", "A", "B", "B", "B"],
"predictX": 7,
"otherPredictor": [2, 4, 5, 8, 9, 13]
}Call#
spearmanRankCorrelationAndKendallTau(input)Returns#
object with 2 fields: spearman, kendallTauA
{
"spearman": 0.9428571428571428,
"kendallTauA": 0.8666666666666667
}Diagrams#
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- Linear Least Squares Regression — NIST/SEMATECH e-Handbook
- Correlation — NIST/SEMATECH e-Handbook
- Historical-example decision