Sub topic 9
Uncertainty and small subgroups
This chapter assumes you can compute per-group rates (selection rate, TPR, FPR, precision) as shown earlier, and that you know basic NumPy and pandas. A gap between two groups is an estimate from a sample. Before you call it a fairness problem, check whether it could be noise.
Why small groups mislead
A group's true positive rate is estimated from only the people whose true label is positive. If group B has 400 people and 10% are positive, its TPR rests on 40 people. One extra correct prediction moves the TPR by 2.5 points. A 5-point gap between groups can therefore appear from chance alone.
The sample size that matters depends on the metric:
| Metric | Sample it is computed on |
|---|---|
| Selection rate | All people in the group |
| TPR | Group members with true label 1 |
| FPR | Group members with true label 0 |
| Precision | Group members predicted 1 |
Always report these denominators next to each rate. Rare outcomes make them small even when the group looks large.
Confidence intervals by bootstrap
The bootstrap resamples the data with replacement and recomputes the metric many times. The spread of those results shows how much the metric could vary. This function builds an interval for the TPR gap between two groups.
import numpy as np
def tpr(y_true, y_pred):
pos = y_true == 1
return y_pred[pos].mean() if pos.any() else np.nan
def bootstrap_tpr_gap(y_true, y_pred, group, g1, g2, n_boot=2000, seed=0):
rng = np.random.default_rng(seed)
y_true, y_pred, group = map(np.asarray, (y_true, y_pred, group))
idx1 = np.where(group == g1)[0]
idx2 = np.where(group == g2)[0]
gaps = []
for _ in range(n_boot):
s1 = rng.choice(idx1, size=len(idx1), replace=True)
s2 = rng.choice(idx2, size=len(idx2), replace=True)
gap = tpr(y_true[s1], y_pred[s1]) - tpr(y_true[s2], y_pred[s2])
gaps.append(gap)
gaps = np.array(gaps)
gaps = gaps[~np.isnan(gaps)]
return np.percentile(gaps, [2.5, 97.5])
rng = np.random.default_rng(1)
group = np.array(["A"] * 1000 + ["B"] * 120)
y_true = rng.integers(0, 2, size=len(group))
y_pred = np.where(rng.random(len(group)) < 0.8, y_true, 1 - y_true)
low, high = bootstrap_tpr_gap(y_true, y_pred, group, "A", "B")
print(f"95% interval for TPR gap (A - B): [{low:.3f}, {high:.3f}]")The predictions here have the same accuracy in both groups, so the true gap is zero. Because group B is small, the interval is still wide. If the interval contains 0, the data do not show a real gap. That does not prove the model is fair. It means you cannot tell.
Statistical and practical significance
Two questions are separate.
- Statistical significance asks whether the gap could be chance.
- Practical significance asks whether the gap is big enough to matter in this deployment.
With a very large sample, a 0.5-point gap can be statistically clear and still harmless. With a small sample, a 15-point gap can be unclear and still alarming. Decide your threshold for a meaningful gap before you look at results, based on the harms discussed earlier.
Common errors
- Reading a point estimate as fact. Report the interval, not only the gap.
- Testing many metrics and groups. With 20 comparisons, one will look significant by chance. State which comparisons you planned, or adjust for multiple testing.
- Treating "no significant gap" as "fair". A wide interval means low power.
- Dropping small groups. Hiding them removes the people most likely to be harmed. Report them with a warning.
- Ignoring undefined rates. A group with no positive labels has no TPR. The code above returns
nanand the bootstrap discards those resamples.
What to do with small groups
- Report the group's size and the denominator for each metric.
- Show intervals for every gap.
- Pool data over a longer time window, or collect more samples for that group.
- Mark results as inconclusive rather than passing.
Exercise
Group B has 600 members, but only 30 have a true positive label. The TPR gap between A and B is 8 points, with a bootstrap 95% interval of [-9, +25]. What do you conclude, and what do you do next?
Show answer
The interval contains 0 and is wide, so the result is inconclusive. It does not show a real gap, and it does not show fairness. The TPR rests on only 30 people. Report the gap with its interval and the denominator, then collect more positive-label examples for group B or pool a longer time window before deciding.