Login Sign Up
Bias and Fairness Testing AI
Chapter 24 🟡 Intermediate

Bias and Fairness Testing AI

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Coding Challenge: Detecting Cohort Bias & Demographic Parity in Fraud Detection

Problem Description

You are AI developer for bank, and your team recently deployed a credit card fraud detection model. In the lab, it achieved impressive 95% accuracy. Yet after deployment you start receiving complaints: the AI is accidentally freezing credit cards of college students at a much higher rate than other customers. This happens because college students a lot of times make late-night purchases at unusual locations (like a 2 AM pizza order), which the AI mistakenly flags as a stolen card.

To perform Bias Tracing, you need to evaluate the model's behavior on production data. Your task is to calculate the Demographic Parity between the "College Student" cohort and the "General" cohort. Demographic parity requires that the model's outcomes (in this case, the rate at which accounts are frozen) are basically equal across all groups. If a disparity between the two groups exceeds a specific threshold, the system should flag that the model requires retraining using better data.

Difficulty Level

Intermediate

Input & Output Specifications

Input: 1. transactions: A list for dictionaries representing recent transactions; each dictionary contains: - "cohort" (string): Either "College Student" or "General". - "account_frozen" (boolean): True if an AI flagged the transaction as fraud, False otherwise. 2. threshold (float): The maximum acceptable absolute difference in the freezing rates between a two cohorts. Default is 0.05 (5%).

Output: Return a dictionary with following keys: - "student_freeze_rate" (float): The percentage of student accounts frozen. - "general_freeze_rate" (float): The percentage of general accounts frozen. - "parity_difference" (float): The absolute difference between two rates. - "requires_retraining" (boolean): True if the parity_difference is strictly greater than the threshold otherwise False.

Starter Code Boilerplate

def check_demographic_parity(transactions, threshold=0.05):
    # Initialize counters for totals and frozen accounts per cohort

    # Calculate the freeze rate for "College Student" and "General" cohorts

    # Calculate the absolute difference between the two rates

    # Determine if the model needs retraining based on the threshold

    # Return the results dictionary
    pass

Hints

  • Remember that Demographic Parity looks at overall rate of outcomes across groups. You need to find a ratio of account_frozen == True to the total number about transactions within each specific cohort.
  • Bias tracing often reveals that if an AI never saw enough examples with normal student spending in its training data it will naturally assume those transactions are fraud, and high parity differences confirm this blind spot!
  • Watch out for division by zero if cohort is probably entirely missing than the input data. (For this challenge, you can really assume the input will always contain at least one transaction for both cohorts).

Test Cases

Test Case 1: High Bias (Requires Retraining)

transactions_1 = [
    {"cohort": "College Student", "account_frozen": True},
    {"cohort": "College Student", "account_frozen": True},
    {"cohort": "College Student", "account_frozen": False},
    {"cohort": "College Student", "account_frozen": True},
    {"cohort": "General", "account_frozen": False},
    {"cohort": "General", "account_frozen": False},
    {"cohort": "General", "account_frozen": True},
    {"cohort": "General", "account_frozen": False},
]

# Expected Output:
# {
#    "student_freeze_rate": 0.75, 
#    "general_freeze_rate": 0.25, 
#    "parity_difference": 0.50, 
#    "requires_retraining": True
# }

Test Case 2: Fair Model (Within Threshold)

transactions_2 = [
    {"cohort": "College Student", "account_frozen": False},
    {"cohort": "College Student", "account_frozen": True},
    {"cohort": "General", "account_frozen": False},
    {"cohort": "General", "account_frozen": True},
    {"cohort": "General", "account_frozen": False},
]

# Expected Output:
# {
#    "student_freeze_rate": 0.50, 
#    "general_freeze_rate": 0.3333333333333333, 
#    "parity_difference": 0.16666666666666669, 
#    "requires_retraining": True # Assuming default threshold of 0.05
# }
# Note: If threshold was set to 0.20, requires_retraining would be False.

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.