Approximate estimation

Exact estimation costs \(2^n\) model calls. The samplers mc, complementary and neyman_cc score a budget of coalitions; regression estimators: Regression estimators and hierarchical players.

Budget model

budget takes one key; counts include the empty and grand coalitions.

Key

Coalitions evaluated

fraction in \((0, 1]\)

\(\lfloor f\,2^n \rfloor\)

evaluations

the given count, at most \(2^n\)

per_player \(\alpha\)

\(\lceil \alpha n \rceil\), as evaluations

For long prompts a fraction of \(2^n\) is meaningless; the literature budgets in multiples of \(n\). Floors: 2 for mc, \(2n + 2\) for paired estimators, which round down to whole pairs. \(v(N)\) comes free with the base generation.

“Cancel my old phone plan today” (\(n = 6\)) with per_player: 4 scores 24 of 64 coalitions. mc spends 14 on the empty, grand, six one-word and six five-word coalitions, then draws 10. complementary spends 14 on \((N, \emptyset)\) and six singleton pairs, then draws 5 pairs.

Monte Carlo

mc (Stratified SVARM) evaluates the empty, grand, singleton and \((n-1)\)-player coalitions, then draws a uniform size among sizes with unseen coalitions and an unseen coalition of that size. The estimand plugin aggregates, so one sample serves Shapley and Banzhaf.

What the aggregator computes from partial samples

A full table gives exact values. On a partial sample, \(\bar v^{+}_{i,\ell}\) is the mean payoff of sampled size-\((\ell + 1)\) coalitions with \(i\), and \(\bar v^{-}_{i,\ell}\) of sampled size-\(\ell\) coalitions without it. \(L_i\) holds the strata with both means; \(p_\ell\) is the stratum probability (\(1/n\) for Shapley, \(\binom{n-1}{\ell} / 2^{n-1}\) for Banzhaf):

\[\hat\phi_i = \frac{\sum_{\ell \in L_i} p_\ell\, (\bar v^{+}_{i,\ell} - \bar v^{-}_{i,\ell})}{\sum_{\ell \in L_i} p_\ell}\]

A player with no such stratum gets 0 and a warning. The estimate is consistent but biased at low coverage: missing strata inherit the observed mean difference. Values add up exactly only on the full table, under leverage and with explanation.efficiency: additive.

Complementary contributions

complementary evaluates pairs \((S, N\setminus S)\) and records \(u = v(S) - v(N\setminus S)\). Players in \(S\) add \(u\) to cell \(C_{i,|S|}\); the others subtract it from \(C_{i,n-|S|}\). \(M_{i,j}\) counts \(i\) in size-\(j\) coalitions.

Coalition S keeps Cancel and plan, its complement the other four words; u is added to column 2 for Cancel and plan and subtracted from column 4 for the rest.

Illustrative. One complementary pair over the running example.

\[\hat\phi_i = \frac{1}{n} \sum_{j=1}^{n} \frac{C_{i,j}}{M_{i,j}}\]

Column \(j = n\) comes only from \((N, \emptyset)\), sampled once: each player gets \((v(N) - v(\emptyset))/n\). Masks pair with complements by value, in any order.

An empty cell (\(M_{i,j} = 0\)) contributes zero and biases small budgets. So after \((N, \emptyset)\) and the singleton pairs, the sampler covers each size \(2 \le \ell \le n/2\). It splits the players into blocks of \(\ell\) and draws their pairs, so every player sits on both sides at that size. This costs about \(n \ln n\) pairs; below that, random pairs follow and a warning names the empty sizes.

Neyman allocation

neyman_cc samples complementary pairs in two phases:

  1. After the boundary and singleton pairs, passes fill each cell \(M_{i,j}\) up to \(m_{\text{init}}\) times, capped by the \(\binom{n-1}{j-1}\) size-\(j\) coalitions with \(i\). A pass draws once per player at one size, uniformly among that size’s coalitions with the player. Passes run size by size until every feasible cell is full, a pass adds nothing or the next pass does not fit.

  2. The rest goes to sizes \(j \ge \lceil n/2 \rceil\) in proportion to estimated standard deviations, capped by each size’s unsampled pairs. If all variances are zero, pairs follow the unsampled counts.

With the cell variance of the \(u\) values

\[\hat\sigma^2_{i,j} = \frac{1}{M_{i,j}-1} \left(\sum u^2 - \frac{(\sum u)^2}{M_{i,j}}\right),\]

pairs with larger side \(j\) get the Neyman share

\[\hat M_j \propto \sqrt{ \sum_{i} \frac{\hat\sigma^2_{i,j}}{j} + \sum_{i} \frac{\hat\sigma^2_{i,n-j}}{n-j} }\]

A pair puts \(i\) on the size-\(j\) side with probability \(j/n\), so the rule minimises \(\sum \hat\sigma^2_{i,c} / M_{i,c}\) for a fixed number of pairs. At \(j = n/2\) both sides are one column, counted once as \(\sum_i \hat\sigma^2_{i,j} / n\). If phase one uses the whole budget, neyman_cc equals complementary.

Phase two needs phase-one payoffs: sample_masks(), begin_allocation(), then sample_allocation_masks(). ExplainRunner runs both phases of any TwoPhaseEstimator.

Coalition sizes drawn

At \(n = 10\) and per_player: 8 (80 of 1,024 coalitions), with a toy game for Neyman’s second phase:

Code for this figure
from collections import Counter

import matplotlib.pyplot as plt
import numpy as np

from mllm_shap import (
    ComplementaryEstimator,
    MonteCarloEstimator,
    NeymanEstimator,
    PlayerSet,
)

n = 10
players = PlayerSet(player_ids=tuple(f"p{i}" for i in range(n)))
fraction = 8 * n / 2**n  # what budget.per_player: 8 passes to the samplers

def payoff(mask):  # toy game: player weights plus a pair bonus
    present = mask.present
    return sum(i for i in range(n) if present[i]) / n + 2.0 * (present[0] and present[1])

neyman = NeymanEstimator()
phase_one = list(neyman.sample_masks(players, fraction, False, seed=42))
neyman.begin_allocation(phase_one, [payoff(mask) for mask in phase_one])
samples = {
    "mc": MonteCarloEstimator().sample_masks(players, fraction, False, seed=42),
    "complementary": ComplementaryEstimator().sample_masks(
        players, fraction, False, seed=42
    ),
    "neyman_cc": phase_one + list(neyman.sample_allocation_masks()),
}
fig, ax = plt.subplots(figsize=(6.4, 3.2))
colors = ("#2a78d6", "#eb6834", "#1baf7a")
for offset, (name, masks), color in zip((-0.27, 0, 0.27), samples.items(), colors):
    sizes = Counter(sum(mask.present) for mask in masks)
    counts = [sizes[size] for size in range(n + 1)]
    ax.bar(np.arange(n + 1) + offset, counts, 0.27, color=color, label=name)
ax.set(xlabel="coalition size |S|", ylabel="coalitions drawn", xticks=range(n + 1))
ax.yaxis.get_major_locator().set_params(integer=True)
ax.spines[["top", "right"]].set_visible(False)
ax.legend(frameon=False, ncols=3, loc="lower center", bbox_to_anchor=(0.5, 1))

Coalition sizes drawn at 80 evaluations, n = 10

Bars of coalitions drawn per size. mc and complementary cover sizes 0 to 10; neyman_cc skips sizes 4 to 6.

Seed 42. All three take every one-word and nine-word coalition. neyman_cc spends phase one on sizes 2, 3, 7 and 8, so here it never sees sizes 4 to 6.

Choosing a budget

Convergence depends on the game. Estimator benchmark gives each estimator’s error at \(2n\) to \(32n\) coalitions on five games. Repeat it on a similar game, or on your prompt with a cheap backend and exact as reference. Target errors and stopping: Permutation, LIME, standard errors and budgets.

References

  • Zhang, J., Sun, Q., Liu, J., Xiong, L., Pei, J., Ren, K. (2023). Efficient Sampling Approaches to Shapley Value Approximation. Proc. ACM Manag. Data 1(1). https://doi.org/10.1145/3588728

  • Neyman, J. (1934). On the Two Different Aspects of the Representative Method. J. R. Stat. Soc. 97(4). https://doi.org/10.2307/2342192

  • Castro, J., Gómez, D., Tejada, J. (2009). Polynomial calculation of the Shapley value based on sampling. Comput. Oper. Res. 36(5). https://doi.org/10.1016/j.cor.2008.04.004

  • Mitchell, R., Cooper, J., Frank, E., Holmes, G. (2022). Sampling Permutations for Shapley Value Estimation. Journal of Machine Learning Research 23(43).

  • Goldshmidt, R., Horovicz, M. (2024). TokenSHAP: Interpreting Large Language Models with Monte Carlo Shapley Value Estimation. arXiv:2407.10114. https://arxiv.org/abs/2407.10114

Further reading