Algorithmic Fairness Testing Protocol
This protocol provides a structured, repeatable framework for auditing machine learning models for fairness. It applies to binary classification tasks (extendable to multi-class/regression with adaptations). The goal is to detect disparities across protected cohorts using standardized fairness metrics, then apply remediation if thresholds are violated.
Assumptions:
- Access to a labeled dataset with protected attributes (e.g., gender, race).
- Model outputs include predictions (
Ŷ) and optionally predicted probabilities (P(Ŷ=1)).
- Testing on holdout/test set stratified by cohorts.
- Tools: Libraries like
fairlearn, aif360, or dalex.
1. Cohort Definition
Cohorts are subgroups defined by protected attributes (sensitive features). Use intersectional cohorts (e.g., gender × race) for robustness. Minimum cohort size: 100 samples to ensure statistical reliability (use bootstrap for smaller sets).
| Protected Attribute | Cohorts | Rationale |
|---|
| Gender | Male, Female, Non-binary/Other | Binary gender bias common in hiring/credit models. |
| Race/Ethnicity | White, Black/African American, Hispanic/Latino, Asian, Other | Historical disparities in criminal justice/lending. |
| Age | <30, 30-50, >50 | Age discrimination in employment/insurance. |
| Disability | Yes, No | Accessibility biases in healthcare/employment. |
| Intersectional | e.g., Black Female, White Male | Captures compounded biases. |
Steps:
- Verify protected attributes are present and non-proxy (e.g., ZIP code ≠ race).
- Handle missing data: Impute or exclude cohorts with >20% missing.
- Stratify data to ensure balanced representation.
2. Fairness Metrics
Compute metrics separately for privileged (e.g., Male/White) vs. unprivileged (e.g., Female/Black) cohorts. Use group-level and pairwise comparisons.
| Metric | Formula | Interpretation | Type |
|---|
| Demographic Parity (DP) Difference | |P(Ŷ=1 | A=0) - P(Ŷ=1 | A=1)| | Statistical parity: Equal positive prediction rates. | Group fairness |
| Disparate Impact (DI) Ratio | min(P(Ŷ=1 | A=a) / P(Ŷ=1 | A=priv)) for unprivileged A=a | Selection rate ratio (ideal: ≥0.8). | Group fairness |
| Equalized Odds (EO) Difference | max(|P(Ŷ=1 | Y=y, A=0) - P(Ŷ=1 | Y=y, A=1)| for y=0,1) | Equal TPR/FPR across groups. | Predictive fairness |
| Equal Opportunity (EOpp) Difference | |P(Ŷ=1 | Y=1, A=0) - P(Ŷ=1 | Y=1, A=1)| | Equal TPR for true positives. | Predictive fairness |
| Average Odds Difference (AOD) | 0.5 × (|TPR_0 - TPR_1| + |FPR_0 - FPR_1|) | Balanced EO. | Predictive fairness |
| Predictive Parity (PPV/NPV Difference) | |PPV_0 - PPV_1| or |NPV_0 - NPV_1| | Equal precision/negative precision. | Calibration fairness |
| Calibration Difference | max |P(Y=1 | Ŷ=1, A=0, p) - p| - |P(Y=1 | Ŷ=1, A=1, p) - p| | Predicted probs match outcomes equally. | Calibration |
Key Terms:
- A=0: Unprivileged; A=1: Privileged.
- TPR: True Positive Rate; FPR: False Positive Rate; PPV: Positive Predictive Value; NPV: Negative Predictive Value.
3. Testing Procedure
- Prepare Data: Split dataset (80/10/10 train/val/test). Stratify by Y and cohorts.
- Train/Baseline Model: Train model (e.g., logistic regression, XGBoost).
- Compute Metrics:
- Generate predictions on test set.
- Use libraries:
fairlearn.metrics.MetricFrame or aif360.datasets.
- Aggregate: Mean, worst-case (max disparity), and intersectional disparities.
- Statistical Testing: Bootstrap (n=1000) for confidence intervals (95% CI). Flag if disparity > threshold with p<0.05.
- Document: Report per-metric, per-cohort table + visualizations (e.g., fairness radar plot).
Example Output Table (Gender cohorts, Hiring Model):
| Metric | Male (Priv) | Female (Unpriv) | Disparity | Passes Threshold? |
|---|
| DP Diff | - | - | 0.12 | ❌ (>0.1) |
| DI Ratio | - | - | 0.75 | ❌ (<0.8) |
| EO Diff | - | - | 0.08 | ✅ (<0.1) |
4. Thresholds and Decision Criteria
-
Acceptable: All metrics within thresholds (conservative for deployment).
| Metric | Threshold |
|---|
| DP Difference | ≤0.10 |
| DI Ratio | ≥0.80 |
| EO/EOpp/AOD Difference | ≤0.10 |
| PPV/NPV Difference | ≤0.10 |
| Calibration Difference | ≤0.05 |
-
Warning: 1-2 violations.
-
Fail: ≥3 violations or any CI excludes threshold.
-
Re-test after hyperparameter tuning or retraining.
5. Remediation Strategies
Apply iteratively: Test → Remediate → Retest. Prioritize based on severity.
| Strategy | Type | Description | When to Use | Tools |
|---|
| Data Reweighting/Sampling | Pre-processing | Upsample unprivileged; reweight loss by cohort inverse frequency. | Small disparities, imbalanced data. | SMOTE, fairlearn.reductions |
| Massaging | Pre-processing | Flip k labels in privileged group to balance rates. | Label noise tolerable. | AIF360 Massaging |
| Fairness Constraints | In-processing | Add Lagrangian term to loss: min(L + λ × fairness_violation). | Custom models. | fairlearn.reductions.ExponentiatedGradient |
| Threshold Optimization | Post-processing | Derive cohort-specific thresholds for equalized odds. | Deployment-ready models. | fairlearn.postprocessing.ThresholdOptimizer |
| Adversarial Debiasing | In-processing | Train adversary to unlearn protected attribute; minimize its accuracy. | High-stakes (e.g., recidivism). | aif360.algorithms.AdversarialDebiasing |
| Rejects/Abstentions | Post-processing | Allow model to abstain on uncertain predictions for unprivileged. | Reject option available. | fairlearn.postprocessing |
| Data Improvements | Upstream | Audit sources for bias; augment with synthetic fair data. | Root-cause in data. | GANs (e.g., FairGAN) |
Remediation Workflow:
- Start with pre-processing (low-risk).
- If fails, in-processing.
- Post-process as last resort (may hurt accuracy).
- Monitor utility trade-off: ΔAccuracy <5%; if >, iterate.
- Deployment Guardrails: Continuous monitoring post-deploy; retrain quarterly.
Risks & Best Practices:
- Trade-offs: Fairness often reduces accuracy (5-15%); quantify Pareto frontier.
- Legal: Align with US EEOC 80% rule, EU AI Act high-risk requirements.
- Documentation: Log all runs in artifact store (e.g., MLflow).
- Human Review: Escalate fails to ethicist/stakeholder.
This protocol is adaptable—tailor cohorts/metrics to domain (e.g., add income for lending). Implement as a CI/CD pipeline for automation. If you provide a specific model/dataset, I can run a demo audit.