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: Explainability and Responsible AI — Implementation

Open in Colab
  1. Permutation importance by groups of features

  2. Partial dependence in two dimensions: seeing an interaction

  3. Shapley values for every feature, by sampling orders

  4. From local to global: the mean absolute Shapley value

  5. A counterfactual that may change two features

  6. A fairness report, group by group

  7. A model card

gradient boosting on diabetes: test R² 0.281

1. Permutation Importance by Groups

The six blood-serum measures s1 to s6 of the diabetes data are correlated. We compare each one alone with the whole group, on the test set, as the drop in R².

each serum measure alone: {'s1': -0.013, 's2': 0.01, 's3': -0.018, 's4': -0.001, 's5': 0.197, 's6': 0.012}
sum of the six single drops 0.186; the six shuffled together 0.286

s5 alone carries most of it, but the six shuffled together cost more than the sum of their single drops: the others share information with each other. Slightly negative drops mean shuffling happened to help a little: noise around zero.

2. Partial Dependence in Two Dimensions

A one-dimensional PDP averages over everything else. A two-dimensional PDP shows how two features act together; if the effect of one depends on the level of the other, they interact.

Source
<Figure size 1100x380 with 4 Axes>

3. Shapley Values for Every Feature, by Sampling

With 10 features, the exact formula needs 210=1 0242^{10} = 1\,024 subsets per feature, each averaged over a background sample. A cheaper estimate samples random orders of the features: walk through each order, add the features one by one, and record how much each one changes the prediction. Averaged over many orders, these changes converge to the Shapley values, and they always add up exactly to (prediction − average prediction) for each order.

test patient 118: prediction 367.0, average 162.9, average + sum of shares 367.0
  bmi    +99.4
  s5     +49.9
  bp     +23.7
  s2     +10.6
  age     +8.5

The library shap does this far faster for trees (TreeSHAP computes exact values in polynomial time), and adds the standard plots. The idea is the one above.

4. From Local to Global

Averaging the absolute Shapley values over many patients gives a global importance that is built from local explanations, and can be compared with permutation importance:

feature   mean |SHAP|  permutation
s5               27.2        0.221
bmi              25.4        0.181
bp                9.9        0.043
s3                7.2       -0.029
s2                6.3       -0.003
s6                5.3        0.017

Both put bmi and s5 on top. They measure different things (average push on the prediction, against loss of accuracy) and need not agree further down.

5. A Counterfactual That May Change Two Features

For a tumour the logistic regression calls malignant, we search the pairs of the five most important features, each scaled by a factor between 0.5 and 1.5, for the change that flips the decision with the smallest total relative change.

tumour 139 (P(benign) 0.24): "radius error" -25% and "mean concave points" -2% flip it to benign (total change 27%)

Here the pair is no improvement: its total change (27%) is about the single change of the chapter (26%, “radius error” alone); for other cases two small changes beat one large one, and only the search can tell. Real counterfactual tools add constraints: only features a person can act on, values that occur in the data, and a preference for few changes.

6. A Fairness Report

A function that reports, for each group, the size, the base rate, the approval rate, the true and false positive rates, and the precision: the numbers behind demographic parity, equal opportunity and predictive parity. The chapter’s simulated loans:

group      n  base rate  approved    TPR    FPR  precision
A       1148       0.68      0.75   0.91   0.41       0.82
B        852       0.47      0.37   0.64   0.14       0.81

Group B repays less often on average (a lower base rate, from the shorter histories), is approved half as often, and its good applicants are approved less often (TPR). When base rates differ, equal TPRs, equal FPRs and equal precision cannot all be reached at once: the report shows the trade-off; people decide it.

7. A Model Card

A model card is a short document that travels with the model. For the loan model above:

SectionContent
Purposerank loan applications for review by a credit officer; not for automatic refusal
Data4 000 simulated past applications (income, debt, credit-history length); past decisions as labels
Performanceaccuracy and recall on 2 000 held-out applications, with the fairness report above
Known limitstrained on biased past decisions; credit history is a proxy for group membership; untested on applicants outside the income range 10–90
Fairnessequal-opportunity difference −0.27 between groups; mitigation options and their costs (Section 12.8 of the chapter)
Oversightevery refusal reviewed by a person; monthly monitoring of approval rates by group
Contact and versionwho maintains it; version, date, training code

Summary

  • Permute correlated features as a group; single drops underestimate shared information.

  • Two-dimensional partial dependence reveals interactions that averages hide.

  • Sampling feature orders estimates Shapley values for any model; the shares always add up.

  • Mean absolute Shapley values give a global importance built from local explanations.

  • Counterfactual search can trade a few small changes against one large one.

  • A fairness report lays out base rates, approvals, TPR, FPR and precision per group.

  • A model card documents purpose, data, performance, limits, fairness and oversight.