SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Robust Gain Continuing with the example of a person who has a loan accepted to buy a house, the semifactual should sometimes be robust to distribution shifts. For example, if the person uses the semifactual explanation to triple their loan amount (recall Figure 1), they will likely need upwards of six months to locate a new house during which the semifactual should hold if the person e.g. gets an additional credit card. Hence, we define our semifactual robustness such that while taking action  $a$ , any close neighbor of the generated semifactual  $\mathbb{S}\mathbb{F}(\mathbf{x},a;\mathcal{M})$  can still receive a positive outcome. The  $\epsilon$ -neighborhood of  $\mathbf{x}$  centering around an individual  $\mathbf{x}$  is
$$
\mathcal {B} (\mathbf {x}) = \{\boldsymbol {\theta} = \mathbb {S F} (\mathbf {x}, a; \mathcal {M}): \forall a \in \mathcal {A}, \delta (\boldsymbol {\theta}, \mathbf {x}) \leq \epsilon \} \tag {7}
$$
which covers all neighbors that can be reached from  $\mathbf{x}$  by taking an actionable feature change  $a$  through the SCM  $\mathcal{M}$ . By definition,  $\mathbf{x}$  is also a neighbor of itself since  $\mathbf{x} \in \mathcal{B}(\mathbf{x})$  holds given  $\delta(\mathbf{x},\mathbf{x}) = 0$ . Let us represent  $\mathcal{B}(\mathbb{S}\mathbb{F}(\mathbf{x},a;\mathcal{M}))$  by  $\mathcal{B}_s(\mathbf{x},a)$  for simplicity. Given the predictive model  $h(\cdot)$  and an individual  $\mathbf{x}$ , an action  $a$  is robust for individual  $\mathbf{x}$  if  $h(\pmb{\theta}) > \psi$ ,  $\forall \pmb{\theta} \in \mathcal{B}_s(\mathbf{x},a)$ , which is equivalent to  $\min_{\pmb{\theta} \in \mathcal{B}_s(\mathbf{x},a)} h(\pmb{\theta}) - \psi > 0$ . For instance,  $\psi = 0.5$  works for a binary model case. We hence denote the term related to the robustness by
$$
H (\mathbf {x}, a) = \min  _ {\boldsymbol {\theta} \in \mathcal {B} _ {s} (\mathbf {x}, a)} h (\boldsymbol {\theta}) - \psi , \tag {8}
$$
which will be useful for constructing the final objective.
Diverse Gain It is generally preferred to offer a number of suggested actions  $\{a_1,\ldots ,a_m\}$ , rather than a single one [60]. Like prior work in counterfactuals, we define diversity as the average pair-wise distance among a set of entities [39, 61]. We reuse the distance function  $\delta$  and define the diversity objective within a set of SFs  $\{\pmb {\theta}_i\}_{i = 1}^m\subseteq \mathcal{X}^m$  as
$$
R \left(\left\{\boldsymbol {\theta} _ {i} \right\} _ {i = 1} ^ {m}\right) = \left\{ \begin{array}{l l} \frac {2}{m (m - 1)} \sum_ {i = 1} ^ {m} \sum_ {j > i} ^ {m} L _ {2} \circ \delta \left(\boldsymbol {\theta} _ {i}, \boldsymbol {\theta} _ {j}\right) & m > 1 \\ 0 & m = 1 \end{array} \right. \tag {9}
$$
which represents a pairwise mean distance among the set of data points, based on the  $L_{2}$  norm. One may accommodate  $m = 1$  for the case when only a single semifactual is desired.
3.4.1 Semifactual Objective
The final objective may be constructed as follows.
Definition 3.2 (Semifactual Objective). We consider a simple composition multiplication function for  $f(\cdot)$ . Considering gain, plausibility, robustness, and diversity, the semifactual objective function is:
$$
\max  \frac {1}{m} \sum_ {i = 1} ^ {m} P (\mathbf {x}, a _ {i}) G (\mathbf {x}, a _ {i}) + \gamma R \left(\left\{\mathbb {S F} (\mathbf {x}, a _ {i}; \mathcal {M}) \right\} _ {i = 1} ^ {m}\right) \tag {10}
$$
$$
\begin{array}{l} \text {s . t .} \quad \forall i = 1, \dots , m: a _ {i} \in \mathcal {A}, H (\mathbf {x}, a _ {i}) > \psi . \end{array}
$$
In optimisation [10], an adversarial interpretation from the perspective of a two-player zero sum game can further simplify Equation (10) to
$$
\mathcal {J} := \min  _ {\lambda_ {1}, \dots , \lambda_ {m} \geq 0} \max  _ {a _ {1}, \dots , a _ {m} \in \mathcal {A}} \frac {1}{m} \sum_ {i = 1} ^ {m} P (\mathbf {x}, a _ {i}) G (\mathbf {x}, a _ {i}) + \lambda_ {i} H (\mathbf {x}, a _ {i}) + \gamma R \left(\left\{\mathbb {S F} (\mathbf {x}, a _ {i}; \mathcal {M}) \right\} _ {i = 1} ^ {m}\right), \tag {11}
$$
where  $H(\mathbf{x}, a)$  is the Lagrangian. The primal player tries to maximise the plausibility-weighted gain and diversity, with regard to  $a$ , whilst the dual player tries to minimise regarding a set of  $\lambda$ .
Since there are  $m$  suggestions, the constraints for robustness will be  $m$  times. Observing the objective, the robustness is a hard constraint, whilst the diversity can be regarded as regularisation.  $P$  can be seen as a scaling factor for  $G$  which helps to guarantee that high expected gain is only possible alongside high plausibility, simply adding them misses this special property.
3.5 Properties of the Framework
Effective Solution Space. We discuss the set of meaningful solutions here and the result validates the re-formulation [i.e., Equation (11)] of the semifactual framework [i.e., Equation (10)]. First, we depict the lemma.
Lemma 3.3. Assume that the limit of the gain function and diversity term are finite. Also, assume that  $\mathcal{A}^{+} := \{a \in \mathcal{A} : G(\mathbf{x}, a) \geq 0\}$  is non-empty for an individual  $\mathbf{x}$ . The semifactual objective  $\mathcal{J} \geq 0$  when  $\forall i = 1, \ldots, m, a_{i} \in \mathcal{A}^{+} : H(\mathbf{x}, a_{i}) \geq 0$ , otherwise  $\mathcal{J} = -\infty$ .
See Section A.1 for the proof. We can summarise that the action set which is able to provide the positive payoff can be defined by the named effective solution space for  $\mathbf{x}$ :  $\mathcal{A} = \{a\in \mathcal{A}:H(\mathbf{x},a)\geq 0,G(\mathbf{x},a) > 0\}$ . Hence, repeated suggestions will be produced when the number of actions in this space is smaller than the required  $m$ . Otherwise, the solution will provide more versatile options. There are no suggestions to achieve an effective semifactual(s) (i.e., with positive gain) if this solution space is empty. However, similar situations exist for counterfactuals when they are also impossible to generate, assuming a similar set of actionable constraints are defined.
4 Implementation Details
We now introduce our methods to solve Equation (11), henceforth called Semifactual-recourse GENeration (S-GEN), for both causal and non-causal domains. In the following paragraphs, we use  $\hat{G}$  to denote an empirical approximation of  $G$ , and likewise for  $P, H, R$ , and  $\mathcal{I}$ .
4.1 Causal Case
Assuming the presence of a differentiable classifier  $h(\cdot)$  and SCM  $\mathcal{M}$ , (recall the latter guarantees plausibility), let  $\Omega_i(\mathbf{x}) = \{\mathrm{Pr}(\pmb{\theta}): \pmb{\theta} \in \mathcal{B}_s(\mathbf{x}, a_i)\}$  be the probability distribution over the  $\epsilon$ -neighborhood of  $\mathbb{S}\mathbb{F}(\mathbf{x}, a_i; \mathcal{M})$ . Also, let  $\mathbf{B}_i$  represent a finite subset of  $\mathcal{B}_s(\mathbf{x}, a_i)$  sampled according to  $\Omega_i(\mathbf{x})$ . Our objective is:
$$
\begin{array}{l} \max  _ {a _ {1}, \dots , a _ {m}} \min  _ {\lambda_ {1}, \dots , \lambda_ {m}} \frac {1}{m} \sum_ {i = 1} ^ {m} - \lambda_ {i} \mathcal {L} (h (\mathbb {S F} (\mathbf {x}, a _ {i}; \mathcal {M}), h (\mathbf {x})) - \frac {1}{| \mathbf {B} _ {i} |} \sum_ {\boldsymbol {\theta} _ {i} \in \mathbf {B} _ {i}} \lambda_ {i} \mathcal {L} (h (\boldsymbol {\theta} _ {i}), h (\mathbf {x})) \\ + \hat {P} (\mathbf {x}, a _ {i}) \hat {G} (\mathbf {x}, a _ {i}) + \gamma \hat {R} \left(\left\{\mathbb {S F} (\mathbf {x}, a _ {i}; \mathcal {M}) \right\} _ {i = 1} ^ {m}\right) \\ \text {s . t .} \quad \forall i = 1, \dots , m: a _ {i} \in \mathcal {A}, \lambda_ {i} > 0 \tag {12} \\ \end{array}
$$
where  $\mathcal{L}$  is the binary cross entropy loss. For robustness, we used Monte Carlo (MC) sampling with an epsilon  $\epsilon$  robust hypersphere, and if either the instance or sampling return a negative outcome with  $h(\cdot)$ , we use the prior optimisation step as the solution. For diversity,  $m$  is set to the number of actionable feature sets, and a solution is obtained for each. We utilise the causal recourse approach of Karimi et al. [26] for solving the maximin. The actionable bounds are clipped each iteration, and  $\lambda$  is iteratively decreased to put more emphasis on gain over time (see Algorithm 2).
4.2 Non-Causal Case
For the non-causal case, we use a genetic algorithm [53, 58] which only assumes a binary predictive model  $h(\cdot)$ . This approach follows the standard design for genetic algorithms, with some minor alterations specifically for semifactual generation, see Appendix D for the pseudocode. Next we present the fitness function which optimises our objective.
4.2.1 Fitness Function
For gain, the average distance between an individual  $\mathbf{x}$  and each semifactual  $\mathbb{S}\mathbb{F}(\mathbf{x},a)$  is measured as  $\hat{G} (\mathbf{x},a) = \| \mathbb{S}\mathbb{F}(\mathbf{x},a) - \mathbf{x}\| _2$ . For robustness, we relax it to two constraints:  $H_{p}$  is the probabilistic robustness for the neighbor points where the generated semifactuals for a query are randomly perturbed using MC simulation to make sure the surrounding neighborhood is robust, and  $H_{a}$  the absolute robustness for the individual  $\mathbf{x}$  (more detail in Section A.2). For the first constraint, a score of  $\hat{H}_p(\mathbf{x},a) = \frac{1}{n}\sum_{i = 1}^n\mathbb{1}\{h(\mathbf{x}) = h(\boldsymbol {\theta}_i)\}$  where  $\pmb{\theta}\sim \operatorname *{Pr}(\mathcal{B}_s(\mathbf{x},a))$ , is returned. For
the second constraint, a score of  $\hat{H}_a(\mathbf{x},a) = \mathbb{1}\{h(\mathbf{x}) = h\circ \mathbb{S}\mathbb{F}(\mathbf{x},a)\}$  is returned. Hence, the solution is rewarded for (1) the neighborhood samples, and (2) the semifactuals themselves being classified as  $h(\mathbf{x})$ . For plausibility, we take from prior work and directly use the training data [29]. Specifically, considering the training data set  $\mathcal{D}$ , we define the notion of plausibility using the distance of each semifactual generated to the nearest training data point. As the term must be maximised, we use a function which is monotonically decreasing with respect to the distance with  $P(\mathbf{x},a)\approx \hat{P}_{\mathcal{D}}(\mathbf{x},a) = \exp \{1 / (\min_{\theta \in \mathcal{D}}\| \mathbb{S}\mathbb{F}(\mathbf{x},a) - \pmb {\theta}\|_2^2 +\gamma_p)\}$  where  $\gamma_{p}$  is to account for when a perfect match to the semifactual exists in the training data (thus the division is undefined), and  $\hat{P}_{\mathcal{D}}(\mathbf{x},a)$  is an empirical approximation (based on  $\mathcal{D}$ ) of the plausibility. Lastly, for diversity [39, 61], we take the mean distance between all  $m$  generated semifactuals with  $\hat{R} (\{\mathbb{S}\mathbb{F}(\mathbf{x},a_i)\}_{i = 1}^m)$  which precisely follows Equation (9). This objective collapses to 0 when  $m = 1$ .
Certain objectives need to be weighted individually based on the problem. For example, explanations which can be acted upon immediately perhaps don't need robustness. Notably, the multiplier  $\lambda$  in Equation (11) is split to  $\lambda_{p}$  and  $\lambda_{a}$ , for  $H_{p}$  and  $H_{a}$  respectively. In this work, we treat them as hyperparameters. Also, they are used alongside  $\gamma$  to balance the objectives. Since  $\lambda$  and  $\gamma$  are selected as hyperparameters, they are removed under the min operator. Finally, the objective (fitness) function is defined as:
$$
\max  _ {a _ {1}, \dots , a _ {m} \in \mathcal {A} ^ {+}} \frac {1}{m} \sum_ {i = 1} ^ {m} \hat {P} _ {\mathcal {D}} (\mathbf {x}, a _ {i}) \hat {G} (\mathbf {x}, a _ {i}) + \lambda_ {p} \hat {H} _ {p} (\mathbf {x}, a _ {i}) + \lambda_ {s} \hat {H} _ {a} (\mathbf {x}, a _ {i}) + \gamma \hat {R} (\{\mathbb {S F} (\mathbf {x}, a _ {i}; \mathcal {M}) \} _ {i = 1} ^ {m}).
$$
We selected the hyperparameters via a grid search, see Appendix C. Crucially, we also weight the fitness function output by  $\hat{H}_p(\mathbf{x},a)$  to encourage solutions with more semifactuals (see Algorithm 1).
Here we test S-GEN in both causal and non-causal settings. We show the effectiveness of our method in optimising a user's positive outcome compared to baselines and open source our code (see Appendix E). The actionability constraints are detailed in Appendix B. Baselines were modified to be appropriately compared, most importantly, we stopped counterfactual techniques before they crossed a decision boundary (thus generating semifactuals), and modified semifactual techniques to work on tabular data, Appendix G details the peripheral modifications.
In the non-causal setting, we consider three datasets, Loan Application [33], German Credit [21], and BCSC [11]. All categorical variables are one hot encoded. Three models were used, a decision tree, logistic regression, and naive bayes, each with 30 random test data point explanation samples gotten by varying the random seed. Note that because the range varied on each dataset, the results were normalised and averaged for each, but Appendix F details each individual dataset for completeness. For baselines, we modify three techniques, DiCE by Mothilal et al. [39] (henceforth DiCE*), PIECE by Kenny & Keane [29] (henceforth PIECE*), and Diverse semifactual Explanations of Reject by Artelt & Hammer [1] (henceforth DSER*). Plausibility is measured as the distance between a generated semifactual(s) and the nearest training example; thus, the smaller the better. Robustness is measured by MC sampling  $n = 100$  single feature perturbations of each semifactual  $\theta_{i}$ , predicting their class, and returning a float between 0-1 of the success rate as described in Section 4.2.1.
In the causal setting, the Adult [31] and COMPAS [5] datasets are considered. The SCMs from Nabi & Shpitser [41] were used, and the structural equations from Dominguez et al. [18]. All categorical features are treated as real-valued. We use the pre-trained MLP classifiers from Dominguez et al. [18] and take 30 averaged samples from 5 random seeds. As baselines we modify the technique of Karimi et al. [26] [henceforth Karimi et al.(2021)*], and Dominguez et al. [18] [henceforth Dominguez et al.(2022)*], the latter optimises with robustness in mind. We optimise the relevant techniques to be robust in an  $\epsilon = 0.1$  hypersphere, and the C&W adversarial attack by Carlini & Wagner [12] measures robustness by checking if the nearest adversarial attack is outside this radius.
For all tests, the main metric of concern is gain, that is, the mean distance between a query and its generated semifactual(s), the larger this number, the better. Diversity is also measured for all tests as the mean distance between all  $m$  generated semifactuals for an individual  $\mathbf{x}$ , the higher the number, the better. To be in line with prior art, the  $L_{2}$  norm is used in non-causal tests [1], and the  $L_{1}$  in causal [18]. Note for causal tests the SCM guarantees plausibility so this metric is not reported.
5.1 Non-Causal Results
Our purpose here is to show that current methods are insufficient to meet the basic requirements for semifactual explanation discussed in Section 3.4. Specifically, a technique needs to optimise gain, while remaining plausible, robust, and offering diverse explanations.
Observing the average normalised results across all datasets (note robustness was not normalised since it is already 0-1 range), Figure 2 shows that S-GEN performed the best on all metrics for all values of  $m$  (1-10). The results demonstrate that traditional counterfactual approaches (DiCE*) are not suitable to achieve optimal gain, due to them focusing on minimising cost. Moreover, methods built for semifactual generation specifically (i.e., DSER* & PIECE*) that do actually maximise gain somewhat, still fail to match the results of S-GEN. This shows that S-GEN is superior to existing semifactual methods (and popular counterfactual approaches appropriately modified) for maximising a user's gain in positive outcomes. Moreover, it does so while maintaining superior plausibility, robustness, and diversity in all tests.
5.2 Causal Results
We evaluate our algorithm in a causal setting where the SCMs and structural equations are known. The primary purpose of this test is to demonstrate that the semantics of semifactual "even if" thinking is better captured in a causal setting due to dependencies being taken into account when calculating a person's gain. With regard to diversity, we fix  $m$  to the maximum number of feature sets available from the actionable features (so only one  $m$  value is tested).
Figures 3a and 3b show the initial gain achieved by a person after taking a certain action (i.e., the Action Gain), and how this gain transforms after considering the causal relationship between features (i.e., the Causal Gain). Firstly, the total gain achieved by S-GEN is much larger than the baselines in both datasets and hence consistent with our non-causal tests. More importantly however, the change in gain a person achieves after considering the causal relations in the adult dataset is significantly higher both in significance testing and effect size  $(0.055 \pm 0.001$  v.  $0.063 \pm 0.001$ ; t-test  $p < 0.02$ ; Cohen's  $d = 2.24$ ), showing it is beneficial to consider causality when calculating a person's gain. The results of diversity put S-GEN first also (S-GEN  $= 0.84 \pm 0.09$  v. Karimi  $= 0.43 \pm 0.03$  v. Domineguez  $= 0.34 \pm 0.03$ ). In robustness, both S-GEN and Domineguz et al. (2022)* did reasonably well (S-GEN  $= 87\%$  success v. Domineguz  $= 54\%$  success), but Karimi et al. (2021)* did not (7.2% success), likely due to the latter not being designed for this.
The primary motivation behind this work is the hypothesis that semifactual explanation would be preferred by users over counterfactuals in positive outcome settings. To test this assumption, we design the first user test in XAI directly comparing the two. Specifically, we show users three materials in which a person has a bank loan accepted, and three in which they don't. Users were then shown both explanation types for each material, and asked to rate on a scale from 1-5 how useful each were. So, the study was a within-subjects design, and the condition was the explanation type. Note that although we are studying the effect of the explanation type on loan acceptance, the
Figure 2: Results: The ability of S-GEN to create semifactuals is compared to DiCE*, DSER*, and PIECE*. Overall, S-GEN does the best, achieving significantly better results to all baselines in all tests. Note we normalised all results before averaging because each dataset has different scaling. Standard error bars are shown.
(a) Causal Experiment: Adult
Figure 3: Causal Experiment & User Study Results: (a/b) show the gain achieved by all methods both before and after considering the causal dependencies. Firstly, note that S-GEN achieves significantly more gain than the alternatively proposed approaches. Most importantly however, (a) shows there is significantly more gain achieved on the Adult data by S-GEN after taking causal dependencies into account, showing the importance of a causal formalisation. (c) Shows the user study results, where people perceive semifactual explanation as being significantly more useful than counterfactuals in the positive outcome of having a loan accepted. Standard error bars are shown.
(b) Causal Experiment: COMPAS
(c) User Study
loan rejection scenarios were also included to balance people's view of the problem setting, and as attention checks to verify that users were engaging with the materials and varying their scores accordingly. For analysis, each user's scores for counterfactuals and semifactuals were averaged in both loan acceptance and rejection materials into four decimal scores per user, thus allowing us to analyse the discrete Likert scores with t-tests [28]. As is a popular approach [22], we don't explicitly define what "useful" means to users, but rather let them use their own natural interpretation, as the results returned were reasonably consistent across individuals, they appear to have converged on a common interpretation of this word. The null hypothesis is that people will find both explanation types not significantly different in loan acceptance. The alternative is that people will find semifactuals significantly more useful in loan acceptance.
A power analysis [13] of two dependent means with an effect size \( dz = 0.8 \), alpha \( \alpha = 0.05 \) and power \( (1 - \beta \text{ err prob}) = 0.9 \) informed a sample of 15 was appropriate for t-tests. Users were gathered from Prolific.com, 8 males, 7 females, aged \( 18+ \), native English speakers, and from the U.S. People were paid \( \\( {12}/\mathrm{{hr}} \) ,which totalled \( \$ {35} \) . The semifactuals were generated with S-GEN,and the counterfactuals with DiCE [39], notably these are equivalent to positive counterfactuals by McGrath et al. [36] for explaining loan acceptance situations. The study obtained IRB approval from MIT.
All users engaged and changed their ratings significantly depending on whether a loan was accepted or rejected, so none were excluded. Figure 3c shows users find semifactuals significantly more useful in loan acceptance (S-GEN=3.60±0.27 v. DiCE=2.33±0.34;  $p < .005$ ) compared to rejections when counterfactuals are preferred (S-GEN=2.6±0.32 v. DiCE=4.53±0.17;  $p < .0001$ ). Hence we reject the null and lend credible evidence that semifactuals are more useful to explain positive outcomes.
Although much XAI work has explored how to explain positive outcomes, to the best of our knowledge, no consideration has been given towards explaining how to optimise them. Here, we have taken the novel step of exploring this, and showed how semifactuals are especially suited for the purpose. This required building on prior work in semifactuals by (1) introducing the concept of Gain, (2) re-framing them in a causal setting, and (3) conducting their first user test in XAI. Perhaps the notable limitation of our work is that although we have shown people do perceive semifactuals as being more useful in positive outcomes, we have not demonstrated this quantitatively, notably because of the difficulties acquiring an appropriate user base alongside the ethical considerations of such a study. Moreover, considering a casual formulation of semifactuals requires an SCM, which is not always realistic, but we have provided a non-causal algorithm for these situations. In future work, it would be interesting to formalise the utility of semifactuals for optimising positive outcomes in other domains such as robotics, which likely requires other considerations.
The authors would like to thank Neil J. Hurley, alongside Mark T. Keane and Ruth M.J. Byrne who both inspired early consideration of the ideas in this paper. The authors would also like to thank MIT for their support in this project. This research wasn't directly supported by any grants or funding. We hope readers find the ideas interesting and useful for application.