ROC-AUC From Scratch With Tied Scores

Quick Overview

Implement ROC-AUC from scratch in NumPy by sweeping thresholds over the distinct scores and integrating the curve with the trapezoidal rule. Tests the handling of tied scores, boolean labels and single-class input, and the link between AUC and pairwise ranking.

ROC-AUC From Scratch With Tied Scores

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `compute_roc_auc(y_true, y_scores)`, which returns the ROC-AUC of a binary classifier as a Python `float`. Sweep thresholds over the distinct scores in descending order, grouping tied scores into a single threshold so that a tied block produces one diagonal step on the curve. Start the curve at (0, 0) and integrate it with the trapezoidal rule. Example: ```python compute_roc_auc([1, 0, 1, 0], [0.5, 0.5, 0.9, 0.1]) # 0.875: the tied positive/negative pair at 0.5 counts as half compute_roc_auc([0, 1, 1, 0, 1], [0.42, 0.42, 0.42, 0.42, 0.42]) # 0.5: every score is tied ``` ```hint Where the curve can turn After sorting by score, a ROC point can only sit at the end of a block of equal scores. Find the last index of each block and read the cumulative counts there. ``` ```hint Check the tied block by hand Compute the trapezoid area over one tied block with a positives and b negatives, and compare it with what pairwise counting would give those a times b pairs. ``` ### Constraints & Assumptions - Inputs may be NumPy arrays or Python lists. Labels may be `0/1` or booleans: `1` or `True` is positive, everything else is negative. - A length mismatch between `y_true` and `y_scores` raises `ValueError`. - If only one class is present, return `float('nan')`. - Whenever both classes are present, the result must match `sklearn.metrics.roc_auc_score` to `rtol=1e-9`. - Do not use `sklearn` in the solution. `numpy` is available. ### What a Strong Answer Covers - Label handling for integer and boolean inputs, the length check, and the one-class case. - A descending sort with exactly one ROC point per distinct score. - TPR and FPR at each threshold, the starting point at (0, 0), and the trapezoidal sum. - An argument for why the diagonal over a tied block gives the correct credit to tied pairs. - Time and space complexity. ### Follow-up Questions - How does ROC-AUC relate to the Mann-Whitney U statistic? - How would you support sample weights? - When is ROC-AUC misleading, and what would you use instead?

Overview: Implement ROC-AUC from scratch in NumPy by sweeping thresholds over the distinct scores and integrating the curve with the trapezoidal rule. Tests the handling of tied scores, boolean labels and single-class input, and the link between AUC and pairwise ranking.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement compute_roc_auc(y_true, y_scores), which returns the ROC-AUC of a binary classifier as a Python float. Sweep thresholds over the distinct scores in descending order, grouping tied scores into a single threshold so that a tied block produces one diagonal step on the curve. Start the curve at (0, 0) and integrate it with the trapezoidal rule.

Example:

compute_roc_auc([1, 0, 1, 0], [0.5, 0.5, 0.9, 0.1])
# 0.875: the tied positive/negative pair at 0.5 counts as half

compute_roc_auc([0, 1, 1, 0, 1], [0.42, 0.42, 0.42, 0.42, 0.42])
# 0.5: every score is tied

Constraints & Assumptions

  • Inputs may be NumPy arrays or Python lists. Labels may be 0/1 or booleans: 1 or True is positive, everything else is negative.
  • A length mismatch between y_true and y_scores raises ValueError .
  • If only one class is present, return float('nan') .
  • Whenever both classes are present, the result must match sklearn.metrics.roc_auc_score to rtol=1e-9 .
  • Do not use sklearn in the solution. numpy is available.

What a Strong Answer Covers Guidance

  • Label handling for integer and boolean inputs, the length check, and the one-class case.
  • A descending sort with exactly one ROC point per distinct score.
  • TPR and FPR at each threshold, the starting point at (0, 0), and the trapezoidal sum.
  • An argument for why the diagonal over a tied block gives the correct credit to tied pairs.
  • Time and space complexity.

Follow-up Questions Guidance

  • How does ROC-AUC relate to the Mann-Whitney U statistic?
  • How would you support sample weights?
  • When is ROC-AUC misleading, and what would you use instead?