# Real-Time, Model-Agnostic and User-Driven Counterfactual Explanations Using Autoencoders
## 5 Results and Discussion
### Gearbox
\begin{split}\mathbf{RTGCEx\;wo\;\mathcal{L}^{\mathrm{AE}}}:& \mathcal{L}=\alpha\cdot\mathcal{L}^{\mathrm{G}}(\mathbf{x},\mathbf{x}_{cf})+ \beta\cdot\mathcal{L}^{\mathrm{D}}(\mathbf{y}_{cf},\mathbf{y}^{\prime}_{cf}) \\ \mathbf{RTGCEx\;wo\;\mathcal{L}^{\mathrm{G}}}:& \mathcal{L}=\beta\cdot\mathcal{L}^{\mathrm{f}}(\mathbf{y}_{cf},\mathbf{y}^{ \prime}_{cf})+\gamma\cdot\mathcal{L}^{\mathrm{AE}}(\mathbf{x}_{cf},\mathbf{x}^ {\prime}_{cf})\\ \mathbf{RTGCEx\;\;\colon}&\mathcal{L}=\alpha\cdot \mathcal{L}^{\mathrm{G}}(\mathbf{x},\mathbf{x}_{cf})+\beta\cdot\mathcal{L}^{ \mathrm{f}}(\mathbf{y}_{cf},\mathbf{y}^{\prime}_{cf})+\gamma\cdot\mathcal{L}^ {\mathrm{AE}}(\mathbf{x}_{cf},\mathbf{x}^{\prime}_{cf})\end{split}  \tag{18}\]  
This table presents the results of an evaluation of each term's impact in the RTGCEx algorithm's loss function on counterfactual generation. The results demonstrate that the term which ensures data similarity (represented by \(\mathcal{L}^{\mathrm{G}}\)) is crucial for generating counterfactual explanations that are highly similar to the input data and involve minimal changes. Removal of this term from the loss function leads to counterfactuals that are dissimilar to the input data and involve significant changes. The numerical analysis clearly shows  
Figure 13: A normal sample and the counterfactual explanation for introducing anomalies. \begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Method** & **Closeness Loss** & **Counterfactual Loss** & **Data Manifold Loss** & \
\begin{tabular}{c} **Speed (s)** \\ **One Sample** \\ \end{tabular} &
\begin{tabular}{c} **Test Data** \\ **Test Data** \\ \end{tabular} \\ \hline
**RTGCEx\(wo\;\mathcal{L}^{\mathrm{AE}}\)** & \(3.23\times 10^{-4}\) & \(4.91\times 10^{-7}\) & \(1.60\times 10^{-4}\) & \\
**RTGCEx\(wo\;\mathcal{L}^{\mathrm{G}}\)** & \(0.056\) & \(4.18\times 10^{-7}\) & \(2.32\times 10^{-4}\) & \(0.062\pm 0.091\) & \(5.46\) \\
**RTGCEx** & \(3.59\times 10^{-4}\) & \(5.83\times 10^{-7}\) & \(1.29\times 10^{-4}\) & \\ \hline \hline \end{tabular}
\end{table} 
Table 5: Mean values obtained for each loss function in test data using the three variations of the losses optimized in RTGCEx. this, as the closeness loss increases from \(3.59\times 10^{-4}\) to 0.056, and the data manifold loss increases from \(1.29\times 10^{-4}\) to \(2.32\times 10^{-4}\) upon removal of this term. Inclusion of this term in the loss function results in small values in data closeness loss, specifically \(3.23\times 10^{-4}\) for RTGCEx without \(\mathcal{L}^{\text{AE}}\) and \(3.59\times 10^{-4}\) for RTGCEx, indicating that the generated counterfactuals involve minor modifications to the input data. Furthermore, the value of the data manifold loss is low for RTGCEx and also when \(\mathcal{L}^{\text{AE}}\) is removed, indicating that the changes made are logical in both cases. The counterfactual loss is not significantly different across all methods, as the class of the counterfactual instances matches the intended class in all cases. The amortized inference requirement is particularly important in anomaly detection [24], as a quick response is often required when an anomaly occurs. Therefore, the table also shows the average time it takes for the model to generate explanations for a single instance and for the entire test dataset (2020 instances). All methods take the same amount of time, as the generator used to generate the explanations has the same architecture in all three cases. For a single explanation, the generator takes on average 0.062 s with a variation of 0.091 s, while generating explanations for the whole test set takes 5.46 s. This demonstrates that the proposed method provides counterfactual explanations in real time, satisfying the amortized inference property.