- Introduced by
- Sandra Wachter, Brent Mittelstadt & Chris Russell
- Year
- 2017
- Type
- Post-hoc, model-agnostic explanation
- Family
- Explainable AI
What does it do?
Counterfactual explanations, first circulated in 2017 as an arXiv preprint by Wachter, Mittelstadt, and Russell, answer the question: 'What is the smallest change to the input that would have produced a different model output?' Rather than explaining why a model made a decision, they describe what would need to change to reverse it.
The approach is often discussed for high-stakes settings such as credit scoring, medical diagnosis, and hiring, where affected individuals may need an explanation of an automated decision under data-protection rules such as the EU GDPR. The term algorithmic recourse is sometimes used for this line of work, but it refers to the broader question of how individuals can obtain a different outcome, and counterfactuals are only one tool within it.
The intuition behind the method
Imagine you are denied a loan and the system tells you: 'If your annual income had been $5,000 higher and your existing debt $2,000 lower, your application would have been approved.' That is a counterfactual explanation. It does not expose the inner workings of the model, but it provides an actionable, contrastive statement that allows you to understand the decision boundary and, if desired, to act on the advice to obtain a different outcome in the future.
History
The framework was first circulated in 2017 as an arXiv preprint (arXiv:1711.00399) by Wachter, Mittelstadt, and Russell. The article is cited here as published in the Harvard Journal of Law & Technology, vol. 31, pp. 841–887. The work framed counterfactual explanations in relation to GDPR requirements for automated decisions and proposed an optimization-based formulation. Later work extended the approach, including toward sets of diverse counterfactuals.
When to use it
Choose counterfactual explanations when an individual needs to know what would have to change for a particular automated decision to be different, for example a credit or hiring decision where the person can act on the feedback. The method needs query access to the model, which suits black-box systems. The MAD scaling in the distance term also requires the training data or a representative reference sample, and gradient-based variants additionally require a differentiable model. Do not use it as the main tool for detecting bias or for describing model behaviour across a population; global methods such as SHAP are better suited to that question. Because several valid counterfactuals usually exist, decide in advance which criteria (sparsity, feasibility of the suggested changes, diversity) will be used to select among them, and report how many candidates were checked and how many actually changed the predicted class.
Limitations and considerations
- May produce counterfactuals with unrealistic or immutable feature combinations (e.g., changing age or race)
- Multiple counterfactuals can exist with no principled way to choose among them without additional criteria
- Proximity alone does not guarantee feasibility or causally valid recourse for the affected individual
- Gradient-based search is limited to differentiable models; black-box search can be computationally expensive
- Assumes the decision boundary is stable: a counterfactual computed for one model version may not hold after retraining or under distribution shift
- Unconstrained search can return points off the data manifold that the model has rarely or never seen, so the suggested change may be unrealistic even when the model's output flips
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Model-agnostic: works with any classifier or regressor that can be queried
- Actionable and human-interpretable: directly tells users what to change to obtain a different outcome
- Relevant to explanation requirements: the framing was motivated by GDPR-related concerns about automated decisions, as the cited title indicates
- No need to expose model internals, which helps protect proprietary models; repeated queries can still reveal information about the decision boundary (see the FAQ)
Applications
- Credit scoring: explaining why a loan application was rejected and what changes the model would need to see to approve it
- Clinical decision support: showing which model inputs, such as biomarker values or lifestyle factors, would need to change to shift a diagnostic prediction; this describes the model's boundary and does not establish that changing those factors would change the patient's actual condition, so clinical use requires causal validation
- Hiring and recruitment: providing candidates with actionable feedback, restricted to features they can realistically change, when algorithmic screening rejects an application
- Fraud detection: helping analysts understand the minimal feature profile that distinguishes flagged from non-flagged transactions
How it works
How it works
The core idea is to frame counterfactual search as a constrained optimization problem. Given an original input x and a desired outcome y', we seek a new input x' that achieves y' while remaining as close as possible to x. The trade-off between closeness and achieving the target outcome is controlled by a hyperparameter λ, which balances the two competing objectives in a single loss function.
Formula
\min_{x'} \; d(x, x') + \lambda \cdot \ell(f(x'), y')Proximity is measured as a sum over features of the absolute difference between the original and candidate values, each divided by that feature's median absolute deviation (MAD) in the training data, as the formula shows. Dividing by MAD places features with different units and spreads on a comparable scale, so a feature with large natural variation does not dominate simply because its numbers are larger. Computing each MAD_j requires the training data or a representative reference sample, in addition to query access to the model's predictions. The formula contains no explicit feature weights, so the distance is an unweighted sum of scaled differences.
Formula
d(x, x') = \sum_{j} \frac{|x_j - x'_j|}{\text{MAD}_j}The outcome term penalizes the squared gap between the model output f(x') and the target value y', where y' is chosen to differ from the original prediction f(x). Minimizing this term moves the model output toward y', but the squared-error loss on its own does not guarantee that the output crosses the decision boundary. Whether a candidate actually changes the predicted class depends on the chosen target and on λ, so each candidate's predicted class should be checked rather than assumed to have flipped.
Formula
\ell(f(x'), y') = (f(x') - y')^2, \quad y' \neq f(x)
The combined objective is minimized over x'. Gradient-based optimization can be used when the model is differentiable; otherwise heuristic or black-box search is needed. The returned x* is one candidate, and the solution set may contain several. Choosing among them for diversity, sparsity, and actionability is a separate step that the objective above does not perform. The weight λ sets the trade-off: a small λ prioritises closeness to x and may leave the candidate on the original side of the decision boundary, while a large λ prioritises reaching y' at the cost of larger changes. Each candidate should be checked by re-running the model to confirm that its predicted class has changed. Common measures to report are validity (the share of candidates that change the prediction), proximity (the distance d(x, x')), sparsity (the number of features changed), and diversity across the returned set.
Formula
x^* = \arg\min_{x'} \left[ d(x, x') + \lambda \cdot \ell(f(x'), y') \right]Common pitfalls
- Treating counterfactuals as global explanations: they explain a single prediction, not the model as a whole
- Ignoring feature mutability: suggesting changes to immutable attributes (e.g., birthplace) is misleading and potentially discriminatory
- Using a single counterfactual when multiple diverse ones should be presented to avoid anchoring the user
- Conflating counterfactual explanations with causal recourse: the former describe the model's boundary, not real-world causal mechanisms
Frequently asked questions
Do counterfactual explanations reveal the model's internal parameters?
No. The method requires only query access to the model's predictions, not its weights or architecture, so it can be used when the model is proprietary. However, a sequence of queries can in principle reveal information about the decision boundary, so deployments may consider limits on query rates. If the MAD-based distance is used, the training data or a representative reference sample is also needed, which may be a separate constraint.
How is a counterfactual different from a sensitivity analysis or LIME explanation?
Sensitivity analysis and LIME describe how the model's output changes locally as inputs vary; this is often summarised as feature importance. Counterfactual explanations instead identify a specific alternative input that achieves a particular target outcome. The counterfactual is prescriptive and contrastive ('change these features to get this result'), whereas LIME is descriptive ('these features were most influential for this prediction').
What does 'actionable' mean for a counterfactual, and why does it matter?
An actionable counterfactual suggests changes to features that the individual can realistically alter, such as income, savings, or debt, rather than immutable attributes such as age or nationality. Actionability matters because the primary value of counterfactual explanations is to provide recourse. If the suggested changes are impossible, the explanation is technically valid but practically useless and may even cause harm by raising false hope or implying that protected attributes influenced the decision.
Related methods
Sources
Wachter, S., Mittelstadt, B., & Russell, C. (2017). Counterfactual explanations without opening the black box: Automated decisions and the GDPR. Harvard Journal of Law & Technology, 31, 841–887.
Open publication reference