Ethical AI & Bias Mitigation
By the end of this lesson you'll be able to measure whether a model treats groups fairly, explain its decisions, protect people's privacy, and write the audit code that catches bias before it ships.
Learn Ethical AI & Bias Mitigation in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice…
Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
Real-World Analogy: a fair referee
Think of an AI model as a referee in a football match. A fair referee applies the same rules to both teams: a foul is a foul whoever commits it, and a goal counts no matter which side scores. The crowd can see every decision, so the referee can be questioned and overruled.
A biased referee blows the whistle more often on one team — maybe without even realising it, because that's how they were trained. AI models do exactly this: they absorb bias from historical data and apply it at massive scale, silently. Everything in this lesson is a tool for being a fair referee: measure the calls per team (fairness metrics), show your reasoning (explainability), respect the players (privacy), and let yourself be reviewed (accountability).
1 Where Does Bias Enter?
Bias is a systematic error that pushes a model's outputs in one direction for one group. It rarely comes from one villain — it leaks in at several points, and your job is to know all of them.
- Training data: if past hiring favoured one group, the model learns "favour that group" as a rule.
- Sampling: if a group is barely present in the data, the model never learns to serve it well.
- Labels: the "correct answers" themselves may encode human prejudice (who was approved before).
- Proxy variables: a feature like ZIP code or first name can secretly stand in for race or gender.
- Objective: optimising only for accuracy lets the model ignore small groups entirely.
Real systems have caused real harm by missing these:
Case
What Happened
Where bias entered
Amazon hiring
Résumé screener penalised women
Biased historical labels
COMPAS
Recidivism model harsher on Black defendants
Proxy variables + labels
Healthcare algorithm
Used cost as a proxy for need
Bad objective / proxy
Facial recognition
35% error for dark-skinned women vs 1% for light-skinned men
Unbalanced sampling
2 Measuring Fairness — Parity, the 4/5ths Rule & Equalised Odds
You can't fix what you don't measure. The simplest fairness metric is demographic parity : the selection rate (the share of a group that gets a positive outcome) should be roughly equal across groups.
Regulators turn that into a number with the 4/5ths rule : divide the lowest group's selection rate by the highest to get the disparate-impact ratio . If it falls below 0.8 , that is treated as adverse impact you must investigate.
Parity alone can be misleading, so also check equalised odds : among people who actually deserved a positive outcome, the model's success rate should be the same for every group. The worked example below computes selection rates per group and the disparate-impact ratio from a tiny dataset — read every comment, then run it.
3 Transparency & Explainability (SHAP / LIME)
If a model denies someone a loan, they deserve to know why . Explainability tools answer that by attributing one prediction to the features that drove it: "+0.30 from income, −0.90 from late payments". That turns a black box into a sentence a human can challenge.
SHAP uses game theory to split a prediction fairly among its features and is consistent across the whole model. LIME builds a quick, simple approximation around a single prediction. The worked example shows the core idea — every feature's contribution is its value times its weight — and points to the real libraries.
4 Privacy — PII & Differential Privacy
PII (personally identifiable information) is anything that pins data to a real person — name, email, exact address. The first rule is simple: strip direct identifiers before the data reaches the model .
But redaction is not enough — clever attackers can re-identify people from "anonymous" patterns. Differential privacy fixes this by adding calibrated random noise so that adding or removing any single person barely changes the result. The knob is the privacy budget epsilon (ε) : lower ε means more privacy and less accuracy. The worked example redacts PII and applies the differential-privacy idea to a salary average.
5 Accountability & Harmful-Misuse Risk
Accountability means a specific, named human owns the model's outcomes — not "the algorithm". When a decision is wrong, there must be a person to appeal to, a log of what the model did, and a clear path to override it. "The model said so" is never an acceptable answer.
- An owner: a named team responsible for harms, not just accuracy.
- An audit trail: logged inputs, outputs, and model versions so you can reconstruct any decision.
- A human in the loop: someone who can review and reverse high-stakes outcomes.
- A model card: a short document stating the model's intended use, limits, and known risks.
You also have to think about misuse — harm caused by using the model outside its intended purpose. A face-matcher built for unlocking phones could be repurposed for mass surveillance; a text generator could mass-produce scams or disinformation. Mitigation means restricting access, rate-limiting, watermarking outputs, refusing dangerous requests, and red-teaming the system before release.
6 Common Mistakes (And How to Fix Them)
Feeding in years of skewed historical decisions teaches the model to copy them.
✅ Fix: audit the data first — check per-group base rates and rebalance or reweight before training.
A 95%-accurate model can still fail a minority group completely, because accuracy averages over everyone.
✅ Fix: track per-group metrics (selection rate, true-positive rate) alongside overall accuracy.
"It passed validation" is not the same as "it's fair and safe in the real world".
✅ Fix: run a fairness audit (the 4/5ths rule), write a model card, and re-audit on a schedule after launch.
❌ Dropping a protected column and assuming you're done
Removing "gender" does nothing if a proxy variable (name, ZIP code, club membership) still encodes it.
✅ Fix: test remaining features for correlation with the protected attribute, then drop or decorrelate the proxies too.
📋 Quick Reference — Ethical AI
Concept
What it means
Demographic parity
Equal selection rates across groups
Equalised odds
Equal true-positive (and false-positive) rates across groups
Disparate impact / 4/5ths rule
min(rate) / max(rate); below 0.8 = adverse impact
Proxy variable
A feature that secretly encodes a protected attribute
SHAP / LIME
Attribute one prediction to its features (explainability)
PII
Data that identifies a real person; redact before training
Differential privacy (ε)
Add noise so no individual is recoverable; lower ε = more privacy
Accountability
A named owner, audit trail, and human override
❓ Frequently Asked Questions
🎯 Mini-Challenge: Build a Fairness Auditor
Time to fly solo. Using only what you've learned, write a small program that audits a model's decisions for two regions and flags adverse impact. The starter block has the brief and the expected output — the logic is up to you.
🎉 Lesson Complete!
You can now spot where bias enters a system, measure it with demographic parity, equalised odds, and the disparate-impact ratio, explain a single prediction the SHAP/LIME way, protect privacy with PII redaction and differential privacy, and name who's accountable when things go wrong. That is the toolkit of a fair referee.
🚀 Up next: the Final Project — put everything from this course together into a complete, responsible end-to-end ML system.
Practice quiz
What is bias in an AI model?
- A systematic error that pushes outputs in one direction for a group
- A random error that averages out
- The model's learning rate
- The number of features used
Answer: A systematic error that pushes outputs in one direction for a group. Bias is a systematic error — for example consistently scoring one group lower than another.
What does demographic parity require?
- Equal accuracy on the training set
- Roughly equal selection rates across groups
- Equal numbers of features per group
- That protected attributes are used directly
Answer: Roughly equal selection rates across groups. Demographic parity asks that the selection rate (share given a positive outcome) be roughly equal across groups.
How is the disparate-impact ratio computed?
- Highest selection rate divided by lowest
- Lowest selection rate divided by highest
- Total approvals divided by total applicants
- Accuracy divided by precision
Answer: Lowest selection rate divided by highest. The disparate-impact ratio is the lowest group's selection rate divided by the highest group's rate.
Under the four-fifths (80%) rule, what ratio signals adverse impact?
- Below 0.8
- Above 0.8
- Exactly 1.0
- Above 0.5
Answer: Below 0.8. A disparate-impact ratio below 0.8 is treated as evidence of adverse impact that must be justified or fixed.
What is a proxy variable?
- A feature that secretly stands in for a protected attribute
- A variable that holds the model's accuracy
- A backup copy of the dataset
- A feature with no predictive power
Answer: A feature that secretly stands in for a protected attribute. A proxy (like ZIP code for race) secretly encodes a protected attribute, so dropping the protected column alone does not remove the bias.
What do SHAP and LIME do?
- Speed up training on GPUs
- Attribute a single prediction to the features that drove it
- Add noise for privacy
- Balance the dataset across groups
Answer: Attribute a single prediction to the features that drove it. SHAP and LIME are explainability tools that attribute a prediction to its features, turning a black box into something auditable.
What is differential privacy?
- Encrypting the model weights
- Adding calibrated noise so no single person's data can be re-identified
- Deleting all numeric columns
- Training a separate model per user
Answer: Adding calibrated noise so no single person's data can be re-identified. Differential privacy adds calibrated random noise so adding or removing any one person barely changes the output.
In differential privacy, what does a lower epsilon (ε) mean?
- More privacy and less accuracy
- Less privacy and more accuracy
- No effect on the trade-off
- Higher learning rate
Answer: More privacy and less accuracy. The privacy budget epsilon controls the trade-off: lower ε means more privacy but noisier, less accurate results.
Why is optimising only for accuracy a problem for fairness?
- Accuracy is impossible to measure
- Accuracy averages over everyone and can hide poor performance on small groups
- Accuracy always favours the minority group
- Accuracy requires protected attributes
Answer: Accuracy averages over everyone and can hide poor performance on small groups. A high overall accuracy can hide that a minority group is served badly, so you must track per-group metrics too.
What does accountability for an AI system require?
- That the model is fully autonomous with no oversight
- A named owner, an audit trail, and a human who can override decisions
- That you delete all logs after deployment
- That accuracy is above 99%
Answer: A named owner, an audit trail, and a human who can override decisions. Accountability means a specific person owns outcomes, with logs and a human-in-the-loop able to review and reverse decisions.
Continue this course
- Previous: AutoML & NAS
- Next: Final Project