skip to content

A shap waterfall for a binary XGBoost readmission model shows f(x) = -0.85 while `predict_proba` gives 0.30 — why, and how do you reconcile them?

level: middleimportance: must knowfreq 18%

answer

  1. which scale was explained
  2. raw margin, not probability
  3. logistic function of the sum
  4. per-bar probabilities do not add
  5. force plot link="logit" relabels ticks

basics

~20 s

By default TreeExplainer explains a binary XGBoost model on its raw log-odds margin, so base value plus SHAP values equals -0.85 in log-odds. The logistic function maps it to 0.30, the predicted probability; individual bars have no fixed probability equivalent.

solid answer

~40 s

Nothing is wrong: the explanation lives on a different scale. By default shap explains a binary XGBoost model's raw output, its log-odds margin, so `base_values + values.sum()` equals the margin, -0.85, and the waterfall's axis is log-odds. `predict_proba` applies the logistic function, `1 / (1 + exp(-m))`, which turns -0.85 into 0.30 and the base value -1.20 into 0.23. Reconcile with `scipy.special.expit` on the sum, never on single bars: the logistic curve is non-linear, so a +0.62 bar is worth about 13 points of probability from the base value but about 11 points as the last step into f(x). To show probabilities to readers, relabel instead of rescaling: `shap.plots.force(row, link="logit")` prints probability ticks while the widths stay additive in log-odds.

code

python · 8 lines
python
from scipy.special import expit

row = sv[17]                                  # one readmission patient
margin = row.base_values + row.values.sum()   # -0.85 on the log-odds scale
print(expit(margin))                          # ~0.30, matches predict_proba
print(expit(row.base_values))                 # ~0.23 for the average patient
shap.initjs()
shap.plots.force(row, link="logit")           # probability tick labels, log-odds widths

go deeper

for a junior

Remember that a boosted classifier's explanation is often on the log-odds margin, and that the logistic function turns the margin into the probability you see from predict_proba.

for a middle

Explain why additivity holds on the explained scale only, convert the endpoints with expit, and show why per-bar probability numbers depend on order.

for a senior

Show the presentation judgement: label scales, use a probability-labelled force plot or endpoint text for non-technical readers, and verify the scale from the output rather than the arguments.

for a principal

Decide which scale an organisation's explanation reports use and enforce it, since mixing margin and probability plots across models makes them impossible to compare.

## Two scales for one prediction A binary gradient-boosted classifier produces a **margin** first: the sum of its trees' leaf values plus a bias, a number on the **log-odds** scale that can be any real value. The probability that `predict_proba` reports is that margin passed through the **logistic function**: `p = 1 / (1 + exp(-m))` shap's TreeExplainer explains the model's raw output unless it is configured otherwise, and for an XGBoost binary classifier the raw output is that margin. TreeExplainer's own documentation spells out the consequence: the values sum, with the expected value, to the model output in the explained space, and for an XGBoost classifier that is log-odds, so a logistic inverse transform such as `scipy.special.expit` must be applied to the sum before comparing it with `predict_proba`. ## The readmission numbers, step by step For one discharged patient of a hospital's 30-day readmission model: | Quantity | Log-odds (explained scale) | Probability (`expit`) | |---|---|---| | Base value `E[f(X)]` | -1.20 | 0.23 | | Sum of SHAP values | +0.35 | (no meaning on its own) | | Output `f(x)` | -0.85 | 0.30 | So the waterfall's `f(x) = -0.85` and the 0.30 from `predict_proba` are the same prediction. The check in code: ```python from scipy.special import expit row = sv[17] margin = row.base_values + row.values.sum() # -0.85, log-odds print(expit(margin)) # ~0.30, the predicted probability print(expit(row.base_values)) # ~0.23, the average patient ``` ## Why single bars cannot be converted The logistic curve is steep near the middle and flat near the ends, so the probability a fixed log-odds step buys depends on where it starts. Take the patient's `prior_admissions_12m` bar of +0.62: 1. Added first, from the base value: `expit(-0.58) - expit(-1.20)` is about +0.13. 2. Added last, into `f(x)`: `expit(-0.85) - expit(-1.47)` is about +0.11. Neither is "the" probability contribution. Applying `expit` to each bar separately gives numbers that do not sum to anything meaningful, which is why additivity is promised on the explained scale only. ## The base value on the margin scale The base value deserves the same care as the bars. `E[f(X)] = -1.20` is the **average margin** over the reference data. Its logistic, about 0.23, is a convenient anchor for readers, but it is not exactly the average predicted probability across those patients: the logistic of an average margin differs from the average of the logistic of each margin, because the curve bends. For a rare positive class the base value is usually negative, which is expected and not a sign of a broken explanation. When someone asks "what is the starting risk on this chart?", the honest answer is "an average log-odds of -1.20, roughly 0.23 as a probability". ## Ways to present probability without breaking additivity - **Relabel the axis.** `shap.plots.force(row, link="logit")` keeps the additive widths in log-odds but prints the tick labels as probabilities. Its default is `link="identity"`. The waterfall has no such argument; label its axis as log-odds in the caption instead. - **Report the endpoints.** State the base value and `f(x)` as probabilities in text (0.23 to 0.30) and keep the bars in log-odds. - **Explain probability directly.** An explainer can be configured to explain the probability output instead of the margin, so the values sum to the probability; that is a choice made when the explainer is built, and it changes what every downstream plot shows. ## The `link=` argument on the unified Explainer `shap.Explainer(model, masker, link=...)` takes a link function, `shap.links.identity` by default; `shap.links.logit` makes model-agnostic explainers that call a probability function compute their averages in probability units while reporting values in log-odds units. It does not reach every path: when the unified Explainer routes a tree model to TreeExplainer, the current source accepts `link` and ignores it, so the values stay on the raw margin. Read the scale off the output, not off the arguments you passed. ## What a careful reader checks - Whether `base_values` looks like a probability (between 0 and 1, close to the readmission rate) or a margin (often negative for a rare positive class). - Whether `base_values + values.sum()` matches `predict_proba` directly or only after `expit`. - Whether every plot shown to a care-management team states its scale in the caption.

  • Why can't you report each bar as 'adds N percentage points of readmission risk'?
    The logistic function is non-linear, so a log-odds step is worth a different amount of probability depending on where it starts. The +0.62 prior-admissions bar is about +0.13 when added to the base value and about +0.11 as the last step into f(x). Only the endpoints convert cleanly; the per-bar decomposition is additive in log-odds alone.
  • Your colleague passes `link=shap.links.logit` to `shap.Explainer` for the XGBoost model and expects probability-scale values. What do they get?
    The unified Explainer routes the XGBoost model to TreeExplainer, which accepts `link` but ignores it in the current source, so the values are still raw log-odds margins. The link function matters for model-agnostic explainers that call a probability function; for a tree model, check the scale by comparing `base_values + values.sum()` with `predict_proba`.

  • SHAP values for a binary XGBoost model always sum to the predicted probability.
  • Apply the logistic function to each SHAP value to get its probability contribution.
  • A negative base value means the explanation is broken.
  • Passing link=shap.links.logit always converts a tree model's values to probabilities.
  • The waterfall's f(x) should equal predict_proba exactly, or the explainer has a bug.