Fairness
How a model can treat groups of people differently, how to measure the gaps and which mitigations exist.
Lesson 64 of 6614 minIntermediate
You will learn to
- Define group fairness metrics: selection rate, true positive rate and false positive rate by group
- Find where bias enters: data, labels, features and feedback
- Describe mitigation options and their trade-offs
Before this lesson
Intuition
Imagine a school that grades a maths test written in a language half the class speaks as a second language. The average score looks fine, but one group is systematically held back by something other than maths. A model can do the same to people. The overall accuracy is good, yet one group gets worse decisions.
Fairness in machine learning means checking whether a model's decisions or errors differ in a harmful way between groups, usually groups defined by a sensitive attribute such as age, gender, ethnicity, disability or region.
Bias enters through several doors:
- Data. If a group is under-represented, the model sees few examples of it and learns it worse.
- Labels. If past decisions were biased (for example, who got hired or who got a loan), a model trained to imitate them learns that bias. The label is a record of human choices, not of truth.
- Features. Columns can act as proxies for a sensitive attribute: a postcode can stand in for ethnicity, a first name for gender.
- Feedback. A model's decisions change the world that produces its next training data. If only approved applicants are observed repaying, the model never learns about those it rejected.
No tool can tell you what is fair. What you can do is measure differences, discuss them with the people affected, and decide on a policy.
When a gap is found, there are three families of mitigation. Before training: collect more data for the weak group, fix labels, or reweight rows. During training: add a fairness constraint or penalty. After training: use different decision thresholds per group, or send uncertain cases to a person. Each one trades something: accuracy, simplicity, or the legal and ethical question of treating groups differently on purpose. Often the most effective step is not technical: improve how the data is collected.
Picture it
The math
For a binary decision, count for each group the outcomes of the confusion matrix: true positives TP, false positives FP, false negatives FN and true negatives TN. Three group-level rates cover most discussions.
The true positive rate (TPR) is the share of the people who truly deserve the positive outcome who receive it. The false positive rate (FPR) is the share of those who do not deserve it who receive it anyway.
Common criteria compare these rates between groups and :
- Demographic parity: equal selection rates. The gap is , or the ratio . A ratio below 0.8 is a widely used warning level (the "four-fifths rule" from employment practice), but it is a screening rule of thumb, not a legal or statistical proof.
- Equal opportunity: equal TPR.
- Equalised odds: equal TPR and equal FPR.
- Predictive parity: equal precision, .
When the groups really differ in how often the outcome occurs (their base rates), it is generally impossible to satisfy all of these at once, except for a perfect classifier. Choosing a criterion is a decision about values and context, not a technical detail.
Worked example
A model screens 1,000 applicants. The numbers are illustrative.
| Group | Applicants | Truly positive | TP | FN | FP | TN |
|---|---|---|---|---|---|---|
| A | 600 | 300 | 240 | 60 | 45 | 255 |
| B | 400 | 120 | 72 | 48 | 40 | 240 |
Overall accuracy is (240 + 255 + 72 + 240) / 1,000 = 80.7%, which looks fine. Now by group:
| Metric | Group A | Group B |
|---|---|---|
| Selection rate | 285 / 600 = 47.5% | 112 / 400 = 28.0% |
| TPR | 240 / 300 = 80.0% | 72 / 120 = 60.0% |
| FPR | 45 / 300 = 15.0% | 40 / 280 = 14.3% |
| Precision | 240 / 285 = 84.2% | 72 / 112 = 64.3% |
| Accuracy | 495 / 600 = 82.5% | 312 / 400 = 78.0% |
Reading it:
- The selection ratio is 0.28 / 0.475 = 0.59, below the 0.8 warning level.
- Part of the gap is the base rate: 50% of group A and 30% of group B are truly positive. Demographic parity would ask for equal selection anyway, and that is a policy choice.
- The TPR gap is 20 points. Among people who deserve the positive outcome, 80% of group A get it, against 60% of group B. That is a gap in opportunity that the base rate does not explain.
- The FPR is almost equal, so the model does not wrongly approve one group much more than the other.
A single accuracy number hid all of this. The first step is to look.
How mlflow does it
mlflow does not compute fairness metrics for you, but it gives you the building block: Cohort metrics. On a model candidate, the Validation tab lets you pick a column in Slice by column. The held-out rows are grouped by that column (numeric columns are cut into quartiles) and each group shows its number of rows, n, and its metrics: accuracy, F1, precision and recall for classification, or R2, MAE and RMSE for regression. The groups are sorted by size.
Two cautions. First, precision, recall and F1 there are weighted averages over the classes, so they describe overall quality in each group, not the true positive rate of the positive class. For selection rate or TPR as in the example, download the predictions and count by group. Second, you can only slice by a column that is in the data. If you removed the sensitive attribute from training, keep it in a separate table so you can still audit by group.
Common mistakes
- Reporting only overall accuracy. Averages hide the groups that suffer. Always break results down by group.
- Dropping the sensitive column and calling it done. Proxies remain, and you lose the ability to measure the gap. Keep it for auditing.
- Slicing into tiny groups. A group of 12 rows gives a noisy rate. Show the group size and be careful with conclusions.
- Picking one fairness metric without saying why. State which criterion you chose, the reason, and what it trades off.
- Treating mitigation as free. Reweighting data, adjusting thresholds per group, or post-processing can reduce one gap while raising another error or lowering accuracy. Measure again after each change, and involve the people who own the decision.
Try it in mlflow
Compare model quality across groups
- Open Runs, click a classification run and open a model candidate.
- Open the Validation tab and find Cohort metrics.
- Under Slice by column, pick a column that describes groups (for example a region or a customer type).
- Compare n, accuracy, F1, precision and recall across the groups. Which group has the weakest numbers, and how many rows does it have?