Counterfactual Explanations#
Explaining a decision by the smallest input change that would flip it.
Important
✨ AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
What it is#
A counterfactual explanation shows how a prediction would change if the input were different, answering: what minimal change to this input would flip the model’s decision? It borrows the causal idea of a counterfactual — what would have happened if …?
The formal definition#
Given an instance \(x\) with prediction \(f(x)\), a counterfactual is an alternative \(x'\) that changes the outcome yet stays as close as possible to \(x\):
with \(f(x') \neq f(x)\) and \(d(\cdot)\) a distance metric (L1, L2 or a feature-specific cost).
A worked example#
For a denied loan, a counterfactual might say: if income rose by $5,000, or the credit
score were 650 instead of 600, the loan would be approved — actionable recourse. Good
counterfactuals are valid (they flip the prediction), proximal and sparse (few, small
changes), actionable (realistic and controllable — “get older” is not), and diverse (several
options).
Methods and uses#
They are generated by optimisation, gradient search (for differentiable models) or
generative models (VAE, GAN) for realism, via libraries like alibi and dice-ml. They power
user recourse (“what must I change?”), fairness audits (do minority groups need unfairly large
changes for the same decision?) and model debugging — but they can be unrealistic (e.g. “if
gender changed”), are non-unique, and need domain constraints to stay valid and fair.
Theme: Explainability & Governance · All terminology
Hint
Mind map — connected ideas
SHAP (SHapley Additive exPlanations) · LIME (Local Interpretable Model-agnostic Explanations) · Causal Effect · Causal Inference · Post-hoc Explainability · Equalized Odds (Fairness)
Hint
More in Explainability & Governance
Basel III · Fair Lending laws · High-Stakes Domains · LIME (Local Interpretable Model-agnostic Explanations) · Post-hoc Explainability · SHAP (SHapley Additive exPlanations)
See also
Source article Adapted (context, re-expressed) in our own words from: Counterfactual Explanations (insightful-data-lab.com).