Skip to main content

auc

Function auc 

pub fn auc(scored: &[(f64, bool)]) -> Option<f64>
Expand description

Area under the ROC curve for binary labels ranked by score, computed via the Mann-Whitney U statistic with midpoint tie handling. None when either class is empty.

NaN scores are accepted, never panic, and rank deterministically at an extreme of the ordering (IEEE-754 total order via f64::total_cmp) — callers wanting NaN rejected must filter beforehand.

§Algorithm

Sort all (score, label) pairs ascending by score and assign each a 1-based rank; a run of tied scores shares the average of the ranks it would otherwise occupy (the standard mid-rank tie-breaking rule for the Mann-Whitney U statistic). Summing the ranks of the positive-labeled rows and subtracting n_pos * (n_pos + 1) / 2 (the minimum possible sum, reached when every positive ranks below every negative) yields U, which counts — across every positive/negative pair — how many pairs rank the positive above the negative (a tie contributing one half). Normalizing by n_pos * n_neg gives the AUC: the probability a randomly chosen positive scores higher than a randomly chosen negative.

Comparisons use f64::total_cmp rather than ==/< so scores sort into a well-defined total order (and ties are detected via Ordering::Equal) without tripping over partial-ordering edge cases.