Skip to content

Model Fairness & Ethical AI

Model Fairness & Ethical AI

As ML models are increasingly used to make life-impacting decisions (hiring, lending, policing), ensuring that these models are fair and unbiased is a critical engineering requirement, not just an academic one.


๐Ÿ—๏ธ Sources of Bias

  1. Data Bias: The training data reflects historical human biases (e.g., a hiring model trained on 1950s data).
  2. Sampling Bias: The data used to train the model is not representative of the real population (e.g., facial recognition trained only on light-skinned individuals).
  3. Measurement Bias: The features used are proxies for protected attributes (e.g., using โ€œZip Codeโ€ as a proxy for race).

๐Ÿš€ Measuring Fairness

Engineers use mathematical metrics to detect bias:

  • Demographic Parity: The likelihood of a positive outcome (e.g., being hired) should be the same for all groups.
  • Equal Opportunity: The True Positive Rate should be the same for all groups (e.g., if you are qualified, you have the same chance of being hired regardless of gender).
  • Disparate Impact: A ratio of the selection rate of a protected group vs. a control group.

๐Ÿ› ๏ธ Mitigation Strategies

1. Pre-processing (Data Level)

  • Re-weighting: Giving more weight to under-represented examples in the loss function.
  • Data Augmentation: Synthetically creating more diverse data points.

2. In-processing (Model Level)

  • Adversarial Debias: Training a second model (the adversary) that tries to guess the protected attribute from the first modelโ€™s predictions. The first model is penalized if the adversary succeeds.

3. Post-processing (Output Level)

  • Threshold Adjustment: Using different probability thresholds for different groups to ensure equal outcomes.

๐Ÿ“Š Tools for Fairness

  • Fairlearn (Python): A library to assess and improve the fairness of machine learning models.
  • AI Fairness 360 (AIF360): An IBM-developed toolkit for bias detection and mitigation.
  • What-If Tool (Google): A visual interface for exploring model behavior and fairness.

๐Ÿ› ๏ธ Code Example: Fairness Audit (Fairlearn)

This example shows how to check for Demographic Parity using the Fairlearn library.

from fairlearn.metrics import MetricFrame, selection_rate
from sklearn.metrics import accuracy_score

# 1. Group your data by a sensitive attribute (e.g., Gender)
gm = MetricFrame(
    metrics={
        'accuracy': accuracy_score,
        'selection_rate': selection_rate # Probability of a positive outcome
    },
    y_true=y_test,
    y_pred=y_predictions,
    sensitive_features=X_test['gender']
)

# 2. Check metrics by group
print(gm.by_group)

# 3. Calculate Difference (Demographic Parity Difference)
print(f"Fairness Gap: {gm.difference().max()}")