Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 12 — Lab: Explainability and Responsible AI

Open in Colab

Lab Overview

You will write the main explanation methods yourself, for any model given as a function, and audit a model for fairness.

ExerciseTopicPoints
1Permutation importance15
2Importance of a group of features10
3Partial dependence15
4Exact Shapley values25
5A one-feature counterfactual15
6Fairness gaps20
ChallengeA threshold for equal opportunity+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Models are given as plain functions, so your code works for any of them. Do not change the check cells.


Exercise 1: Permutation Importance (15 points)

perm_importance(predict, X, y, score, repeats=10, seed=0) returns one number per feature: the mean drop of score(y, predict(X)) when that feature’s column is shuffled, over repeats shuffles. Use rng = np.random.default_rng(seed) and rng.permutation.


Exercise 2: A Group of Features (10 points)

group_importance(predict, X, y, score, cols, repeats=10, seed=0) shuffles the columns cols together (the same permutation of rows for all of them) and returns the mean drop.


Exercise 3: Partial Dependence (15 points)

pdp(predict, X, j, grid) returns, for each value v in grid, the average of predict(Z) where Z is X with column j set to v for every row.


Exercise 4: Exact Shapley Values (25 points)

shapley(value, n) takes a function value(S) giving the worth of any tuple of feature indices S (sorted), and returns the Shapley value of each of the n features:

ϕj=∑S⊆F∖{j}∣S∣! (n−∣S∣−1)!n! [v(S∪{j})−v(S)]\phi_j = \sum_{S \subseteq F \setminus \{j\}} \frac{|S|!\,(n - |S| - 1)!}{n!}\,\big[v(S \cup \{j\}) - v(S)\big]

Pass S and S ∪ {j} to value as sorted tuples.


Exercise 5: A One-Feature Counterfactual (15 points)

counterfactual(predict, x, j, factors) multiplies feature j of the single example x by each factor in factors, trying the factors closest to 1 first, and returns the first factor that changes predict (applied to x[None]) from its original value; None if none does.


Exercise 6: Fairness Gaps (20 points)

fairness_gaps(approve, y, group) takes three boolean arrays (approved, deserves approval, member of group B) and returns (demographic parity difference, equal opportunity difference), both as B minus A: the difference in approval rates, and the difference in approval rates among those with y true.


Challenge: A Threshold for Equal Opportunity (+10 points)

equal_opportunity_threshold(p, y, group, t_a=0.5): group A keeps the threshold t_a. Return the threshold for group B that gives B the same true positive rate as A, as the (1 − TPR_A) quantile of the scores of B’s deserving members (np.quantile).


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: the challenge equalises true positive rates. Which other gap does it change, and in which direction? Would you use it for loans? For hiring?