Game Theories in AI and Beyond: The Shapley Value Explained
The Shapley Value: Fair Credit in Cooperative Games and AI Explainability
Imagine three colleagues — Alice, Bob, and Carol — land a consulting contract worth $120,000. Alice is a domain expert, Bob handles client relations, and Carol does technical execution. How do you divide the payment fairly? This is precisely the question Lloyd Shapley formalized in 1953, earning him (jointly) the 2012 Nobel Prize in Economic Sciences.
Cooperative Game Theory Basics
In cooperative (coalitional) game theory, a game is defined by:
- N = {1, 2, ..., n}: a set of players - v: 2^N → ℝ: a characteristic function giving the worth of every possible coalition S ⊆ N - v(∅) = 0 (empty coalition generates nothing)
The question: how should the total value v(N) be divided among players such that the division is fair?
The Shapley Value Formula
Shapley's answer: each player's fair share is their average marginal contribution over all possible orderings in which players can join the coalition.
Where v(S ∪ {i}) − v(S) is the marginal contribution of player i when joining coalition S. The weight |S|!(|N|−|S|−1)!/|N|! counts what fraction of all orderings have S as the set of players before i.
Worked Example: Alice, Bob and Carol
Let's define the coalitional values:
- v({A}) = 40, v({B}) = 20, v({C}) = 30 - v({A,B}) = 70, v({A,C}) = 80, v({B,C}) = 55 - v({A,B,C}) = 120
Computing φ_A (Alice's Shapley value) requires averaging her marginal contributions over all 3! = 6 orderings:
from itertools
import permutations
players = ['A', 'B', 'C']
v = {
frozenset(): 0,
frozenset(['A']): 40, frozenset(['B']): 20, frozenset(['C']): 30,
frozenset(['A','B']): 70, frozenset(['A','C']): 80, frozenset(['B','C']): 55,
frozenset(['A','B','C']): 120,
}
def shapley(player, players, v):
n = len(players)
total = 0
for perm in permutations(players):
idx = perm.index(player)
before = frozenset(perm[:idx])
with_player = before | {player}
total += v[with_player] - v[before]
return total / len(list(permutations(players)))for p in players: print(f"φ_{p} = {shapley(p, players, v):.1f}") # φ_A = 50.0, φ_B = 28.3, φ_C = 41.7 ```
Four Axioms That Make Shapley Unique
Shapley proved his value is the ONLY allocation satisfying all four of these axioms simultaneously:
- Efficiency: φ₁ + φ₂ + ... + φₙ = v(N) — the full value is distributed 2. Symmetry: identical players get equal shares 3. Dummy: a player who contributes nothing to any coalition gets 0 4. Additivity: for two games v and w, φᵢ(v+w) = φᵢ(v) + φᵢ(w)
SHAP: Shapley Values for Machine Learning
In 2017, Lundberg & Lee showed that Shapley values provide a principled way to explain any ML model prediction. For a model f and input x:
- Players = input features {x₁, x₂, ..., xₙ} - Coalition value v(S) = E[f(x) | x_S] — expected model output given only features in S - φᵢ = SHAP value = how much feature xᵢ contributes to the prediction relative to the base rate E[f(x)]
Where φ₀ = E[f(x)] is the base value (average prediction). The SHAP values sum to the difference between the actual prediction and the base rate — a property called efficiency.
SHAP in Practice
import shap
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
model = xgb.XGBClassifier().fit(X, y)
explainer = shap.TreeExplainer(model)
shap_values = explainer(X[:100])
# Beeswarm plot: shows feature importance + directionality
shap.plots.beeswarm(shap_values)
# Waterfall plot: explains a single prediction
shap.plots.waterfall(shap_values[0])TreeSHAP (for tree-based models) computes exact Shapley values in polynomial time using the tree structure — making it practical even for large ensembles with hundreds of trees.
Applications Beyond ML Explainability
- Economics & Voting: measuring the power of individual voters or countries in coalitions - Supply Chain: attributing profit contributions across vendors and logistics partners - Feature Selection: ranking features by their average marginal contribution to model accuracy - Data Valuation: fairly pricing datasets contributed to federated learning pools - Multi-Agent RL: assigning credit to individual agents in cooperative settings
Conclusion
The Shapley value elegantly solves one of the oldest problems in social science — fair division — using a mathematically rigorous framework. In AI, it has become the gold standard for explainability because it satisfies provably desirable properties that simpler attribution methods lack. Whether you're dividing consulting fees or explaining why a neural network flagged a loan application, the Shapley value gives you a principled, fair answer.