Chapter 12 — Assignment Project: Auditing a Credit Model
Overview¶
A bank asks you to audit a credit model before it is used. The model must be understood (which features drive it, and how), explainable to applicants (why was I refused, and what would change it?), and fair across two groups of applicants. You end with a model card.
The data are generated below. Group membership is not a feature of the model, but you may use it for the audit.
| Part | Task | Points |
|---|---|---|
| A | Global explanation | 20 |
| B | Local explanations for three applicants | 25 |
| C | Fairness audit and two remedies | 30 |
| D | Model card | 25 |
import numpy as np
import warnings
import itertools
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.inspection import permutation_importance
warnings.filterwarnings('ignore')
def make_credit(n=8000, seed=5):
rng = np.random.default_rng(seed)
group = rng.random(n) < 0.35 # True = group B
age = np.clip(rng.normal(np.where(group, 30, 42), 9, n), 18, 80)
income = rng.lognormal(np.log(np.where(group, 2600, 3200)), 0.35, n)
debt_ratio = np.clip(rng.normal(0.32, 0.12, n), 0, 1)
late_payments = rng.poisson(np.where(group, 0.9, 0.6), n)
years_at_job = np.clip(rng.exponential(np.where(group, 3, 6), n), 0, 40)
district_risk = np.clip(rng.normal(np.where(group, 0.6, 0.4), 0.15, n), 0, 1) # a postcode-based index
logit = 0.0009 * (income - 3000) - 4 * (debt_ratio - 0.32) - 0.7 * late_payments + 0.08 * years_at_job + 0.6
repays = rng.random(n) < 1 / (1 + np.exp(-logit))
X = np.column_stack([age, income, debt_ratio, late_payments, years_at_job, district_risk])
return X, repays, group
names = ['age', 'income', 'debt_ratio', 'late_payments', 'years_at_job', 'district_risk']
X, y, group = make_credit()
X_tr, X_te, y_tr, y_te, g_tr, g_te = train_test_split(X, y, group, test_size=0.3, random_state=0, stratify=y)
model = GradientBoostingClassifier(random_state=0).fit(X_tr, y_tr)
print(f'repayment rate {y.mean():.2f}; test accuracy {model.score(X_te, y_te):.3f}')Part A — Global Explanation (20 points)¶
Compute the permutation importance of each feature on the test set (accuracy drop, 20 repeats).
age,years_at_jobanddistrict_riskall differ by group. Shuffle them together and report the drop.In a short paragraph: which features drive the model? Which of them could stand in for group membership?
# Your code hereYour paragraph:
Part B — Local Explanations (25 points)¶
Write a function that estimates Shapley values by sampling 200 feature orders (as in the implementation notebook), with a background of 100 training rows, for the model’s predicted probability of repayment.
Explain three applicants: the one the model is most sure will repay, the one it is most sure will not, and one refused applicant with a probability near 0.4.
For the refused applicant, find the smallest change to one actionable feature (
income,debt_ratioorlate_payments) that gets them approved. Explain whyageanddistrict_riskshould not be used for this.
# Your code hereYour explanations:
Part C — Fairness Audit (30 points)¶
For groups A and B on the test set: base rate of repayment, approval rate, true positive rate and false positive rate. Report the demographic parity and equal opportunity differences.
Remedy 1: retrain without
district_risk. Remedy 2: keep the model and choose a separate threshold for B that equalises the true positive rates. Report the same numbers for each, and the accuracy.Which remedy would you recommend to the bank, and what would you tell them about its costs?
# Your code hereYour recommendation:
Part D — Model Card (25 points)¶
Write the model card (Section 7 of the implementation notebook): purpose and intended users; data; performance with uncertainty; global and local explanation methods available to applicants; fairness results and the remedy chosen; known limits; human oversight; monitoring plan.
Grading Rubric¶
| Criterion | Excellent | Adequate | Insufficient |
|---|---|---|---|
| A (20) | single and group importances, proxies identified | importances only | missing |
| B (25) | three correct explanations, an actionable counterfactual, the non-actionable features argued | explanations without counterfactual | missing |
| C (30) | full audit, both remedies, a reasoned recommendation with costs | numbers without recommendation | missing |
| D (25) | complete, honest, specific | generic | missing |