Sub topic 12
Running a fairness audit
This chapter assumes you can compute a confusion matrix, know the group metrics from earlier chapters (selection rate, TPR, FPR, precision), and can write basic NumPy. An audit combines those pieces into a repeatable procedure. You run it before release and again whenever the model, the data, or the population changes.
The audit steps
- Define the harm and context. Decide who is affected and what a wrong prediction costs. If a false negative denies someone a loan, equal opportunity (equal TPR) matters more than demographic parity.
- Choose groups. List the sensitive attributes and their intersections. Check that each group has enough rows to measure.
- Choose metrics before looking at results. Pick one or two criteria that fit the harm and write down the gap you will tolerate. Choosing after you see the numbers lets you pick whichever criterion the model happens to pass.
- Measure with uncertainty. Report each gap with a confidence interval, not a single number.
- Decide and mitigate. If a gap is large and its interval excludes zero, consider mitigation, then re-run steps 4 and 5.
- Document. Record what you measured, what you found, and what you decided.
Measuring a gap with a bootstrap interval
A gap of 0.04 means little if one group has 40 rows. A bootstrap resamples each group with replacement and recomputes the gap many times. The spread of those gaps gives an interval.
import numpy as np
rng = np.random.default_rng(0)
n = 2000
group = rng.choice(["A", "B"], size=n, p=[0.7, 0.3])
y_true = rng.binomial(1, 0.35, size=n)
# Simulated model: it finds fewer true positives in group B
p_pos = np.where(group == "A", 0.80, 0.65)
y_pred = np.where(y_true == 1, rng.binomial(1, p_pos), rng.binomial(1, 0.15, size=n))
def tpr(yt, yp):
return yp[yt == 1].mean()
def tpr_gap_ci(y_true, y_pred, group, n_boot=1000, seed=0):
r = np.random.default_rng(seed)
ia = np.where(group == "A")[0]
ib = np.where(group == "B")[0]
gaps = []
for _ in range(n_boot):
ra = r.choice(ia, len(ia))
rb = r.choice(ib, len(ib))
gaps.append(tpr(y_true[ra], y_pred[ra]) - tpr(y_true[rb], y_pred[rb]))
return np.percentile(gaps, [2.5, 97.5])
ma, mb = group == "A", group == "B"
gap = tpr(y_true[ma], y_pred[ma]) - tpr(y_true[mb], y_pred[mb])
low, high = tpr_gap_ci(y_true, y_pred, group)
print(f"TPR gap A-B: {gap:.3f}, 95% CI [{low:.3f}, {high:.3f}]")If the interval excludes zero, the gap is unlikely to be sampling noise. If it includes zero, you cannot tell. Either way, say which in your report.
Common errors
- Auditing only on training data. Use held-out data that resembles production.
- Reporting only overall accuracy. Accuracy can look fine while one group's TPR is far lower.
- Ignoring tiny groups. A missing or
nanrate usually means a group has no positives. Report it as "not measurable" instead of dropping it. - Treating one passed metric as proof of fairness. Criteria conflict, so state which one you chose and why.
- Auditing once. Mitigation changes the model, so re-run the full audit afterward.
What to record
Keep a short record with each audit:
| Item | Example |
|---|---|
| Model and data version | `credit-v3`, test set from March |
| Groups and sample sizes | A: 1,400; B: 600 |
| Metrics and thresholds | TPR gap under 0.05 |
| Results with intervals | 0.15, CI [0.09, 0.21] |
| Decision and follow-up | Apply threshold post-processing, re-audit |
Exercise
An audit finds a TPR gap of 0.06 between two groups, with a 95% bootstrap interval of [-0.05, 0.17]. Your tolerance is 0.05. Do you conclude the model violates the tolerance? What would you do next?
Show answer
No. The interval includes zero and values below 0.05, so the data cannot separate a real gap from noise. Report the result as inconclusive. Collect more data for the smaller group, or use a larger evaluation set, then re-run the audit. Do not apply mitigation based on this result alone.