# Anomaly Detection in Financial Time Series by Principal Component Analysis and Neural Networks +
## 3 Data Generation Process
### Data Augmentation  
By definition, anomalies are rare events and thus represent only a low fraction of the data set. Yet, the suggested approach needs an important training set for efficient learning. To overcome this issue, we apply a sliding window data augmentation technique [7]. This method not only extends the number of anomalies within the data set but also allows the model to learn that anomalies could be located anywhere in the time series. It consists of extracting \(N_{p}=T-p+1\) sub-time series of length \(p\) of the initial observed time series of length \(T\). Note that we have to split the data into training and test sets before augmentation to guarantee that the time series considered in the training set do not share any observations with the ones we used for the model evaluation. In view of simplification, we introduce the data augmentation process without a loss of generality for \(\mathbf{S}\) and \(\mathbf{Y}\). In practice, this process has to be applied to \(\mathbf{S}^{\text{{Train}}}\) and \(\mathbf{Y}^{\text{{Train}}}\), and \(\mathbf{S}^{\text{{Test}}}\) and \(\mathbf{Y}^{\text{{Test}}}\) separately. For \(i=1,\dots,N\) and \(q\in\{1,\dots,N_{p}\}\), the sub-time series \(S^{i,q}\) and the associated labels \(Y^{i,q}\) are defined by Algorithm 7  
\[S^{i,q} =\Big{\{}S^{i}_{t_{j}}\,,\,j=q,\dots,q+p-1\Big{\}}\] \[Y^{i,q} =\Big{\{}Y^{i}_{t_{j}}\,,\,j=q,\dots,q+p-1\Big{\}}.\]  
Each sub-time series \(S^{i,q+1}\) thus results from the shift forward in time of one observation of the previous sub-time series \(S^{i,q}\). The final data set \(\mathbf{X}\) and the labels \({}^{\mathbf{s}}\mathbf{Y}\) are then defined as the matrices whose rows correspond to the sub-time series \(S^{i,q}_{q=1,\dots,N_{p};i=1,\dots,N}\), and \(Y^{i,q}_{q=1,\dots,N_{p};i=1,\dots,N}\), respectively, i.e.,  
\[\mathbf{X} =\Big{(}S^{1,1},S^{1,2},\dots,S^{1,N_{p}},S^{2,1},\dots,S^{i,q}, \dots,S^{N,N_{p}}\Big{)}^{\top},\] \[{}^{\mathbf{s}}\mathbf{Y} =\Big{(}Y^{1,1},Y^{1,2},\dots,Y^{1,N_{p}},Y^{2,1},\dots,Y^{i,q}, \dots,Y^{N,N_{p}}\Big{)}^{\top}.\]With this data configuration, an observation refers to a time series \(S^{i,q}\) obtained through the sliding window technique. Two observations may represent the same stock but at different time intervals. ```
inputs:window size \(p\),  data and labels \(S_{i}\boldsymbol{Y}\);  Initialize empty slided time series and labels matrices \(\boldsymbol{X},{}^{s}\boldsymbol{Y}\); for\(i:=1\) to \(N\)do for\(q:=1\) to \(N_{p}\)do \(S^{i,q}\leftarrow\left\{S^{i}_{i_{j}}|j\in\{q,\ldots,q+p-1\}\right\}\) \(Y^{i,q}\leftarrow\left\{Y^{i}_{i_{j}}|j\in\{q,\ldots,q+p-1\}\right\}\) end \(\boldsymbol{X}\leftarrow\text{Concatenate}\left(\boldsymbol{X},S^{i,q}\right)\) \({}^{s}\boldsymbol{Y}\leftarrow\text{Concatenate}\left({}^{s}\boldsymbol{Y},Y^ {i,q}\right)\) end outputs:resulting slided time series and labels \(\boldsymbol{X},{}^{s}\boldsymbol{Y}\)
```  
**Algorithm 7** Data Augmentation with sliding window technique  
Although the fact that the observations share the same values can be argued to wrongly impact the learning process, we should point out that a real benefit can be drawn from this situation. Indeed, thanks to this sliding window technique, the number of anomalies is considerably increased. This technique also allows the model to learn that anomalies could be located anywhere in the time series, reducing the dependency on the event location [8]. However, once we apply the sliding window technique, we do not only extend the number of contaminated time series; the number of uncontaminated time series is also increased. However, the minority class (contaminated time series) has at least a significant number of instances, denoted by \(N^{c}\). In order to obtain a more balanced data set for the training set, we perform an undersampling, randomly selecting \(N^{c}\) observations from the \(N^{u}\) uncontaminated time series without any anomalies (RandomSampling in Algorithm 8). The resulting retained number of observations \(2N^{c}\) is now sufficient to train the model.