Theoretical Background
Shapley Values in Sensitivity Analysis
Shapley values originate from cooperative game theory and provide a principled way to:
- Fairly distribute the "payout" (output variance) among "players" (input parameters)
- Account for all possible interaction effects
- Provide unique attribution under certain axioms (efficiency, symmetry, dummy, additivity)
Shapley Effects — Mathematical Formulation
For a model output \(Y = f(X_1, X_2, \ldots, X_d)\), the Shapley effect \(\phi_i\) for parameter \(X_i\) is:
where:
- \(D = \{1, 2, \ldots, d\}\) is the set of all input parameters
- \(S\) is a subset of parameters excluding \(i\)
- \(X_S\) represents the parameters in subset \(S\)
- The weight \(\frac{|S|!\,(d-|S|-1)!}{d!}\) ensures each coalition size is equally represented
Relationship to Sobol Indices
Shapley effects generalise Sobol indices by combining all order effects involving a parameter:
- A Sobol index \(S_u\) quantifies the contribution of subset \(u\) alone
- A Shapley effect \(\phi_i\) redistributes every interaction term equally among its participating variables
This yields a complete decomposition where:
- \(\sum_{i=1}^d \phi_i = \text{Var}(Y)\) (efficiency)
- Each \(\phi_i \geq 0\) (non-negativity under independent inputs)
Advantages of Shapley Effects
- Complete Attribution: Accounts for all interactions, unlike first-order Sobol indices
- Additivity: Effects sum to total output variance
- Interpretability: Direct measure of importance in variance units
- Single number per input: Avoids the proliferation of \(2^d - 1\) Sobol indices
Implementation via RS-HDMR (Independent Inputs)
ShapleyX computes Shapley effects from a sparse polynomial surrogate model built by:
- Polynomial chaos expansion using shifted Legendre polynomials (orthonormal on \([0, 1]\))
- Automatic Relevance Determination (ARD) for sparse Bayesian regression — prunes irrelevant basis terms
- Coefficient-based index extraction: squared coefficients are grouped by variable labels and distributed according to the Shapley weights
Under independent inputs, the variance contributed by a basis term \(\psi_u(\mathbf{x})\) is simply its squared coefficient. The Shapley effect for variable \(i\) is then the sum of all coefficient-squared terms divided equally among the variables appearing in each term's label.
Shapley Effects with Correlated Inputs
When inputs are correlated, the coefficient-based decomposition used by RS-HDMR breaks down because:
- The variance of a product term no longer factorises into independent contributions
- Conditional expectations \(E[Y \mid X_S]\) depend on the joint distribution in a non-trivial way
The Monte Carlo Shapley method addresses this by directly estimating the conditional variance terms from samples of the joint distribution.
The Covariance-Based Formulation
Owen & Prieur (2017) showed that the Shapley value function can be written as:
where \(\mathbf{X}_u\) is a random vector constructed by:
- Drawing a joint sample \(\mathbf{x} \sim P_{\mathbf{X}}\)
- Fixing the coordinates in \(u\) to \(\mathbf{x}_u\)
- Drawing the remaining coordinates from the conditional distribution \(P(\mathbf{X}_{-u} \mid \mathbf{X}_u = \mathbf{x}_u)\)
The MC Shapley algorithm estimates \(v(u)\) for each subset by Monte Carlo, then assembles the Shapley effects via the standard coalition formula.
MC Sobol Indices as a By-Product
The same \(v(u)\) values equal the closed Sobol indices:
This is a direct consequence of the Owen & Prieur (2017) formulation — see the proof in their paper. From \(v(u)\) we obtain both first-order and total-order Sobol indices without additional model evaluations:
where \(v(\text{full}) = \mathbb{V}(f(\mathbf{X}))\) is the total output variance.
The MCShapley.compute() method returns sobol_first and sobol_total columns
alongside the Shapley effects, with bootstrap confidence intervals available
via bootstrap_sobol(). This gives practitioners three complementary perspectives
from a single data collection: \(S_i\) (main effect), Shapley (fair attribution
including interactions), and \(T_i\) (total effect).
Important: Under correlated inputs, the standard inequality \(S_i \leq T_i\) can break down — correlation inflates \(S_i\) (shared information through dependence) and deflates \(T_i\) (other variables proxy for \(X_i\)). The Shapley effect remains axiomatically interpretable regardless of the correlation structure.
Distribution Classes
ShapleyX provides two distribution families with built-in conditional sampling:
| Class | Description |
|---|---|
GaussianCopulaUniform |
Uniform marginals \([a_i, b_i]\) with dependence induced by a latent multivariate normal. Correlation matrix \(\mathbf{R}\) controls dependence. |
MultivariateNormal |
Jointly normal with mean \(\boldsymbol{\mu}\) and covariance \(\boldsymbol{\Sigma}\). Conditional distributions are analytically tractable. |
TruncatedMultivariateNormal |
Jointly normal with per-dimension truncation bounds \([a_i, b_i]\). Both joint and conditional sampling use Gibbs sampling, making it suitable for any hyper-rectangular truncation region. |
Computation Methods
| Method | Description | Complexity |
|---|---|---|
'exhaustive' |
Enumerates all \(2^d - 1\) non-empty subsets. Each subset requires \(N\) Monte Carlo iterations. | \(O(2^d \cdot N)\) |
'permutation' |
Evaluates only subsets encountered in random permutations, with lazy caching. | \(O(n_{\text{perm}} \cdot d \cdot N)\) |
'exhaustive' with k_max |
Enumerates only subsets up to size \(k_{\max}\) (plus the full set). Exact when interactions are bounded at order \(k_{\max}\). | \(O(d^{k_{\max}} \cdot N)\) |
Bootstrap confidence intervals are available for all methods by resampling the stored output arrays.
Coalition Truncation
The exhaustive method's \(O(2^d)\) complexity limits its use to \(d \leq 8\) in
practice. However, many models — particularly RS-HDMR surrogates — have
bounded interaction order. An RS-HDMR model built with polys=[10, 5]
contains only first-order (main effects) and second-order (pairwise)
interactions; all higher-order terms are identically zero.
When the model's HDMR decomposition has no interactions above order \(k\), the Shapley value for variable \(i\) simplifies: coalitions larger than \(k\) contribute nothing beyond what is already captured by their sub-coalitions of size \(\leq k\). Formally, for any \(u\) with \(|u| > k\):
and the Shapley difference \(v(u \cup \{i\}) - v(u)\) only involves terms where \(i \in w\) and \(|w| \leq k\). The Shapley weight for coalitions of size \(|u| > k\) distributes these contributions identically to how they are distributed through the sub-coalitions already evaluated.
This justifies setting k_max to the highest interaction order of the
surrogate model. ShapleyX auto-detects this from the polys parameter
(\(k_{\max} = \text{len}(\text{polys})\)) when using model.get_mc_shapley().
For general models where the interaction order is unknown, k_max provides a
controlled approximation that converges to the exact result as \(k_{\max} \to d\).
Reference: Owen, A. B., & Prieur, C. (2017). On Shapley value for measuring importance of dependent inputs. SIAM/ASA Journal on Uncertainty Quantification, 5(1), 986–1004.