How do you estimate Shapley values when exact enumeration over 2^40 coalitions is impossible?
answer
- the coalition count is exponential
- draw orderings instead of enumerating them
- unbiased average of marginal contributions
- error falls with the square root
- stop on standard error, not a fixed budget
basics
~20 sSample random feature orderings instead of enumerating all coalitions: average each feature's marginal contribution over the sampled orderings. The estimate is unbiased, and its error falls roughly as one over the square root of the number of draws.
solid answer
~50 sThe definition ranges over `2^n` coalitions - for a 40-feature pricing model that is about a trillion - so exact enumeration is off the table. The standard escape is Monte Carlo over orderings: draw a random permutation of the features, walk it, record each feature's jump in the model's output as it is added, and average those jumps over many permutations. Each permutation updates every feature at once, and the estimator is unbiased. Error shrinks like one over the square root of the number of permutations, so an extra digit of precision costs a hundred times the compute. In practice you track the standard error per feature and stop when the decision the explanation drives is stable - the ranking of the top few features settles long before the values do. Cost also scales with the background set, since each coalition value is averaged over background rows.
go deeper
Know that the exact computation grows exponentially with the number of features, so real implementations approximate it, and that an attribution you are shown is usually an estimate rather than an exact number.
Explain the sampling procedure itself: draw random feature orderings, accumulate each feature's jump in the output, average. Say why the estimator is unbiased and why error shrinks with the square root of the draws.
Show the operating judgment - a stopping rule based on per-feature standard error, the ranking stabilising before the values, variance reduction such as antithetic orderings, and the background set as a cost multiplier.
Frame the budget as a product decision: what latency and cost the explanation surface can carry, whether per-row real-time attribution is required at all, and when a different model family or a cheaper explanation is the right trade.
## Where the cost comes from The Shapley value of feature `i` is an average of `v(S + i) - v(S)` over all coalitions `S` that exclude `i`. With `n` features there are `2^n` distinct coalitions and `n!` orderings. For a 40-feature telecom pricing model, `2^40` is about 1.1 trillion coalition evaluations *per explained row*, and each evaluation is a forward pass through the model - possibly averaged over a background set on top of that. Exact enumeration is practical for roughly a dozen features and nothing beyond. Three escapes exist. The most general is sampling; the second is exploiting structure in the model family so the exact value can be computed in time polynomial in the model's size; the third is not computing Shapley values at all and using a cheaper local explanation. This section is about sampling. ## Permutation sampling The ordering formulation is what makes sampling easy. The Shapley value is the *expected* marginal contribution when features are revealed in a uniformly random order, so: ``` repeat m times: draw a uniformly random ordering of the n features S <- empty; prev <- v(empty) for each feature i in that ordering: S <- S + i cur <- v(S) contribution[i] += (cur - prev) prev <- cur phi_i = contribution[i] / m ``` Two properties matter. First, the estimator is **unbiased** - its expectation is the exact Shapley value, for every `m`. Whatever error you have is variance, not a systematic tilt. Second, one sampled ordering yields a contribution for every feature at once, from `n + 1` model evaluations, so the per-feature cost is amortised rather than multiplied. ## How the error behaves Because this is a plain Monte Carlo average of independent draws, the standard error of `phi_i` falls as `sigma_i / sqrt(m)`, where `sigma_i` is the spread of feature `i`'s marginal contribution across orderings. Two consequences: - **Diminishing returns.** Cutting the error in half costs four times the samples; one more decimal digit costs a hundred times. - **The error is per-feature.** A feature involved in strong interactions has a wide spread of marginal contributions across orderings and needs more draws than a feature with a near-constant effect. A uniform sample budget over-serves the easy features and under-serves the hard ones. Because the estimator is unbiased with computable per-feature variance, you can turn the running sample variance into a **standard error** and use it as a stopping rule instead of a fixed budget: keep drawing until each reported attribution's interval is narrow enough for the use it is put to. ## What "narrow enough" means depends on the decision This is the judgment part. If the explanation is displayed as a ranked list of the strongest drivers, what has to be stable is the *ranking of the top few*, not the third decimal of each value. Ranking is a much easier target: the top features usually separate from the rest early, and additional draws mostly refine values that are already clearly ordered. The right stopping test is therefore an operational one - draw until repeated runs of the estimator on the same row agree on the reported set, and until any pair of features whose order flips between runs are close enough that the flip does not change what a reader would do. If two features genuinely sit within noise of each other, more sampling will not resolve them into a stable order; that is a fact about the model, not a budget problem, and the honest report says they are comparable. ## Variance reduction and other levers - **Antithetic sampling**: evaluate an ordering and its exact reverse together. A feature that arrives early in one arrives late in the other, which cancels much of the ordering-induced variance. - **Stratification / adaptive allocation**: spend draws on the features whose running variance is largest rather than splitting the budget evenly. - **Truncation**: along an ordering, once the running coalition value has essentially converged to the full prediction, the remaining features' marginal contributions are near zero and the walk can be cut short. - **Background size**: the value function is itself an average over background rows, so cost is (orderings) x (features) x (background rows). Shrinking a background of thousands of rows to a well-chosen small sample is often the single largest saving, at the price of extra variance in `v` itself. - **Batching by row**: explanations for many rows share the same model and background, so evaluations can be batched. ## When sampling is the wrong answer If every scored row needs an explanation in real time, per-row Monte Carlo may simply not fit the latency budget, and the options are a model family whose structure admits an exact fast computation, precomputing explanations offline for the rows that will actually be read, or a cheaper explanation method. Recognising that the sampling budget is a product-level constraint - not just a tuning knob - is what separates a senior answer here.
- How do you decide how many permutations are enough for a given row?Track the running variance of each feature's marginal contributions and turn it into a standard error, then stop when the intervals are tight enough for the decision the explanation supports. If the output is a ranked top-k list, the test is that repeated runs agree on the set and on any ordering a reader would act on - far fewer draws than pinning the values themselves.
- Why does the size of the background set multiply the cost of a sampled estimate?Because the coalition value is itself an expectation: the features outside the coalition are filled from the background, and the model's output is averaged over those rows. So the work is orderings times features times background rows. Sub-sampling the background to a small representative set is usually the cheapest large saving available.
- Is the sampled estimate biased toward the features that happen to be drawn early?No. Orderings are drawn uniformly, so over many draws each feature appears in every position equally often and the estimator's expectation is the exact value. What early-position draws create is variance, not bias - which is exactly what antithetic pairing of an ordering with its reverse attacks.
saying these in an interview costs you the question
- Says exact computation is fine, the model is fast
- Claims sampling introduces bias rather than variance
- Thinks doubling the samples halves the error
- Ignores the background set as a cost multiplier
- Reports attributions without any notion of estimation error