Skip to content

Fairness across groups

Measure how a model treats groups on several definitions, see why you can't satisfy them all, and read what a fair threshold costs. Then decide with the people affected.

Applied ~12 min10 steps

What runs in CodeSplash

You will be able to

  • Compute demographic parity, equal opportunity and equalized odds gaps
  • Explain why fairness definitions conflict when base rates differ
  • Read the cost of a fair threshold, and say who should decide

Read first

01

Same model, different treatment

Read

A model can be accurate overall and treat groups differently. Compare per group, on decisions at your threshold:

MeasureCompares
Demographic parityDemographic parityEvery group is selected at the same rate, whatever their outcomes.Selection rate
Equal opportunityEqual opportunityAmong people who truly qualify, every group is selected at the same rate.True-positive rate
Equalized oddsEqualized oddsTrue positive and false positive rates both match across groups.True- and false-positive rates
Predictive parityPrecision
Calibration within groupsPredicted against observed

Each is a real concern, and each belongs to a different idea of fair.

02

Read one audit

Read

Credit decisions, across 3 groups and 888 applicants:

  • Selection rate gap 0.288, and a ratio of 0.563 between lowest and highest. Under 0.8, that fails the four-fifths rule.
  • True-positive rate gap 0.284: among applicants who'd repay, one group is approved much less often.
  • Precision gap 0.0595: those approved repay at about the same rate.

One audit can look fair on one row and unfair on another.

03

Fix them all?

Predict

Predict first

Repayment rates differ between groups. Can a realistic model have equal selection rates, equal true- and false-positive rates, and equal precision across them, all at once?

Yes, with enough dataYes, with a better modelNo, unless base rates match or the model is perfect
Reveal

No. When base rates differ and prediction isn't perfect, these can't all hold. Kleinberg and colleagues and Chouldechova showed this in 2016 and 2017. It's arithmetic, so you can't tune your way out. Someone has to choose which gap matters for this decision, and why.

04

Moving thresholds

Read

One remedy changes only the decision: a threshold per group, chosen to reduce a gap at the lowest cost.

Requiring exact demographic parity on these decisions: selection gap 0.288 to 0, and true-positive gap 0.284 to 0.0107. The cost per row goes from 0.291 with no constraint to 0.303: a pricePrice of fairnessHow much decision cost rises when you add a fairness constraint, compared with the best unconstrained thresholds. of 0.0121.

05

Which constraint costs more?

Predict

Predict first

On the same data, you constrain either demographic parity or equalized odds. Which has the higher price?

Parity costs moreEqualized odds costs moreThey cost the same
Reveal

Equalized odds: 0.0215, against 0.0121 for parity. It must equalize two rates, so it's more constrained. It closes the true-positive gap fully, but leaves a selection gap of 0.0438. Add a capacity cap, and the price changes again: 0.0570.

06

Beyond the threshold

Read

A threshold can't repair what the data got wrong. Ask where the gap comes from:

  • Labels: "repaid" only exists for those who were approved.
  • Measurement: do features mean the same thing in each group?
  • Proxies: leaving out the attribute doesn't remove it. Postcode can stand in.
  • Size: a small group's gap has a wide interval.
07

Who decides?

Reflect

Choose a model that affects people. Who is helped by a false positive, and who is harmed by a false negative? Which gap would they find hardest to accept? Is that choice yours to make, or do those people, or the law, have a say?

08

Check your understanding

Checkpoint

Check yourself

Group A is selected at a rate of 60%, and group B at 40%. What is the demographic parity difference, as a proportion?

Show the answer

Answer: 0.2

0.6−0.4=0.20.6 - 0.4 = 0.2. The ratio is 0.4/0.6≈0.670.4/0.6 \approx 0.67, which is under the four-fifths line.

Check yourself

What does equal opportunity compare?

Show the answer

Answer: b

It's the true-positive rate. It asks whether qualified people have the same chance, wherever they come from.

Check yourself

You remove the sensitive attribute from the features. Is the model now fair?

Show the answer

Answer: b

You can only check for a disparity if you keep the attribute for auditing. Leaving it out of the audit only hides the problem.

Check yourself

A small group shows a big gap, but its interval runs from nearly zero to very large. What should you do?

Show the answer

Answer: b

Lack of evidence isn't evidence of fairness. The app's bootstrap intervals are stratified by group, so a small group shows a wide range.

09

Audit a model

In the app

A binary run has a Fairness tab. Choose a Group (sensitive attribute), the Rows, a Decision threshold and a Reference group. It lists Disparities across groups with a Meaning for each, then Selection, Base rate, TPR, FPR, Precision, Accuracy and Calibration gap per group. For the cost of fixing a gap, open Decisions & fairness in Studios, where the walkthrough compares {{decision}} across {{group}}.

Python
import pandas as pd

df = pd.DataFrame({
    "group": ["a"] * 6 + ["b"] * 6,
    "approved": [1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0],
    "repaid": [1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0],
})
rows = []
for g, d in df.groupby("group"):
    good = d[d.repaid == 1]
    bad = d[d.repaid == 0]
    rows.append({"group": g, "selection": d.approved.mean(), "tpr": good.approved.mean(), "fpr": bad.approved.mean()})
out = pd.DataFrame(rows).set_index("group")
print(out.round(3))
print("gaps", (out.max() - out.min()).round(3).to_dict())
R
df <- data.frame(group = rep(c("a", "b"), each = 6),
                 approved = c(1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0),
                 repaid = c(1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0))
out <- t(sapply(split(df, df$group), function(d) {
  c(selection = mean(d$approved), tpr = mean(d$approved[d$repaid == 1]), fpr = mean(d$approved[d$repaid == 0]))
}))
round(out, 3)
round(apply(out, 2, function(x) max(x) - min(x)), 3)
10

Recap

Recap
  • Fairness has several definitions, and they conflict when base rates differ.
  • Group thresholds can close a chosen gap at a measurable price. They can't fix bad labels.
  • Which gap matters is a decision about people, not a setting. Make it openly.

Concepts in this lesson