# Anomaly Detection in Financial Time Series by Principal Component Analysis and Neural Networks +
## 2 The PCA NN Anomaly Detection Approach
### Contaminated Time Series Identification
#### 2.4.2 Neural Network Approach
Each update of \(\Theta\) AdamStep in Algorithm 2 is carried out following the Adam optimization algorithm of [6]. Figure 2 and Table 1 show that with the NN approach, the densities of the scores assigned to each type of time series display the expected behaviours on the left-hand (right-hand) side of the cutoff value for contaminated (uncontaminated) time series for both the training and test sets. Actually, the lower the AUCDensity\({}^{u}\) (AUCDensity\({}^{c}\)), the lower the number of uncontaminated (contaminated) time series to which the anomaly scores above (below) the cutoff value are assigned, which prevents mislabelling. \begin{table}
\begin{tabular}{c c c c c c} \hline \hline
**Approach** & **AUCDensity\({}^{a}\)** & **AUCDensity\({}^{c}\)** & **Approach** & **AUCDensity\({}^{a}\)** & **AUCDensity\({}^{c}\)** \\ \hline Naive & 0.1725 & 0.7898 & Naive & 0.1897 & 0.6354 \\ \hline NN & 0.05153 & 0.1550 & NN & 0.1290 & 0.1367 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: AUC obtained with the naive and the NN approaches for the training set (**left**) and the test set (**right**). ```
inputs:learning rate \(lr\) number of maximum iterations \(K\) kernel density estimator parameters \(\mathcal{K}\),\(\mathfrak{h}\) training data \(\varepsilon^{Train}\),\(\mathbf{A}^{Train}\) Initialize parameter \(\Theta\), \(\widehat{\mathcal{L}}=\infty\) and count index \(k=0\) while\(k<K\)do \(scores\gets F_{\Theta}\big{(}\varepsilon^{Train}\big{)}\) \(scores^{c}\gets scores\mathds{1}_{\mathbf{A}^{Train}=1}\) \(scores^{u}\gets scores\mathds{1}_{\mathbf{A}^{Train}=0}\)// Density estimation following (4) \(f_{\mathbf{A}^{Train},scores^{u}}^{u}=\text{KernelDensityEstimator}(\mathcal{K}, \mathfrak{h},scores^{u})\) \(f_{\mathbf{A}^{Train},scores^{c}}^{c}=\text{KernelDensityEstimator}(\mathcal{K}, \mathfrak{h},scores^{c})\) // Loss evaluation following (5) and (6) \(\text{AUCDensity}^{u}\leftarrow\text{NumericalIntegration}\big{(}\hat{f}_{\mathbf{A}^{Train},scores^{u}}^{u},\mathfrak{s}\big{)}\) \(\text{AUCDensity}^{c}\leftarrow\text{NumericalIntegration}\big{(}\hat{f}_{\mathbf{A}^{Train},scores^{c}}^{c},\mathfrak{s}\big{)}\) \(\widehat{\mathbf{A}}\leftarrow\mathds{1}_{scores^{c}\geq\mathfrak{s}}\) \(\mathcal{L}\leftarrow\text{BCE}(\mathbf{A}^{Train},\widehat{\mathbf{A}})+\text{ AUCDensity}^{u}+\text{AUCDensity}^{c}\) if\(\widehat{\mathcal{L}}>\mathcal{L}\)then \(\widehat{\mathcal{L}}\leftarrow\mathcal{L}\) \(\widehat{\Theta}\leftarrow\Theta\)  end if \(\Theta\leftarrow\text{AdamStep}(\mathcal{L},\Theta,lr)\) \(k\gets k+1\) end outputs:best calibrated parameter \(\widehat{\Theta}=\{\widehat{\mathbf{W}},\hat{\mathbf{b}},\hat{s}\}\). ```  
**Algorithm 2** Scoring and cutoff calibration  
Once the features \(\varepsilon\) and the calibrated NN are provided, the contaminated times series identification model is ready for use. This step is described by Algorithm 3. Figure 2: Empirical densities of anomaly scores given by the NN approach for uncontaminated time series in black and contaminated time series in red for the training set (**left**) and test set (**right**). The dotted dark red lines represent the calibrated cutoff value \(\hat{s}\). ```
inputs:time series to analyze \(X^{i}\),  calibrated model parameter \(\widehat{\Theta}\), \(\epsilon^{i}\leftarrow\text{PCAFeaturesExtraction}(X^{i})\) ; // cf. Algorithm 1 in Section 2.3\(score^{i}\leftarrow\text{F}_{\Theta}(\epsilon^{i})\) \(\hat{A}^{i}\leftarrow\mathds{1}_{score^{i}>\delta}\) outputs:identification label \(\hat{A}^{i}\). ```  
**Algorithm 3** Contaminated time series identification model