Implement Demographic Parity and Equalized Odds Difference Metrics with Sample Weights
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
You are given binary labels `y_true`, binary predictions `y_pred`, a group label per sample `sensitive`, and an optional `sample_weight`. Implement `group_rates`, `demographic_parity_difference` and `equalized_odds_difference`, which measure how a binary classifier's behavior differs across groups.
```python
def group_rates(y_true, y_pred, sensitive, sample_weight=None) -> pd.DataFrame: ...
def demographic_parity_difference(y_pred, sensitive, sample_weight=None) -> float: ...
def equalized_odds_difference(y_true, y_pred, sensitive, sample_weight=None) -> float: ...
```
Requirements:
- `group_rates` returns a `pd.DataFrame` with columns `group`, `count` (unweighted, `int64`), `selection_rate`, `tpr` and `fpr` (float), one row per group in sorted order.
- Rates are weighted (`sample_weight=None` means all weights are 1). A zero denominator gives `NaN`.
- `demographic_parity_difference` is the maximum minus the minimum selection rate across groups.
- `equalized_odds_difference` is the larger of the TPR gap and the FPR gap. Each gap ignores `NaN` groups and is `0.0` if no values remain.
- Both metrics return a Python `float`.
- A length mismatch of `sensitive`, `y_true` or `sample_weight` against `y_pred` raises `ValueError`.
Example:
```python
y_true = [1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0]
y_pred = [1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0]
sens = ["a"]*4 + ["b"]*4 + ["c"]*3
# tpr: a=1.0, b=0.5, c=NaN (no positives) -> gap 0.5
# fpr: a=0.0, b=0.5, c=0.0 -> gap 0.5
equalized_odds_difference(y_true, y_pred, sens) # 0.5
demographic_parity_difference(y_pred, sens) # 0.5 (rates 0.5, 0.5, 0.0)
```
```hint Weighted rates
Each rate is a ratio of two weighted sums over one group's rows. Write down which rows belong in the denominator for selection rate, for TPR and for FPR.
```
```hint Undefined rates
Group c in the example has no positive labels. Check what its TPR becomes in your code and whether it can move the TPR gap.
```
### Constraints & Assumptions
- `y_true` and `y_pred` hold 0/1 values; `sensitive` holds sortable group labels such as strings or integers.
- Inputs may arrive as Python lists, NumPy arrays or pandas Series.
- Sample weights are non-negative.
### What a Strong Answer Covers
- The weighted definitions of selection rate, TPR and FPR per group, with the correct denominator for each.
- How zero denominators and `NaN` rates are handled in the spread, including the single-group and all-negative cases.
- The exact output contract: sorted groups, unweighted `int64` counts, Python `float` return values.
- Length validation that raises `ValueError` before any computation.
- Time complexity in terms of samples and groups, and how to avoid one mask per group when there are many groups.
### Follow-up Questions
- How would you report these as ratios instead of differences?
- How would you handle intersectional groups (for example race and gender together)?
- Small groups make these gaps noisy. How would you quantify uncertainty?
Overview: Implement group fairness metrics for a binary classifier in Python: weighted per-group selection rate, TPR and FPR, then the demographic parity difference and the equalized odds difference. Tests careful handling of sample weights, groups with undefined rates, output types and input validation.