Sub topic 7
Predictive parity and calibration by group
This chapter assumes you can compute a confusion matrix, precision, recall, and false positive rate. It also assumes you can use NumPy and pandas, and that you know the per-group rates from the earlier chapter on measuring outcomes and errors per group. Here we look at fairness from the point of view of the person who reads the model's output.
The question these criteria ask
Demographic parity and equalized odds condition on the true outcome or ignore it. They ask how the model treats people who are actually positive or actually negative. Predictive parity and calibration ask the reverse: given what the model said, how often was it right, and is that the same for every group?
This is the view of a decision-maker. A loan officer sees a score of 0.8 and wants it to mean the same thing for an applicant from group A as for one from group B. If it doesn't, they must mentally adjust the score by group, which defeats the purpose of using one model.
Predictive parity
Predictive parity (also called outcome test or positive predictive value parity) holds when precision is equal across groups:
P(Y = 1 | Ŷ = 1, G = a) = P(Y = 1 | Ŷ = 1, G = b)
It applies to hard decisions. Among the people the model flags, the same share of each group should truly be positive. You measure it as the gap in precision, which you already computed in group_rates:
predictive_parity_gap = abs(a["precision"] - b["precision"])A related variant, false omission rate parity, compares P(Y = 1 | Ŷ = 0) across groups. Together, equal precision and equal false omission rate say that a prediction, positive or negative, carries the same information in every group.
Calibration by group
Calibration applies to scores or probabilities. A score S is calibrated if, among all cases that receive score s, a fraction s are actually positive. Calibration within groups requires this for each group separately:
P(Y = 1 | S = s, G = g) = s for every score s and every group g
Calibration within groups is the stronger sibling of predictive parity. If scores are calibrated per group, then thresholding them gives predictive parity only when the groups' score distributions above the threshold line up. In general, calibration does not guarantee equal precision at one shared threshold. It guarantees that the score itself is honest in every group.
| Criterion | Works on | Condition | Question answered |
|---|---|---|---|
| Predictive parity | Hard predictions | Equal precision per group | Does "flagged" mean the same thing for each group? |
| Calibration within groups | Scores or probabilities | Observed rate equals score in every bin, per group | Does "0.8" mean the same thing for each group? |
| Equalized odds (previous chapter) | Hard predictions | Equal TPR and FPR | Are qualified and unqualified people treated alike? |
Measuring it: a reliability table per group
To check calibration, bin the scores and compare the mean score in each bin with the observed positive rate. Do this separately for each group. The following example simulates two groups with different base rates and scores that are calibrated by construction, so you can see what a clean result looks like.
import numpy as np
import pandas as pd
def calibration_by_group(y_true, scores, group, n_bins=5):
"""Reliability table: mean score vs. observed positive rate, per group and bin."""
df = pd.DataFrame({"y": np.asarray(y_true),
"s": np.asarray(scores),
"g": np.asarray(group)})
edges = np.linspace(0, 1, n_bins + 1)
df["bin"] = pd.cut(df["s"], edges, include_lowest=True)
table = (
df.groupby(["g", "bin"], observed=True)
.agg(n=("y", "size"), mean_score=("s", "mean"), observed_rate=("y", "mean"))
.reset_index()
)
table["gap"] = table["observed_rate"] - table["mean_score"]
return table
def expected_calibration_error(table):
"""Weighted mean absolute gap per group."""
return table.groupby("g").apply(
lambda t: (t["n"] * t["gap"].abs()).sum() / t["n"].sum(),
include_groups=False,
)
rng = np.random.default_rng(0)
n = 4000
group = np.where(rng.random(n) < 0.5, "A", "B")
p = np.where(group == "A", rng.beta(3, 2, n), rng.beta(2, 3, n)) # true risk
y = (rng.random(n) < p).astype(int)
scores = p # calibrated by construction
table = calibration_by_group(y, scores, group)
print(table.round(3))
print(expected_calibration_error(table).round(3))
# Hard decisions at one threshold: precision still differs a little by group.
pred = (scores >= 0.5).astype(int)
for g in ["A", "B"]:
m = group == g
print(g, "precision:", round(y[m][pred[m] == 1].mean(), 3),
"selection rate:", round(pred[m].mean(), 3))Running it, you should see small gap values in both groups, limited only by sampling noise. Yet the selection rates differ sharply, because group A has higher risk. Precision at the shared threshold also differs somewhat, since the score distributions above 0.5 differ. Exact numbers depend on the random seed and library versions.
This shows the main point of the chapter. Scores can be perfectly calibrated in each group while demographic parity fails badly. Calibration only promises honest scores. It does not promise equal outcomes.
To test a model that is miscalibrated for one group, shift one group's scores, for example scores = np.where(group == "B", np.clip(p + 0.1, 0, 1), p), and rerun. Group B's gap values turn negative: the model overstates their risk.
Reading the results
The following diagram shows a practical order of checks.
Look for these patterns in the table:
- Gaps of one sign across all bins in one group. The model systematically over- or under-predicts risk for that group. This often points to a label problem or a shifted feature distribution.
- Gaps only in the extreme bins. Often caused by few samples. Check the
ncolumn before reading anything into it. - Both groups calibrated but different precision. Expected when base rates differ. This is not a bug, but it is a trade-off you should document.
Pitfalls
- Precision depends on the threshold. Predictive parity can hold at 0.5 and fail at 0.7. Audit at the threshold that will be deployed, and report a sweep if the threshold may change.
- Precision depends on the base rate. When groups have different prevalence of the positive label, even a model with identical per-group TPR and FPR will have different precision. This is the root of the conflict covered in the next chapter on why you cannot satisfy every criterion at once.
- Binning choices change the picture. Five equal-width bins may hide problems that ten quantile bins reveal. With skewed scores, equal-width bins leave most bins nearly empty. Prefer quantile bins when scores cluster, and state your choice.
- Noisy labels carry over. Calibration measures agreement with the recorded label. If the label is a biased proxy, for example "was arrested" in place of "committed an offense", a perfectly calibrated model is calibrated to the wrong target.
- Overall calibration can hide group miscalibration. A model that overpredicts for group A and underpredicts for group B by the same amount looks calibrated in aggregate. Always compute the table per group.
Fixing calibration gaps
If a real gap appears, three common options exist.
- Recalibrate per group. Fit a monotone mapping from raw score to probability separately for each group, such as isotonic regression or Platt scaling, on held-out data. This directly targets calibration within groups.
- Fix the upstream cause. Recheck labels, features, and sampling for the affected group. Recalibration treats a symptom.
- Choose a different criterion. If calibration is the priority, accept that error rates may differ. If equalized odds is the priority, post-processing thresholds will typically break calibration. Pick deliberately and record why.
Per-group recalibration uses the sensitive attribute at prediction time. Check that your legal and policy context allows it before adopting it.
Summary of when to prioritize this view
Favor predictive parity and calibration when a human reads the score and acts on it, as in risk scores for clinicians or caseworkers. The key requirement there is that the number means the same thing for everyone. Favor error-rate criteria when the harm falls on people who are wrongly rejected or wrongly flagged, and you want equal chances for equally qualified people. Many audits report both and explain the tension.
Exercise
A model's scores are calibrated within groups A and B. At threshold 0.5, group A has precision 0.78 and group B has precision 0.70. Group A has a higher base rate. Is the model miscalibrated for B? Name two things you would check before calling this a fairness problem.
Show answer
Not necessarily. Calibration within groups can hold while precision at a fixed threshold differs, because precision depends on the score distribution above the threshold and on the base rate. First, check the reliability table for B with confidence intervals, to confirm the scores really are calibrated and the precision gap is not sampling noise (look at the number of flagged people in each group). Second, check whether the gap persists at the threshold that will actually be deployed and across nearby thresholds. You could also compare TPR and FPR, since calibration with different base rates usually means equalized odds fails, and document which criterion you chose to prioritize.