Topic 4 of 5 · 5 min read
Testing a system for unfair outcomes
Goal: Design a subgroup or name-swap test for an AI screening tool and read its results without over- or under-claiming.
To find unfair outcomes, compare results for different groups on matched cases, with enough cases to tell a real gap from chance.
Check yourself · 1 of 3
A model is 92% accurate overall. What is the best next step to check for unfair outcomes?
Why it matters
An overall score hides who the system fails. A model can be 90% accurate and still fail one group much more often than another. You only see this by splitting the results.
Two kinds of test
- Subgroup metrics. Take a test set where you know the right answer and the group of each case. Compute the same measure for each group: selection rate, true positive rate, false positive rate. Compare them. Tools such as Fairlearn do this per group.
- Counterfactual or name-swap tests. Take one case and change only the sensitive signal: the name, the pronoun, the dialect. Run both and compare. Because everything else is identical, a difference is caused by the change. This works on tools you cannot look inside, including a hosted chatbot.
Steps
- Write down which groups and which metric, before running anything.
- Build or collect an audit set: real or realistic cases, labelled, with each group represented.
- Run the system, record the output for every case, and repeat runs where the output is random.
- Compute the metric per group and the gap.
- Check whether the gap is bigger than noise. With 10 cases per group, a 20-point gap can occur by chance. Report the sample size and a confidence interval or repeat the test with more cases.
- Report what you found and what you did not test.
Before and after
Weak: "We tried five CVs and it looked fine."
Better: "We built 200 matched pairs of CVs that differ only in the name. The model's score was lower for the second name in 31 pairs and higher in 12, the rest equal. That gap is large enough to repeat with 500 pairs before we ship."
What goes wrong
- Testing on too few cases and claiming there is no bias. A small test cannot show absence.
- Testing only the groups you thought of. Check combinations too, such as older women or non-native speakers from one region.
- Over-claiming from a small gap: "the model is racist" from three examples.
- Testing once. Prompts, models and data change, so the test is rerun on every change.
If you cannot collect group labels, a name-swap or proxy-based test can still show a problem, and you should say that it is an estimate.
# Fairlearn: compare a metric across groups
from fairlearn.metrics import MetricFrame, selection_rate
mf = MetricFrame(metrics=selection_rate, y_true=y, y_pred=pred, sensitive_features=group)
print(mf.by_group)
Examples
Reading a subgroup table
| Group | Cases | Qualified | Qualified who passed |
|---|---|---|---|
| Group X | 400 | 120 | 96 (80%) |
| Group Y | 380 | 114 | 68 (60%) |
The true positive rate is 80% against 60%, a 20-point gap on over 100 qualified cases each. That is large enough to act on. A 20-point gap with 10 qualified cases per group would not be.
A name-swap test written as a script
pairs = [(cv.replace(NAME_A, 'Emily Walsh'), cv.replace(NAME_A, 'Lakisha Washington')) for cv in cvs]
lower = sum(score(b) < score(a) for a, b in pairs)
higher = sum(score(b) > score(a) for a, b in pairs)
print(lower, higher, len(pairs))
If lower and higher are close, the name does not matter. If lower is far greater, it does.
Key points
- Split results by group, because overall accuracy hides who the errors fall on.
- A name-swap test changes only the sensitive signal, so any difference is caused by it.
- Decide the groups and the metric before running the test.
- A small sample cannot prove there is no bias or that there is a large gap.
- Repeat the test after every change to the model, prompt or data.
Common mistakes
- Reporting only overall accuracy.
- Declaring a system fair after a handful of examples.
- Choosing the groups and metric after seeing the results.
- Testing once and never again after the prompt or model changes.
Check yourself · 2 of 3
In a name-swap test, why must everything except the name stay identical?
Check yourself · 3 of 3
A team tests 8 candidates per group, sees no difference and writes 'no bias found'. What is wrong?
Practice items, not counted towards your mastery.
Start here
- Fairness and Machine Learning (Barocas, Hardt, Narayanan) (opens in a new tab)BookFree
Free textbook; the chapters on classification and legal background cover every definition in this skill.
- Towards a Standard for Identifying and Managing Bias in AI (NIST SP 1270) (opens in a new tab)PaperFree
Splits AI bias into statistical, human and systemic sources, which is the map used in the first topic.
- Machine Learning Crash Course: Fairness (Google) (opens in a new tab)CourseFree
Short lessons on bias types, how to evaluate for bias and fairness metrics, with worked numbers.
More for this topic
- Fairlearn: open-source fairness assessment toolkit (opens in a new tab)ToolFree
Python library that computes metrics per subgroup and offers mitigation methods.