POLS 4728
From Shapley values to SHAP
1 / 20 · Start with Shapley
F: all features; m = |F|.
Scroll to highlight each part, or use Next.
2 / 20 · The observation x
3 / 20 · The prediction ŷ
4 / 20 · Before and after feature i
Both values average the same model’s predictions.
Before
i: background values
After
i: value from x
S: features fixed to x. Unfixed inputs: background rows.
i: the additional feature. No retraining.
5 / 20 · The feature’s credit
6 / 20 · Bread and Peace
Bread: real income growth. Peace: fewer war fatalities.
Stylized version; variables centered and standardized.
Douglas A. Hibbs Jr. (2000). “Bread and Peace Voting in U.S. Presidential Elections.” Public Choice 104, 149–180.
7 / 20 · Three correlations
8 / 20 · The OLS fits
9 / 20 · One election’s prediction
10 / 20 · Bread first
11 / 20 · Peace first
12 / 20 · The two-feature SHAP values
For bread 1 and peace 1, the two orders give bread the same marginal of +0.5 and peace the same marginal of +0.5. Their SHAP values add to the prediction, 1, from a zero baseline.
13 / 20 · When features interact
14 / 20 · The model and the observation
f(x) = ŷ(x): the same earnings prediction rule.
For the adverse neighborhood profile, ability is 1, neighborhood opportunity is −1, and experience is 1. The model predicts $30,000 per year.
15 / 20 · The term vₓ(S)
16 / 20 · The term vₓ(S ∪ {i})
17 / 20 · The difference vₓ(S ∪ {i}) − vₓ(S)
18 / 20 · The factorial weight
19 / 20 · The sum and its result
20 / 20 · From the formula to the plot
For an observation x and a revealed feature group S, copy that observation’s values for the group into every background row. Keep the unrevealed values from the same row, predict with the fixed model, and average. The earnings example uses eight equally weighted reference rows:
Here b(r) is reference row r, xS supplies the observation’s values for columns in S, and −S denotes all the other columns. Together, (xS, b(r)−S) specifies one complete hybrid input row, with every value in its original column.
Revealing feature i changes this average by vx(S ∪ {i}) − vx(S). Its SHAP value averages that marginal contribution across equally weighted feature orders, exactly as in the earlier Shapley story.
Both examples use interventional background replacement: average over all reference rows, rather than filtering them to observations that match the revealed characteristics. Excluded columns stay together from the same reference row, preserving their dependence with one another; they are not conditioned on the fixed inputs. Changing the background or masking rule can change the explanation.
The observation x is a complete input row, with every feature value known. A fitted model f predicts its outcome: ŷ(x) = f(x). The observed outcome y is used in fitting; the explanation concerns the model’s prediction. It does not compare that prediction with an observed outcome.
The feature universe F has m members. The preceding group S contains features already fixed to x; feature i is the additional feature, outside S. The value vx(S) is the average of the fixed model’s predictions over hybrid rows. Included columns take their values from x; excluded columns take theirs from the background. This averaging is what we mean by integrating out the excluded features. Every model call still uses every input column.
With nothing fixed, vx(∅) is the background average prediction. With everything fixed, every hybrid row equals x, so vx(F) = ŷ(x). Adding no feature leaves a group unchanged: S ∪ ∅ = S.
With the model and background held fixed, ϕi(x) is shorthand for ϕi(vx). It gives feature i’s credit relative to the background average, in prediction units. Repeat the calculation for each feature to recover the prediction as baseline plus contributions.
The Bread and Peace warm-up uses bread, peace, and observed incumbent-party two-party vote share: each has mean zero and SD one. Bread is standardized real income growth. Peace is minus standardized war fatalities, so higher peace means fewer fatalities. The stipulated correlations are bread–peace ρ = +0.5, bread–vote +0.75, and peace–vote +0.75. In a standardized univariate regression, the slope equals the feature–outcome correlation, giving bread-only and peace-only slopes of +0.75.
For each bivariate slope, subtract the feature correlation times the other feature’s outcome correlation, then divide by 1 − ρ²:
The fitted bivariate equation is ŷ(x) = 0.5 × bread + 0.5 × peace. Centering makes all three intercepts zero. Predictions use the outcome’s SD units; they are not themselves rescaled to SD one. A peace value of +1 means one SD fewer war fatalities than average.
Refitting without peace produces 0.75 = 0.5 + (0.5)(0.5): the full-model bread coefficient plus the omitted-variable term. SHAP keeps the bivariate coefficients fixed and changes which inputs are averaged. With this additive model and overall background means of zero, a feature’s marginal is the same in either order, despite the correlation between the features.
This is a teaching example inspired by Douglas Hibbs’s Bread and Peace model. The correlations and election profiles are constructed, rather than estimated from historical elections. Hibbs’s original specification also estimates income-growth time weights; this warm-up uses direct two-feature OLS and reverses the standardized war-fatalities measure to define peace.
Call the fixed prediction rule f, so f(x) = ŷ(x). In a machine-learning application, training inputs X and outcomes y are used to fit it once. Here we supply a transparent earnings equation so every prediction can be checked. The explanation procedure holds that equation fixed:
The three features are ability, neighborhood opportunity (written as neighborhood), and experience. Ability and experience range from 0 to 1; neighborhood opportunity ranges from −1 to +1. The interaction ability × neighborhood is computed inside f, rather than treated as a fourth player. It can outweigh ability’s positive main term in an adverse neighborhood.
The eight equally weighted background rows cover every endpoint combination: low or high ability, adverse or favorable neighborhood, and low or high experience. Their average prediction is $47,000. They define our reference distribution; their observed outcomes are not needed for the calculation. Zero is a legitimate input value, rather than a marker for an excluded feature.
Here the adverse context makes revealing this person’s ability lower the averaged prediction. This sign reversal comes from the interaction in our equation.
The equation and reference people are constructed for teaching. SHAP assigns credit for its prediction; it does not estimate a causal effect of changing a person’s features. Choosing another person changes the coalition values and credits while the equation, background, and baseline stay fixed.
For three features, there are 3! = 6 equally weighted revealing orders. A particular preceding group S appears in |S|!(3 − |S| − 1)! of them: arrange the features in S before feature i, and the remaining features after it. Divide this count by six to get its weight.
The empty group gets 0!2!/3! = 2/6; each one-feature group gets 1!1!/3! = 1/6; the two-feature group gets 2!0!/3! = 2/6. These weights sum to one across the four preceding groups that exclude i.
In the earnings example, ability’s marginal depends on whether neighborhood is already fixed, and neighborhood’s depends on whether ability is already fixed. Experience’s position does not affect those marginals because experience enters additively. Each of the two contexts has total weight 3/6. Experience itself has the same marginal in all six orders, giving total weight one.
Removing peace and refitting OLS changes the bread coefficient. The omitted-variable identity compares that new coefficient with the full-model coefficient. Our SHAP calculation instead keeps the full fitted model and averages over the background peace distribution.
Conditional SHAP is another possible game: it averages excluded features using their distribution conditional on the included values. Correlated features can then share credit differently, even in a linear model. That still keeps the prediction model fixed; it is not refitting a reduced model. See SHAP’s linear-model explanation.
In the linear OLS warm-up, averaging predictions equals predicting with excluded features at their overall background means. The row-averaging definition also works for nonlinear and interaction models, where substituting a single mean row need not reproduce the average prediction.
Include a column to copy this person’s value into every row. Exclude it to keep the original background values. All three input columns remain in the table and in the model.
The transparent equation lets us check the arithmetic. In a machine-learning application, the prediction function below can be replaced by an already fitted model’s predict method. The model remains fixed while SHAP explains its output.
import numpy as np
import shap
def predict(rows):
ability, neighborhood, experience = rows.T
return 40 + 10*ability + 24*ability*neighborhood + 4*experience
def coalition_value(revealed):
hybrid = background.copy()
hybrid[:, revealed] = x[revealed]
return predict(hybrid).mean()
# Ability joins after neighborhood.
before = coalition_value([1])
after = coalition_value([1, 0])
marginal = after - before
masker = shap.maskers.Independent(
background, max_samples=len(background)
)
explainer = shap.ExactExplainer(predict, masker)
explanation = explainer(x.reshape(1, -1))
shap.plots.waterfall(explanation[0])
Here background holds the eight reference rows and x is the selected person’s three inputs. The reproduction files define those arrays, enumerate every coalition, and verify the values against Python SHAP. Larger models use explainers suited to their model class and computational budget.
Numbers are rounded only for display; calculations use the stored values at full precision.
The earnings equation uses synthetic reference people and chosen coefficients. It is not an estimated earnings equation or evidence about causal neighborhood effects. The positive ability–neighborhood interaction encodes complementarity; negative opportunity allows it to work against productive earnings.
Method references: SHAP’s introduction to the prediction game, background masker, exact explainer, and waterfall plot.