TEC-DATA85-10 · Lesson 6 · 14 clock hours · lab
Algorithmic Bias: Detection, Measurement, and Mitigation
Objective. Students will identify at least two sources of bias in a provided machine-learning pipeline, calculate at least three fairness metrics (demographic parity, equalized odds, and predictive parity), and apply one mitigation strategy, comparing metrics before and after.
⚠ Pending professional review
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Why Algorithmic Bias Matters
A machine-learning model trained on historical data can reproduce — and even amplify — patterns of discrimination that existed in that history. In high-stakes domains like loan approval, hiring, and healthcare, a biased model can cause real harm to real people. Regulators, employers, and professional standards increasingly expect data practitioners to detect and document bias before deployment. This lesson gives you the practical skills to do that.
Sources of Bias in a Machine-Learning Pipeline
Bias can enter a pipeline at several stages:
Historical bias exists in the real world before data collection begins. If lenders historically approved fewer loans for a particular demographic group, a model trained on those approvals will learn that pattern.
Representation bias occurs when a training dataset under-represents a group. If only 8% of training records belong to one demographic group, the model has too few examples to learn accurate decision boundaries for that group.
Measurement bias happens when features are recorded less accurately for some groups. For example, credit-bureau data may be thinner for recent immigrants, producing more missing values and less reliable scores.
Proxy features are variables that correlate strongly with a protected attribute even though they are not the attribute itself. ZIP code, for instance, reflects residential segregation and can serve as a proxy for race. Leaving such features in the model can reintroduce the very discrimination you tried to avoid by omitting race directly.
Label bias arises when the outcome label itself reflects past human judgment that was biased — for example, "default or not" labels that reflect discriminatory collection practices rather than true creditworthiness.
In your audit, you should document which stages produced the bias you find and explain the mechanism.
Three Fairness Metrics You Will Calculate
Fairness metrics compare model performance across demographic groups. There is no single metric that captures all dimensions of fairness; each answers a different question. You will use a threshold of 0.10: a difference above that threshold indicates a meaningful disparity.
Demographic Parity Difference (DPD) measures whether the model approves loans at the same rate for both groups regardless of their actual creditworthiness.
> DPD = P(Ŷ = 1 | Group A) − P(Ŷ = 1 | Group B)
A DPD of 0 means equal approval rates. A positive value means Group A is approved more often.
Equalized Odds Difference (EOD) checks whether both the true positive rate (TPR) and the false positive rate (FPR) are equal across groups. Equalized odds requires the model to be equally accurate for applicants who truly should be approved and equally cautious for those who should not.
> EOD = max(|TPR_A − TPR_B|, |FPR_A − FPR_B|)
Some implementations report TPR difference and FPR difference separately; consult the Fairlearn documentation for the exact form your version uses.
Predictive Parity (also called precision parity) asks: among all applicants the model approves, does the proportion who truly qualify differ by group?
> Predictive Parity Difference = Precision_A − Precision_B
where Precision = TP / (TP + FP) for each group.
These three metrics sometimes conflict with each other mathematically. Improving demographic parity can reduce equalized odds, and vice versa. This is not a bug in your calculations; it reflects a proven theoretical result. Document the trade-offs and justify your choice of mitigation.
Conducting a Bias Audit
1. Separate your confusion matrices. Filter the test dataset by demographic group and build a confusion matrix for each group independently. Never compute fairness metrics on the aggregate dataset — aggregation hides disparities.
2. Examine class imbalance. Use `value_counts()` on the label column within each group. Large imbalances mean the model saw far fewer positive examples for one group during training.
3. Inspect missing-value patterns by group. Use `df.groupby('group')['feature'].isna().mean()` to detect whether missingness is concentrated in one group. Imputation strategies that ignore group membership can propagate bias.
4. Identify proxy features. Compute correlation or mutual information between each feature and the protected attribute. Flag any feature with a notably high association.
5. Document everything in the provided bias-audit report template. Write a plain-language explanation of each finding suitable for a non-technical manager.
Mitigation Strategies
You will apply one pre-processing strategy in this lab:
- Sample reweighting assigns higher training weights to under-represented group-label combinations so the model treats them as more important. The `imbalanced-learn` library and Fairlearn both provide utilities for this.
- Proxy feature removal eliminates the feature most responsible for transmitting bias. After removal, retrain and recalculate metrics. Note that removal alone may not be enough if other correlated features remain.
After applying your chosen strategy, retrain the model and recalculate all three metrics. Present a before/after comparison table and write a paragraph interpreting the change. Acknowledge any metric that did not improve or that worsened.
Safety and Professional Responsibility
Data work does not involve physical hazards, but it carries ethical hazards with real consequences for people outside the room. Treat demographic data with care: use only the synthetic dataset provided; never upload real personal data to a shared server or public notebook. Follow your program's data-handling policy. When you write your audit report, be honest about limitations — a mitigation that improves one metric while degrading another must be disclosed, not hidden.
Key terms8
Demographic Parity DifferenceThe difference in positive prediction rates (e.g., loan approval rates) between two demographic groups; values above 0.10 in magnitude indicate a meaningful disparity.
Equalized OddsA fairness criterion requiring that both the true positive rate and the false positive rate are equal across demographic groups, so the model is equally accurate and equally cautious for all groups.
Predictive ParityA fairness criterion requiring that the precision (proportion of positive predictions that are correct) is equal across demographic groups.
Proxy FeatureA variable that is not a protected attribute itself but correlates strongly with one — for example, ZIP code as a proxy for race — and can allow a model to discriminate indirectly.
Representation BiasBias that arises when a training dataset contains far fewer records for one demographic group than another, giving the model insufficient examples to learn that group's patterns accurately.
Sample ReweightingA pre-processing mitigation technique that assigns higher training weights to under-represented group-label combinations so the learning algorithm treats them as more important during optimization.
Confusion MatrixA table showing true positives, true negatives, false positives, and false negatives for a classification model; computing separate matrices per demographic group is the foundation of a bias audit.
Historical BiasBias that originates in real-world patterns of discrimination before any data is collected, and which a model can learn and perpetuate if trained on records that reflect those patterns.
Worked examples4
1. You compute the following confusion matrices from a loan-approval model tested on 200 applicants in Group A and 200 in Group B. Group A: TP=80, FP=20, FN=10, TN=90. Group B: TP=50, FP=10, FN=30, TN=110. Calculate the Demographic Parity Difference.
- Step 1 — Calculate the approval (positive prediction) rate for Group A. Approvals for A = TP + FP = 80 + 20 = 100. Total A = 200. Rate_A = 100 / 200 = 0.50.
- Step 2 — Calculate the approval rate for Group B. Approvals for B = TP + FP = 50 + 10 = 60. Total B = 200. Rate_B = 60 / 200 = 0.30.
- Step 3 — Subtract: DPD = Rate_A − Rate_B = 0.50 − 0.30 = 0.20.
- Step 4 — Compare to threshold: |0.20| > 0.10, so this disparity is meaningful and must be reported.
Answer: Demographic Parity Difference = 0.20, which exceeds the 0.10 threshold, indicating Group B receives approvals at a substantially lower rate than Group A.
2. Using the same confusion matrices (Group A: TP=80, FP=20, FN=10, TN=90; Group B: TP=50, FP=10, FN=30, TN=110), calculate the Equalized Odds Difference.
- Step 1 — Calculate the True Positive Rate (TPR) for each group. TPR_A = TP / (TP + FN) = 80 / (80 + 10) = 80 / 90 ≈ 0.889. TPR_B = 50 / (50 + 30) = 50 / 80 = 0.625.
- Step 2 — Calculate the False Positive Rate (FPR) for each group. FPR_A = FP / (FP + TN) = 20 / (20 + 90) = 20 / 110 ≈ 0.182. FPR_B = 10 / (10 + 110) = 10 / 120 ≈ 0.083.
- Step 3 — Compute the absolute differences. |TPR_A − TPR_B| = |0.889 − 0.625| = 0.264. |FPR_A − FPR_B| = |0.182 − 0.083| = 0.099.
- Step 4 — Equalized Odds Difference = max(0.264, 0.099) = 0.264.
- Step 5 — Compare to threshold: 0.264 > 0.10, indicating a significant disparity in true positive rates across groups.
Answer: Equalized Odds Difference = 0.264, driven primarily by the TPR gap (0.264). The model correctly approves qualified applicants in Group B far less often than in Group A.
3. Using the same confusion matrices (Group A: TP=80, FP=20; Group B: TP=50, FP=10), calculate the Predictive Parity Difference.
- Step 1 — Calculate Precision for Group A. Precision_A = TP / (TP + FP) = 80 / (80 + 20) = 80 / 100 = 0.80.
- Step 2 — Calculate Precision for Group B. Precision_B = TP / (TP + FP) = 50 / (50 + 10) = 50 / 60 ≈ 0.833.
- Step 3 — Compute Predictive Parity Difference = Precision_A − Precision_B = 0.80 − 0.833 = −0.033.
- Step 4 — Compare |−0.033| = 0.033 to threshold 0.10. The difference is below the threshold, so predictive parity is approximately satisfied even though demographic parity and equalized odds are not.
Answer: Predictive Parity Difference ≈ −0.033, within the 0.10 threshold. Among approved applicants, both groups are equally likely to be truly qualified — but the model still approves far fewer Group B applicants overall.
4. After applying sample reweighting, a retrained model produces: Group A: TP=75, FP=15, FN=15, TN=95; Group B: TP=65, FP=15, FN=15, TN=105. Recalculate Demographic Parity Difference and determine whether the mitigation improved it.
- Step 1 — New approval rate for Group A = (TP + FP) / total = (75 + 15) / 200 = 90 / 200 = 0.45.
- Step 2 — New approval rate for Group B = (65 + 15) / 200 = 80 / 200 = 0.40.
- Step 3 — New DPD = 0.45 − 0.40 = 0.05.
- Step 4 — Before mitigation DPD was 0.20; after mitigation DPD is 0.05, now below the 0.10 threshold.
- Step 5 — Conclude that the mitigation improved demographic parity substantially. Document this result in the before/after comparison table and note any changes to equalized odds and predictive parity as well.
Answer: Post-mitigation Demographic Parity Difference = 0.05, below the 0.10 threshold. The reweighting strategy meaningfully reduced the approval-rate disparity between the two groups.
The math in this lesson1
Learners compute group approval rates and their difference to measure demographic parity.
7.RP.3
In class: Bias audit: students load a provided pre-trained binary classification model and its test dataset (synthetic loan-approval data with demographic attributes) into a Jupyter Notebook; they compute confusion matrices separately for two demographic groups and calculate demographic parity difference, equalized odds difference, and predictive parity, interpreting each value against a threshold of 0.10. · Root-cause analysis: students trace bias back to the training data and feature set by examining class imbalance, missing-value patterns by group, and proxy features (for example, ZIP code as a proxy for race); they document findings in a structured bias-audit report template. · Mitigation and re-evaluation: students apply one pre-processing mitigation — reweighting training samples using the imbalanced-learn library or removing the most problematic proxy feature — then retrain the model and recalculate the three fairness metrics, presenting a before/after comparison table with a written interpretation.
Materials: Jupyter Notebook, scikit-learn, imbalanced-learn, Fairlearn library (fairlearn.org), synthetic loan-approval dataset (instructor-created), bias-audit report template, Barocas et al. Fairness and Machine Learning textbook (free at fairmlbook.org)
Check: Bias-audit report and before/after comparison table reviewed by instructor; pass requires all three fairness metrics correctly calculated before mitigation, at least one mitigation applied with documented rationale, and metrics recalculated correctly after mitigation. Pass mark: 75% on a 20-point rubric.
Practice check8
Answer each question, then check it. Nothing is recorded; this is your own practice.
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.