Intermediate level · Module 4: AI Fairness Metrics and Evaluation · Lesson 2 of 3
Disaggregated Evaluation with Fairlearn MetricFrame
Learners use Fairlearn's MetricFrame to compute several metrics for each group in one step, and the summary functions to report differences and ratios between groups.
In this lesson you will
- Compute by-group metrics with MetricFrame
- Report differences and ratios with Fairlearn's summary metrics
Key concepts
MetricFrameby_groupdifference()ratio()sensitive_features
The lesson
MetricFrame takes a dictionary of metric functions, the true labels, the predictions and the sensitive features. It returns a table of each metric for each group (by_group), plus the overall value. Its difference() and ratio() methods summarise the gap between groups.
Fairlearn also provides ready-made summary functions: demographic_parity_difference, demographic_parity_ratio and equalized_odds_difference. They take y_true, y_pred and sensitive_features and return a single number, which is useful for tracking over time.
Several sensitive features can be passed together (for example gender and county type) to see intersectional groups. Intersectional groups are often smaller, so report their sizes too, using the count function from fairlearn.metrics.
Step by step
- Install Fairlearn (pip install fairlearn) or use the offline wheel provided by the instructor.
- Build a MetricFrame with selection_rate, true_positive_rate, false_positive_rate, accuracy_score and count.
- Print mf.by_group and mf.difference().
- Repeat with sensitive_features set to both gender and county type, and note the smallest group.
Comparing fairness metrics for a recruitment shortlist with Fairlearn
MetricFrame reports selection rate, true and false positive rates and accuracy by gender, and three summary metrics show different sides of the same gap.
import pandas as pdfrom sklearn.metrics import accuracy_scorefrom fairlearn.metrics import ( MetricFrame, selection_rate, true_positive_rate, false_positive_rate, demographic_parity_difference, demographic_parity_ratio, equalized_odds_difference,) # Fictional shortlist for 20 applicants: 1 = qualified / shortlistedqualified = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]shortlisted = [1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0]gender = pd.Series(["M"] * 10 + ["F"] * 10, name="gender") mf = MetricFrame( metrics={ "selection_rate": selection_rate, "TPR": true_positive_rate, # qualified people who were shortlisted "FPR": false_positive_rate, # unqualified people who were shortlisted "accuracy": accuracy_score, }, y_true=qualified, y_pred=shortlisted, sensitive_features=gender,)print(mf.by_group) args = dict(y_true=qualified, y_pred=shortlisted, sensitive_features=gender)print("Demographic parity difference:", round(demographic_parity_difference(**args), 2))print("Demographic parity ratio: ", round(demographic_parity_ratio(**args), 2))print("Equalized odds difference: ", round(equalized_odds_difference(**args), 2))
Output
selection_rate TPR FPR accuracy
gender
F 0.3 0.4 0.2 0.6
M 0.6 0.8 0.4 0.7
Demographic parity difference: 0.3
Demographic parity ratio: 0.5
Equalized odds difference: 0.4How it works
Both groups have 5 qualified and 5 unqualified applicants. Men are shortlisted at twice the rate of women (0.6 against 0.3), so the demographic parity difference is 0.3 and the ratio is 0.5. The more serious finding is in the true positive rate: 4 of 5 qualified men were shortlisted but only 2 of 5 qualified women, so qualified women are missed twice as often. The equalized odds difference reports the larger of the TPR gap (0.4) and the FPR gap (0.2), which is 0.4. Accuracy differs by only 0.1, which shows why accuracy alone hides unfairness. With 10 people per group these numbers illustrate the method; real conclusions need larger groups and confidence intervals.
Key concepts
MetricFrame.by_groupselection ratetrue and false positive ratesdemographic parity vs equalised oddswhy accuracy hides unfairness
Practice activity
Produce a MetricFrame fairness table for the project model by gender, by county type and for their intersection.
Expected result: A correct by-group fairness table, with differences and group sizes reported.
Check your understanding
-
What does MetricFrame.by_group return?
Show answer
A table of each metric for each group.
-
Why report group counts with intersectional results?
Show answer
Intersectional groups can be small, which makes their metrics less reliable.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.