Bias and Fairness

Updated · 8 min read
mediumPython 3.13aifairnessbiasresponsible-aiscikit-learnmetrics

Bias in AI means a system gives systematically worse results to some group of people, for example by gender, race, age, language or where they live. Fairness is the goal of avoiding that, and it can be measured. Bias usually comes from the data: a model trained on past human decisions learns the prejudices in them, and removing the group column doesn't remove the bias, because other columns can stand in for it.

"AI Bias in Everyday Tools" and "Data Quality and Bias in Data" introduced the problem. This lesson measures it.

Where Bias Comes From

SourceDescription
Biased labelsIt is used to describe training on past decisions that were unfair, such as hiring or loan approvals
Unrepresentative dataSome groups have few examples, so the model learns them poorly
Proxy featuresColumns like zip code or school that are linked to a protected group
Measurement biasThe data measures some groups less accurately, such as photos taken in poor light
Deployment biasA model used for people or places it wasn't built for

Fairness Measures

MeasureDescription
Selection rateIt is used to compare the share of each group that gets the positive outcome
Four-fifths ruleA US hiring guideline: a group's selection rate below 80% of the highest group's is a warning sign
Equal opportunityIt is used to compare the share of qualified people accepted in each group
Error rates by groupFalse positives and false negatives for each group, as in "Evaluating Models: Metrics"

Different measures can conflict: a model can't always satisfy all of them at once, so teams must choose which matter most for the decision and explain why.

Example: A Biased Scholarship Model

This program makes up 4,000 scholarship applications. Both groups have the same spread of grades and essay scores, so a fair process would accept both groups at about the same rate. But past committees were biased: group B needed about 6 more points to be accepted. A model is then trained on those past decisions, without ever being given the group column.

The model learned the old bias; fair labels removed it
Python
import numpy as np
from sklearn.linear_model import LogisticRegression
 
# Made-up scholarship applications. Both groups have the same spread of grades and essay scores.
rng = np.random.default_rng(33)
n = 4000
group = rng.integers(0, 2, n)                              # 0 = group A, 1 = group B
grades = rng.normal(80, 8, n)
essay = rng.normal(70, 10, n)
area = np.where(rng.random(n) < 0.8, group, 1 - group)     # where they live: matches their group 80% of the time
qualified = grades + 0.5 * essay > 117                     # the fair rule the committee meant to use
# Past committees were biased: group B needed about 6 more points to be accepted
past = grades + 0.5 * essay - 6 * group + rng.normal(0, 2, n) > 117
 
def report(name, accepted, rows=slice(None)):
    g, q, a = group[rows], qualified[rows], accepted
    rates = [a[g == k].mean() for k in (0, 1)]
    found = [a[(g == k) & q].mean() for k in (0, 1)]           # share of qualified people accepted
    print(f"{name:30} accepted A {rates[0]:5.1%}  B {rates[1]:5.1%}  ratio {rates[1] / rates[0]:.2f}"
          f"  | qualified accepted A {found[0]:5.1%}  B {found[1]:5.1%}")
 
report("Truly qualified", qualified)
report("Past committee decisions", past)
 
train, test = slice(0, 3000), slice(3000, None)
def model(features, labels):
    X = np.column_stack(features)
    return LogisticRegression(max_iter=1000).fit(X[train], labels[train]).predict(X[test])
 
print("\nModels, scored on 1,000 new applicants (the group column is never given to them):")
report("Learned from past decisions", model([grades, essay, area], past), test)
report("Same, area column removed", model([grades, essay], past), test)
report("Learned from fair labels", model([grades, essay, area], qualified), test)
Output
Truly qualified                accepted A 43.3%  B 42.0%  ratio 0.97  | qualified accepted A 100.0%  B 100.0%
Past committee decisions       accepted A 43.2%  B 19.7%  ratio 0.46  | qualified accepted A 92.9%  B 47.0%
 
Models, scored on 1,000 new applicants (the group column is never given to them):
Learned from past decisions    accepted A 32.5%  B 25.2%  ratio 0.78  | qualified accepted A 80.4%  B 61.5%
Same, area column removed      accepted A 31.0%  B 27.7%  ratio 0.89  | qualified accepted A 76.8%  B 67.6%
Learned from fair labels       accepted A 40.4%  B 41.0%  ratio 1.01  | qualified accepted A 100.0%  B 100.0%

Explanation:

DecisionsAccepted AAccepted BRatioQualified accepted, A vs B
Truly qualified43.3%42.0%0.97100% vs 100%
Past committees43.2%19.7%0.4692.9% vs 47.0%
Model from past decisions32.5%25.2%0.7880.4% vs 61.5%
Same, area removed31.0%27.7%0.8976.8% vs 67.6%
Model from fair labels40.4%41.0%1.01100% vs 100%
  • The past decisions were badly unfair: only 47.0% of qualified group B applicants were accepted, against 92.9% of group A.
  • The model learned that bias without seeing the group column. Its ratio of 0.78 is below the four-fifths line, and qualified B applicants are accepted less often (61.5% against 80.4%). It found the bias through the area column, which matches the group 80% of the time: a proxy.
  • Removing the proxy helps but doesn't fix it (0.89). The labels themselves still teach the model that B applicants are less deserving.
  • Training on fair labels fixes it: equal rates (1.01) and every qualified applicant accepted in both groups. The cure was in the data, not in hiding a column.

The data is made up so the true answer is known. With real data, the "truly qualified" column usually isn't available, which is exactly why measuring outcomes by group is so important.

Reducing Bias

StageWhat to do
Before trainingCheck who is in the data; fix biased labels; collect more data for underrepresented groups
During trainingUse fairness-aware methods, or reweight examples
After trainingMeasure fairness by group on a test set; adjust decision thresholds if justified
In useMonitor outcomes by group; let people appeal; keep a human in important decisions

Common Mistakes

Measuring only overall accuracy. A system can be accurate on average and unfair to a group.

Trusting "we don't use race or gender". Proxies carry the same information.

Fixing the model but not the process. If the labels come from biased decisions, the bias returns with the next training run.

Choosing a fairness measure without explaining why. Measures conflict; document the choice.

Interview Corner

How can a model be biased if it doesn't use a protected attribute?

Through proxy features correlated with the attribute, such as zip code, school or name, and through labels that came from biased past decisions. The model learns the pattern from whatever features carry it.

How would you measure fairness in a classifier?

Compare outcomes by group: selection rates (and their ratio, as in the four-fifths rule), error rates such as false positives and false negatives, and the share of qualified people accepted (equal opportunity). Choose the measures that matter for the decision, and monitor them after launch.

Practice

Exercise: a stronger proxy. Change 0.8 to 0.95 in the line that makes area, so where people live matches their group more closely. How do the ratio and the qualified acceptance rates of the first model change?

Check your understanding:

  1. How did the model learn the bias without the group column?
  2. Why didn't removing the area column fully fix the model?
  3. What does a ratio of 0.78 mean under the four-fifths rule?

Quick check: bias and fairness

4 questions
1What was the first model's acceptance ratio, B to A?
2What carried the bias into the model without the group column?
3What fixed the model?
4What does equal opportunity compare?

Key Takeaways

Quick reference

NeedCode or practice
Selection rate by groupaccepted[group == g].mean()
Four-fifths checkrate_B / rate_A >= 0.8
Equal opportunityaccepted[(group == g) & qualified].mean()
Find proxiesCheck how well each feature predicts the group
FixFairer labels, more data, monitoring, appeals

Was this tutorial helpful?

Lesson 170 of 186