## 4 Experimental Setup
### Anomaly Detection Models  
Once the best-performing prediction model has been obtained from the procedures explained in Section 4.1; we proposed to alter the forecast produced by this model to generate anomalies within our dataset and, thus, labeled each observation as anomalous or normal. The period corresponding to the forecast used for alteration and anomaly generation is from 13 July 2023 to 11 August 2023, which represents the last 30 days of the dataset. The following criteria were used to alter the observations within the electricity consumption forecast time series:  
* The forecast was made for 30 days.
* Three different categories of anomalies (see Table 7) were established.
* Twenty percent of the observations (i.e., six days) were randomly altered (based on the frequency of Table 7, three days for category A, two days for category B, and one day for category C).
* Each randomly selected date was multiplied by a factor \(f\) based on the values shown in Table 7.
* The randomly selected dates do not repeat. There are always six different dates.  
The outlier detection methods tested in this work were: Median Absolute Deviation (MAD), isolation forest (IF), and the Local Outlier Factor (LOF). These methods were compared using accuracy, precision, recall, and the F1-score as metrics. A methodology has been designed to subject each anomaly detection method to a robust evaluation process according to the following criteria:  
1. The data corresponding to \(n\) (\(n=6\)) random dates were modified following the previously mentioned criteria to generate an anomaly.  
\begin{table}
\begin{tabular}{c c c c c c} \hline \hline  & **Category** & **Anomaly** & **Factor (\(f\))** & **Frequency** & **Real Scenario** \\ \hline A & Weekends and holidays & Increase in consumption & Random between & 3 & Visits, celebrations, and \\  & & & 1.01 and 1.15 & & parties \\ B & Random dates & Decrease in consumption & Random between & 2 & Vacations \\ C & Random dates & Electrical noise & White noise signal & 1 & Problems with appliances \\ \hline \hline \end{tabular}
\end{table}
Table 7: Three categories used to generate anomalies. Source: own elaboration.  
2. The method was trained using the altered data (anomaly inclusion process) from the forecast generated by the prediction model.
3. The metrics of _accuracy_, _precision_, _recall_, and the _F1-score_ were calculated by evaluating the method with the modified anomalous forecast.
4. The process was iterated 30 times, and the metrics of interest were collected in each iteration.
5. An average of the metrics of interest was calculated as the final result.
6. Once the 30 iterations were completed, the process began again with a different criterion or hyperparameter, depending on the method.
7. After completing the 30 iterations for each criterion or hyperparameter to be evaluated, the final result was the average obtained over the 30 iterations for each criterion or hyperparameter.  
Each method contains different criteria or hyperparameters, which can be fine-tuned according to the needs of the problem or application (see Table 8). All hyperparameters tuned in this work were chosen based on their relevance and impact on the performance of each respective technique. For instance, the _n\({}_{e}\)stimators_ hyperparameter in the isolation forest method defines the number of estimators used, and by increasing its value, the model's ability to identify outliers increases as well [58]. Therefore, the evaluation methodology includes the process of selecting different values to achieve a _grid search_ similar to the approach used during the selection of the regressor for generating the electricity consumption forecast. Additionally, cross-validation was performed by evaluating the chosen value at different points in the time series during each iteration.