## 3 Materials and Methods
### Overview of Loss Functions Used
#### 3.8.1 Binary Cross-Entropy  
The first loss function used is Binary Cross-Entropy (BCE) ([[https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html](https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html)]([https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html](https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html)), accessed on 2 January 2025), one of the most commonly used loss functions in binary classification problems. It measures the dissimilarity between the probabilities predicted by the model and the actual target values, assigning a higher penalty to more significant errors. It is based on the concept of entropy and is calculated for each sample independently. BCE, as defined in the literature (see PyTorch documentation ([[https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html](https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html)]([https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html](https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html)), accessed on 2 January 2025)), is expressed as follows:  
\[\text{BCE}(y,\hat{y})=-\frac{1}{N}\sum_{i=1}^{N}(y_{i}\log(\hat{y}_{i})+(1-y_{ i})\log(1-\hat{y}_{i})) \tag{1}\]  
Binary Cross-Entropy heavily penalizes incorrect predictions, particularly when the prediction deviates significantly from the actual target value. In fact, due to the logarithmic nature of the function, it penalizes errors nonlinearly, amplifying the penalty for substantial errors and reducing it for smaller errors. However, one disadvantage of BCE emerges when there is class imbalance in the dataset, as the model tends to give more weight to the majority class, neglecting instances of the less frequent class. To address this issue, two different approaches were used. The first involves using a common variant of Binary Cross-Entropy, namely, Weighted Binary Cross-Entropy, which assigns different weights to the classes based on their frequency in the dataset. This approach leverages the \"weight\" parameter to adjust class importance (see PyTorch documentation ([[https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html](https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html)]([https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html](https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html)), accessed on 2 January 2025)) and is defined as follows:  
\[\text{Weighted BCE}(y,\hat{y})=-\frac{1}{N}\sum_{i=1}^{N}(w_{1}y_{i}\log(\hat{y }_{i})+w_{0}(1-y_{i})\log(1-\hat{y}_{i})) \tag{2}\]  
where \(w_{1}\) and \(w_{0}\) are the weights assigned to classes 1 and 0, respectively, to balance the contribution of the less frequent classes compared to the majority class. The second approach, which was ultimately used, is based on the Oversampling technique described earlier. This technique involves replicating examples from the minority class (i.e., anomalies) to increase their presence in the dataset. This allows the Binary Cross-Entropy Loss to function effectively because the classes have an equivalent number of samples. In conclusion, Binary Cross-Entropy is an extremely effective loss function for binary classification, as it allows for penalizing incorrect predictions based on their severity. This enables the model to converge more quickly to an optimal solution and progressively improve its ability to distinguish observations from different classes.