DeLong Test for Correlated ROC-AUCs
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `delong_roc_test(y_true, score_a, score_b)` for two models scored on the same samples. Return a tuple of Python floats `(auc_a, auc_b, z, p_value)`, where `z = (auc_a - auc_b) / se` uses the DeLong variance and `p_value` is two-sided under a standard normal.
For example:
```python
y_true = [1, 1, 0, 0]
score_a = [0.9, 0.4, 0.5, 0.1]
score_b = [0.8, 0.7, 0.3, 0.6]
delong_roc_test(y_true, score_a, score_b)
# (0.75, 1.0, -0.7071..., 0.4795...) z = -1/sqrt(2)
```
```hint AUC as a pair statistic
AUC is the share of (positive, negative) pairs a model orders correctly, with ties counting half. Its variance can be estimated from how much each single positive and each single negative contributes on average.
```
```hint Shared samples
Both models score the same samples, so their AUC estimates are correlated. The variance of the difference needs a covariance term next to the two variances.
```
```hint Validation pitfalls
Check the labels before converting them, since casting `0.5` to an integer silently turns it into 0. Also think about what `ddof=1` requires from each class.
```
### Constraints & Assumptions
- Inputs may be lists or arrays. Labels are 0/1 or booleans.
- A tied positive/negative pair counts as `0.5`. Covariances of structural components use `ddof=1`.
- If the variance of the difference is at most `1e-15`, equal AUCs give `z = 0.0, p = 1.0`; otherwise `z = ±inf` (the sign of the difference) and `p = 0.0`.
- Raise `ValueError` for mismatched lengths, non-binary labels, or fewer than 2 positives or 2 negatives.
### What a Strong Answer Covers
- AUC written as the Mann-Whitney statistic over (positive, negative) pairs, with ties counted as 0.5.
- The structural components of each model and the 2x2 covariance matrix built from both, with `ddof=1`.
- The variance of the difference including the covariance term, the z statistic and the two-sided p-value.
- The near-zero variance branch and input validation (lengths, binary or boolean labels, at least two of each class).
- The time and memory cost of the pairwise kernel.
### Follow-up Questions
- How would you scale this beyond the O(m·n) pairwise matrix?
- How would you report a confidence interval for the AUC difference?
- How would you compare more than two models at once?
Overview: Implement the DeLong test to compare the ROC AUCs of two models scored on the same samples, returning both AUCs, the z statistic and a two-sided p-value. It tests AUC as a pairwise statistic, the covariance between correlated estimates, tie handling and degenerate variance cases.