Bird
Raised Fist0
ML Pythonml~20 mins

Fairness metrics in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fairness Metrics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Equal Opportunity in Fairness Metrics

Which statement best describes the Equal Opportunity fairness metric in machine learning?

AIt requires that the overall accuracy is equal across different groups.
BIt requires that the false positive rates are equal across different groups.
CIt requires that the true positive rates are equal across different groups.
DIt requires that the predicted positive rates are equal across different groups.
Attempts:
2 left
💡 Hint

Think about which error type Equal Opportunity focuses on balancing.

❓ Predict Output
intermediate
2:00remaining
Output of Fairness Metric Calculation

Given the following confusion matrices for two groups, what is the difference in false positive rates (FPR) between Group A and Group B?

Group A: TP=40, FP=10, TN=50, FN=5
Group B: TP=30, FP=20, TN=40, FN=10
A0.17
B0.20
C0.10
D0.25
Attempts:
2 left
💡 Hint

FPR = FP / (FP + TN). Calculate for each group and subtract.

❓ Model Choice
advanced
2:00remaining
Choosing a Model to Optimize Demographic Parity

You want to build a classifier that satisfies Demographic Parity. Which model choice below is most likely to help achieve this?

AA model trained with a fairness constraint that equalizes predicted positive rates across groups.
BA model trained to minimize false negative rates only.
CA model trained to maximize overall accuracy without constraints.
DA model trained with a loss function ignoring group membership.
Attempts:
2 left
💡 Hint

Demographic Parity focuses on equalizing positive predictions, not just accuracy.

❓ Metrics
advanced
2:00remaining
Interpreting Disparate Impact Metric

What does a Disparate Impact value of 0.6 indicate about a model's fairness?

AThe model's false positive rate is 60% higher for the privileged group.
BThe model favors the unprivileged group by predicting positives more often.
CThe model has 60% accuracy on the unprivileged group.
DThe model predicts positive outcomes for the unprivileged group at 60% the rate of the privileged group.
Attempts:
2 left
💡 Hint

Disparate Impact compares positive prediction rates between groups.

🔧 Debug
expert
3:00remaining
Debugging Fairness Metric Calculation Code

What error will this Python code raise when calculating the statistical parity difference?

def stat_parity_difference(y_pred, group):
    pos_rate_priv = sum(y_pred[group == 0]) / len(y_pred[group == 0])
    pos_rate_unpriv = sum(y_pred[group == 1]) / len(y_pred[group == 1])
    return pos_rate_unpriv - pos_rate_priv

# y_pred = [1, 0, 1, 1], group = [0, 1, 0]
ML Python
def stat_parity_difference(y_pred, group):
    pos_rate_priv = sum(y_pred[group == 0]) / len(y_pred[group == 0])
    pos_rate_unpriv = sum(y_pred[group == 1]) / len(y_pred[group == 1])
    return pos_rate_unpriv - pos_rate_priv

# y_pred = [1, 0, 1, 1], group = [0, 1, 0]
ANo error; the function returns a float value.
BTypeError because boolean indexing is not supported on lists.
CIndexError due to mismatched lengths of y_pred and group arrays.
DZeroDivisionError because one group has zero members.
Attempts:
2 left
💡 Hint

Check the data types and operations used for indexing.

Practice

(1/5)
1. What is the main purpose of fairness metrics in machine learning models?
easy
A. To improve the speed of model training
B. To check if the model treats different groups equally
C. To increase the size of the training dataset
D. To reduce the number of model parameters

Solution

  1. Step 1: Understand fairness metrics

    Fairness metrics are designed to measure if a model treats different groups fairly and without bias.
  2. Step 2: Compare options

    Options B, C, and D relate to performance or data size, not fairness.
  3. Final Answer:

    To check if the model treats different groups equally -> Option B
  4. Quick Check:

    Fairness metrics = check equal treatment [OK]
Hint: Fairness metrics check equality across groups [OK]
Common Mistakes:
  • Confusing fairness with accuracy
  • Thinking fairness improves training speed
  • Mixing fairness with data size
2. Which of the following is a correct way to calculate demographic parity difference between two groups?
easy
A. Sum of false negatives in both groups
B. Ratio of training samples in each group
C. Difference in positive prediction rates between groups
D. Difference in model training times

Solution

  1. Step 1: Recall demographic parity definition

    Demographic parity measures if the positive prediction rates are similar across groups.
  2. Step 2: Evaluate options

    Difference in positive prediction rates between groups matches this definition. Options A, C, and D are unrelated to demographic parity.
  3. Final Answer:

    Difference in positive prediction rates between groups -> Option C
  4. Quick Check:

    Demographic parity = positive rate difference [OK]
Hint: Demographic parity = compare positive rates [OK]
Common Mistakes:
  • Using false negatives instead of positive rates
  • Confusing sample ratios with fairness metrics
  • Mixing training time with fairness
3. Given the following confusion matrices for two groups, what is the equal opportunity difference? Group A: TP=40, FN=10, FP=20, TN=30 Group B: TP=30, FN=20, FP=15, TN=35
medium
A. 0.25
B. 0.10
C. 0.05
D. 0.20

Solution

  1. 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.
  2. Step 2: Find equal opportunity difference

    Equal opportunity difference = |0.8 - 0.6| = 0.20.
  3. Final Answer:

    0.20 -> Option D
  4. Quick Check:

    Equal opportunity difference = |TPR_A - TPR_B| = 0.20 [OK]
Hint: Equal opportunity = difference in true positive rates [OK]
Common Mistakes:
  • Confusing false positive rate with true positive rate
  • Not dividing by (TP + FN)
  • Ignoring absolute difference
4. You have this Python code to compute statistical parity difference but it gives wrong results:
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?
medium
A. The difference sign is reversed; should be group1 - group0
B. Using y_true instead of y_pred for positive rates
C. Not handling division by zero if group has no samples
D. Using sum instead of mean for positive rate

Solution

  1. Step 1: Understand statistical parity difference

    It is usually calculated as positive rate of group 1 minus positive rate of group 0.
  2. Step 2: Check code difference calculation

    The code returns pos_rate_group0 - pos_rate_group1, which reverses the sign.
  3. Final Answer:

    The difference sign is reversed; should be group1 - group0 -> Option A
  4. Quick Check:

    Sign order matters in parity difference [OK]
Hint: Stat parity = group1 rate minus group0 rate [OK]
Common Mistakes:
  • Mixing up group order in subtraction
  • Ignoring zero division errors
  • Confusing y_true and y_pred
5. You want to ensure your loan approval model is fair by equalizing false positive rates (FPR) across two groups. Given these FPRs: Group X: 0.10 Group Y: 0.25 Which action best reduces this fairness gap?
hard
A. Adjust the model threshold for Group Y to be more strict
B. Increase training data size for Group X
C. Remove Group Y samples from training
D. Use accuracy as the only metric to evaluate fairness

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Adjust the model threshold for Group Y to be more strict -> Option A
  4. Quick Check:

    Threshold tuning reduces FPR gap [OK]
Hint: Higher threshold for high FPR group to reduce false positives [OK]
Common Mistakes:
  • Ignoring threshold adjustment
  • Removing data harms fairness
  • Using accuracy alone misses fairness issues
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard