ROC-AUC from scratchMedium

ROC-AUC from scratch

Background

The ROC curve plots the true-positive rate against the false-positive rate as the decision threshold sweeps from high to low; the AUC (area under it) is a single, threshold-independent score for a binary classifier's ranking quality. AUC =1= 1 is perfect ranking, 0.50.5 is random, and it equals the probability that a randomly chosen positive is scored above a randomly chosen negative.

Problem statement

Implement roc_auc(scores, labels) returning the area under the ROC curve via the equivalent rank/pairwise definition (the Mann-Whitney U statistic): over all positive-negative pairs, count how often the positive outscores the negative, with ties counting a half:

AUC=1PNiPjN[1(si>sj)+121(si=sj)]\text{AUC} = \frac{1}{|P|\,|N|}\sum_{i \in P}\sum_{j \in N}\Big[\mathbb{1}(s_i > s_j) + \tfrac{1}{2}\,\mathbb{1}(s_i = s_j)\Big]

Input

  • scores — array-like of float: predicted scores/probabilities (higher = more positive).
  • labels — array-like of {0, 1}: the true binary labels.

Output

Returns a float in [0,1][0, 1].

Examples

Example 1

Input:  scores = [0.1, 0.4, 0.35, 0.8], labels = [0, 0, 1, 1]
Output: 0.75

Explanation: positives score {0.35,0.8}\{0.35, 0.8\} and negatives {0.1,0.4}\{0.1, 0.4\}. Of the 4 positive-negative pairs, 3 have the positive ranked above the negative (only 0.35<0.40.35 < 0.4 is out of order), so AUC=3/4=0.75\text{AUC} = 3/4 = 0.75.

Constraints

  • AUC is the fraction of positive-negative pairs ordered correctly, with ties counting 0.50.5.
  • Requires at least one positive and one negative; the result lies in [0,1][0, 1].
  • Equivalent to integrating the ROC curve, but the pairwise form avoids threshold bookkeeping.

Notes

  • AUC depends only on the ordering of scores, not their calibration — any monotonic rescaling leaves it unchanged.
  • A perfect classifier scores every positive above every negative (AUC 1); reversing the scores gives 0; random scores give 0.5 in expectation.
Python
Loading...

This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Reference example: AUC = 0.75
  • Perfect ranking gives AUC 1.0
  • Reversed ranking gives AUC 0.0
  • All-tie scores give AUC 0.5