skills/shap/references/theory.md
SHAP applies Shapley-style credit allocation to model explanations. The numerical result is defined by more than the fitted model: it also depends on the model output, background distribution, masking rule, and any coalition constraints.
For one scalar model output and one row x, SHAP constructs an additive decomposition:
f_explained(x) = base_value + sum_i phi_i(x)
f_explained is the exact output selected for explanation.base_value is the expected output under the explainer's reference game.phi_i is the attribution assigned to feature i.For modern tabular shap.Explanation objects:
reconstructed = explanation.base_values + explanation.values.sum(axis=1)
The equation is meaningful only when the output units are known. A raw margin, probability, log loss, and logit are different quantities.
Let F be the set of features and v(S) the value assigned to coalition S. A Shapley value for feature i is:
phi_i =
sum over S subset of F \ {i}
[ |S|! (|F|-|S|-1)! / |F|! ]
* [v(S union {i}) - v(S)]
This averages the marginal contribution of i over all feature orderings.
In machine learning, defining v(S) is the hard part because a model normally requires all inputs. The masker and background distribution specify how hidden features are integrated or imputed.
Classic Shapley values satisfy:
The SHAP paper describes related properties for additive feature-attribution methods:
These are mathematical allocation properties. They do not mean:
Avoid calling SHAP values "fair" without specifying that the term refers only to a cooperative-game allocation axiom.
Common tabular games include:
Hidden features are sampled from a background distribution independently of the observed features.
Conceptually:
v(S) = E_background[f(x_S, X_not_S)]
Advantages:
Limitations:
Hidden features are drawn from a distribution conditional on observed features:
v(S) = E[f(X) | X_S = x_S]
Advantages:
Limitations:
Only coalitions compatible with a hierarchy are considered. The resulting allocations are Owen values.
Advantages:
Limitation: the hierarchy is an assumption and changes the game.
There is no universal answer to "the SHAP value" without specifying the game.
The base value is the reference-game expectation, not automatically:
Different backgrounds can change:
Treat background sensitivity as a substantive analysis, not just a runtime check.
SHAP values use the additive scale selected by the explainer.
Examples:
A display transform such as link="logit" on a force plot changes labels, not the computed SHAP game. If probability-space additivity is required for supported trees, configure TreeExplainer(model_output="probability", feature_perturbation="interventional", data=...).
Do not compare mean absolute SHAP magnitudes across models or outputs on different scales.
Local attribution:
phi_i(x)
Global mean absolute attribution:
E_x[abs(phi_i(x))]
Mean absolute attribution:
Mean signed attribution can cancel heterogeneous effects. Plot the distribution before summarizing.
Unconstrained exact Shapley computation grows exponentially with feature count. SHAP uses model-specific algorithms or sampling:
"Exact" always means exact under a specified output, background, masker, and coalition game.
Tree SHAP can decompose attribution into a symmetric interaction matrix.
For one row:
interaction = explainer.shap_interaction_values(X_eval)
An interaction value describes non-additivity in the fitted model under the chosen game. It does not prove biological, physical, or causal interaction.
Scatter-plot vertical dispersion is only a clue; compute or test interactions before labeling them.
When two features contain similar information, multiple allocations can be defensible:
Instability across folds, seeds, backgrounds, or equivalent model fits is evidence that individual attribution is not uniquely supported by the problem. Report grouped results or sensitivity rather than selecting one convenient ranking.
SHAP makes predictive relationships visible. A positive attribution does not imply that increasing the feature would increase the real-world outcome.
Confounding, mediation, reverse causality, selection bias, and feature redundancy can all make predictive patterns unsuitable for intervention.
Use causal language only when:
Even then, describe exactly what quantity is attributed.
Attribution can help investigate:
It cannot alone establish:
Protected-feature attribution can be small while proxies drive disparities. Conditional attributions can also assign protected-feature credit even when the model does not directly consume it. Pair SHAP with outcome, error, calibration, threshold, and data-quality analyses.
A waterfall answers "how this model output is allocated relative to this reference." It does not answer:
Use dedicated counterfactual/recourse methods with feasibility and causal constraints.
A standard SHAP plot usually shows point estimates, not uncertainty.
Sources of variation include:
Useful stability analyses:
Measures predictive performance degradation after shuffling a feature.
Summarizes how a tree used features during fitting.
Describe change per input unit on the model's linear scale.
Averages predictions while varying a feature.
These answer different questions. Agreement is reassuring but not proof; disagreement should trigger an assumptions review.
Before making a claim: