CoDy: Counterfactual Explainers for Dynamic Graphs

Zhan Qu, Daniel Gomm, Michael Färber

TL;DR

CoDy is the first counterfactual explanation method designed specifically for Temporal Graph Neural Networks (TGNNs). By using Monte Carlo Tree Search (MCTS) to identify the minimal set of past events that would flip a model's prediction, CoDy provides more intuitive, actionable, and concise insights than traditional "factual" explainers.

What Problem Does This Paper Address?

Temporal Graph Neural Networks (TGNNs) are state-of-the-art for modeling systems that evolve over time, such as social networks or e-commerce platforms. However, they are notorious "black boxes." While we have tools to explain static graphs, dynamic graphs introduce a "time" dimension that makes things much messier—events are interconnected across both space (who interacts with whom) and time (when it happened).

Existing explainers for TGNNs are mostly factual: they try to identify which nodes and edges contributed to a prediction. This often results in complex, hard-to-read subgraphs. This paper tackles the need for counterfactual explanations: instead of asking "What caused this?", it asks "What is the smallest change I could make to the past to change this future outcome?" As shown in the figure below, counterfactuals can often simplify a complex chain of events into a single, necessary action.

Figure 1. Bipartite dynamic graph of student posts on a university social network. Edge colors represent their ages.

Methods

The authors introduce CoDy (Counterfactual Explainer for Dynamic Graphs), a model-agnostic approach that treats the TGNN as a black box.

Key components include:

  • Monte Carlo Tree Search (MCTS): CoDy explores the massive search space of possible graph perturbations using MCTS. Each node in the search tree represents a specific set of omitted past events.
  • Spatio-Temporal Constraints: To keep the search efficient, CoDy focuses on events within a specific spatial neighborhood (k-hops) and temporal window ($m_{max}$ recent events) of the target prediction.
  • Heuristic Selection Policies: Instead of searching blindly, CoDy uses four policies to prioritize which events to "remove" during the search:
  • Random: A baseline for comparison.
  • Temporal: Prioritizes removing the most recent events.
  • Spatio-Temporal: Combines spatial proximity and temporal recency.
  • Event-Impact: Measures the direct change in the model's prediction logit when a single event is removed.
  • GreeDy Baseline: Since no counterfactual explainer for TGNNs existed, the authors also developed a simpler greedy search baseline called GreeDy.
Figure 2. CoDy search framework. The left side shows an example of a temporal input graph, where the explained future link is highlighted in blue, and the other links are color-coded based on their ages. On the right, one iteration of the CoDy algorithm is illustrated. Each rectangle represents a node in the search tree, corresponding to a specific perturbation of the input graph.

Datasets

The researchers evaluated CoDy on three diverse dynamic graph datasets:

  • Wikipedia: A bipartite graph of users editing articles, featuring rich edge attributes.
  • UCI-Messages: A unipartite social network of messages sent between students.
  • UCI-Forums: A bipartite graph of students interacting on university forums.

These datasets were chosen for their varying structures and temporal densities to test the generalizability of the explainer.

Key Insights and Ideas

  • Necessity vs. Sufficiency: Factual explanations aim for sufficiency (what is enough to produce the result), while counterfactuals aim for necessity (what is required). The authors argue that necessity is often more intuitive for human users trying to understand decision boundaries.
  • The Power of Spatio-Temporal Context: The "Spatio-Temporal" selection policy consistently performed best for correct predictions. This confirms that for well-functioning models, events that are both physically close and temporally recent are the most influential.
  • Explaining Errors: Interestingly, the "Event-Impact" policy was more effective at explaining incorrect predictions. This suggests that when a model makes a mistake, the influential features might not follow standard spatio-temporal patterns, and directly measuring the model's sensitivity is more revealing.
  • Conciseness: Counterfactual methods naturally lead to "sparser" explanations—they find the absolute minimum number of changes needed, making them easier for humans to digest.

Key Results

CoDy significantly outperformed both factual baselines (like T-GNNExplainer) and the GreeDy counterfactual baseline across all datasets.

  • Performance Boost: CoDy achieved a 16% improvement in AUFSC+ (Area Under the Fidelity-Sparsity Curve for necessity) over the strongest baseline.
  • Fidelity at Low Sparsity: As shown in the figure below, CoDy variants (solid lines) reach high fidelity much faster than the factual T-GNNExplainer (dash-dotted line), meaning they find better explanations while removing fewer events.
  • Robustness: CoDy proved to be more robust than GreeDy; while GreeDy is faster, it often gets stuck in local optima, whereas CoDy’s MCTS approach allows it to explore the search space more effectively.
Figure 3. Cumulative fid+ score relative to an upper sparsity limit for incorrect predictions with TGN as target model. PGExplainer is excluded since it is assessed with a fixed sparsity.

Limitations and Future Work

  • Removal Only: Currently, CoDy only generates explanations by removing events. Future work could involve adding events or modifying existing features and timestamps, which would further broaden the "what-if" scenarios.
  • Computational Cost: MCTS is more computationally intensive than greedy search. While the authors suggest a "two-stage" approximation to speed things up, real-time explanation for massive graphs remains a challenge.
  • Human-in-the-Loop: The authors highlight the need for user studies to confirm that these counterfactuals are indeed more helpful for real-world practitioners in fields like finance or healthcare.

Summary updated

BibTeX
@inproceedings{qu2025cody,
  author = {Zhan Qu and Daniel Gomm and Michael Färber},
  title = {{CoDy: Counterfactual Explainers for Dynamic Graphs}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=FE9QN8d536},
}
Figure