Metrics · Hard
Grouped AUC
Implement Grouped AUC with production-minded edge case handling.
Task
Implement grouped_auc(labels: list[int], scores: list[float], groups: list[int]) -> float. Compute AUC independently inside each valid group, then return the unweighted mean across groups that contain both classes.
Requirements
- Treat labels as binary values where 1 is positive and 0 is negative.
- Within each group, compare every positive score with every negative score.
- Award 1 for a correctly ordered pair, 0.5 for a tied pair, and 0 otherwise.
- Skip groups that do not contain at least one positive and one negative example.
- Give every valid group equal weight, regardless of its number of rows or pairs.
- Return 0.0 when there are no valid groups, and do not mutate the inputs.
Example
grouped_auc(
[1, 0, 1, 0],
[0.9, 0.2, 0.4, 0.4],
[10, 10, 20, 20],
)
# 0.75