Part XXI — Explainable AI & Model Interpretability · Chapter 8

Counterfactual explanations

Hook

SHAP — short for SHapley Additive exPlanations — answers "how much did each feature contribute?" A rejected applicant usually wants a more actionable answer: "what's the smallest change to my application that would have gotten me approved?"

Intuition
decision value -20.0 — denied, distance to boundary 4.00

This is Part I Chapter 6's gradient field again, but the "loss" is now a linear approval score, and the gradient — constant everywhere, since the score is linear — always points in the one direction that increases it fastest. Drag the point: that arrow is the cheapest possible direction to move toward "approved."

Formalize

For a linear decision function f(x)=wx+bf(x) = w\cdot x + b, approved when f(x)0f(x) \geq 0, the closest point on the boundary itself — the smallest possible change that flips the decision — has a closed form:

xcf=xf(x)w2w,distance=f(x)wx^{\text{cf}} = x - \frac{f(x)}{\lVert w \rVert^2}\,w, \qquad \text{distance} = \frac{|f(x)|}{\lVert w \rVert}
  • xx — the current point, e.g. an applicant's feature values.
  • f(x)f(x) — the linear decision function's score at xx; approved when f(x)0f(x) \geq 0.
  • ww — the decision function's weight vector.
  • xcfx^{\text{cf}} — the counterfactual point: the nearest point on the decision boundary.
  • distance\text{distance} — how far xx sits from the boundary.
  1. The same gradient direction, scaled to land exactly on the boundary

    This is exactly Chapter 6's gradient direction, scaled to land precisely on f=0f=0 instead of just pointing that way — the same idea Part I used to walk downhill, now used to walk to the nearest point where a linear classifier's answer changes.

Play
nearest point that flips the decision: (2.40, 3.20) — 4.00 units away

Drag the point anywhere. The marked counterfactual point always sits on the boundary, always at the foot of the perpendicular from wherever you are — never straight toward the origin, never toward any other fixed spot. The cheapest fix depends entirely on where the applicant currently stands.

Worked example

With weights w=(3,4)w=(3,4) (so w=5\lVert w \rVert = 5) and bias b=20b=-20, starting from (0,0)(0,0):

  1. Check the current decision

    f(0,0)=3(0)+4(0)20=20f(0,0) = 3(0)+4(0)-20 = -20 — denied, and 20/5=420/5 = 4 units from the boundary.

  2. Find the nearest counterfactual

    xcf=(0,0)2025(3,4)=(0,0)+0.8(3,4)=(2.4, 3.2)x^{\text{cf}} = (0,0) - \frac{-20}{25}(3,4) = (0,0) + 0.8\,(3,4) = (2.4,\ 3.2).

  3. Verify it's exactly on the boundary

    f(2.4,3.2)=3(2.4)+4(3.2)20=7.2+12.820=0f(2.4, 3.2) = 3(2.4)+4(3.2)-20 = 7.2+12.8-20=0 — exactly zero, confirming (2.4,3.2)(2.4,3.2) is the smallest possible change from (0,0)(0,0) that flips the decision.

Checkpoint

Drag the point until the decision flips from denied to approved.

decision value -20.0 — denied
Drag the point to try it
Summary
xcf=xf(x)w2w,distance=f(x)wx^{\text{cf}} = x - \frac{f(x)}{\lVert w \rVert^2}\,w, \qquad \text{distance} = \frac{|f(x)|}{\lVert w \rVert}

For a linear model this is exact and cheap — the same closed form the SHAP chapter used. For a real, nonlinear classifier, there's no single formula: finding the nearest point that flips the decision becomes its own small optimization problem, usually solved by taking gradient steps toward the boundary rather than jumping there in one shot. But the question stays exactly the same, and it's often the most useful answer of all: not why the model decided what it did, but what would have to change.