# Online Forecasting and Anomaly Detection Based on the ARIMA Model
## 2 Materials and Methods
### Anomaly Detection Algorithms
#### 2.4.1 Novel Anomaly Detection Algorithm Based on OGD Algorithm
```
Input Hyperparameters \(p,d,q\), learning rate \(\eta\), threshold \(\epsilon_{th}\), empty list \(r\); Set \(m=\log_{\max}\left((TLM_{\max}q)^{-1}\right)\) For\(t\) from 1 to \(T-1\)do: \(\text{Predict }\tilde{X}_{t}\left(\gamma^{\prime}\right)=\sum_{i=1}^{p+m}\gamma_{i} \
abla^{d}X_{t-i}+\sum_{i=0}^{d-1}\
abla^{i}X_{t-1}\); \(\text{Get }X_{t}\) and \(\ell_{t}^{m}\left(\gamma^{\prime}\right)=\ell_{t}\left(X_{t},\tilde{X}_{t} \left(\gamma^{\prime}\right)\right)\); \(\text{Set }\
abla_{t}=\
abla\ell_{t}^{m}\left(\gamma^{t}\right)\); \(\text{Update }\gamma^{t+1}\leftarrow\prod_{K}\left(\gamma^{t}-\frac{1}{\eta} \
abla_{t}\right)\) \(\text{Update }\
abla\gamma^{t}=-\frac{1}{\eta}\
abla_{t}\) For\(w\) from 1 to \(p+m\)do: \(\text{If }|\
abla\gamma_{tw}^{t}|<\epsilon_{th}\)then \(\text{Add value }t\text{ to }r\) \(\text{ALERT}\) End for End for End for
```  
**Algorithm 4** ARIMA Anomaly detection. In practice, when verification is provided using benchmarks, the sample size is not big enough (unlike real technical systems) to achieve a global optimum by stochastic gradient descent. In this regard, four heuristic methods have been developed, which are more sensitive to anomalies in this case. The first three are similar in meaning and can be used in batch processing. They are distinguished by metrics that generalize the entire set of weight coefficients with one point for each available time moment. The fourth method can only be used for offline analysis (this method is not overfitted for a particular dataset because similar results have been obtained for other datasets). All methods are presented below (executed instead of the condition in Algorithm 4):  
1. Metric based on Euclidean norm \[\text{\emph{Metric}}_{1}^{t}=\sqrt{\sum_{n=1}^{S}(\
abla\gamma_{ts})^{2}}\] \[\epsilon_{th_{1}}^{t}=\mu(\text{\emph{Metric}}_{1}^{t})_{t-win}^{ t}+3\cdot\sigma(\text{\emph{Metric}}_{1}^{t})_{t-win}^{t}\] \[\text{If \emph{Metric}}_{1}^{t+1}>\epsilon_{th_{1}}^{t}\text{ then ALERT}\] where \(S\) is the number of all weight coefficients of the ARIMA models for the considered system, \(t\) is the time moment, \(\
abla\gamma_{ts}\) is a discrete difference in one specific weight coefficient, \(win\) is the width of the window (requiring separate tuning, can be an equally timed physical process), \(\mu\) is a function for computing the mean of a metric in a particular window \(win\), \(\sigma\) is a function for computing the standard deviation of a metric in a particular window \(win\), and \(\epsilon_{th_{1}}^{t}\) is the threshold of the metric. 2. Using a metric based on the maximum absolute values \[\text{\emph{Metric}}_{2}^{t}=\max\{|\
abla\gamma_{ts}|\}_{s=1}^{S}\] \[\epsilon_{th_{2}}^{t}=\mu(\text{\emph{Metric}}_{2}^{t})_{t-win}^{ t}+3\cdot\sigma(\text{\emph{Metric}}_{2}^{t})_{t-win}^{t}\] \[\text{If \emph{Metric}}_{2}^{t+1}>\epsilon_{th_{2}}^{t}\text{ then ALERT}\]Metric based on the mean of max/std  
\[\gamma^{t+1}=\gamma^{t}-\frac{1}{\eta}\
abla_{t} \tag{6}\]0 \[\gamma^{t+1}=\gamma^{t}-\frac{1}{\eta}\
abla_{t} \tag{6}\]1 \[\gamma^{t+1}=\gamma^{t}-\frac{1}{\eta}\
abla_{t} \tag{6}\]2 \[\gamma^{t+1}=\gamma^{t}-\frac{1}{\eta}\
abla_{t} \tag{6}\]3
4. Complex metric \[\gamma^{t+1}=\gamma^{t}-\frac{1}{\eta}\
abla_{t} \tag{6}\]4 \[\gamma^{t+1}=\gamma^{t}-\frac{1}{\eta}\
abla_{t} \tag{6}\]5 \[Metric_{43}^{t}= \begin{cases}0,&\text{if }Metric_{42}^{t}<Q\\ Metric_{42}^{t},&\text{else}\end{cases} \] where \(Q\) is the first value after index which is greater than \(\frac{9\cdot T}{10}\) of a sorted smallest to largest values of \(Metric_{43}\) If \(Metric_{43}^{t}\) is local maxima then ALERT