fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Spearman Rank Correlation and Kendall Tau

Install and import#

bash
npm install fintech-algorithms
ts
import { 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#

NameTypeNotes
inputD00InputOne 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#

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#

Spearman Rank Correlation and Kendall Tau — article hero
Spearman Rank Correlation and Kendall Tau — calculation ledger
Spearman Rank Correlation and Kendall Tau — concept anatomy
Spearman Rank Correlation and Kendall Tau — failure boundary
Spearman Rank Correlation and Kendall Tau — method map
Spearman Rank Correlation and Kendall Tau — scenario contrast

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.

Read the article →

References#

The rest of the Dependence, Regression, and Model Foundations family#