Shapley Values

Shapley Values were first introduced in 1952 as a concept in coalitional game theory. Here's the setup: a group of players form a coalition and play a game together, earning a collective payout. The question Shapley Values answer is — how do we fairly distribute that payout based on each player's contribution?

Duke hosts a Generative AI Hackathon every fall. Say you get two friends together and compete as a team — that's your coalition. The hackathon is the game. If you win a monetary award, that's your payout. Shapley Values tell you how to fairly split that prize based on what each person contributed.

In machine learning, the coalition is the full set of features used by the model. The prediction is the payout. And the Shapley value for each feature tells us how much that feature contributed to the prediction.

But how do we measure contribution? Two things matter:

  • Individual contribution — you're an ML engineer who built the entire model. That model was the winning idea, so you had a large individual impact on the prize.
  • Interaction effects — you were only able to build that model because your friend Casey scraped the dataset. Without Casey, your contribution would have been smaller. Players can amplify or dampen each other.

To account for both, we use a thought experiment: jump into a time machine and re-run the hackathon with every possible subset of your team. Average each person's marginal contribution across all those subsets. That average is their Shapley value.

Split the Hackathon Prize

Select a coalition to see what each non-member would contribute by joining it. The Shapley value is the weighted average of those marginal contributions across all 8 coalitions.

All coalitions — select one

CoalitionValue

Marginal contribution of joining ∅ (no one)

Idea
$10k
Model
$15k
Demo
$8k

Shapley values (weighted avg, all 8 coalitions)

Idea
$31.8k
Model
$40.8k
Demo
$27.3k
The Shapley values update as you explore — they are the true weighted average across every subset, not just the orderings where all three are present.

How to interpret (and not interpret) Shapley Values

Do interpret as: the contribution of a feature value to the difference between the actual prediction and the mean prediction, given the current set of feature values.

Do not interpret as: the difference in prediction you would get by removing that feature from model training. That is a different — and incorrect — reading.

It is easy to misread Shapley Values, so hold this distinction carefully.

Now for the math. The Shapley value for feature jj is:

ϕj=SF{j}S!(pS1)!p![f(S{j})f(S)]\phi_j=\sum_{S\subseteq F\setminus\{j\}}\frac{|S|!(p-|S|-1)!}{p!}\left[f(S\cup\{j\})-f(S)\right]

For each subset SS that does not include feature jj, we find the difference between the prediction with jj added and the prediction without it. We weight that difference by the size of the subset relative to the total number of features pp, then sum across all subsets.

The exact calculation requires evaluating 2p2^p subsets — one for every possible coalition. For models with many features, this becomes computationally infeasible, which is why practical implementations approximate the Shapley values rather than computing them exactly.

Handling missing features in practice

Most models expect a fixed set of inputs — you cannot simply "remove" a feature. What we do instead is replace the absent feature with a random value sampled from that feature's distribution in the training set. Averaged across all subsets, the randomness washes out and the Shapley value reflects true marginal contribution.

For example, if a subset uses only features 2 and 3, features 1 and 4 are replaced with random draws from the training data for those features. The prediction on that subset is then compared to one where feature jj is added at its actual value.

Handling Absent Features

Click features in the top row to include them in subset S. Features left out cannot be removed — the model still needs an input for them, so they are replaced with a random draw from the training distribution.

All features

Create subset

Subset S (absent → random draw)

random()
feature 2
feature 3
random()
Averaged across many subsets and many random draws, the randomness cancels. What remains is each feature's true marginal contribution to the prediction.
Checkpoint

A Shapley value for a feature is sometimes described as its 'fair share' of the prediction. What does the time machine analogy reveal about why the averaging step is necessary?