Shapley values and axioms¶
The Shapley value splits \(v(N) - v(\emptyset)\) among the players of a cooperative game (Cooperative games).
Definition¶
For \(n\) players and characteristic function \(v\), player \(i\) gets
If players join in a uniformly random order, \(\phi_i\) is the expected marginal
contribution of \(i\) when it joins. Estimands: Shapley vs Banzhaf compares the size weights with
other estimands; ShapleyAggregator
implements them.
The four axioms¶
Shapley (1953) proved \(\phi\) is the only allocation rule with these four properties.
Efficiency¶
\(\sum_i \phi_i(v) = v(N)\) when \(v(\emptyset) = 0\). Payoffs in mllm-shap
score the output, so the empty prompt usually scores above zero. The library applies the
axiom to the normalised game \(v'(S) = v(S) - v(\emptyset)\):
Subtracting a constant changes no marginal contribution, so no \(\phi_i\). Each result
reports grand_value (\(v(N)\)) and
base_value (\(v(\emptyset)\)).
On the full coalition table (exact, or any estimator at budget.fraction: 1.0) the
identity holds to floating-point precision; check it first in an audit. Sampled estimates
approach it as the budget grows. The running example, exact, mock backend,
tf_logprob payoffs:
import math
from mllm_shap import (
ConversationSnapshot,
ExplainConfig,
ExplainRunner,
Message,
Role,
Turn,
)
prompt = "Cancel my old phone plan today"
snapshot = ConversationSnapshot.from_turns((
Turn(messages=(Message(role=Role.USER, text=prompt),)),
))
config = ExplainConfig.model_validate({
"backend": {"kind": "mock", "model_id": "stub"},
"explanation": {
"estimator": "exact",
"value_fn": "tf_logprob",
"archive": {"path": ""},
},
})
result = ExplainRunner(config).explain_sync(snapshot).result
phi = result.values
print("v(empty):", round(result.base_value, 3), " v(N):", round(result.grand_value, 3))
print("phi:", tuple(round(value, 3) for value in phi))
print("efficient:", math.isclose(sum(phi), result.grand_value - result.base_value))
Output
v(empty): -2.949 v(N): -2.184
phi: (0.117, 0.161, 0.213, 0.148, -0.02, 0.147)
efficient: True
Code for this figure
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6.4, 3.2))
level = 0.0
for i, value in enumerate(phi):
ax.bar(i, value, bottom=level, color="#2a78d6" if value >= 0 else "#eb6834")
ax.text(i, level + max(value, 0) + 0.01, f"{value:+.3f}", ha="center", fontsize=9)
level += value
ax.bar(len(phi), level, color="#8a8984")
ax.set_xticks(range(len(phi) + 1), [*prompt.split(), "v(N) -\nv(empty)"])
ax.set(ylabel="payoff minus v(empty)", ylim=(0, level * 1.12))
ax.spines[["top", "right"]].set_visible(False)
Efficiency on the running example
Mock backend, seed 42. The six values stack to \(v(N) - v(\emptyset) = 0.765\); “plan” lowers the payoff.
Symmetry¶
If \(v(S \cup \{i\}) = v(S \cup \{j\})\) for every \(S \subseteq N \setminus \{i, j\}\), then \(\phi_i = \phi_j\). Interchangeable tokens, such as duplicate padding, get equal credit.
Dummy player¶
If \(v(S \cup \{i\}) = v(S)\) for every \(S \subseteq N \setminus \{i\}\), then \(\phi_i = 0\): a token that never changes the scored output gets zero.
Additivity¶
For games \(v\) and \(w\) on the same players, \(\phi_i(v + w) = \phi_i(v) + \phi_i(w)\). A utility summing independent scores, such as a multi-objective value function, splits into its parts’ attributions.
SHAP¶
Lundberg & Lee (2017) treat features as players and \(v(S)\) as the model output when only \(S\) is observed. Three properties make the Shapley value the unique additive explanation, called SHAP:
local accuracy: attributions sum to the change from a baseline;
missingness: absent features get zero;
consistency: if the model changes so a feature’s marginal contributions never shrink, its attribution does not shrink.
SHAP names a family of methods; the estimand is what a run targets. Averaging sampled
marginals uniformly targets the Banzhaf index. Averaging within each coalition size, then
across sizes, targets the Shapley value; mllm-shap does this for Shapley
(Estimands: Shapley vs Banzhaf, Approximate estimation).
When to use Shapley values¶
Use them when attributions must add up to the total utility, treat symmetric players alike, split additive utilities or match the fairness axioms a regulator cites. To ask how often a token swings the outcome (voting power), use the Banzhaf index (Estimands: Shapley vs Banzhaf).
References¶
Shapley, L. S. (1953). A value for n-person games. In H. W. Kuhn & A. W. Tucker (Eds.), Contributions to the Theory of Games II (pp. 307–317). Princeton University Press. DOI:10.1515/9781400881970-018
Shapley, L. S. (1952). A value for n-person games (RAND Paper P-295). RAND Corporation. RAND P-295
Lundberg, S. M., & Lee, S.-I. (2017). A unified approach to interpreting model predictions. Advances in Neural Information Processing Systems, 30. ML Anthology; arXiv:1705.07874
Roth, A. E. (Ed.). (1988). The Shapley Value: Essays in Honor of Lloyd S. Shapley. Cambridge University Press.
Young, H. P. (1985). Monotonic solutions of cooperative games. International Journal of Game Theory, 14(2), 65–72. DOI:10.1007/BF01769885