# Improved Surprise Adequacy Tools for Corner Case Data Description and Detection
## 3 Corner Cases Detection Based on SA
### Distance-Based SA and Its Modification  
Considering SA can capture the behavior of testing data, therefore we may think about the possibility of using SA to describe behaviors of data in a corner case. Here, we mainly consider the distance-based SA (DSA), as it describes the relation of testing data with respect to the classification boundary. According to the DSA definition in [21], firstly assuming a DL model M consisting of a set of neurons \(N\), it is learned based on a training dataset \(Tr\) with \(C\) classes. According to the denotation of SA, the activation traces of \(Tr\) can be calculated as \(A_{N}(Tr)\) based on the trained M. Then, for a new testing input \(x\) in the class \(c_{x}\in C\), its nearest neighbor in \(c_{x}\) is denoted as \(x_{a}\). Finally, the surprise of \(x\) with respect to class \(c_{x}\) can be calculated as the distance between their activation trace \(a_{N}(*)\), as the following form  
\[x_{a}=\underset{x_{i}\in X\text{ s.t. }c_{x_{i}}=c_{x}}{\text{argmin}}\|a_{N}(x)- \alpha_{N}(x_{i})\| \tag{4}\]  
\[dist_{a}=\|a_{N}(x)-\alpha_{N}(x_{a})\| \tag{5}\]  
where, \(\|*\|\) is the Euclidean distance. Subsequently, taking \(x_{a}\) as the reference point and finding its nearest neighbor \(x_{b}\) in a class different from \(c_{x}\), then the surprise between \(x_{a}\) and \(x_{b}\) is also calculated  
\[x_{b}=\underset{x_{i}\in X\text{ s.t. }c_{x_{i}}\
eq c_{x}}{\text{argmin}}\|a_{N}(x _{a})-\alpha_{N}(x_{i})\| \tag{6}\]  
\[dist_{b}=\|a_{N}(x_{a})-\alpha_{N}(x_{b})\| \tag{7}\]  
Combining the above definitions, the SA of testing data \(x\) to the training data is consequently defined as the ratio between \(dist_{a}\) and \(dist_{b}\), as below  
\[\text{DSA}(x)=\frac{dist_{a}}{dist_{b}} \tag{8}\]  
According to this definition, it is seen if the numerator is larger and the denominator is smaller, the value of DSA will be larger, implying that the testing data \(x\) is a surprise to the data of class \(c_{x}\) in the training set, as shown in Figure 2. Therefore, we can see that DSA shows a way of describing data's surprise to training data by distance, and it is also useful to describe data's activation behaviors with respect to the given DL model. While the above definition of DSA is simple, it has some drawbacks to deal with independent data points in extreme cases. Moreover, to evaluate its capability in detecting the data of corner cases, several additional modifications are also proposed in this paper to formalize DSA for corner case detection. Figure 2: Diagram of the original DSA definition. In the original DSA definition, we see that the denominator is actually to calculate the surprise of \(x_{a}\) (the nearest neighbor of the testing data \(x\) in the same class) respect to data of other different classes. Therefore, DSA can be regarded as the comparison between \(x\)'s novelty in its belonging class and its class novelty to other classes. This definition may be useful to describe the surprise of comparing the testing data with the whole training data. However, to evaluate if a data sample belongs to corner case, its own novelty with respect to all classes seems more important. Hence, we modify the original DSA by just adjusting the definition of \(dist_{b}\) as the following form  
\[x_{b}=\underset{x_{i}\in X\text{ s.t. }c_{x_{i}}\
eq c_{x}}{\text{argmin}}\| \alpha_{N}(x)-\alpha_{N}(x_{i})\| \tag{9}\]  
\[dist_{b}=\|\alpha_{N}(x)-\alpha_{N}(x_{b})\| \tag{10}\]  
The calculation of DSA keeps unchanged. This modification makes DSA consider surprises of testing data \(x\) to all classes independently, which may be helpful to reflect the behaviors of data in corner cases, as shown in Figure 3.  
(2) Modification 2: novelty calculation via global data descriptors  
Compared with the original DSA, the first modification considers surprise of testing data \(x\) to data of all classes. While, these two DSA definitions may have a common shortage on processing pair-wise rare data points, especially on describing behaviors of corner case.