# Predicting Saudi Stock Market Index by Using Multivariate Time Series Based on Deep Learning
## 4 Methodology and Implementation
### LSTM Prediction Algorithm  
Long short-term memory (LSTM) is an RNN architecture that is frequently used to predict TSs and process natural language. It can also be used for both univariate and multivariate TS prediction problems.  
Univariate LSTMs are used when a TS only contains a single input feature, such as a stock's closing price on a daily basis or a city's temperature on a daily basis. In such cases,  
Figure 1: LSTM cell.  
the LSTM is trained to forecast future values solely using the single input feature's past values.  
Multivariate LSTMs, on the other hand, are used when a TS contains multiple input features, such as a stock's closing price on a daily basis, a city's temperature on a daily basis, and the volume of the stock on a daily basis. In such cases, the LSTM is trained to forecast future values using all these input features' past values.  
Regardless of whether an LSTM is univariate or multivariate, the principal idea of its architecture remains the same. Long short-term memory (LSTM) uses gates to control the flow of information through a network as well as preserve dependencies that are long term in the data. Therefore, LSTMs are ideal and well-suited for TS prediction problems where the correlation between past and future values is complex and may not be easily modelled using traditional statistical methods.  
\begin{tabular}{p{142.3 pt} p{142.3 pt}} \hline \hline
**Univariate LSTM Algorithm** & **Multivariate LSTM Algorithm** \\ \hline
1. Libraries Importing and Data Loading: Import the necessary libraries, such as TensorFlow and Numpy, and load the data into the system. & 1. Libraries Importing and Data Loading: Import the necessary libraries, such as TensorFlow and Numpy, and load the data into the system. \\ \hline
2. Data Preprocessing: Preprocess the data to prepare it for training by removing noise using exponential smoothing (ES), which scales, transforms, and splits the data into training and validation sets. & 2. Data Preprocessing: Preprocess the data to prepare it for training by removing noise using ES, which scales, transforms, and splits the data into training and validation sets. \\ \hline
3. Model Definition: Define the architecture of the LSTM model using the number of units, layers, activation functions, and other hyperparameters. & 3. Data Reshaping: Reshape the data into a three-dimensional (3D) format with dimensions such as time steps, features, and samples. \\ \hline
4. Model Compilation: Compile the model by defining the optimiser, loss function, and metrics to be used. & 4. Model Definition: Define the architecture of the LSTM model using the number of units, layers, activation functions, and other hyperparameters. \\ \hline
5. Model Training: Train the model by using the fit) function to fit it to the training data. & 5. Model Compilation: Compile the model by defining the optimiser, loss function, and metrics to be used. \\ \hline
6. Model Evaluation: Evaluate the model using the validation dataset and the evaluate() function to calculate performance metrics; such as accuracy and loss. & 6. Model Training: Train the model by using the fit) function to fit it to the training data. \\ \hline
7. Prediction Generation: Use the model to create predictions using the new data and the predict() function. & 7. Model Evaluation: Evaluate the model using the validation dataset and the evaluate() function to calculate performance metrics; such as accuracy and loss. \\ \hline
8. Results Plotting: Plot the results to visualise the model’s performance and compare it to the actual values. & 8. Prediction Generation: Use the model to create predictions using the new data and the predict() function. \\ \hline
9. Save the Model: Save the trained model to a disc for later use. & 9. Results Plotting: Plot the results to visualise the model’s performance and compare it to the actual values. \\ \hline \hline \end{tabular}