Counterfactual Maps: What They Are and How to Find Them

Awa Khouna, Julien Ferry, Thibaut VidalNeurIPS 2026

TL;DR

Finding the exact minimal changes needed to flip a complex tree ensemble's decision—like turning a loan rejection into an approval—usually forces an ugly choice between slow math or unreliable guesswork. This paper sidesteps that trade-off by realizing the entire feature space is just a patchwork of boxes, turning counterfactual generation into a geometric map-lookup problem. After a one-time preprocessing step, this method delivers mathematically optimal explanations in milliseconds.

What Problem Does This Paper Address?

Imagine an algorithm denies your credit application. A counterfactual explanation tells you the smallest change needed to flip that outcome (e.g., "increase income by $2,000"). For tabular data, random forests and gradient-boosted trees are king, yet generating reliable counterfactuals for them is surprisingly painful.

Existing methods treat every single user query as a brand-new optimization problem from scratch. Heuristic approaches try to tweak features or trace tree paths, which is fast, but they often fail to find valid answers or demand much larger changes than necessary. Exact methods use heavy-duty Mixed-Integer Programming (MIP) to guarantee the best answer, but solving a fresh mathematical program for every query is far too slow for real-time, interactive applications. Nobody has been able to offer both mathematical optimality and instant latency at the same time.

Methods

The authors reframe counterfactual generation from a per-query optimization chore into a two-stage "preprocess-and-query" spatial search, calling the resulting global structure a Counterfactual Map (CF-Maps).

Two-stage workflow for counterfactual generation
  1. One-Time Build Pipeline: Geometrically, a tree ensemble carves the input space into a massive grid of disjoint, axis-aligned hyperrectangles (boxes), where every point inside a single box gets the exact same prediction. Using an existing compression framework called Born-Again Trees (BA-Trees), the authors extract a compact, faithful set of these labeled boxes. They then group the boxes by prediction class and index them into volumetric KD-trees—hierarchical spatial data structures typically used in computer graphics and robotics.
  2. Dynamic Query Pipeline: Finding an optimal counterfactual for a point $x$ now just means finding the closest box belonging to the desired target class. At query time, the algorithm runs a branch-and-bound search down the target class's KD-tree, pruning massive chunks of the feature space that areprovably too far away. Once the nearest target box is found, projecting $x$ onto its boundary is a trivial closed-form calculation.
  3. Budgeted Variant (CF-Maps(B)): For massive ensembles where extracting every box takes too long, the authors cap extraction at $N$ boxes (e.g., $10^6$). Unresolved regions go into a separate KD-tree. By comparing the distance to the closest known target box against the closest unresolved box, the system still returns valid counterfactuals alongside an exact mathematical certificate of how close to optimal the answer is.

Datasets

The authors evaluate their method on a synthetic 2D "blobs" dataset (for visualization) and five standard tabular benchmarks—no new datasets are introduced:

  • High-stakes binary classification: Recidivism prediction (CP), credit risk assessment (FI), and two medical prognosis/prevalence tasks (BC and PD).
  • Multiclass classification: The classical Seeds dataset (SE).

Key Insights and Ideas

  • Recourse is just a Voronoi diagram: If you color every point in the feature space according to which target-class box is closest to it, you get a generalized Voronoi partition. Under $L_1$ or $L_\infty$ distance norms, these regions have crisp, polyhedral boundaries; under $L_2$ (Euclidean), the boundaries curve quadratically. Because building curved high-dimensional Voronoi diagrams explicitly is a nightmare, querying them implicitly via KD-trees is the magic trick that makes this practical.
Figure 1: Counterfactual map for target class y2 . The map corresponds to the generalized Voronoi partition induced by the hyperrectangles of this class. For any input query, it identifies the closest hyperrectangle of this class; projecting the query onto that region yields a globally optimal counter- factual. This illustration uses a random forest with two depth-5 trees trained on the “blobs” dataset.
  • Amortizing the pain: MIP solvers work hard on every single query. CF-Maps pays a heavier computational tax upfront once per model, making subsequent queries practically free—and allowing users to tweak feature weights (like making "age" unchangeable) at query time without rebuilding the index.
  • Heuristics can be quietly unfair: Because heuristics overshoot minimal recourse costs—or outright fail to find solutions that exist—relying on them means some unlucky users are told to work much harder than necessary to change their outcomes.

Key Results

Tested on random forests (typically 100 depth-5 trees across 1,000 queries), CF-Maps shines on both speed and accuracy:

  • Interactive Latency: Once preprocessed, average query times sit in the millisecond to tens-of-milliseconds range across $L_1$, $L_2$, and $L_\infty$ norms.
  • Orders of Magnitude Faster than Exact Baselines: Compared to OCEAN (the state-of-the-art MIP method), generating 1,000 counterfactuals on the PD dataset drops from 42,983 seconds (~12 hours) to 32.1 seconds (plus 467s of one-time preprocessing). On CP, 1,000 queries take just 1.4 seconds versus OCEAN's 619 seconds.
  • Heuristics Fall Short: Heuristic baselines (FT, LIRE, DiCE, EECE) demanded 1.1x to 4.8x more feature change than necessary. Worse, Feature Tweaking failed to find any valid counterfactual up to 93.2% of the time, and DiCE failed up to 16.6% of the time. CF-Maps guarantees 0% failure and 1.0x optimal distance.
  • Budgeting Scales: On a massive 1,000-tree, depth-10 forest (BC dataset), CF-Maps(B) slashed preprocessing time by 97.3% (from 6,633s down to 180s), kept query latency under 24ms, and still certified 100% of the tested queries as globally optimal.

Limitations and Future Work

The main bottleneck is offline preprocessing: as tree ensembles grow deeper and larger, the number of hyperrectangles explodes exponentially. While CF-Maps(B) tames this dramatically, partition extraction remains the primary scaling limit.

For future work, the authors suggest tightening bounds on unresolved regions, designing hybrid "gating" systems that route easy queries to CF-Maps and rare, difficult ones to cold-start MIP solvers, and using the geometric maps to offer users multiple diverse, near-optimal options (Rashomon sets). Finally, while tailored to trees here, the framework naturally extends to any model with axis-aligned, piecewise-constant decisions, such as rule lists.

Summary updated

Abstract

Counterfactual explanations are a central tool in interpretable machine learning, yet computing them exactly for complex models remains challenging. For tree ensembles, predictions are piecewise constant over a large collection of axis-aligned hyperrectangles, implying that an optimal counterfactual for a point corresponds to its projection onto the nearest rectangle with an alternative label under a chosen metric. Existing methods largely overlook this geometric structure, relying either on heuristics with no optimality guarantees or on mixed-integer programming formulations that do not scale to interactive use. In this work, we revisit counterfactual generation through the lens of nearest-region search and introduce *counterfactual maps*, a global representation of recourse. Leveraging the fact that any tree ensemble can be compressed into an equivalent partition of labeled hyperrectangles, we cast counterfactual search as the problem of identifying the *generalized Voronoi cell* associated with the nearest rectangle of an alternative label. This leads to an exact, amortized algorithm based on volumetric k-dimensional (KD) trees, which performs branch-and-bound nearest-region queries with explicit optimality certificates and sublinear average query time after a one-time preprocessing phase. Our experimental analyses across several real datasets from high-stakes application domains show that this approach delivers globally optimal counterfactual explanations with millisecond-level latency, achieving query times that are orders of magnitude faster than existing exact, cold-start optimization methods.
BibTeX
@inproceedings{khouna2026counterfactual,
  author = {Awa Khouna and Julien Ferry and Thibaut Vidal},
  title = {{Counterfactual Maps: What They Are and How to Find Them}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=Oc0x3mRg64},
}
Figure