# Back to the Metrics: Exploration of Distance Metrics in Anomaly Detection
## 2 Methodology
### Visualization Analysis of Different Metrics
#### 2.3.3 Summary  
In summary, the \(k\)-nearest neighbor squared distance mean score calculation function has obvious drawbacks when \(k>1\), but it exhibits better universality when \(k=1\). In most cases, particularly when it is highly probable that the sample is anomalous (anomaly detection tasks generally use the highest anomaly score sample from anomaly segmentation for further anomaly score calculation), this method does not produce the extreme errors seen in previous re-weighting methods. The PatchCore anomaly score calculation function can have better distribution patterns of anomaly scores in certain situations, but it imposes stringent requirements on the sampling method and the distribution of sampling results. When using the \(k\)NN distance metric to calculate the anomaly score, \(k\) needs to be smaller than the number of samples in the cluster, and the anomaly detection performance is best when \(k\) is close to the number of samples in the cluster. Accounting for the above visual analysis results, as shown in Equation (14), we define the memory bank \(\mathcal{M}^{\prime}\) as a set consisting of many clusters \(\mathcal{C}\), each of which consists of \(k\)NN samples. In the equation, \(d\) should be defined based on empirical observations. Choosing a suitable \(k\) for each cluster in the anomaly scoring phase is noteworthy. However, for computational and implementation  
Figure 6: Contour plots of the weight values of Equation (11) under a simulated co-reset with different values of \(k\). Orange dots represent outliers and blue dots represent normal samples.  
Figure 7: We enlarged the abscissa of the six samples in the coreset in Figure 5 by a factor of 7, dividing them into three clusters consisting of 1, 2, and 3 points, respectively, assuming they are samples sampled from the edges of three distribution clusters. Here, this is to facilitate the manual grouping of samples and magnify certain details. In reality, the samples would have thousands of dimensions, with the same issue present in each dimension. In this Figure, orange dots and blue dots both represent normal samples.  
convenience, we simply assume that the \(k\) values of \(\mathcal{C}\) in \(\mathcal{M}^{\prime}\) close to the abnormal samples of the test set are small, while the \(k\) values of other \(\mathcal{C}\) are large.  
\[\mathcal{M}^{\prime}=\{ \tag{14}\] \[\mathcal{C}_{1},\mathcal{C}_{2},\ldots,\mathcal{C}_{n}\,|\,n\in \mathbb{Z}^{+},\] \[(\forall c\in\mathcal{C}_{n},\exists c^{\prime}\in\mathcal{C}_{n} \setminus\{c\},\mathrm{dist}(c,c^{\prime})\leq d),\] \[(\forall c\in\mathcal{C}_{n},\forall c^{\prime}\in\mathcal{M} \setminus\mathcal{C}_{n},\mathrm{dist}(c,c^{\prime})>d)\] \[\},\] \[\mathcal{M}=\bigcup_{\mathcal{C}\in\mathcal{M}^{\prime}}\mathcal{C}.\]  
We pay more attention to abnormal samples in the anomaly detection (AD) task, while normal samples are more predominant in the anomaly scoring (AS) task. Therefore, we should set different values of \(k\) for anomaly score calculation in AD and AS. Specifically, a smaller \(k\) value is designed for AD anomaly score calculation, and a larger \(k\) value is designed for AS anomaly score calculation.