Sub topic 14
Limitations: correlation, causality and cost
This chapter assumes you can compute SHAP values and read the standard plots from earlier chapters. It covers three places where SHAP output is easy to misread or expensive to produce.
Correlated features share credit unpredictably
SHAP values describe how a model uses its inputs. They do not describe how the world works. When two features carry the same signal, the split of credit between them depends on which one the model happened to learn.
The brute-force sketch below shows this. Both models give the same predictions on real rows, where the two features are always equal. Their SHAP values differ.
from itertools import permutations
def shapley(f, x, baseline):
n = len(x)
phi = [0.0] * n
orders = list(permutations(range(n)))
for order in orders:
z = list(baseline)
prev = f(z)
for i in order:
z[i] = x[i]
cur = f(z)
phi[i] += cur - prev
prev = cur
return [p / len(orders) for p in phi]
x = [1, 1] # two copies of the same signal
baseline = [0, 0]
uses_first_only = lambda z: 2 * z[0]
uses_both = lambda z: z[0] + z[1]
print(shapley(uses_first_only, x, baseline)) # [2.0, 0.0]
print(shapley(uses_both, x, baseline)) # [1.0, 1.0]A tree ensemble trained on near-duplicate columns can behave like either model, and a different random seed can change the split. Do not rank correlated features by their individual SHAP values. Compare them as a group, for example by summing their values per row.
Correlation also affects how features are removed. Methods that mask a feature by sampling it from the background data can create rows that never occur in practice, such as a tall person with a very low weight. The model's output on those rows may be unreliable, and it still feeds into the attribution.
SHAP is not causal
A large SHAP value means the model's output moves a lot when that feature changes. It does not mean that changing the feature in the real world would change the outcome.
- A feature can be a proxy for something else. A model may use "number of hospital visits" to predict illness, and the visits do not cause the illness.
- Changing a feature's value for one person may also change other features that are linked to it. SHAP values do not account for that.
- A dependence plot shows what the model learned from the data. It does not show a treatment effect.
Other misreadings
- Global plots hide detail. A bar plot of mean absolute SHAP values shows average magnitude. A feature that matters a lot for 2% of rows can look unimportant.
- Results depend on the background data. A different background sample changes the baseline and can change the attributions. Report which background you used.
- Units matter. For classifiers, values are often in log-odds, not probability. A SHAP value of 0.5 is not a 50 percentage-point change.
- Explanations describe the model, not its quality. A badly trained model gets faithful explanations of its bad behavior.
Computational cost
Exact Shapley values need every subset of features. The number of subsets doubles with each added feature.
for n in (3, 10, 20, 30):
print(n, 2 ** n)
# 3 8
# 10 1024
# 20 1048576
# 30 1073741824In practice the cost depends on the explainer.
| Explainer | Cost | Notes |
|---|---|---|
| TreeExplainer | Polynomial in tree size | Fast for tree ensembles. Interaction values cost much more. |
| PermutationExplainer | Grows with features, background size and number of permutations | Approximate. Needs many model calls. |
| KernelExplainer | Grows quickly with background size and samples | Slow. Reduce the background with a summary such as k-means. |
To keep cost down, explain a sample of rows, shrink the background to around 100 rows, and use a dedicated explainer when one exists.
Exercise
Two features, A and B, are copies of each other. After retraining with a new random seed, A's mean absolute SHAP value drops and B's rises. Has the model's behavior on real data changed, and what should you report?
Show answer
Probably not. The model likely splits credit between interchangeable features differently. Report the combined SHAP value of A and B as a group, because the individual ranking is unstable.
Exercise
A SHAP plot shows that a high "discount" value pushes predicted sales up. Can you conclude that raising discounts will raise sales?
Show answer
No. SHAP shows how the model uses the feature, not what would happen if you changed it. Discounts may coincide with holidays or promotions that drive sales. A causal analysis or experiment is needed to support that claim.