POLS 4728 home

POLS 4728

SHAP

SHapley Additive exPlanations

Two round, torso-less stick figures examine a faceless Y-hat in the open cockpit of a landed spaceship; one holds a magnifying glass while the other scratches its head.

How do features explain the gap between a prediction and the background average?

Start the explanation

1 / 31 · Start with Shapley

From Shapley values to SHAP

  • Players → input features.
  • Game → one observation’s prediction.
  • Same Shapley formula; new value function.
ϕi⁡(vx)= ∑S⊆F∖{i} |S|!(p−|S|−1)!p! [vx⁡(S∪{i})−vx⁡(S)]

F: all features; p=|F|. We use interventional SHAP.

Scroll to highlight each part, or use Next.

2 / 31 · The observation x

The observation x

  • x: one complete input row.
  • Its values supply every fixed feature.
  • Same observation throughout each explanation.

3 / 31 · The prediction y^

The prediction y^

  • y^⁡(x): fitted-model prediction for the complete row x.
  • y: observed outcome.

4 / 31 · Average background predictions

Average background predictions

Interventional SHAP · any prediction model

Keep the features in S at their values in x. Predict with each background row, then average the predictions.

Use every background row without conditioning on the included values.

xS: included values from this observation.
z−S(b): the other inputs from background row b.
B: number of background rows.

5 / 31 · The feature’s credit

Credit for feature i

  • ϕi⁡(vx): feature i’s credit.
  • Its marginal contribution is the change in the average prediction when we also fix i to its value in x.
  • Average these marginals across orders to get its SHAP value.
  • Measured in prediction units.

6 / 31 · Bread and Peace

Bread and Peace

Interventional SHAP

A compact loaf of bread and a peace symbol, each with expressive stick arms and legs.

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 / 31 · Three correlations

Bread and Peace

  • Bread and peace predict vote share.
  • All variables are standardized.

8 / 31 · Both features are fixed

Both features are fixed

This election: bread=1, peace=1. Inputs to y^ are written in that order.

Use this election’s bread and peace values in the fitted two-feature model.

Predictions: SD of vote share.

9 / 31 · What if only bread is fixed?

What if only bread is fixed?

vx⁡({bread}) = ?   Bread in this election: 1.

A bread-only refit changes the model

y^bread=0.75×bread
y^bread(1)=0.75

Not used for SHAP. We do not refit the model.

10 / 31 · Keep the model; average over peace

Keep the model; average over peace

vx⁡({bread}) = ?   Bread in this election: 1.

Keep the model; average over peace

Hold bread fixed; use all eight background peace values, without conditioning on bread.

Mean peace is 0. This shortcut need not hold for nonlinear models.

Predictions: SD of vote share.

11 / 31 · Bread first

Bread first

  • Start at y^⁡(0,0)=0.
  • Bread joins ∅.
  • Peace joins {bread}.

12 / 31 · Peace first

Peace first

  • Restart at the same zero baseline.
  • Peace joins ∅.
  • Bread joins {peace}.

13 / 31 · The two-feature SHAP values

Average the two orders

  • Two orders; weight 12 each.
  • Average each feature’s two marginals.
  • Baseline + SHAP values = prediction.

y^(x)=y^(0,0)+ϕbread(vx)+ϕpeace(vx)

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.

14 / 31 · The value-function recap

What does vx(S) mean?

Interventional SHAP: average predictions

Keep included inputs at this observation’s values; use every original background row for the other inputs, without conditioning.

Linear model: use mean excluded inputs

x¯−S: overall background means. This shortcut can fail for nonlinear models.

Bread and peace both have background mean 0.
SBreadPeacevx(S)
∅Bread0background meanPeace0background meany^(0,0)=0
{bread}Bread1from this electionPeace0background meany^(1,0)=0.5
{peace}Bread0background meanPeace1from this electiony^(0,1)=0.5
{bread,peace}Bread1from this electionPeace1from this electiony^(1,1)=1

15 / 31 · When features interact

When features interact

Two round body-and-face characters with stick limbs and no torso stand on a tilted map of Germany. The smiling western character wears a top hat; the frowning eastern character wears a patched cap.

In this model, ability’s contribution depends on neighborhood.

West and East Germany Map: Kunz & Moeschl · IEG-Maps 360 · CC BY-NC 4.0.

16 / 31 · The model and the observation

Add an interaction

  • Ability, experience: 0–1.
  • Neighborhood: −1 (adverse) to +1 (favorable); changes ability’s marginal.
  • Output: yearly earnings ($1,000).

f⁡(x)=y^⁡(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.

17 / 31 · The term vx(S)

Find vx(∅)

Selected observation x.

Baseline:$47,000

18 / 31 · The term vx(S∪{i})

Add ability

  • S=∅; i=ability.
  • Copy ability = 1 into every row.
  • Marginal: +$5,000.

19 / 31 · The difference vx(S∪{i})−vx(S)

Reveal ability after neighborhood

  • S={neighborhood}; i=ability.
  • Keep neighborhood fixed; add ability.
  • Marginal: −$7,000.

20 / 31 · The factorial weight

Count the orders

  • Three features → 3!=6 orders.
  • Weight = fraction with this preceding S.
  • Open the six orders to count them.

21 / 31 · The sum and its result

Average the marginals

  • Ability: average six orders.
  • Before neighborhood: +$5,000. After: −$7,000.
  • Each weight 36 → −$1,000.

22 / 31 · From the formula to the plot

Baseline + contributions

y^(x)=vx(∅)+∑iϕi(vx)
  • Start at the baseline; add contributions from bottom to top.
  • Inputs beside names; rows ranked by magnitude.

23 / 31 · One common zero

Keep the contribution, move its origin.

  • The waterfall adds contributions along a running total.
  • Move each bar to zero; keep its signed length.

24 / 31 · One person, three dots

A dot records each contribution.

  • Its position is the SHAP value.
  • Left lowers the prediction; right raises it.

25 / 31 · Explain more people

Suppose we had a larger dataset.

  • One SHAP value per feature for each person.
  • Use the same model and reference background.

26 / 31 · Make overlap visible

From waterfalls to a beeswarm.

  • Spread overlapping dots vertically.
  • Horizontal position still measures the contribution.

27 / 31 · Add the input values

Which inputs go with which contributions?

  • Color now represents the input: low to high within each feature.
  • High ability can contribute in either direction here.

28 / 31 · Opposing contributions cancel

A signed average can hide magnitude.

  • Ability and neighborhood have positive and negative contributions.
  • Here, all three signed averages are zero.

29 / 31 · Measure distance from zero

Keep the size of every contribution.

  • Fold negative contributions to the right.
  • Absolute SHAP values measure magnitude.

30 / 31 · Average the distances

One summary for each feature.

  • Average the absolute SHAP values across people in each feature row.
  • The bar measures typical contribution magnitude.

31 / 31 · Rank the averages

Global importance for this dataset.

  • Neighborhood: 7.2; ability: 3.24; experience: 1.2.
  • The ranking depends on the model, reference background, and dataset being explained. Units: $1,000/year.

Notes and exploration

Both examples are constructed for teaching. SHAP explains their model predictions; it does not estimate causal effects.

Model and background assumptions

The prediction model stays fixed throughout each explanation. The earnings model has three features: ability, neighborhood opportunity, and experience. The ability–neighborhood interaction is part of the prediction rule, rather than a fourth feature.

The earnings background contains eight equally weighted endpoint profiles, with an average prediction of $47,000. Included features take this observation’s values; the excluded inputs stay together from each reference row. Choosing another person changes the explanation while the model and background remain fixed.

We use interventional SHAP: average over all original background rows without conditioning on the included values. Conditional SHAP uses the excluded inputs’ distribution conditional on those values and can allocate credit differently when features are correlated. More on these background choices.

Removing a feature and refitting would change the prediction model. Our calculation keeps the original model and averages its predictions. Substituting overall background means is a shortcut for linear models; it does not require standardization and can fail for nonlinear models.

Explore vx⁡(S) with a data table

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.

Features included in S

Explore a person’s waterfall

The figures illustrate a larger dataset using 125 constructed profiles: five values for each input, with ability and experience from 0 to 1, and neighborhood opportunity from −1 to +1. Each explanation uses the same eight reference profiles, fixed model, and baseline.

Select a profile to see all three contributions together. Selecting a dot in the colored beeswarm opens the same explanation.

Global importance averages each feature’s absolute SHAP values across these N=125 profiles:

The bars summarize magnitude, losing the direction and spread visible in the beeswarm. Their ranking describes this model, background, and population; changing the background or population can change the result.

Calculation and reproduction files.

Method references: the prediction game, background replacement, and global importance.

Every background row