Counterfactual Explanations
Find the minimum L1 or Euclidean L2 change to flip a linear classifier decision.
By Riano · Published · Updated · Report a correction
Counterfactual Explanations
Concept Overview
A counterfactual explanation describes the smallest change to the feature values of an instance that changes the prediction to a predefined output (such as a favorable outcome). For a given negative prediction, the counterfactual finds a point on or across the decision boundary by minimizing a specific distance metric (like L1 or L2 norm), subject to some features being immutable. It illustrates model recourse; the mathematical suggestion alone does not establish a feasible real-world action.
Mathematical Definition
Given a fixed linear classifier defined by the score function:
The positive class is assigned when score ≥ 0. If an input has a negative score, we find the minimum distance perturbation Δ = (Δx1, Δx2) to reach the decision boundary score = 0. This is formulated as an optimization problem:
subject to: (x1 + Δx1) + 2(x2 + Δx2) - 1 = 0
and Δxi = 0 for all immutable features i.
Worked example: Finding the L2 Counterfactual
Consider an input with x1 = 0 and x2 = 0. We want to find the L2 counterfactual when no features are immutable.
- Calculate the initial score:
score = 0 + 2(0) - 1 = -1. Since it is negative, a counterfactual is needed. - Calculate the required gap to the boundary:
gap = -score = 1. - Identify the free coefficient vector
afree = (1, 2). The squared norm is||afree||2 = 12 + 22 = 5. - Calculate the delta for the L2 distance:
Δ = gap * afree / ||afree||2 = 1 * (1, 2) / 5 = (1/5, 2/5) = (0.2, 0.4). - Calculate the minimum distance:
√(0.22 + 0.42) = √(0.04 + 0.16) = √0.20 ≈ 0.447214. - The final counterfactual point is
(0.2, 0.4).
Experimenting with the Module
Use the interactive Counterfactual Explanations module to explore how distance metrics and immutable features affect the recourse path:
- Set Feature x1 to 0 and Feature x2 to 0.
- Set Distance to L2 and Immutable to None. Observe the delta
[1/5, 2/5]and the distance0.447214. - Change the Distance to L1. Notice the delta changes to
[0, 1/2](distance0.5), as the L1 norm places the entire change on the feature with the highest coefficient magnitude (x2) to minimize the absolute sum. - Set the Immutable dropdown to
x2. The delta becomes[1, 0](distance1.0), because x2 cannot be changed and all the burden falls on x1. - Set the Immutable dropdown to
Both. The module correctly reports "Infeasible: both features immutable" and distance "undefined", since the point cannot reach the boundary. - Set Feature x1 to 1 and Feature x2 to 0. The point now lies exactly on the boundary (score 0), resulting in a "Already positive" status and 0 distance.
- For an output outside the input range, set Feature x1 = -1, Feature x2 = -1 and Immutable = x2. Under either distance, only x1 can move: Δ = [4, 0], the counterfactual is [3, -1], and the minimum distance is 4. The plot expands; 3 is not clipped to the slider maximum 1.
Limitations and Real-World Constraints
While counterfactuals provide a mathematical recipe for recourse, this formulation has notable limitations:
- Causal independence assumption: The distance minimization assumes features can be altered independently. In reality, features are often causally linked (e.g., increasing age might naturally increase savings).
- Actionability guarantees: Identifying a mathematical minimum-distance vector does not guarantee that the suggested recourse is ethically, legally, or practically feasible in the real world.
- Display rounding: Coordinates, deltas and L1 distances use exact reduced fractions on the supported rational grid; decimals round to six places. L2 distance is the Euclidean norm, not its square, and can be irrational. Input sliders do not impose bounds on counterfactual outputs. An infeasible case has no counterfactual vector, and the view shows only the original point.
References
Primary literature detailing the concept of counterfactual explanations for algorithmic decision-making:
- Wachter, S., Mittelstadt, B., & Russell, C. (2017). Counterfactual Explanations without Opening the Black Box: Automated Decisions and the GDPR. Harvard Journal of Law & Technology. This paper introduces the foundational framework for generating counterfactual explanations as a mechanism for model interpretability and recourse.
Experience it interactively
Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Counterfactual Explanations module.
Try Counterfactual Explanations on Riano →