Skip to content
Back to skills

Kendall Tau Ordering Metric

ASecurity

Evaluates predicted sequence ordering quality using Kendall Tau correlation via efficient O(n log n) inversion counting.

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
code-qualitypython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill kendall-tau-ordering-metric --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Kendall Tau Ordering Metric?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Kendall Tau Ordering Metric
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-kendall-tau-ordering-metric/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-kendall-tau-ordering-metric)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-kendall-tau-ordering-metric
description: >
  Evaluates predicted sequence ordering quality using Kendall Tau correlation via efficient O(n log n) inversion counting.
---
# Kendall Tau Ordering Metric

## Overview

For tasks that predict the order of items (cell ordering, passage ranking, document sorting), you need a metric that measures how close the predicted permutation is to the ground truth. Kendall Tau counts the number of pairwise inversions (swapped pairs) and normalizes to [-1, 1]. A score of 1 means perfect order, 0 is random, -1 is fully reversed. Uses bisect-based inversion counting for O(n log n) per sample.

## Quick Start

```python
from bisect import bisect

def count_inversions(a):
    inversions = 0
    sorted_so_far = []
    for i, u in enumerate(a):
        j = bisect(sorted_so_far, u)
        inversions += i - j
        sorted_so_far.insert(j, u)
    return inversions

def kendall_tau(ground_truth, predictions):
    total_inversions = 0
    total_2max = 0
    for gt, pred in zip(ground_truth, predictions):
        ranks = [gt.index(x) for x in pred]
        total_inversions += count_inversions(ranks)
        n = len(gt)
        total_2max += n * (n - 1)
    return 1 - 4 * total_inversions / total_2max
```

## Workflow

1. For each sample, map predicted order to ground-truth rank indices
2. Count pairwise inversions in the rank sequence using bisect insertion
3. Normalize: `1 - 4 * inversions / (n * (n - 1))`
4. Average across all samples

## Key Decisions

- **vs Spearman**: Kendall Tau is more robust to outliers; Spearman is smoother
- **Complexity**: O(n log n) via bisect, not O(n^2) naive counting
- **Ties**: This implementation assumes no ties; use scipy for tie-corrected version
- **Alternative**: `scipy.stats.kendalltau` for single pairs, but this batches efficiently

## References

- [Getting Started with AI4Code](https://www.kaggle.com/code/ryanholbrook/getting-started-with-ai4code)
- [AI4Code Pytorch DistilBert Baseline](https://www.kaggle.com/code/aerdem4/ai4code-pytorch-distilbert-baseline)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.