TimeSHAP: Explaining Recurrent Models through Sequence Perturbations

João Bento, Pedro Saleiro, André F. Cruz, Mário A. T. Figueiredo, Pedro Bizarro · DOI

TL;DR

TimeSHAP extends the popular KernelSHAP explainability framework to the sequential domain, providing model-agnostic, post-hoc explanations for Recurrent Neural Networks (RNNs). By introducing a novel temporal pruning algorithm, it successfully overcomes the exponential computational complexity of sequence perturbations while significantly reducing explanation variance.

What Problem Does This Paper Address?

While recurrent neural networks (like LSTMs and GRUs) excel at processing sequential data, explaining their predictions remains a notorious challenge. Standard post-hoc explainers like KernelSHAP are designed for static, single-instance inputs. When applied blindly to RNNs, they only calculate feature importance for the current timestep, completely ignoring the historical context encoded in the model's hidden state.

Model-specific alternatives, such as attention mechanisms or gradient-based methods, either lack flexibility, fail to provide event-level granularity, or suffer from issues like vanishing gradients. To bridge this gap, the authors introduce TimeSHAP, a model-agnostic explainer capable of computing feature-, timestep-, and cell-level attributions across an entire sequence.

Figure 1: Current SHAP-based methods (left) only calculate attributions for a single input vector. TimeSHAP (right) ap- plies perturbations throughout the input sequence.

Methods

TimeSHAP adapts the cooperative game theory framework of Shapley values to sequences by defining perturbations along two main axes of an input matrix $X \in \mathbb{R}^{d \times l}$ (where $l$ is sequence length and $d$ is the number of features):

  1. Feature-wise Perturbations: Toggling specific feature rows across all events in the sequence by replacing them with uninformative background values (e.g., dataset averages).
  2. Event-wise Perturbations: Toggling entire columns (events) off to evaluate the impact of specific timesteps.

Temporal Coalition Pruning

Because sequence lengths can be arbitrarily long, evaluating all possible event coalitions scales exponentially ($O(2^l)$). TimeSHAP solves this with a Temporal Coalition Pruning algorithm. Recognizing that recurrent models rarely retain significant information from the distant past, the algorithm scans backward from the most recent event to find a threshold where older history becomes negligible. It then lumps these historical events into a single aggregate "past" coalition, drastically slashing the computational footprint to $O(l \cdot 2^{l-i})$.

Cell-Level Explanations

To achieve granular cell-level (feature-event intersection) insights without an intractable $O(2^{ld})$ grid search, TimeSHAP employs a pruning-and-intersection strategy. It isolates the most relevant rows and columns that exceed a user-defined importance threshold $\theta$, focusing computation purely on their intersections.

Figure 2: Cell-level grouping strategy, illustrated on a hy- pothetical input matrix with 60 events and 10 features. The pruning algorithm grouped the first 53 events together. Events −4 and 0, and features 3, 6, and 9 were considered rel- evant (Shapley value greater than 𝜃).

Datasets

The method was validated using a real-world production dataset from Feedzai for bank account takeover fraud detection.

  • Size: Approximately 20 million tabular instances.
  • Composition: Highly unbalanced sequential data consisting of three event types: Logins (~70%), Transactions (~20%), and Enrollments (~10%).
  • Model: A GRU-based architecture paired with an embedding layer and a feed-forward classifier.

Key Insights and Ideas

  • The Present Isn't Everything: On average, the most recent event (the transaction being scored) only accounts for 41% of the model's final score. The remaining 59% of the decision weight is carried by the historical context in the hidden state, proving that the RNN actively utilizes its recurrent memory.
  • Uncovering Hidden Bias: Global feature explanations highlighted that the model placed a surprisingly high positive attribution on the client's age. A subsequent bias audit confirmed that this led to higher false positive rates for older clients, demonstrating TimeSHAP's utility in auditing models for fairness.
  • Validating Domain Knowledge: Local event-level explanations successfully identified the classic "enrollment-login-transaction" fraud pattern (e.g., a password change followed by an immediate login and transaction from a suspicious cloud-hosting IP), matching the heuristics used by human fraud analysts.

Key Results

The temporal pruning algorithm proved essential for making sequence explainability practical:

  • Exact Calculations: For original unpruned sequences, exact Shapley values could only be computed for 10% of the data. Applying a pruning tolerance of $\eta = 0.025$ allowed 58.3% of the sequences to be computed exactly.
  • Variance Reduction: Pruning dramatically stabilized the explanations. The Relative Standard Deviation (RSD) of the Shapley values across multiple runs plummeted from a highly unstable 1.71 on unpruned sequences down to 0.98 with optimal pruning.
  • Sequence Compression: The average sequence length requiring active explanation dropped from 182.1 events to just 32.9 events.
Figure 3: Event Shapley values density over the sequence in- dex. The event 0 corresponds to the current input event. As the model only scores transactions, the preceding event is a login, having lower importance.

Limitations and Future Work

While TimeSHAP provides a powerful framework, its performance relies heavily on the choice of the background matrix used to represent "missing" data. Using dataset averages can sometimes create unrealistic synthetic sequences during perturbation. Additionally, the cell-level grouping strategy requires manual tuning of the threshold parameter $\theta$ to balance explanation granularity against computational limits. Future work could explore more sophisticated, context-aware background generation methods to improve perturbation realism.

Summary updated

BibTeX
@misc{bento2020timeshap,
  author = {João Bento and Pedro Saleiro and André F. Cruz and Mário A. T. Figueiredo and Pedro Bizarro},
  title = {{TimeSHAP: Explaining Recurrent Models through Sequence Perturbations}},
  year = {2020},
  eprint = {2012.00073},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2012.00073},
}
Figure