Compute ROC AUC from Mann-Whitney U ranks without sklearn helpers

Quick Overview

Implement ROC AUC for positive-class probabilities using the Mann-Whitney U rank identity, without sklearn's AUC or ROC functions, and match roc_auc_score on tied scores. It tests what the rank sum counts, how average ranks handle ties, and the complexity of rank-based metrics.

Compute ROC AUC from Mann-Whitney U ranks without sklearn helpers

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `auc_from_ranks(y_true, y_proba)` to compute the ROC AUC of positive-class probabilities via the Mann-Whitney U identity, without calling `roc_auc_score`, `roc_curve` or `auc`. With `r = scipy.stats.rankdata(y_proba)`, `n1 = y_true.sum()` and `n0 = len(y_true) - n1`, AUC is `(r[y_true == 1].sum() - n1 * (n1 + 1) / 2) / (n1 * n0)`. - Compute AUC from ranks only, with no sklearn AUC or ROC helper. - Use `rankdata` (average ranks) so tied probabilities are handled exactly. - Return a Python `float`. - The result must match `roc_auc_score` via `assert_allclose(rtol=1e-9)` on a fixed input and on an input with tied probabilities. For example, with one tie between a positive and a negative: ```python y_true = np.array([0, 1, 0, 1, 1, 0]) y_proba = np.array([0.2, 0.6, 0.6, 0.9, 0.4, 0.1]) auc_from_ranks(y_true, y_proba) # -> 0.8333333333... (matches roc_auc_score) ``` ```hint What the rank sum counts Each positive's rank counts the examples scored at or below it. Ask which of those are negatives and which are other positives. ``` ```hint Ties Decide how much a positive and a negative with the same score should contribute to the AUC, then check which ranking method gives that contribution. ``` ### Constraints & Assumptions - `y_true` is binary (0/1) with at least one positive and one negative. - `y_true` and `y_proba` have the same length, and `y_proba` may contain tied values. ### What a Strong Answer Covers - A correct implementation of the rank identity with no sklearn AUC or ROC helper. - Tie handling through average ranks, and why it agrees with `roc_auc_score`. - Why subtracting `n1 * (n1 + 1) / 2` from the positive rank sum leaves the count of positive-over-negative wins. - Time and space complexity compared with a naive pairwise count. ### Follow-up Questions - Why does AUC stay fixed under a monotone transform while Brier score and accuracy-at-0.5 change? - What is the time complexity, and how would you scale to millions of rows? - How would you put a confidence interval on this AUC?

Overview: Implement ROC AUC for positive-class probabilities using the Mann-Whitney U rank identity, without sklearn's AUC or ROC functions, and match roc_auc_score on tied scores. It tests what the rank sum counts, how average ranks handle ties, and the complexity of rank-based metrics.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement auc_from_ranks(y_true, y_proba) to compute the ROC AUC of positive-class probabilities via the Mann-Whitney U identity, without calling roc_auc_score, roc_curve or auc. With r = scipy.stats.rankdata(y_proba), n1 = y_true.sum() and n0 = len(y_true) - n1, AUC is (r[y_true == 1].sum() - n1 * (n1 + 1) / 2) / (n1 * n0).

  • Compute AUC from ranks only, with no sklearn AUC or ROC helper.
  • Use rankdata (average ranks) so tied probabilities are handled exactly.
  • Return a Python float .
  • The result must match roc_auc_score via assert_allclose(rtol=1e-9) on a fixed input and on an input with tied probabilities.

For example, with one tie between a positive and a negative:

y_true = np.array([0, 1, 0, 1, 1, 0])
y_proba = np.array([0.2, 0.6, 0.6, 0.9, 0.4, 0.1])
auc_from_ranks(y_true, y_proba)  # -> 0.8333333333...  (matches roc_auc_score)

Constraints & Assumptions

  • y_true is binary (0/1) with at least one positive and one negative.
  • y_true and y_proba have the same length, and y_proba may contain tied values.

What a Strong Answer Covers Guidance

  • A correct implementation of the rank identity with no sklearn AUC or ROC helper.
  • Tie handling through average ranks, and why it agrees with roc_auc_score .
  • Why subtracting n1 * (n1 + 1) / 2 from the positive rank sum leaves the count of positive-over-negative wins.
  • Time and space complexity compared with a naive pairwise count.

Follow-up Questions Guidance

  • Why does AUC stay fixed under a monotone transform while Brier score and accuracy-at-0.5 change?
  • What is the time complexity, and how would you scale to millions of rows?
  • How would you put a confidence interval on this AUC?