Sub topic 7
Predictive Parity and Calibration by Group
This chapter assumes you can read a confusion matrix, know precision and recall, and can run basic Python with NumPy. It also assumes you have the group_rates helper from the earlier chapter on per-group rates.
What predictive parity asks
Demographic parity compares who gets selected. Equalized odds compares error rates among people whose true label is known. Predictive parity looks from the other side. It asks: when the model says "positive," how often is it right, and is that the same for every group?
Predictive parity holds when precision (the positive predictive value) is equal across groups:
P(Y = 1 | Ŷ = 1, G = A) = P(Y = 1 | Ŷ = 1, G = B)
This matters to the person acting on the prediction. A loan officer who sees "likely to repay" wants that label to mean the same thing for every applicant.
Computing the gap
Reuse the per-group rates from earlier:
rates = group_rates(y_true, y_pred, group) # y_true, y_pred in {0, 1}; group holds labels such as "A" and "B"
a, b = rates["A"], rates["B"]
predictive_parity_gap = abs(a["precision"] - b["precision"])Precision depends on the base rate, which is the share of true positives in each group. If group A has more true positives than group B, a classifier with identical TPR and FPR in both groups will still have higher precision for A. This is why predictive parity conflicts with equalized odds, a point the impossibility chapter develops.
Calibration by group
Predictive parity checks hard labels. Calibration checks scores. A score is calibrated when, among people given a score of 0.7, about 70% are actually positive. Calibration within groups requires this to hold for each group separately.
To check it, bin the scores and compare the mean predicted probability with the observed positive rate in each bin, per group.
import numpy as np
def calibration_by_group(y_true, scores, group, bins=5):
y_true = np.asarray(y_true)
scores = np.asarray(scores)
group = np.asarray(group)
edges = np.linspace(0, 1, bins + 1)
idx = np.clip(np.digitize(scores, edges) - 1, 0, bins - 1)
table = {}
for g in np.unique(group):
rows = []
for k in range(bins):
m = (group == g) & (idx == k)
if m.sum() > 0:
rows.append((k, int(m.sum()), scores[m].mean(), y_true[m].mean()))
table[g] = rows
return table
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1]
scores = [0.9, 0.2, 0.7, 0.8, 0.3, 0.1, 0.6, 0.4, 0.9, 0.2, 0.5, 0.7]
group = ["A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B"]
for g, rows in calibration_by_group(y_true, scores, group).items():
print(g)
for k, n, mean_score, observed in rows:
print(f" bin {k}: n={n}, mean score={mean_score:.2f}, observed rate={observed:.2f}")If the observed rate sits well below the mean score for one group but not the other, the model overstates risk or merit for that group.
Common errors
- Reporting calibration only overall. A model can be calibrated on average and miscalibrated inside each group in opposite directions.
- Using bins with very few people. Observed rates in a bin of five people are noisy, so check bin counts first.
- Treating predictive parity as proof of fairness. A model can have equal precision and still impose far higher false positive rates on one group.
- Forgetting that the labels may themselves be biased. If the label records who was arrested rather than who offended, "observed rate" inherits that bias.
Exercise
Exercise
Group A and group B have the same TPR and FPR, but group A has a higher base rate of true positives. Which group will have higher precision, and why?
Show answer
Group A. Precision is TP / (TP + FP). With equal TPR and FPR, a higher base rate means more actual positives, so TP grows relative to FP. Precision therefore rises with the base rate, and predictive parity fails even though equalized odds holds.