Fairness metrics help us check if a model treats different groups equally. We want to avoid bias that can hurt some people unfairly. Common fairness metrics include Demographic Parity (checks if positive outcomes are equal across groups), Equal Opportunity (checks if true positive rates are equal), and Equalized Odds (checks if both true positive and false positive rates are equal). These metrics matter because they show if the model is fair in decisions, not just accurate.
Fairness metrics in ML Python - Model Metrics & Evaluation
Imagine a model predicting loan approval for two groups: Group A and Group B.
Group A Confusion Matrix:
TP=40 FP=10
FN=5 TN=45
Group B Confusion Matrix:
TP=30 FP=20
FN=15 TN=35
We calculate True Positive Rate (Recall) for each group:
- Group A Recall = 40 / (40 + 5) = 0.89
- Group B Recall = 30 / (30 + 15) = 0.67
The difference shows Group B gets fewer correct positive predictions, indicating possible unfairness.
Sometimes improving fairness can reduce overall accuracy. For example, if a model favors one group, it might be more accurate but unfair. Adjusting the model to treat groups equally might lower accuracy but is fairer. This tradeoff is important to understand and balance based on the problem.
Example: A hiring model might be very accurate but reject many qualified candidates from a minority group. Improving fairness means accepting more candidates from that group, which might slightly reduce accuracy but improves fairness.
Good: Similar true positive rates and false positive rates across groups (e.g., TPR difference < 0.05). This means the model treats groups equally.
Bad: Large differences in metrics (e.g., one group has TPR 0.9, another 0.5). This means the model favors one group and is unfair.
- Ignoring context: Fairness depends on the problem and groups; one metric does not fit all.
- Data imbalance: Small group sizes can make metrics unstable or misleading.
- Accuracy paradox: A model can be accurate but unfair if it ignores minority groups.
- Overfitting fairness: Adjusting too much for fairness on training data can hurt real-world performance.
- Ignoring multiple fairness aspects: Focusing on one metric may hide unfairness in others.
Your model has 95% accuracy overall but the true positive rate for a minority group is 40%, while for the majority group it is 85%. Is this model good for fairness? Why or why not?
Answer: No, this model is not fair. Even though accuracy is high, the minority group has a much lower true positive rate, meaning they get fewer correct positive predictions. This shows bias and unfair treatment.
Practice
Solution
Step 1: Understand fairness metrics
Fairness metrics are designed to measure if a model treats different groups fairly and without bias.Step 2: Compare options
Options B, C, and D relate to performance or data size, not fairness.Final Answer:
To check if the model treats different groups equally -> Option BQuick Check:
Fairness metrics = check equal treatment [OK]
- Confusing fairness with accuracy
- Thinking fairness improves training speed
- Mixing fairness with data size
Solution
Step 1: Recall demographic parity definition
Demographic parity measures if the positive prediction rates are similar across groups.Step 2: Evaluate options
Difference in positive prediction rates between groups matches this definition. Options A, C, and D are unrelated to demographic parity.Final Answer:
Difference in positive prediction rates between groups -> Option CQuick Check:
Demographic parity = positive rate difference [OK]
- Using false negatives instead of positive rates
- Confusing sample ratios with fairness metrics
- Mixing training time with fairness
Solution
Step 1: Calculate true positive rates (TPR) for both groups
TPR = TP / (TP + FN). For Group A: 40 / (40 + 10) = 0.8. For Group B: 30 / (30 + 20) = 0.6.Step 2: Find equal opportunity difference
Equal opportunity difference = |0.8 - 0.6| = 0.20.Final Answer:
0.20 -> Option DQuick Check:
Equal opportunity difference = |TPR_A - TPR_B| = 0.20 [OK]
- Confusing false positive rate with true positive rate
- Not dividing by (TP + FN)
- Ignoring absolute difference
def stat_parity_diff(y_true, y_pred, group):
pos_rate_group1 = sum(y_pred[group == 1]) / len(y_pred[group == 1])
pos_rate_group0 = sum(y_pred[group == 0]) / len(y_pred[group == 0])
return pos_rate_group0 - pos_rate_group1
What is the main bug?Solution
Step 1: Understand statistical parity difference
It is usually calculated as positive rate of group 1 minus positive rate of group 0.Step 2: Check code difference calculation
The code returns pos_rate_group0 - pos_rate_group1, which reverses the sign.Final Answer:
The difference sign is reversed; should be group1 - group0 -> Option AQuick Check:
Sign order matters in parity difference [OK]
- Mixing up group order in subtraction
- Ignoring zero division errors
- Confusing y_true and y_pred
Solution
Step 1: Understand false positive rate difference
Group Y has a higher FPR (0.25) than Group X (0.10), meaning more false alarms for Group Y.Step 2: Choose action to reduce FPR gap
Adjusting the decision threshold for Group Y to be more strict reduces false positives, lowering FPR closer to Group X.Final Answer:
Adjust the model threshold for Group Y to be more strict -> Option AQuick Check:
Threshold tuning reduces FPR gap [OK]
- Ignoring threshold adjustment
- Removing data harms fairness
- Using accuracy alone misses fairness issues
Ready to prove it?
Pass the test and this pattern moves unknown
