skip to content

TreeExplainer raises 'Additivity check failed in TreeExplainer!' on a readmission scoring batch — what is it telling you, and is `check_additivity=False` the fix?

level: seniorimportance: nice to knowfreq 5%

answer

  1. two computations disagree
  2. base value plus values against the model's output
  3. tolerance of about one percent
  4. only on the raw output
  5. turning it off hides, never repairs

basics

~20 s

The error means shap's tree walk and the model's own prediction disagree for some row: base value plus SHAP values misses the model's raw output beyond a 1e-2 tolerance. Setting check_additivity=False only skips the check; the unverified values may not add up.

solid answer

~40 s

TreeExplainer's `check_additivity=True` default compares, for every row, `expected_value + values.sum()` with the model's own raw output, using `np.allclose` with `atol=1e-2` and `rtol=1e-2`. When they differ, it raises an `ExplainerError` whose message reports the sum and the model output for the worst row. That means shap's parsed copy of the trees and the model's prediction path read the rows differently; the message's first suspect is a data matrix not shaped or encoded as the model was trained on. `check_additivity=False` skips the comparison and returns the values anyway, so every waterfall built from them may end somewhere other than the model's real score. I would find the mismatch first, reproduce the check by hand on a few rows, and switch the check off only for a gap I understand and can accept.

go deeper

for a junior

Know that shap checks that base value plus SHAP values equals the model output, and that a failure means the explanation is not trustworthy as it stands.

for a middle

Explain what the check compares, its tolerance of about 1e-2, that it runs on the raw output only, and what disabling it does and does not do.

for a senior

Diagnose the cause from the error's numbers and the input schema, isolate failing rows in a batch job, and only disable the check for a gap you can explain.

for a principal

Treat additivity failures as a release gate for explanation pipelines, with failing rows quarantined and every disabled check recorded with its reason.

## What the check compares shap promises **local additivity**: for each row, the base value plus that row's SHAP values equals the model output on the explained scale. TreeExplainer enforces the promise at run time. Its call and its `shap_values` method take `check_additivity=True` by default, and after computing the values it runs, per row: `expected_value + values.sum()` against the model's own prediction for that row The comparison is `np.allclose(..., atol=1e-2, rtol=1e-2)`, so differences of about a hundredth are tolerated and anything larger fails. The check runs only when the explanation is on the model's **raw** output, the default; TreeExplainer's documentation states that it currently runs only when explaining the margin. The GPU tree explainer and the deep-learning explainers take the same argument; the model-agnostic explainers do not. ## Reading the error A failure raises an `ExplainerError` with a message that does three things: 1. It opens with "Additivity check failed in TreeExplainer!" and asks you to ensure the data matrix passed to the explainer is the same shape the model was trained on. 2. When the explainer is not on its interventional path, it suggests retrying with that option. 3. It reports, for the worst row, the sum of the SHAP values and the model output, then says `check_additivity=False` disables the check if the difference is acceptable. The size of that gap is diagnostic: | Gap in the message | Likely meaning | |---|---| | Just above the tolerance on a large margin | Possibly numerical precision; inspect before accepting | | Same sign, clearly larger | Part of the model is being read differently, for example a subset of trees or a transformed input | | Wildly different, even opposite sign | The rows are being interpreted differently by shap and by the model | ## Where the mismatch usually comes from The check compares two computations of the same prediction: shap's walk through its parsed copy of the trees, and the model's own predict call. They disagree when: - **the input differs from training**, in column count, column order, dtypes or how categorical columns are encoded, so the two paths do not see the same feature values; - **the model has changed since the explainer parsed it**, for example a model object refitted in place after the explainer was built, so shap walks stale trees; - **the explainer's configuration and the model disagree**, which is what the message's interventional suggestion addresses; that belongs to how the explainer is set up; - **a genuine library bug** affects that model type, which the message asks you to report. ## Why switching the check off is not a fix `check_additivity=False` does exactly one thing: it skips the comparison. The values are returned unchanged. Every consumer downstream inherits the problem: - a care-management waterfall ends at an `f(x)` that is not the model's real score for that patient; - a beeswarm ranks features with values that do not account for the prediction; - nobody looking at the plot can tell, because the plot always closes on its own arithmetic. Batch jobs make this worse: a scoring job that explains thousands of patients overnight with the check disabled publishes every row, including the broken ones, and the first sign of trouble is a clinician asking why a waterfall disagrees with the risk score on the screen. Turning it off is reasonable only when you have found the cause and the remaining gap is understood and small, for example a documented precision difference on a large margin. Record that decision next to the explainer configuration. ## A manual check for the batch job Before switching anything off, reproduce the check on a sample of rows: ```python import numpy as np sample = X_batch[:500] sv = explainer(sample, check_additivity=False) # diagnostic run only recon = sv.base_values + sv.values.sum(axis=1) # explained scale margin = readmit_model.predict(sample, output_margin=True) # XGBoost's raw margin, same rows gap = np.abs(recon - margin) print(gap.max(), np.argmax(gap)) # inspect the worst row's inputs ``` The model's raw output must be computed on **the same rows in the same order**, a requirement TreeExplainer's documentation states. Inspect the worst row's raw inputs against the training schema; a mis-typed or reordered column usually shows up immediately.

  • Why might your own additivity check fail even though TreeExplainer's passes?
    You may be comparing against the wrong scale or the wrong rows. For a binary XGBoost model the reconstruction is a log-odds margin, so it matches the probability only after the logistic function. TreeExplainer's documentation also requires the SHAP values and the predictions to come from the same samples in the same order.
  • The batch must ship tonight and the failing rows are 3 of 50,000. What do you do?
    Explain the clean rows with the check on, quarantine the three, and inspect their raw inputs against the training schema. Shipping the whole batch with `check_additivity=False` hides which rows are wrong; isolating them keeps every published explanation verified and gives you the evidence to fix the cause.

  • Setting check_additivity=False rescales the values so they add up.
  • An additivity failure just means the background data was too small.
  • For an XGBoost classifier, the additivity check compares the SHAP sum with predict_proba.
  • The additivity error is harmless floating-point noise and can always be ignored.
  • Every shap explainer accepts check_additivity.