Visual walkthrough — Counterfactual explanations
This is the visual companion to the main note. We only need one setting: an input the model rejected, and the question "what is the smallest nudge that gets it accepted?"
Step 1 — What is a "point" and what is the model doing to it?
WHAT. Picture a person applying for a loan described by two numbers: income (horizontal) and credit score (vertical). That pair of numbers is a single point on a flat sheet of paper — a plane. We write it . The bold letter just means "a bundle of numbers"; the little subscripts are which slot.
WHY. Before any formula, we must agree that an applicant = a dot on a map. Every idea below is about moving that dot.
PICTURE. The amber dot is our applicant . The whole map is split into two coloured regions by a line — the model's verdict.
Step 2 — The straight-line model: reading off which side you are on
WHAT. The simplest possible border is a straight line. To test which side a dot is on, the model builds one number: Then it looks only at the sign of : positive → approved, negative → denied, exactly zero → sitting on the border. We keep this convention for the rest of the page: means approved, means denied.
WHY. We need a rule that turns a 2-D dot into a single "how approved are you" score. A weighted sum is the cheapest such rule. The weights say how much each feature counts; shifts the whole border sideways so it need not pass through the origin.
PICTURE. The score grows as you march in one particular direction. That direction — the direction of steepest increase of the score — is the arrow .
Step 3 — Why the weight arrow points straight across the fence
WHAT. Take two dots and that both sit on the boundary. Both have score , so Subtract them: . The little arrow runs along the fence.
WHY. A dot product of two arrows equals zero exactly when they are at right angles (this is what "" means geometrically — no overlap in direction). So is perpendicular to every direction along the fence. In one sentence: is the normal to the boundary — it points straight across it.
PICTURE. The cyan line is the fence; the amber arrow juts out of it at a perfect .
Step 4 — "Closest point": what are we even minimising?
WHAT. A counterfactual is the applicant we wish we were: same map, but on the approved side, and as near to us as possible. "Near" needs a ruler. The ruler here is straight-line distance, and minimising distance is the same as minimising its square:
WHY. Why the square? Because the square has no ugly square-root, so its slope (gradient) is clean, and squaring doesn't change which point is closest — the nearest dot stays nearest. We also add a rule the answer must obey: land on the fence, . (Reaching the fence is enough to flip the verdict, and going even one inch past it only wastes distance.)
PICTURE. Circles of equal distance grow outward from . The first circle that kisses the fence touches it at exactly one point — our target.
Step 5 — Turning "closest under a rule" into one equation (Lagrange)
WHAT. We minimise distance while obeying the on-the-fence rule. The bookkeeping trick for "minimise A subject to rule B = 0" is to build one combined quantity: The extra knob (a single number, the Lagrange multiplier) tunes how hard the rule pulls.
WHY. At the true best spot, the "make distance smaller" push and the "stay on the fence" push must exactly cancel — otherwise you could slide along the fence and do better. Setting the slope of to zero encodes that cancellation in one line.
PICTURE. Two arrows at the answer: the amber "shrink distance" arrow and the cyan "along-the-fence" constraint. They line up perpendicular — the balance condition.
The slope with respect to : Read it directly: the move must be a multiple of . Rearranged: This is the promise from Step 3, now forced by algebra: you move along , nowhere else.
Step 6 — Solving for how far to move, and the final formula
WHAT. We know the direction (); we need the amount . Plug into the rule "land on the fence": Expand: , so
WHY. is just "the arrow dotted with itself" = its length squared. Substituting the amount back into the direction gives the closed form: Term by term: start at , step against (minus sign — you were on the negative side, you climb toward positive), by an amount = how far off the fence your score is divided by how strong the arrow is .
PICTURE. The green counterfactual lands exactly on the fence, connected to by the shortest possible spoke, parallel to .
Step 7 — The degenerate cases (never let the reader fall off the map)
WHAT & WHY. Three things can go wrong; each has a clear picture.
- Already approved (): the step size is positive, so walks you backward onto the fence you already cleared. The formula still works; it just returns a small backward nudge.
- Exactly on the fence (): step size , . You are already the counterfactual — no change needed.
- Flat model (): then and the formula divides by zero. Picture: the model gives every dot the same verdict — there is no fence, so no counterfactual exists. The math screaming "divide by zero" is the honest answer "impossible".
PICTURE. Three mini-panels: overshoot-back, zero-move, and no-fence.
The one-picture summary
Everything above collapses into a single diagram: dot , the fence , the normal arrow , and the shortest spoke landing at .
Recall Feynman retelling — say it like a story
Imagine you're a dot on a map, standing in the "denied" field. Somewhere across the map is a straight fence separating "denied" from "approved". You want to reach the approved side by walking the least. Common sense says: walk straight at the fence, not diagonally. But which way is "straight at it"? The model secretly carries an arrow, , that always points across the fence — because the fence is drawn exactly so that walking along it never changes your score. So you walk toward the approved side along that arrow. How far? Just enough to zero out your score: your current score divided by the arrow's strength tells you the number of steps. Land, and you're on the fence — the smallest possible change that flips the verdict. If the model has no fence (everyone gets the same answer), the arrow is zero, dividing blows up, and honestly there's nothing you can do. That's the whole story, and it's the same story neural nets tell — only their fence bends, so you take baby steps instead of one leap.
Recall Quick self-test
Why must the counterfactual move be parallel to ? ::: Because minimising distance subject to landing on the fence forces , i.e. the move is a multiple of ; geometrically, is perpendicular to the fence, the shortest crossing. What does measure? ::: A signed "how far off the fence" score — positive = approved side, negative = denied side, size = how deep into a region you are. What happens when ? ::: The model has no decision boundary; the formula divides by zero — no counterfactual exists. How does this extend to non-linear models? ::: Replace the single global by the local gradient and take small repeated steps (gradient descent) instead of one jump.
See also: Recourse (turning this move into real-world advice), Causal Inference & FACE-style constraints (which moves are actually allowed), Model Debugging (using counterfactuals to spot spurious fences).