Part XXI — Explainable AI & Model Interpretability · Chapter 7

SHAP for real models

Hook

The pizza game had 3 features and 6 orderings to average — already a lot of arithmetic by hand. A real model can have hundreds of features, and n!n! grows far too fast to ever enumerate. SHAP — short for SHapley Additive exPlanations — is what makes Shapley values usable in practice — and for one common case, it doesn't need the orderings at all.

Intuition

A linear model predicting a house's price from four features. Every bar is that feature's SHAP value for this specific house — and unlike the pizza chapter, none of these came from averaging over 4!=244! = 24 orderings. For a linear model, there's an exact shortcut.

Formalize

For a linear model f(x)=b+iwixif(x) = b + \sum_i w_i x_i, the Shapley value has a closed form — no permutations needed at all:

ϕi=wi(xixˉi)\phi_i = w_i\,(x_i - \bar x_i)
  • ϕi\phi_i — feature ii's SHAP value: its contribution to this specific prediction.
  • wiw_i — the linear model's weight (coefficient) for feature ii.
  • xix_i — this instance's actual value for feature ii.
  • xˉi\bar x_i — feature ii's baseline value, typically its average across some reference population.
  1. Credit is weight times distance from typical

    A feature's credit is just its weight times how far this instance sits from "typical" — exactly what you'd guess a fair split should look like.

  2. Provably matches the full permutation average

    For a linear model specifically, this closed form is provably the same answer the full permutation average from the pizza chapter would have given.

Play

Drag the size slider. Only size's own bar moves — every other feature's SHAP value is untouched, because in a linear model each feature's contribution genuinely doesn't depend on any other feature's value. Watch the total, though: baseline plus every bar summed still always equals the exact prediction, at any size you drag to.

Worked example

Baseline (a typical house): size 2020, age 1515, distance 1010, not renovated — predicting 320320. This house: size 2525, age 55, distance 88, renovated — predicting 445445:

  1. Size's contribution

    wsize(2520)=15×5=75w_{\text{size}}(25-20) = 15 \times 5 = 75 — five units above baseline, each worth 1515.

  2. Age and distance's contributions
    • Age: wage(515)=2×(10)=20w_{\text{age}}(5-15) = -2\times(-10) = 20 — younger than baseline, and age's weight is negative, so a below-baseline age adds value.
    • Distance: 5×(810)=10-5\times(8-10) = 10, the same logic.
  3. Renovated's contribution, and the total

    Renovated: 20×(10)=2020\times(1-0)=20. All four sum to 75+20+10+20=12575+20+10+20=125, and 320+125=445320 + 125 = 445 — exactly the predicted price, with no approximation anywhere.

Checkpoint

Drag size until its own SHAP contribution reaches at least 90.

Drag the slider to try it
Summary
ϕi=wi(xixˉi)\phi_i = w_i\,(x_i - \bar x_i)

This exact shortcut only exists because the model is linear — the moment there's any interaction between features (a tree split, a neural network's nonlinearity), the shortcut breaks and real SHAP implementations fall back to smart sampling strategies that approximate the permutation average without ever computing all n!n! of them. The takeaway carries over regardless: SHAP is Chapter 5's fair-credit idea, engineered to actually run on models with more than three features. The next chapter asks a different kind of question about the same prediction: not "how much did each feature contribute," but "what's the smallest change that would have flipped the answer?"