SHAP (SHapley Additive exPlanations) › Formula
Machine Learning from ScratchAll chapters SHAP (SHapley Additive exPlanations) › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
At its heart, SHAP answers a deceptively simple question: How much does each feature contribute to this specific prediction? Yet answering this question fairly requires accounting for a fundamental challenge: the contribution of a feature depends on what other features are present. A feature might have a large impact when considered alone, but a smaller impact when other correlated features are already in the model. SHAP solves this attribution problem by drawing on a concept from cooperative game theory called Shapley values, which provide a mathematically principled way to fairly distribute credit among contributors.
The key insight is that we cannot simply ask "what happens when we add feature ?" because the answer depends on the context, the set of other features that are already present. Instead, we should consider feature 's contribution across all possible contexts, then average these contributions in a way that ensures fairness. This journey from intuitive question to rigorous formula guides our understanding of how SHAP works.
The Attribution Challenge
To understand why SHAP's formula is structured the way it is, we should first appreciate the fundamental problem it addresses. Imagine you're trying to explain why a machine learning model predicted a house price of $235,000. You have three features: square footage, number of bedrooms, and age of the house. If you simply ask "how much does square footage contribute?", the answer isn't straightforward.
If we look at the difference between the prediction with all features () and the prediction with square footage removed (), we might conclude square footage contributes . But what if we had started from a different baseline? What if we removed bedrooms instead, or age? The contribution would appear different depending on what context we assume.
This reveals the core challenge: a feature's marginal contribution depends on what other features are already in the model. A feature might have a large impact when added to an empty set, but a smaller impact when added to a set that already contains correlated features. To be fair, we need to consider every possible way the feature could have been added to the model, across all possible sequences and contexts.
SHAP solves this by averaging the feature's marginal contribution across all possible subsets of other features, weighted by the probability of encountering each subset in a random ordering. This ensures that the attribution doesn't depend on the arbitrary order in which features are considered, but rather reflects the feature's true average contribution across all possible contexts.
Building the Formula: Marginal Contributions
The foundation of SHAP lies in understanding marginal contributions, the change in prediction that occurs when we add feature to a specific subset of other features. Formally, for a given subset that doesn't include feature , the marginal contribution is:
This captures a simple but important idea: what does feature add when it's included alongside the features in , compared to what the prediction would have been with just ?
However, this single marginal contribution tells only part of the story. The same feature might contribute differently depending on what other features are present. For example, square footage might contribute more when considered alone (because it's picking up information that would otherwise be captured by bedrooms), but contribute less when bedrooms are already in the model (because some of the information overlaps). To get a fair attribution, we need to look at the feature's marginal contribution across all possible subsets .
Simply averaging the marginal contributions would be unfair because not all subsets are equally likely or important. Some subsets might be more representative of how the feature typically appears in practice. This is where the weighting factor comes in. It ensures that each marginal contribution is weighted appropriately based on how likely we are to encounter that particular context.
The Shapley Value Formula
The SHAP value for feature in a prediction is given by:
This formula combines three essential components:
The Summation: ensures we consider every possible subset of features that doesn't include feature . This exhaustive enumeration guarantees that we've examined the feature's contribution across all conceivable contexts, leaving no scenario unaccounted for.
The Marginal Contribution: represents how much feature adds when included alongside subset . This is the raw signal of the feature's impact in each specific context. Notice that this value can differ dramatically depending on . A feature might have a large positive contribution when added to an empty set, but a smaller or even negative contribution when added to a set containing correlated features.
The Weighting Factor: is the sophisticated piece that ensures fairness. This combinatorial weight doesn't just count subsets equally. It accounts for the probability that, if we randomly ordered all features, we would encounter subset before feature , with all remaining features coming after. This probability-based weighting ensures that the final attribution doesn't depend on any arbitrary ordering of features, but reflects the feature's average contribution across all possible orderings.
These three components work together to produce a fair, mathematically principled attribution: we enumerate all possible contexts (summation), measure the feature's impact in each context (marginal contribution), and then average these impacts weighted by how naturally each context arises (weighting factor). The result doesn't depend on the order in which features are considered or the baseline we happen to choose.
Seeing the Formula in Action
To make this concrete, let's walk through a visual example that demonstrates how all these pieces fit together. The visualization below shows the complete calculation process for computing the SHAP value of feature in a model with three features.
You'll see four subsets (shown in blue circles): the empty set , the singleton sets and , and the pair . For each subset, the diagram shows:
- The marginal contribution (shown as red-bordered boxes with values like "125")
- The weighting factor that applies to that marginal contribution (shown in green boxes)
- How these weighted contributions combine to produce the final SHAP value
In this particular example, all marginal contributions happen to equal 125, which simplifies the calculation. In general, these values can vary depending on the model's structure and how features interact with each other. The key insight to take away is how the formula systematically considers every possible context, weights each appropriately, and combines them to produce a single, fair attribution value.
The visualization makes visible what the formula encodes: fair attribution requires us to consider all contexts and weight them by how naturally they arise in random orderings of features.

Understanding the Weighting Factor
The weighting factor is central to ensuring fairness in SHAP's attribution scheme. While it might look intimidating at first, it encodes a simple idea: we should weight each marginal contribution by how likely we are to encounter that particular context in a random ordering of features.
To understand why this matters, consider what happens if we simply averaged all marginal contributions equally. Some subsets would be over-represented (like the empty set or the full set), while others would be under-represented. But more fundamentally, different subsets represent different "stages" in the feature addition process. If we imagine features being added one by one in some order, then subset represents all the features that would come before feature in that ordering.
The weighting factor captures this probabilistic interpretation: if we randomly shuffle all features, what's the probability that exactly the features in subset come before feature , with all remaining features coming after?
Let's break down the formula piece by piece:
-
: This counts the number of ways to arrange the features in subset among themselves. If contains features, there are ways to order them, but what matters here is their position relative to feature , not their internal ordering.
-
: This counts the number of ways to arrange the features that come after feature . Since we have total features, features come before , feature itself is in the middle, leaving features to come after, which can be arranged in ways.
-
: This is the total number of possible orderings of all features, the denominator that normalizes our probability calculation.
The ratio therefore represents the probability that, in a random ordering, we find exactly this configuration: all features in before feature , all remaining features after feature .
Why is this the right weight? Because it ensures that each marginal contribution is weighted by how "typical" or "representative" that context is. Subsets that are more likely to appear in random orderings (like medium-sized subsets) get appropriate weight, while extreme subsets (like the empty set or nearly complete sets) get weights that reflect their relative rarity. This probabilistic weighting guarantees that the final SHAP value doesn't depend on any arbitrary ordering we might choose. It's the average contribution across all possible orderings, weighted by how naturally each ordering arises.
This probabilistic interpretation of the weighting factor completes our understanding of how SHAP achieves fairness. By weighting each marginal contribution by the probability of encountering its context in a random ordering, we ensure that the final SHAP value is independent of any arbitrary feature ordering. The formula doesn't favor one subset over another arbitrarily. It weights them according to how naturally they arise, creating an attribution scheme that is both mathematically principled and intuitively fair.
How Everything Connects: The Efficiency Property
Now that we understand the individual components of the SHAP formula, we can see how they work together to create a coherent whole. The efficiency property provides an important sanity check that demonstrates how the formula's structure ensures completeness:
This equation states that the sum of all SHAP values equals the difference between the model's prediction using all features and the baseline prediction (using no features).
Why is this important? It guarantees that SHAP provides a complete decomposition of the prediction. Every unit of difference between the full prediction and the baseline is accounted for. There are no "orphaned" contributions floating around, and nothing is double-counted. This is a mathematical guarantee that emerges naturally from the way SHAP averages marginal contributions across all subsets.
To see why this holds, consider what happens when we sum all the SHAP values. Each feature's SHAP value is a weighted average of its marginal contributions across all subsets. When we sum these up, we're essentially reconstructing the path from baseline () to full prediction () by adding features one at a time, across all possible orderings. The mathematical structure ensures that this reconstruction is exact. The weighted sum of all marginal contributions perfectly captures the total difference between baseline and final prediction.
This efficiency property is what makes SHAP explanations interpretable and actionable: if you start with the baseline and add each feature's SHAP contribution, you arrive exactly at the model's prediction. There's no missing piece and no unexplained remainder. Just a clean, complete decomposition.
The Mathematical Guarantees
The efficiency property is just one of several fundamental guarantees that SHAP values provide. These properties aren't arbitrary design choices. They emerge naturally from the mathematical structure of Shapley values and give SHAP explanations a consistent and principled basis:
Efficiency: As we've just seen, the sum of all SHAP values equals . This ensures complete attribution with no missing pieces. Every unit of difference between prediction and baseline is accounted for.
Symmetry: If two features and have identical marginal contributions in all possible contexts (i.e., for all subsets ), then they receive identical SHAP values: . This guarantees fair treatment of equivalent features. Features that contribute equally in all contexts are attributed equally.
Dummy: If a feature never changes the prediction regardless of what other features are present (i.e., for all subsets ), then its SHAP value is zero: . This ensures that irrelevant features get zero credit. The mathematical structure naturally filters out features that don't contribute.
Additivity: For models that decompose as a sum of sub-models (e.g., ), the SHAP values of the combined model equal the sum of the SHAP values from the individual models: . This means SHAP explanations compose naturally, making them useful for understanding ensemble models and complex systems.
Together, these properties form a mathematical foundation for SHAP explanations. They ensure that the attributions are not arbitrary or dependent on implementation details, but rather reflect fundamental properties of how features contribute to predictions. This mathematical rigor is what distinguishes SHAP from simpler attribution methods. It provides guarantees that hold regardless of the specific model or data, making the explanations reliable when interpreting models, debugging predictions, or explaining results to stakeholders.