Exercises — Counterfactual explanations

The figure above is our map. The blue line is the boundary . Points below-left get class 0 (denied), above-right get class 1 (approved). The orange arrow is — it points straight "uphill", perpendicular to the boundary. Keep this picture in mind: a counterfactual is just the shortest walk that gets you across the blue line into the approved region.
Level 1 — Recognition
Exercise 1.1
Which of these is the correct definition of a counterfactual explanation for input with prediction ? (a) The single most important feature ranked by weight. (b) A point close to with that is realistic. (c) The average of all training points labelled 1. (d) A perturbation that maximally increases the model's confidence in class 0.
Recall Solution 1.1
Answer: (b). A counterfactual must satisfy three things at once: (1) flips the prediction (), (2) stays close to (minimal change), (3) is realistic (lies on the data manifold). Option (a) is feature importance, not a counterfactual. (c) ignores proximity to . (d) pushes the wrong way — deeper into class 0.
Exercise 1.2
Match each loss term to its job: , , (both hinge and as defined in the opening callouts). Jobs: (A) make the prediction flip, (B) change few features, (C) stay realistic.
Recall Solution 1.2
- (B) change few features. The norm rewards sparse changes (many coordinates stay put).
- (A) make the prediction flip. Recall with : it is positive until the probability crosses (equivalently, until the score crosses ), so minimising it pushes into the target class.
- (C) stay realistic. It is the distance-to-real-data penalty from the opening callout, so it penalises inputs far from the data manifold.
Exercise 1.3
True or false: an adversarial example and a counterfactual are solved by the same optimisation, and the only difference is intent/plausibility.
Recall Solution 1.3
True (with a caveat). Both minimise "small change that flips the prediction". The difference is the plausibility term : a counterfactual wants the result to be realistic and actionable for a human; an adversarial example is happy with an imperceptible, unrealistic perturbation. Drop plausibility + actionability constraints and a counterfactual generator will happily produce adversarial garbage — that is exactly the "Cons" listed for the gradient method.
Level 2 — Application
Exercise 2.1
For our running model , classify the point .
Recall Solution 2.1
Compute the score . Since , we get (denied). The point sits below-left of the blue boundary in the figure.
Exercise 2.2
Find the closest point on the decision boundary to using the projection formula
Recall Solution 2.2
What we compute first — why: the formula needs the score (how far offside we are) and (how "strong" the weight direction is, to convert score into distance).
- Score: (from 2.1).
- .
- Step factor: . Now move: Check it lies on the line: ✓ (And by our convention, that boundary point already counts as approved.) What it looks like: we walked from straight toward the boundary along (the orange arrow) and landed exactly on the blue line — the shortest possible walk.
Exercise 2.3
The Euclidean (recourse) cost to reach the boundary is . Compute it, and explain why it equals .
Recall Solution 2.3
Directly: , so Why : the move is , whose length is . Check: ✓ This is the classic point-to-line distance — the counterfactual on a linear model is just that geometric distance.
Level 3 — Analysis
Exercise 3.1
On our model, feature is immutable (say it's "age"), so only may change. Starting from (denied), find the counterfactual that just reaches the boundary changing only . Compare its cost to the free projection from 2.2.
Recall Solution 3.1
What we do — why: immutability means we hold fixed and solve the boundary equation for alone. This is the "" idea (forbid changing feature ) made concrete. Set . So , an change of in . Comparison: the free projection (2.2) moved both features, with cost . The constrained solution changes only one feature but its cost ()... is actually smaller in here because it concentrates the whole move into the cheaper single coordinate. In though, constrained cost — the constraint never helps the Euclidean distance, since the free projection is by definition the Euclidean minimum. What it looks like: instead of a diagonal walk, we walk straight up the axis until we hit the blue line.

Exercise 3.2
Why does tend to give sparse counterfactuals (few features changed) while spreads the change? Argue geometrically.
Recall Solution 3.2
Picture the "cost ball". Fix a total budget and draw all points reachable at that cost.
- The ball () is a diamond with sharp corners on the axes.
- The ball () is a circle — smooth, no preferred direction. As you grow the budget until the ball first touches the boundary line, the diamond almost always touches at a corner, and corners lie on axes → only one coordinate changed → sparse. The circle touches at a generic point → both coordinates change → spread. This is the same reason /LASSO produces sparse weights.
Exercise 3.3
The hinge loss (defined in the opening callout) is , where is the model's approved-class probability. Evaluate the hinge for a denied point with and an approved point with , and explain why gradient descent stops "pushing" once the flip happens.
Recall Solution 3.3
Recall turns the score into a probability, with exactly on the approved side.
- Denied, : (positive → still penalised → gradient still pushes toward class 1).
- Approved, : (zero → no more prediction penalty). Why it stops: once the max saturates to , so . From that moment the only active gradient is from the proximity term , which pulls back toward — settling it right near the boundary rather than driving to . That's exactly what we want: just barely flipped, minimal change.
Level 4 — Synthesis
Exercise 4.1
Recourse design. A bank uses our model with income (in $10k) and credit-utilisation . Applicant is at , denied. Changing income costs the applicant 3 units of effort per unit, changing utilisation costs 1 unit per unit. Using a weighted cost , which single-feature move to the boundary is cheaper — pure income or pure utilisation?
Recall Solution 4.1
Pure utilisation ( only): from 3.1, need , i.e. . Cost . Pure income ( only): hold , solve , so . Cost . Verdict: utilisation is the actionable, cheap recourse (cost 0.5 ≪ 2.0). Even though income has a smaller raw per unit of boundary-crossing? No — utilisation also had a smaller raw and a smaller unit cost. The weighted formalises "which advice is easiest to follow."
Exercise 4.2
DiCE-style diversity. Two candidate counterfactuals are proposed: (the free projection) and (utilisation-only). Compute their pairwise diversity and explain why DiCE rewards this quantity being large.
Recall Solution 4.2
Why DiCE maximises it: the objective has a term — a minus sign, so making pairs far apart lowers the loss. The point is to offer the user genuinely different options ("raise a little of both" vs "fix utilisation only") rather than two nearly identical suggestions. A diversity of means these two counterfactuals really are distinct recourse paths.
Exercise 4.3
Causal realism. Suppose in the world, raising income causally raises credit-utilisation slack, modelled as (utilisation drops when income rises). The applicant proposes raising income by . What is the true post-intervention point, and does it cross the boundary?
Recall Solution 4.3
What changes — why: a naive counterfactual moves and freezes . But causally, income affects too — ignoring that mis-estimates the flip. Naive prediction (freeze ): → score . By our convention this sits exactly on the boundary and the naive model calls it approved. True causal point: income rising by drags down: . New point → score Comparison & interpretation: the naive score was (borderline approved); the true causal score is , firmly on the denied side. So the naive counterfactual overstates the flip — the applicant would follow the advice ("raise income by $6.7k") and still be denied, because the income rise causally pulled utilisation the wrong way. This is precisely why Causal Inference / FACE-style causal constraints matter: a counterfactual that ignores downstream effects gives false hope.
Level 5 — Mastery
Exercise 5.1
Open synthesis. You must ship counterfactual recourse for a credit model. Design the full objective, listing every term, its distance metric, its constraints, and one validation check that catches the L4 trap. Then state one property no purely local method can guarantee (motivating Model Debugging).
Recall Solution 5.1
Objective (weighted, constrained, plausible): Term by term:
- — weighted for sparse, effort-aware recourse; set for immutable features (age/race), and = per-unit effort otherwise (Ex 4.1). chosen over for few-feature advice (Ex 3.2).
- — hinge or cross-entropy on the soft output to guarantee the flip (validity); hinge saturates once (Ex 3.3) so we land just past the boundary.
- — the manifold penalty from the opening callout (MAD-normalised distance to nearest data or VAE reconstruction) so stays on the data manifold, separating us from Adversarial Examples.
- — DiCE diversity to offer multiple recourse paths (Ex 4.2).
- Constraints: project onto each step (age , categorical rounding), and only allow moves consistent with a causal DAG (FACE).
- Validation catching the L4 trap: apply the proposed intervention through the causal model, recompute the post-intervention input, and re-check on that point (Ex 4.3). Reject any counterfactual that only flips under the naive independence assumption.
- What no local method guarantees: global consistency — a locally cheap flip might exploit a spurious feature or a region where the model is simply wrong. Confirming the recourse reflects a genuine, correct decision boundary (not a bug) requires Model Debugging and out-of-sample testing.
Exercise 5.2
Limiting behaviour. For our linear model, what is the recourse distance as (a) the point sits on the boundary, (b) with fixed, (c) ? Interpret each.
Recall Solution 5.2
- (a) On the boundary: distance . Already at the flip — no change needed. The counterfactual is the point itself (and by it is already approved).
- (b) : distance . A very "steep/confident" model has a razor-thin margin in input space — a tiny nudge flips it (this is precisely the regime where adversarial examples thrive).
- (c) : distance (with fixed and nonzero, ). The model barely depends on the inputs, so no reasonable feature change flips it — recourse is effectively impossible. Signals a degenerate/uninformative model worth Model Debugging.
Recall One-line recap you should be able to say out loud
A counterfactual is the shortest realistic walk across the decision boundary ::: it minimises a weighted distance to subject to flipping and staying plausible + actionable. For a linear model the walk is exactly the projection ::: with recourse distance . gives few-feature (sparse) advice, gives smooth spread-out changes ::: because the ball is a diamond touching the boundary at an axis corner.