## 2 Methodology
### Local Outlier Factor Model  
The LOF model is an unsupervised anomaly detection model that identifies anomalies in the data based on local neighbourhood densities. It is especially useful when the density of the data varies globally across the dataset. This makes the LOF especially useful in this case since the presence of a leak will affect the pressure, flow, and chlorine levels that are closer in distance to the suspected leak.  
The LOF score of a data point is calculated to determine whether it is considered an outlier. The closer the LOF score is to \(-1\), the more \"likely\" it is to be an outlier. To calculate the LOF score of a datapoint, we first need to understand k-distance and k-neighbours. The k-distance of any point A is defined as the distance between point \(A\) and its \(k\)th nearest neighbour. The number of \(k\)-neighbours from point A is denoted as \(N_{k}(A)\), where \(N_{k}(A)\) is always less than or equal to k. The measure of distance used in this analysis is the Minkowski distance with \(p=4\), where \(p\) is the parameter that determines the order of the distance calculation. Since each data point in this dataset contains 4 features (pressure, flow, chlorine, and ratio_pf), the parameter \(p\) is set to 4. The Minkowski distance between two points \(A\) and \(B\) can be computed as follows:  
\[Minkowski(A,\ B)=\left(\sum_{i=1}^{4}\left|A_{i}-B_{i}\right|^{4}\right)^{ \frac{1}{4}} \tag{3}\]  
\begin{table}
\begin{tabular}{c c c c c} \hline  & **Pressure** & **Flow** & **Chlorine** & **Ratio-pf** \\ \hline Shapiro–Wilk & \(8.12\times 10^{-12}\) & \(1.34\times 10^{-23}\) & \(1.93\times 10^{-8}\) & \(5.44\times 10^{-12}\) \\ \hline Kolmogorov–Smirnov & 0.0 & 0.0 & \(5.11\times 10^{-216}\) & \(3.69\times 10^{-175}\) \\ \hline \end{tabular}
\end{table}
Table 1: \(p\)-Values of Shapiro–Wilk and Kolmogorov–Smirnov tests from all 4 features.  
\begin{table}
\begin{tabular}{c c c c c} \hline  & **Pressure** & **Flow** & **Chlorine** & **Ratio-pf** \\ \hline Anderson–Darling & 5.461 & 26.021 & 6.831 & 6.086 \\ \hline \end{tabular}
\end{table}
Table 2: Statistic value of Anderson–Darling from all 4 features.  
The Reachability Distance (\(RD\)) between two points \(A\) and \(B\) can then be defined as the maximum of the k-distance of A and the distance between \(A\) and \(B\):  
\[RD(A,\ B)=max\{k-distance(A),\ distance(A,\ B)\} \tag{4}\]  
The Local Reachability Distance (\(LRD\)) of a point A is defined as the inverse of the average \(RD\) of \(A\) from its k-neighbour. The lower the \(LRD\) value, the further the closest cluster is to point \(A\).  
\[LRD_{k}(A)=\frac{1}{\sum_{X_{j}\in N_{k}(A)}\frac{RD(A,\ X_{j})}{||N_{k}(A)||}} \tag{5}\]  
Finally, the \(LOF\) score of a datapoint \(A\) is calculated as the ratio of the average \(LRD\) of its \(k\)-neighbours to the \(LRD\) of \(A\):  
\[LOF_{k}(A)=\frac{\sum_{X_{j}\in N_{k}(A)}\ LRD_{k}(X_{j})}{||N_{k}(A)||}\times \frac{1}{LRD(A)} \tag{6}\]  
Evaluating how well the model was able to cluster anomalies was performed using the silhouette score of the data. The silhouette score is a metric used to evaluate the quality of clusters generated by a model. It is used in this analysis as a standard to decide how well the model performs. The silhouette score \(S(A)\) for a datapoint \(A\) is calculated as follows:  
\[S(A)=\frac{f(A)-g(A)}{max\{f(A),\ g(A)\}} \tag{7}\]  
where \(f(A)\) is the smallest average distance from \(A\) to points in a different cluster, and \(g(A)\) is the average distance from \(A\) to other points in the same cluster. The silhouette score for the dataset can then be calculated as the average of all individual silhouette scores:  
\[Silhouette\ Score=\frac{1}{N}\sum_{i=1}^{N}S(i) \tag{8}\]  
where \(N\) is the total number of data points. The closer the silhouette score is to 0, the more overlapping there is between the clusters. An \(A\) value closer to \(-1\) indicates possible misclustering of the data, whilst a value closer to 1 indicates more distinct clustering of the data.