Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP011 · Algorithm
DiCE: diverse counterfactual explanations
Find several alternative inputs that change a fixed model’s prediction while balancing proximity, diversity and declared feasibility constraints.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.
- Counterfactuals for a saved model (DiCE) Experimental
Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.
- Availability
- Runs in CodeSplash (Experimental)
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through explain.counterfactual. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Find several alternative inputs that change a fixed model’s prediction while balancing proximity, diversity and declared feasibility constraints.
Question / hypotheses
Which feasible input changes produce the desired model output, and how different are the proposed alternatives? This is model counterfactual search, not automatically real-world causal recourse.
Architecture / computational structure
A fixed prediction function is coupled to an input-space optimizer or search procedure. The objective combines target validity, distance from the observed case, and diversity among candidates; constraints can restrict ranges, immutable features and permitted directions of change.
Assumptions
The feature representation and action constraints must be meaningful. Validity under a predictor does not imply that a proposed change is attainable or changes the real outcome. Correlated features and categorical encodings need explicit handling.
Formula / computational definition
Parameters and notation
Optimized variables are candidate inputs, not predictor weights. Choices include target class/threshold, number k, proximity metric/scales, diversity penalty, optimization method, stopping, immutable features and feasibility constraints. The displayed diversity form follows a determinant-based construction; variants must be named. Here x is the original input, c_i a candidate alternative, y-star the desired model outcome, k the candidate count, ell_target a target loss, d a chosen distance, K the candidate-similarity matrix, and lambda_1 and lambda_2 trade off proximity and diversity.
Fitting, fine-tuning, and adaptation
Regenerate counterfactuals after model adaptation or policy/constraint changes. Evaluate whether recourse remains valid across plausible model versions. If using a learned data manifold or causal model, train and validate that additional model separately.
How to use and calibrate
Report target validity, proximity, diversity, constraint violations and robustness. Audit examples with domain knowledge. Compare unconstrained with constrained search to show how apparent cheap changes can be disallowed or implausible.
Example application
Search a controlled two-feature classifier while holding one feature immutable, then inspect a diverse set of valid alternatives rather than suggesting a single unconstrained coordinate change.
Interpretation / pitfalls
Watch out
A mathematical counterfactual may be off-manifold or legally/physically impossible. Model gaming and causal interventions are different. Diversity can conflict with proximity and actionability. No generated suggestion should be treated as a guaranteed real outcome.
Python and R code examples
Scope: Evaluate validity, immutable-feature compliance and a diversity determinant for supplied candidates; does not implement DiCE optimization. Python: NumPy; R: base R.
Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Evaluate validity, immutable-feature compliance and a diversity determinant for supplied candidates; does not implement DiCE optimization. Python: NumPy; R: base R. · needs numpy
import numpy as np
x=np.array([0.,.2]);C=np.array([[1.,.2],[1.5,.2],[2.,.2]])
valid=C[:,0]+C[:,1]>=1;D=np.abs(C[:,None,:]-C[None,:,:]).sum(-1);K=1/(1+D)
assert valid.all() and np.all(C[:,1]==x[1]);print(valid,np.linalg.det(K))
Component demo, not a trained model — Evaluate validity, immutable-feature compliance and a diversity determinant for supplied candidates; does not implement DiCE optimization. Python: NumPy; R: base R.
x<-c(0,.2);C<-rbind(c(1,.2),c(1.5,.2),c(2,.2))
valid<-rowSums(C)>=1;D<-as.matrix(dist(C,method="manhattan"));K<-1/(1+D)
stopifnot(all(valid),all(C[,2]==x[2]));print(valid);print(det(K))
Sources
- Source 1arxiv.org
- Source 2interpret.ml
From the CodeSplash reference, entry EXP011.