Fairness metrics help us check if a machine learning model treats all groups of people equally. They show if the model is biased or fair.
Fairness metrics in ML Python
# Example: Calculate demographic parity difference from sklearn.metrics import confusion_matrix def demographic_parity_difference(y_true, y_pred, sensitive_attr): groups = set(sensitive_attr) rates = {} for group in groups: idx = [i for i, val in enumerate(sensitive_attr) if val == group] positive_rate = sum(y_pred[i] for i in idx) / len(idx) rates[group] = positive_rate return max(rates.values()) - min(rates.values())
Fairness metrics often compare model outcomes across different groups defined by sensitive attributes like gender or race.
Common fairness metrics include demographic parity, equal opportunity, and equalized odds.
# Demographic parity difference # Measures difference in positive prediction rates between groups def demographic_parity_difference(y_true, y_pred, sensitive_attr): groups = set(sensitive_attr) rates = {} for group in groups: idx = [i for i, val in enumerate(sensitive_attr) if val == group] positive_rate = sum(y_pred[i] for i in idx) / len(idx) rates[group] = positive_rate return max(rates.values()) - min(rates.values())
# Equal opportunity difference # Measures difference in true positive rates between groups def equal_opportunity_difference(y_true, y_pred, sensitive_attr): groups = set(sensitive_attr) tpr = {} for group in groups: idx = [i for i, val in enumerate(sensitive_attr) if val == group] tp = sum(1 for i in idx if y_pred[i] == 1 and y_true[i] == 1) fn = sum(1 for i in idx if y_pred[i] == 0 and y_true[i] == 1) tpr[group] = tp / (tp + fn) if (tp + fn) > 0 else 0 return max(tpr.values()) - min(tpr.values())
This program calculates two fairness metrics on a small example dataset. It shows how much the model's positive predictions and true positive rates differ between two groups.
from sklearn.metrics import confusion_matrix def demographic_parity_difference(y_true, y_pred, sensitive_attr): groups = set(sensitive_attr) rates = {} for group in groups: idx = [i for i, val in enumerate(sensitive_attr) if val == group] positive_rate = sum(y_pred[i] for i in idx) / len(idx) rates[group] = positive_rate return max(rates.values()) - min(rates.values()) def equal_opportunity_difference(y_true, y_pred, sensitive_attr): groups = set(sensitive_attr) tpr = {} for group in groups: idx = [i for i, val in enumerate(sensitive_attr) if val == group] tp = sum(1 for i in idx if y_pred[i] == 1 and y_true[i] == 1) fn = sum(1 for i in idx if y_pred[i] == 0 and y_true[i] == 1) tpr[group] = tp / (tp + fn) if (tp + fn) > 0 else 0 return max(tpr.values()) - min(tpr.values()) # Sample data # y_true: actual labels (1 = positive, 0 = negative) # y_pred: model predictions # sensitive_attr: group membership (e.g., 'A' or 'B') y_true = [1, 0, 1, 0, 1, 0, 1, 0] y_pred = [1, 0, 1, 1, 0, 0, 1, 0] sensitive_attr = ['A', 'A', 'B', 'B', 'A', 'A', 'B', 'B'] dp_diff = demographic_parity_difference(y_true, y_pred, sensitive_attr) eo_diff = equal_opportunity_difference(y_true, y_pred, sensitive_attr) print(f"Demographic Parity Difference: {dp_diff:.2f}") print(f"Equal Opportunity Difference: {eo_diff:.2f}")
Fairness metrics depend on the sensitive attribute you choose, like gender or age.
Perfect fairness is hard to achieve; sometimes you must balance fairness with accuracy.
Use fairness metrics to find and reduce bias in your models early.
Fairness metrics measure if a model treats groups equally.
Common metrics compare positive rates or true positive rates across groups.
Checking fairness helps build trustworthy and ethical AI systems.
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
