Practice in Lab: Treat tied ranking scores explicitly in ROC AUC →
Treat tied ranking scores explicitly in ROC AUC
ROC AUC is the probability that a randomly selected positive example receives a higher score than a negative example, with half credit for a tie. This interpretation yields a small exact reference implementation that is useful for testing a faster ranking implementation.
AUC evaluates ranking, not probability calibration or the cost of one operating threshold. With no positive or no negative examples the pairwise probability is undefined. Returning zero would confuse an impossible evaluation with a perfectly reversed ranking. Report the population and class counts alongside the metric.
positive=[.8,.5]
negative=[.5,.1]
credits=[(p>n)+.5*(p==n) for p in positive for n in negative]
print(sum(credits)/len(credits))Review the design
The quadratic reference is transparent for tiny evaluation fixtures. Large datasets should use a tie-aware rank algorithm. A high AUC can coexist with poor calibration or unacceptable errors at the chosen threshold.
Your task
Implement roc_auc(labels,scores) for equal nonempty lists, binary integer labels and finite scores. Compute exact positive-negative pair credit, half for ties. Reject single-class data.
The explanation opens here once every check passes.