Graph connections

Draft

Rand Index

Evaluate clustering by asking whether every item pair is together or apart in both partitions.

concept beginner machine-learningmetricsclustering

Hook problem: Purity missed a split

The fixture has high Purity: 7/8 = 0.875. But the true graph cards are split across clusters A and B, and the true tree cards are split across clusters B and C.

Rand Index changes the question from “what is each cluster’s majority label?” to “what happened to every pair of items?”

Pair questionsEvery unordered pair answers two yes/no questions.
TPsame label, same cluster
FPdifferent labels, same cluster
FNsame label, different clusters
TNdifferent labels, different clusters

First naive idea: keep the cluster-majority score

Purity gives cluster B one point because it contains one graph card and one tree card, so either label is a majority tie.

That hides the pair mistake: Cy and Di were placed together even though their reference labels differ.

Core invention: score pair agreement

For each unordered pair, ask:

  • Are the two items in the same reference class?
  • Are the two items in the same predicted cluster?

This creates pair counts:

  • TP: same label and same cluster.
  • FP: different labels but same cluster.
  • FN: same label but different clusters.
  • TN: different labels and different clusters.
Rand pair countsRI counts both same-same and different-different agreements.
TP3

together in both

FP1

clustered together by mistake

FN4

split apart by mistake

TN20

apart in both

RI23/28 = 0.821

Formal version

For n items, the number of unordered pairs is:

(n2)=n(n1)2\binom{n}{2}=\frac{n(n-1)}{2}

Rand Index is the fraction of pair decisions that agree:

RI=TP+TN(n2)RI=\frac{TP+TN}{\binom{n}{2}}

For the fixture:

RI=3+2028=23280.821RI=\frac{3+20}{28}=\frac{23}{28}\approx 0.821

Interactive preset lab

Clustering metric preset lab

Explanation: One mixed cluster and two split true classes make Purity look high while pair metrics reveal damage.

Purity0.875

7/8

Rand Index0.821

23/28

Adjusted Rand Index0.444

S=3, E=1

Fowlkes-Mallows Index0.567

pair P=0.75, pair R=0.429

TP3
FP1
FN4
TN20

Static no-JS fallback:

Fixture pair counts
TP3
FP1
FN4
TN20
TOTAL28

Correctness intuition

Each pair has exactly one status: TP, FP, FN, or TN. The numerator TP + TN counts exactly the pairs where both partitions make the same together/apart decision.

Limitation: true negatives can dominate

When many classes exist, most pairs may be different-label pairs. A clustering can get many TN agreements without doing a good job recovering the same-label groups.

That is why the next node adjusts for chance agreement.

Preset contrastThe same four metrics react differently to over-splitting and merging.
Fixture

Purity: 0.875

RI: 0.821

ARI: 0.444

FMI: 0.567

Perfect match

Purity: 1

RI: 1

ARI: 1

FMI: 1

Singleton over-split

Purity: 1

RI: 0.75

ARI: 0

FMI: not available

All merged

Purity: 0.375

RI: 0.25

ARI: 0

FMI: 0.5

Implementation sketch

function randIndex(items: { label: string; cluster: string }[]) {
  let tp = 0, fp = 0, fn = 0, tn = 0;

  for (let i = 0; i < items.length; i += 1) {
    for (let j = i + 1; j < items.length; j += 1) {
      const sameLabel = items[i].label === items[j].label;
      const sameCluster = items[i].cluster === items[j].cluster;
      if (sameLabel && sameCluster) tp += 1;
      else if (!sameLabel && sameCluster) fp += 1;
      else if (sameLabel && !sameCluster) fn += 1;
      else tn += 1;
    }
  }

  const total = tp + fp + fn + tn;
  return total === 0 ? null : (tp + tn) / total;
}

Complexity

Direct pair enumeration is O(n^2) time and O(1) extra counting space. If you already have a contingency table, the same counts can be computed from cell combinations without listing every pair.

Common confusions

  • Rand Index uses pair TP/FP/FN/TN; these are not individual classifier examples.
  • TN means “apart in both partitions,” not “the item is negative.”
  • A high RI may still need chance adjustment.

Exercises

  1. Which pair is the fixture’s only FP?
  2. Why are there four FN pairs?
  3. Why does RI include TN while FMI does not?

Graph connections : Rand Index