# Hybrid Machine Learning-Statistical Method for Anomaly Detection in Flight Data
## 3 Local Outlier Factor
### Implementation
#### 3.2.4 \(k\) Nearest Neighbor  
In the LOF algorithm, parameter \(k\) is the neighborhood size, which defines the neighborhood of a data point for the computation of its local density. The original LOF paper [18] proposed some guidelines for determining the range of value for parameter \(k\). In principle, the value of \(k\) should be lower bound by the minimum number of points in a cluster while the upper bound should be the maximum number of nearest points that can potentially be anomalies. However, such information is generally not available and is highly domain dependent. Even if such information is available, the optimal \(k\) value between the lower bound and upper bound is still undefined. A range of \(k\) values is suggested as one value of \(k\) cannot be generalized over various datasets with diverse underlying data distribution. Aviation data are time-series-based multidimensional data. The dimensions typically run into thousands. This nature of data being of higher dimensions, heterogeneously unstructured and having diverse underlying distributions makes the process of determining the optimal value of \(k\) more challenging. The following paragraphs explain the technique used to determine the optimal \(k\) value while using the LOF algorithm to identify anomalous flights. A locality or local density is defined by \(k\) Nearest Neighbors (\(k\)NN). The distance between point A and its neighbors is calculated to estimate the local density. Then the local density of point A is compared with the local densities of its neighbors. If point A has a lower density than its neighbors, then it is considered an outlier. In Figure 4, point \(A\) has a much lower density than its neighbors 1, 2 and 3. Therefore, point \(A\) is an outlier compared to its three nearest neighbors (\(k=3\)). Figure 4: Illustrating basic concept of LOF. The local density is measured by the typical distance at which point A can be \"reached\" from its neighbors. This defines \"reachability distance\" in the LOF algorithm. The formal definition of the LOF algorithm interpreted by [PERSON] et al. [18] is discussed in the following paragraphs. Let \(k\) distance (A) be the distance of point A to its \(k\)th nearest neighbor. The set of the \(k\)NN includes all the points at this distance, which can, in the case of a tie, be more than \(k\) points. We denote the set of \(k\)NN as Nk(A). The reachability distance of point A from point B is the distance between these two points, which is calculated as the maximum value of the two distance measures, namely \(k\) distance (B), which is the distance of the point B to its \(k\)th nearest neighbor and d(A, B), which is either Euclidean, Manhattan, etc. distance measure. Thus, mathematically reachability distance can be defined as:  
\[\text{reachability distance}_{\text{k}}(\text{A},\text{B})=\text{ max}\{\text{k distance }(\text{B}),\text{ d}(\text{A},\text{B})\} \tag{3}\]  
The local reachability density of point A is defined as  
\[\text{LRD}_{\text{k}}(\text{A})=1/\left(\frac{\sum_{\text{B}\in\text{N}_{ \text{k}}(\text{A})}\text{reachability distance}_{\text{k}}(\text{A},\text{B})}{| \text{N}_{\text{k}}(\text{A})|}\right) \tag{4}\]  
\(\text{LRD}_{\text{k}}\) (A) is the estimated distance at which point \(A\) can be found by its neighbors. If a neighbor were to reach out LRD value distance in any direction, then it would be most likely to find that individual point \(A\). LRD is the count of the items in the kNN set of each flight over the reach distance of the point to all the values in its set, which is the kNN set. Equation (4) summarizes \(\text{LRD}_{\text{k}}\) (A) as the inverse of the average reachability distance of point \(A\) from its neighbors.