# Predicting Saudi Stock Market Index by Using Multivariate Time Series Based on Deep Learning
## 4 Methodology and Implementation
### Prediction Model  
Multiple issues are categorised according to the available data and the desired outcome. Many extant TS data-based studies have concluded that LSTM significantly improves performance, especially when the outputs of the previous state need to be remembered. It has also been found to outperform both a traditional feed forward network (FFN) and RNN in sequence prediction tasks due to its capacity for long-term memory.  
\begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Date** & **Closing** & **Opening** & **Highest** & **Lowest** \\ \hline
21 November 2022 & 37.75 & 37.8 & 37.85 & 37.65 \\
20 November 2022 & 37.8 & 37.85 & 37.85 & 37.6 \\
17 November 2022 & 37.65 & 38 & 38.05 & 37.65 \\
16 November 2022 & 38 & 37.8 & 38 & 37.15 \\
15 November 2022 & 37.8 & 38.3 & 38.3 & 37.5 \\
14 November 2022 & 38.3 & 38.25 & 38.5 & 38 \\ \hline \hline \end{tabular}
\end{table}
Table 1: A snippet of STC stock prices.  
\begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Date** & **Closing** & **Opening** & **Highest** & **Lowest** \\ \hline
21 November 2022 & 35.45 & 35.2 & 35.65 & 34.9 \\
20 November 2022 & 35.85 & 36.5 & 36.5 & 35.2 \\
17 November 2022 & 36.45 & 35.85 & 36.5 & 35.55 \\
16 November 2022 & 36.1 & 35 & 36.5 & 35 \\
15 November 2022 & 35 & 35.8 & 36 & 34.95 \\
14 November 2022 & 36 & 36.3 & 36.8 & 35.8 \\ \hline \hline \end{tabular}
\end{table}
Table 2: A snippet of Mobily stock prices.  
\begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Date** & **Closing** & **Opening** & **Highest** & **Lowest** \\ \hline
24 November 2022 & 11.06 & 11.32 & 11.32 & 11.06 \\
22 November 2022 & 11.4 & 11.3 & 11.42 & 11.26 \\
21 November 2022 & 11.4 & 11.42 & 11.5 & 11.3 \\
20 November 2022 & 11.5 & 11.5 & 11.52 & 11.38 \\
17 November 2022 & 11.52 & 11.48 & 11.54 & 11.36 \\
16 November 2022 & 11.48 & 11.34 & 11.52 & 11.24 \\ \hline \hline \end{tabular}
\end{table}
Table 3: A snippet of Zain KSA stock prices.  
There are input, output, and forget gates in every LSTM cell. The LSTM network stores all the data that enters and uses the forget gate to delete information that is not necessary (Figure 1).  
As seen in Figure 1, the preceding hidden state (\(h_{t}-1\)), prior cell state (\(C_{t}-1\)), and present input (\(X_{t}\)) are the inputs of the current cell state (\(C_{t}\)). An LSTM cell consists of an input gate, output gate, and forget gate.  
The forget gate uses a sigmoid function to identify and decide which information needs to be scrubbed from the block. It examines the previous state (\(h_{t}-1\)), the content input (\(X_{t}\)), and each number in the cell state \(C_{t}-1\) to create a number between 0 (delete) and 1 (store).  
\[f_{t}=\sigma\left(W_{f}[h_{t}-1,\ X_{t}]+b_{f}\right) \tag{1}\]  
An input gate identifies which input value should be utilised to alter the memory. The sigmoid function ascertains whether to permit 0 or 1 values while the \(\tanh\) function assigns weight to the supplied data and ranks its significance on a scale, from \(-1\) to 1.  
\[i_{t}=\sigma(W_{i}[h_{t}-1,\ X_{t}])+b_{i} \tag{2}\]  
\[C_{t}=\tanh(W_{c}\ [h_{t}-1,\ X_{t}]+b_{c}) \tag{3}\]  
An output gate determines the output of a block using its input and memory. The sigmoid function ascertains whether to permit 0 or 1 values while the \(\tanh\) function ascertains which values, 0 or 1, are permitted to pass through as well assigns weight to the supplied values and ranks their significance on a scale, from \(-1\) to 1, and multiplies it with the sigmoid output.  
\[O_{t}=\sigma\left(W_{o}\ [h_{t}-1,\ X_{t}]+b_{o}\right) \tag{4}\]  
\[h_{t}=O_{t}*\tanh(C_{t}) \tag{5}\]