Code / AI & ML Basics / Unit 4: Overfitting and fairness

Bias and fairness पक्षपात और निष्पक्षता

Lesson 11 of 13 · about 20 minutes · Check a model group by group, not just overall.

⚡ 0 XP

Step 1 of 2

Same rule, different outcomes

A (made-up) bank uses a model score to approve loans: score ≥ 65 → approved. Sounds fair — one rule for all.

But suppose the score was trained on old data where people from small towns, or from Hindi-medium schools, were rarely given loans. The model learns that pattern and gives them lower scores, even when they repay just as well. The rule is the same; the data carried the bias.

So always check results per group:

  • approval rate: what share of each group gets a yes?
  • accuracy per group: is the model equally right for everyone?

Try it above: move the threshold and watch the approval rate and accuracy of each group change.

▶ Try it Fairness: a scholarship shortlist (made-up data)
AB

● would do well · ◼ would struggle

SelectedCorrectGood ones missed
Group A (city)50%80%6
Group B (village)7%70%9

Both groups have the same share of strong students, but the old test gave group B lower scores. Who is treated unfairly by one cut-off? Try “separate cut-offs” — then think about why the real fix is better data.

🎯 Challenge: Approval rate per group +15 XP

Write approvalRate(people, group, threshold): look only at people whose group matches, and return the fraction of them with score >= threshold (a number 0–1). If nobody is in the group, return 0.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • At 65, group A: 4 of 6 approved
  • At 65, group B: 2 of 6 approved
  • Edge cases: score equal to threshold, empty group
Lesson notes (text version)

1. Same rule, different outcomes

A (made-up) bank uses a model score to approve loans: score ≥ 65 → approved. Sounds fair — one rule for all.

But suppose the score was trained on old data where people from small towns, or from Hindi-medium schools, were rarely given loans. The model learns that pattern and gives them lower scores, even when they repay just as well. The rule is the same; the data carried the bias.

So always check results per group:

  • approval rate: what share of each group gets a yes?
  • accuracy per group: is the model equally right for everyone?

Try it above: move the threshold and watch the approval rate and accuracy of each group change.

Challenge: Approval rate per group

Write approvalRate(people, group, threshold): look only at people whose group matches, and return the fraction of them with score >= threshold (a number 0–1). If nobody is in the group, return 0.

2. Is it fair? Measure, then fix

In our made-up data, both groups repaid equally often (4 of 6). Yet group A is approved twice as often as group B, and the model is wrong far more often for B. That is a red flag.

What can people do about it?

  • check approval rate and accuracy for every group before launch
  • fix the data: collect fairer, more representative examples
  • drop unfair features (and their stand-ins, like PIN code standing in for community)
  • keep a human in the loop for big decisions, and let people appeal

The same holds for exam shortlisting, job filters or face recognition. A model is never neutral just because it is maths.

Challenge: Fairness report

approvalRate is ready. Write:

  • groupAccuracy(people, group, threshold): in that group, the model says yes when score >= threshold; it is correct when that yes/no equals repaid. Return the fraction correct (0 for an empty group).
  • rateGap(people, threshold): the absolute difference between the approval rates of groups "A" and "B".

Search by task, format or tool name.