## 4 Data Preprocessing
### Outliers
#### 4.3.2 Local Outliers  
Local outliers are more challenging to detect than global ones. We need to know the nature of the time series to find local outliers. For example: \"The current temperature is 27 \({}^{\circ}\)C. Is it an outlier?\". It depends, for example, on the time and location. In the city of Morelia, it is an outlier if it is the month of December at night. If it is the month of May for the day in Morelia, then the measurement is a usual case. The determination of whether the value of today's temperature is an outlier or not depends on the context: the date, time, location, and possibly some other factors [21]. We used the local outlier factor (LOF) method in this situation [22]. The LOF method compares outliers with data in their local neighborhoods rather than the global data distribution. LOF is an outlier detection technique based on the density around an outlier; density around normal data must be higher than the density around outliers. LOF compares the relative density of a point against that of its neighbors. That ratio is an indicator of the degree to which objects are outliers. High-density points have many neighbors at a close distance, and low-density points have fewer. A typical distance measure is the Euclidean distance defined in (3).  
\[d_{E}(p,o)=\sqrt{\sum_{i=1}^{\overline{m}}(p_{i}-o_{i})^{2}} \tag{3}\]  
where \(m\) is the dimensionality of the data points. Let \(C\) be a set of objects in an \(m\)-dimensional space and \(p\) a query object. Let us denote \(D^{\prime}\) as the array of objects \(o\in X\), sorted by distance to \(p\). The \(k\)-nearest neighbor (\(kDistance(p)\)) of \(p\) is \(D^{\prime}_{k}\). Figure 10 shows an example where \(k=3\) in two dimensions.  
The \(k\)-distance neighborhood represents the set of all neighbors of \(p\), the center of the big circle on the left of Figure 10, that are as far from \(p\) as \(kDistance(p)\)--see (4).  
\[N_{k}(p)=\{o|o\in D,d(p,o)\leq kDistance(p)\} \tag{4}\]  
The reachability distance of \(p\) and \(o\) is the maximum between their distance and the \(kDistance(p)\). See (5).  
\[reachdist_{k}(p,o)=max\{kDistance(p),d(p,o)\} \tag{5}\]  
Figure 10: Density around normal data and outliers.  
Local reachability density is the inverse of the average reachability distances. (6) shows the local reachability of point \(p\) within its \(k\)-distance neighborhood.  
\[lrd_{k}(p)=\frac{|N_{k}(p)|}{\sum\limits_{o\in N_{k}(p)}\frac{lrd_{k}(o)}{ reachdist_{k}(p,o)}} \tag{6}\]  
where \(|N_{k}(p)|\) is not necessarily \(k\), since there is the possibility that several neighbors are at the same distance.  
The local outlier factor (LOF) of a point \(p\) is the average of the ratio of the \(lrd_{k}(o)\) and \(lrd_{k}(p)\) for all \(k\)-distance neighbours of \(p\). See (7).  
\[LOF_{k}(p)=\frac{\sum\limits_{o\in N_{k}(p)}\frac{lrd_{k}(o)}{lrd_{k}(p)}}{|N_{ k}(p)|} \tag{7}\]  
We compute the LOF of every point \(p\in D\) to determine outliers and discriminate outliers using (8).  
\[label(p)=\begin{cases}LOF_{k}(p)\approx 1,&normal\\ LOF_{k}(p)<1,&normal\\ LOF_{k}(p)>1,&outlier\end{cases} \tag{8}\]  
The first two cases indicate that the density of around \(p\) is similar to or higher than that of its neighbors; therefore, the data point is labeled as normal. The last case indicates that the density around \(p\) is lower than that of its neighbors; therefore, the data point is labeled as a local outlier. The LOF method detects and labels the local outliers. This process detects outliers, as outlined above. Those points detected as outliers are corrected using data imputation.