Sub topic 4
Measuring Outcomes and Errors per Group
This chapter assumes you know the confusion matrix (TP, FP, TN, FN), precision, recall, and the false positive rate. It also assumes basic NumPy and a train/test split. Every group fairness metric in this tutorial is built from the same few per-group numbers, so we compute them first.
The four per-group rates
Pick one group at a time and keep only its rows. Then compute these rates on that subset:
| Rate | Definition | Question it answers |
|---|---|---|
| Selection rate | P(prediction = 1) | How often does the model give this group the positive outcome? |
| True positive rate (TPR) | P(prediction = 1 given label = 1) | Of the people who deserve a positive, how many get one? |
| False positive rate (FPR) | P(prediction = 1 given label = 0) | Of the people who do not, how many get one anyway? |
| Precision | P(label = 1 given prediction = 1) | When the model says yes, how often is it right? |
Selection rate ignores the labels. TPR and FPR condition on the true label. Precision conditions on the prediction. That difference in conditioning is why later chapters find that the criteria disagree.
Computing the rates
The function below loops over the unique group values and returns a dictionary of rates. It returns nan when a denominator is empty, for example when a group has no positive labels. A silent zero there would look like a real result.
import numpy as np
def group_rates(y_true, y_pred, group):
"""Per-group selection rate, TPR, FPR, and precision for binary labels."""
y_true = np.asarray(y_true)
y_pred = np.asarray(y_pred)
group = np.asarray(group)
out = {}
for g in np.unique(group):
m = group == g
yt, yp = y_true[m], y_pred[m]
out[g] = {
"n": int(m.sum()),
"selection_rate": yp.mean(),
"tpr": yp[yt == 1].mean() if (yt == 1).any() else float("nan"),
"fpr": yp[yt == 0].mean() if (yt == 0).any() else float("nan"),
"precision": yt[yp == 1].mean() if (yp == 1).any() else float("nan"),
}
return out
y_true = [1, 1, 0, 0, 1, 0, 1, 0, 0, 1]
y_pred = [1, 0, 0, 1, 1, 0, 1, 0, 0, 0]
group = ["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"]
for g, r in group_rates(y_true, y_pred, group).items():
print(g, {k: round(float(v), 2) for k, v in r.items()})Running it prints:
A {'n': 5, 'selection_rate': 0.6, 'tpr': 0.67, 'fpr': 0.5, 'precision': 0.67}
B {'n': 5, 'selection_rate': 0.2, 'tpr': 0.5, 'fpr': 0.0, 'precision': 1.0}Group A is selected three times as often as group B. B's precision is 1.0, but it rests on a single positive prediction.
Turning rates into gaps
Each criterion compares one rate across groups. With two groups, the gap is an absolute difference:
rates = group_rates(y_true, y_pred, group)
a, b = rates["A"], rates["B"]
demographic_parity_gap = abs(a["selection_rate"] - b["selection_rate"])
equal_opportunity_gap = abs(a["tpr"] - b["tpr"])
equalized_odds_gap = max(abs(a["tpr"] - b["tpr"]), abs(a["fpr"] - b["fpr"]))
predictive_parity_gap = abs(a["precision"] - b["precision"])The next chapters explain each criterion. For now, note that all four gaps come from the same table of rates.
Common errors
- Computing rates on training data. Use held-out data. A model can look fair on data it has memorized.
- Treating
nanas zero. An empty denominator means the rate is undefined, not low. - Ignoring group size. The
nfield matters. A rate from five people is not comparable to one from five thousand. The chapter on uncertainty returns to this. - Mixing up the conditioning. TPR divides by actual positives. Precision divides by predicted positives. Using the wrong denominator gives a plausible but wrong number.
Exercise
In the example output, group B has a precision of 1.0 and group A has 0.67. Why should you not conclude that the model is more accurate for B?
Show answer
B's precision is based on one positive prediction out of five people, so it has very little data behind it. The difference could easily be chance. Check the group size and the count of predicted positives before comparing, and use confidence intervals for small groups.