6.3.10 · D3Interpretability & Explainability

Worked examples — Counterfactual explanations

3,549 words16 min readBack to topic

This is a practice page for Counterfactual explanations (6.3.10). The parent note built the idea (find the smallest change to the input that flips the model's answer) and the formulas. Here we do nothing but worked cases — one for every kind of situation the topic can throw at you, so that when you meet a new problem you have already seen its shape.

Everything below reuses one small notation. Let me pin every symbol down first so no line surprises you.

A few reminders of tools the parent note defined, so this page stands on its own:

Recall Terse recaps of borrowed tools

::: returns if , if , and is undefined/ambiguous at (right on the boundary). (the "sigmoid") ::: squashes any real number into : , with . Used to turn a raw score into a probability. hinge loss ::: is once the probability has crossed (success), and positive while ; it "pushes" until we win, then stops. (lambda) ::: a positive weight balancing the hinge term against the distance term — big = "cross the boundary at any cost", small = "stay close, even if you barely cross". (alpha) ::: the learning rate, the step size of each gradient descent move — small = slow but safe, big = fast but can overshoot.

If any of those felt fast, look at the parent note's derivation — this page assumes you have read it once.


The scenario matrix

Every counterfactual problem lives in one of these boxes. The examples below are labelled with the box they fill.

Cell What makes it distinct Example
A. Linear, one feature moves closest-boundary formula, sign of the side Ex 1
B. Linear, two features projection along , geometry Ex 2
C. Feature costs / immutable feature weights, Ex 3
D. Degenerate: point already on the other side zero change needed Ex 4
E. Degenerate: or point ON boundary formula divides by zero / limit Ex 5
F. vs give different answers sparse vs spread change Ex 6
G. Non-linear model, gradient walk iterate, project, plausibility Ex 7
H. Diversity (DiCE): two counterfactuals "either/or" options Ex 8
I. Real-world word problem loan, units, actionability Ex 9
J. Exam twist: which direction flips fastest? gradient magnitude reasoning Ex 10

We build up from the cleanest geometry (linear) so each formula is earned, then move to gradients and words.












Recall Quick self-test

Ex 1 flips loan by moving income from to ::: k. In Ex 2 the recourse was income k, savings ::: k, total distance . Why does locking income in Ex 3 raise the total change to k savings? ::: removing a cheaper lever forces the remaining feature to do all the work. When does no counterfactual exist (Ex 5a)? ::: when , the model ignores the input, so no change flips it. vs on the diagonal (Ex 6): picks ::: a sparse corner like ; picks the spread point . Feature that flips fastest (Ex 10) has the ::: largest weight magnitude .