Sub topic 2
Where bias enters the pipeline
This chapter assumes you know binary classification and the confusion matrix, can read precision, recall, and false positive rate, and can write basic Python with NumPy and scikit-learn. It does not define fairness metrics yet. It covers where unequal outcomes come from, so that later you can choose the right measurement and the right fix.
Bias is a property of the whole pipeline
A model is often blamed for unequal outcomes, but the model only learns from what it is given. Unfairness can enter at every stage from the world that produced the data to the way predictions are used. If you know the stage, you know which check to run and which fix could work. A fix at the wrong stage can leave the harm in place.
The diagram shows the stages this chapter covers.
Historical bias
Historical bias exists in the world before any data is collected. Suppose a company's past hiring favored one group. A perfectly accurate record of those decisions still encodes the favoritism. Better sampling does not remove it, because the data faithfully describes an unfair process.
A model trained to imitate that record learns to reproduce it. High accuracy against historical outcomes can therefore mean the model copies past unfairness well.
Representation and sampling bias
Representation bias appears when the training data does not match the population the model will serve. Common causes include:
- Data collected through one channel, such as a mobile app, that some groups use less.
- Data from one region or time period.
- Records that exist only for people who already had access to a service.
- Groups that are rare in the data, so the model sees few examples of them.
The effect is usually higher error for the underrepresented group. The model fits the majority pattern, and if the minority group follows a different pattern, its error rates rise. Overall accuracy hides this, because the majority dominates the average.
Rarity also affects measurement. A small group gives noisy estimates of its error rates, which a later chapter on uncertainty covers.
Measurement and label bias
Many labels are proxies for the thing you care about. Measurement bias arises when the proxy is recorded differently across groups.
Examples:
- "Arrested" stands in for "committed a crime". If some neighborhoods are policed more, their residents are recorded more often at the same true behavior.
- "Hospital spending" stands in for "health need". If a group faces barriers to care, it spends less at the same level of need.
- "Was promoted" stands in for "performed well". If managers rate groups differently, the label carries that difference.
Label bias is especially damaging because standard evaluation trusts the label. A model can score well against biased labels and still be wrong about the real outcome. Every metric you compute later, including true positive rate, is only as valid as the label it compares against.
A related issue is selective labels. You observe the outcome only for people the past process accepted. In lending, you see repayment only for approved applicants. Training on those rows teaches the model about a filtered population.
Feature bias and proxies
Dropping a sensitive attribute, such as race or sex, does not make a model blind to it. Other features correlate with the attribute and act as proxies. Postal code can stand in for race, and name or purchase history can stand in for sex. A model can rebuild the sensitive signal from these features if it helps predict the label.
This approach is called fairness through unawareness, and it usually fails. It also removes your ability to audit, because you can no longer compute per-group metrics without the attribute. Keep the attribute available for evaluation even if the model never sees it.
Features can also be noisier for some groups. A credit history is thin for people new to a country. A model then has less information about them and errs more.
Aggregation and modeling bias
Modeling bias comes from choices in training:
- One model for all groups. If the relationship between features and label differs by group, a single model fits the average and serves neither well.
- Objective choice. Minimizing overall loss rewards accuracy on the largest group.
- Thresholds. One global threshold on a score can give very different error rates when score distributions differ by group.
- Regularization and capacity. Simpler models may ignore patterns that matter only for a small group.
Evaluation bias
Evaluation bias appears when the test data or the metric does not reflect the people the model will affect. A benchmark with few examples from a group cannot reveal problems for that group. Reporting one aggregate number is itself a source of bias, because it lets a failure for a minority group go unseen.
For this reason the audit in this tutorial reports metrics per group, not only overall.
Deployment and feedback loops
Deployment bias arises when a model is used in a setting or way it was not designed for. A risk score built to predict one outcome may be used to decide something else. A human may also over-trust the score for some groups.
Feedback loops make things worse over time. A model that sends more patrols to one area finds more incidents there. Those incidents become new training data, and the model sends even more patrols. The data now reflects where the model looked, not where events occur.
A simulation: proxy features plus label bias
The following script shows measurement bias and a proxy feature together. The true outcome depends only on one feature x, identically in both groups. In group B, positive outcomes are recorded only 60% of the time. The model trains on the recorded labels, and a proxy feature z correlates with group membership. The model never sees the group column.
import numpy as np
from sklearn.linear_model import LogisticRegression
rng = np.random.default_rng(0)
n = 40000
group = rng.choice(["A", "B"], size=n)
is_b = (group == "B").astype(float)
# Feature that truly drives the outcome, same distribution in both groups.
x = rng.normal(size=n)
y_true = rng.binomial(1, 1 / (1 + np.exp(-2 * x)))
# Measurement bias: true positives in group B are recorded only 60% of the time.
recorded = y_true.copy()
missed = (group == "B") & (y_true == 1) & (rng.random(n) > 0.6)
recorded[missed] = 0
# Proxy feature: correlated with group membership, unrelated to the true outcome.
z = is_b + rng.normal(scale=0.5, size=n)
X = np.column_stack([x, z])
# Train on the biased recorded labels. The group column is not a feature.
model = LogisticRegression().fit(X, recorded)
y_pred = model.predict(X)
# Audit against the true outcome, using the group column only for evaluation.
for g in ["A", "B"]:
m = group == g
selection_rate = y_pred[m].mean()
tpr = y_pred[m & (y_true == 1)].mean()
print(f"group {g}: selection_rate={selection_rate:.3f} tpr_vs_true={tpr:.3f}")Group B gets a lower selection rate and a lower true positive rate than group A, even though the outcome was generated identically for both. The model learned that high z goes with a recorded 0, and z tracks group B. Removing the group column did not help, because z carried the same signal.
Matching the source to the symptom
Metrics alone cannot name the source of a gap, but patterns narrow the search.
| Source | Typical symptom | First check |
|---|---|---|
| Historical bias | Accurate model, unequal outcomes that match past decisions | Compare against an independent outcome or a policy baseline |
| Representation bias | Higher error for a small or missing group | Group counts in train and test data; error rates per group |
| Label bias | Lower recall for a group that the real world suggests should not differ | Audit how the label is recorded for each group |
| Proxy features | Gap persists after removing the sensitive attribute | Predict the attribute from the other features |
| Aggregation or thresholds | Different score distributions by group, same threshold | Compare score distributions and per-group thresholds |
| Evaluation bias | Good aggregate metrics, no per-group reporting | Check test-set coverage for each group |
| Feedback loop | Disparity grows after launch | Track outcomes over time and compare with exploration data |
Limits of this diagnosis
- Several sources usually act together, so expect more than one cause.
- You often cannot observe the true outcome. When you cannot, record that assumption as a limitation instead of treating the label as ground truth.
- Knowing the source does not decide what is fair. That depends on the deployment context and the harm, which the earlier chapter covered, and on the criteria in the chapters that follow.
Exercise
A hospital trains a model on past billing costs to flag patients for extra care. Two groups have the same medical need, but one group historically faced barriers to care and has lower billing costs. The sensitive attribute is not a model feature. The model still flags the group with barriers less often. Name the main source of bias, and explain why removing the sensitive attribute did not help and what you would check first.
Show answer
The main source is label (measurement) bias: cost is a proxy for need, and it is recorded as lower for the group with barriers to care at the same level of need. Removing the sensitive attribute did not help because the model learns from the biased label, and features such as past visits or location can also act as proxies for the group. Check first how the label is built. Compare an independent measure of need, such as diagnoses or lab results, with the cost label for each group, and run the proxy test by predicting group membership from the input features.
Exercise
In the simulation, the model trained on recorded labels shows a lower true positive rate for group B when audited against y_true. Why would an audit against the recorded labels hide this?
Show answer
Group B's recorded labels already have 40% of true positives turned into zeros, and the model learned to match those labels, so its predictions agree with them well. Per-group metrics computed on the recorded labels therefore look similar and healthy. The gap only shows when the audit uses the true outcome, which the recorded label misrepresents.