Counterfactual Explanations
What is the smallest change in the inputs that changes the model's decision? An understandable explanation for those affected by the decision.
Formulation
$$x’ = \arg\min_{x’} \; \lambda\,\big(f(x’) – y’\big)^2 + d(x, x’)$$
We search for \(x’\) close to \(x\) such that the prediction becomes the desired \(y’\).
Properties of a good explanation
| Property | Meaning |
|---|---|
| Validity | Actually changes the decision |
| Proximity | A small change |
| Sparsity | Few features |
| Realism | Within the data distribution |
| Actionability | Does not touch fixed or protected features |
| Diversity | Several options instead of one |
An example with DiCE
# pip install dice-ml
import dice_ml
d = dice_ml.Data(dataframe=train_df, continuous_features=["income", "debt"], outcome_name="approved")
m = dice_ml.Model(model=clf, backend="sklearn")
exp = dice_ml.Dice(d, m, method="random")
cf = exp.generate_counterfactuals(query_df.iloc[[0]], total_CFs=3, desired_class="opposite",
features_to_vary=["income", "debt"])
cf.visualize_as_dataframe()(The variables train_df andclf andquery_df are an illustrative example for loan data you have.)
Limits
- Not causal: changing a feature in the model may not change reality in the same way, as a higher income may come with changes in other features.
- Temporal validity: if the model is retrained, the explanation may no longer be valid.
- Privacy and security: many explanations may reveal the model's boundaries and make it easier to game.
Lesson terms
- Counterfactual explanation
- An example close to the original input that gets a different decision, showing the minimal change required.
- Algorithmic recourse
- A set of realistic actions a person can take to change an automated system's decision in their favour.
Abstract
A counterfactual explanation answers the question: if your income were higher by such an amount, your application would have been accepted. It is formulated as an optimization problem that searches for an example close to the original that changes the prediction to the desired class. A good explanation is close, sparse (changes few features), realistic (within the data distribution), and actionable (does not ask for a change in age or ethnicity). There may be several counterfactual explanations, and they may not stay valid if the model changes.
Review questions
-
Why is the explanation "if you were ten years younger" not useful?
Because it is not actionable, and it may reveal a problematic reliance on a sensitive feature.
-
What is meant by sparsity in a counterfactual explanation?
Changing as few features as possible, so that it is easy to understand and act on.
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate