# CETD: Counterfactual Explanations by Considering Temporal Dependencies in Sequential Recommendation
## 3 Propose Model
### The CETD Model  
#### 3.3.1 Perturbation Model  
Our approach involves a perturbation-based method to generate counterfactual histories by substituting items in the original user history \(\mathcal{H}^{u}\). The two mainstream generative models VAE and generative adversarial network (GAN) have their own strengths and weaknesses, with VAE relying on hypothetical conditions and GAN being less interpretable. Given that the user's histories exhibit a non-random pattern, we posit the existence of a ground truth user history distribution and utilize VAE to acquire knowledge of this distribution. Given the chronological order of a user's historical records, the perturbation model's ability to consider temporal and sequential user-item interactions is critical in generating meaningful counterfactual histories. Within the specified timestamp, we posit that the selection of a particular item is influenced by an underlying latent factor that captures user trends and preferences. Indeed, the latent factor is subject to influence from the user history and can be modeled to encompass both long-term preferences and short-term behavior. The basic framework VAE can be utilized to effectively model time-aware user preferences. In this scenario, we presume the presence of timing information denoted by \(T\in\mathbb{R}_{+}\) and incorporate a temporal mark in the elements of \(H^{u}\). The term \(H^{u}_{(t)}\in\mathcal{V}\) (with \(1\leq t\leq T\)) corresponds to the item in \(\mathcal{H}^{u}\) at the t-th position, whereas \(H^{u}_{(1:t)}\) represents the sequence \(H^{u}_{(1)},\ldots,H^{u}_{(t)}\). Ideally, latent variable modeling when used to generate counterfactual histories must be able to express temporal dynamics. In a probabilistic framework, we incorporate temporal dependencies by conditioning each event on the preceding events. \(H^{u}_{(1:T)}\) can be formulated as:  
\[P(H^{u}_{(1:T)})=\prod_{t=0}^{T-1}P(H^{u}_{(t+1)}|H^{u}_{(1:t)}). \tag{1}\]  
Note that \(H^{u}_{(1:0)}\) is an initialization when \(H^{u}\) is the first item in \(\mathcal{H}^{u}\) as \(H^{u}_{(1)}\). This specification highlights two essential aspects: (i) The existence of a recurrent relationship between \(H^{u}_{(t+1)}\) and \(H^{u}_{(1:t)}\), designed by \(P(H^{u}_{(t+1)}|H^{u}_{(1:t)})\), which allows for advantageous modeling; and (ii) the capability to treat each time-step independently, specifically by employing a conditional VAE for modeling. The proposed distribution incorporates a dependency on the latent variable through a recurrent layer, enabling the retrieval of information from the previous history. The GRU model represents an advancement over the Long short-term memory (LSTM) model, as it includes storage units to retain long-term historical data. In the context of time series prediction, GRU exhibits a prediction accuracy that is at least comparable to that of the LSTM model while offering a higher computational efficiency. In this paper, we used GRU to learn the recurrent relationship between \(H^{u}_{(t+1)}\) and \(H^{u}_{(1:t)}\). As shown in Figure 2, in detail, we pass the users' real history \(\mathcal{H}^{u}\) into the embedding layer to obtain the corresponding embedding \(E^{u}\). Then, we pass \(E^{u}\) through a GRU layer, which can learn the temporal dependencies of the previous history and thus further obtain the output of enhanced long-term preference memory \(G\). Counterfactual history \(\widehat{\mathcal{H}^{u}}\) can be precisely derived for any real history \(\mathcal{H}^{u}\) using the VAE, which consists of an encoder \((\mu,\sigma)\) = Encoder(\(\cdot\)) and a decoder \(\widetilde{E}\) = Decoder(\(\cdot\)). Furthermore, the VAE extracts the encoded item sequences' mean and variance from the latent space first, and then samples the latent embedding \(Z\) based on the above variational information. The obtained embedding is then passed to the decoder, which generates the perturbed sequence \(\widetilde{E}^{u}\).