Metrics · Hard
Weighted AUC
Implement Weighted AUC with production-minded edge case handling.
Task
Implement weighted_auc(labels: list[int], scores: list[float], sample_weights: list[float]) -> float. Compute the weighted probability that a positive example scores above a negative example, using the product of their sample weights for each positive-negative pair.
Requirements
- labels, scores, and sample_weights have equal length; labels are binary and sample weights are non-negative.
- For a positive i and negative j, give full credit when scores[i] > scores[j], half credit when the scores are exactly equal, and no credit otherwise.
- Weight each positive-negative comparison by sample_weights[i] * sample_weights[j].
- Divide the credited pair weight by total_positive_weight * total_negative_weight.
- Return 0.0 when the positive total weight or negative total weight is zero, including empty and single-class inputs.
- Zero-weight examples have no effect on either the numerator or denominator.
- Do not mutate the inputs, and do not assume scores are probabilities or already sorted.
Example
weighted_auc(
[1, 0, 1, 0],
[0.5, 0.5, 0.8, 0.5],
[2.0, 3.0, 1.0, 1.0],
)
# 0.6666666667