**Prediction of Future Stock Price Using Recurrent Neural Network**

Received Date: \*date

Accepted Date: \*date

Published Date: \*date

**HIGHLIGHTS**

  - Machine learning process is applied to predict the stock price, which is very volatile, dynamic, and non-linear.

  - The recurrent neural network algorithm is utilized to develop an adequate model of predicting stock price.

  - Rapid Miner software is used to establish the recurrent neural network’s structure and the model’s accuracy is measured based on the value of mean square error and root mean square error.

ABSTRACT

> ***The stock market can affect businesses in a variety of ways. The rise and fall of a company’s share price values affects its market capitalization and thus its market value. Forecasting stock market returns is difficult because financial stock markets are unpredictable and non-linear. The market trend, supply and demand ratio, global economy, public opinion, and a variety of other factors may all influence the price of a particular stock. With the advent of artificial intelligence and increased processing power, programmable*** ***prediction techniques have proven to be more effective in predicting stock values. This study proposed a Recurrent Neural Network (RNN) model that uses a deep learning machine to forecast stock price. The five stages were data analysis, dataset preparation, network design, network training, and network testing. The accuracy of the model examined is determined by the mean square error (MSE) and root mean square error (RMSE), which are 1.24 and 1.12, respectively.*** ***The predicted closing price is compared to the actual closing price. Finally, it is proposed that this approach can be used to forecast the other volatile time-series data.***

*Keywords: Stock Price, Prediction, Recurrent Neural Network, Rapid Miner*

# INTRODUCTION

The market price of a share of stock is referred to as its stock price. The stock price is a relative and proportionate representation of a company’s worth. The price of a stock represents its current value to buyers and sellers (Pinsent, 2021). As a result, it only displays a percentage change in a company’s market capitalization at any given time. Any percentage change in a stock price results in an equal percentage change in a company’s market capitalization. This is one of the primary reasons why investors are concerned about stock prices (Kramer, 2021).

Stock market volatility, dynamism, and nonlinearity are well-known. Accurate stock price forecasting is extremely difficult due to various macro and micro factors such as politics, global economic circumstances, unforeseen occurrences, and a company’s financial performance (Li, 2021). According to Yates (2021), some investors avoid buying a stock or index that has risen too quickly because they believe it is due for a correction. Other investors avoid dropping stocks because they anticipate further declines.

Stock prediction is defined as the act of attempting to forecast the future value of a company’s stock or other financial instruments traded on an exchange (*Stock Market Prediction Definition*, 2018). Prediction approaches can be divided into three groups that overlap: fundamental analysis, technical analysis, and technology procedures. Fundamental analysis is the study of economic factors that affect the price of a stock. Meanwhile, the technical analysis is used to predict what other stockholders are thinking based on available information about stock price and volume. Stock market forecasting has moved into the technological arena with the invention of the digital computer.

According to Abu-Mustofa and Atiya (1996), forecasting the stock market is extremely difficult due to its dynamic and non-linear nature. Neural networks are among those capable of stimulating nonlinear market behavior. Some of the most recent stock market prediction tools include Artificial Neural Networks, Neuro-Fuzzy Systems, Time Series Linear Models (TSLM), and Recurrent Neural Networks (RNN) (Polamuri et al., 2020). Because there are so many different kinds of neural networks, choosing the right one is critical because it can have a big impact on forecast accuracy.

The stock market is always in the news, and we may hear about it when it reaches a new high or low. If an effective algorithm for predicting the short-term price of an individual stock is developed, the pace of investment and business opportunities in the stock market may accelerate. However, accurately forecasting stock market movement is difficult. There are numerous factors that can influence stock price forecasting. First, the future is uncertain because our analysis is only as good as the information we have now. We have no way of knowing what will happen tomorrow. We also cannot plan for every eventuality. Even when everything is equal on some days, other days, weeks, months, or even years defy the odds. If all of these factors could be predicted, investors would be able to make the best decisions for their investment portfolio.

Deep learning models, such as recurrent neural networks (RNN), are frequently used to address problems with sequential input data, such as time series. RNNs are a type of neural network that remembers what it has previously processed and can thus learn from previous iterations during training. RNNs can process variable length input sequences by utilizing their internal state (memory). Because recurrent neural networks are potentially Turing complete, they can execute arbitrary input sequences and run arbitrary programming (Onnen, 2021). The term "Turing complete" refers to the ability to calculate anything that any other computing technique can compute.

# METHODOLOGY

The secondary data used in this study is obtained from Malaysian Pacific Industries’ (MPI) webpage on the website Investing.com. The data showed the daily stock price over a three-year period, from 2 January 2019 to 31 December 2021. A total of 734 data points were used to forecast the future stock price. RapidMiner software was used to forecast the stock price. The recurrent neural network (RNN) model was created using a sequential model. According to Kurnaz & Demir (2021), the sequential model is one of the most basic types of models, consisting of a stack of layers. These layers can be thought of as the basic building elements of the neural network.

Each improved and tested model was used to forecast the trends. The performance of the suggested models was assessed using Mean Square Error (MSE) and Root Mean Square Error (RMSE).

\(MSE = \ \frac{\sum_{i = 1}^{N}{\ \left( y_{i} - \ F_{i} \right)^{2}}}{N}\ \) (1)

\(RMSE = \ \sqrt{\frac{\sum_{i = 1}^{N}{\ \left( y_{i} - \ F_{i} \right)^{2}}}{N}}\) (2)

\(y_{i}\) is the actual values

\(F_{i}\) is the predicted values

\(N\) is the number of the data

MSE and RMSE are used to assess the precision of forecast values. The RMSE can provide the difference between the real and predicted data. The extent to which the RMSE predicted results differ from the actual results.

**Step 1: Collecting Data**

**Before importing the dataset into RapidMiner, it was saved as a Microsoft Excel 97-2003 Worksheet. The data was loaded into the software, and the date and price columns were selected.**

![](63fd7190ad276_media/media/image1.png)

**Figure 1: Import the dataset into RapidMiner**

**The price column’s function has been changed to label. The price is the target to predict the value for the future process.**

**Step 2: Preparing Data**

**The dataset was divided into two parts: the training and testing sets. The training set is a set of data used to train the model. The testing set is a collection of data that is used to assess the performance of a full classifier.**

**The split operator was used during the process to partition the data. The dataset is divided into 7:3 ratios.**

**Table 1: Data partition**

| **Data partition** | **Total data** | **Date**                                                          |
| ------------------ | -------------- | ----------------------------------------------------------------- |
| **Training set**   | **514**        | **2<sup>nd</sup> January 2019 – 9<sup>th</sup> February 2021**    |
| **Testing set**    | **220**        | **10<sup>th</sup> February 2021 – 31<sup>st</sup> December 2021** |
| **Validation**     | **734**        | **2<sup>nd</sup> January 2019 – 31<sup>st</sup> December 2021**   |

**The same testing set data was used in the validation process. After retrieving the MPI data into the process, the cross-validation operator is used to validate the data. It has two sub-processes: training and testing. A model is trained using the training sub-process. After that, the trained model is used in the testing sub-process. During the testing phase, the model’s performance is assessed.**

![](63fd7190ad276_media/media/image2.png)

**Figure 2: Structure of validation process**

![](63fd7190ad276_media/media/image3.png)

**Figure 3: The sub-process model for validation process**

**Step 3: Design Network**

**The structure of the neural network is determined during this phase. The deep learning operators were coerced into participating in the process. There are three types of recurrent neural networks: input, hidden layers, and output. In this model, there is only one node for the input and output layer, as well as two hidden layers of size 50.** **The apply model operator is then applied to the data to apply deep learning.**

![](63fd7190ad276_media/media/image4.png)

**Figure 4: Structure of RNN model**

**Step 4: Train Network**

**The model was then trained over ten epochs with different sample values. Because the data is too large, 10 epochs are chosen to allow the data to pass the algorithm in smaller batches. The number of epochs is critical in determining the model that best describes the sample data with the least amount of inaccuracy.**

**The algorithm used to train the network is a linear regression algorithm that plots a line based on the dependent and explanatory variables. For each epoch, the training was based on root mean square error, deviance, mean absolute error, and R-squared. The results for the epochs training are shown in the table below.**

**Table 2: Scoring history**

| Epochs   | Iterations | Samples        | Training RMSE | Training Deviance | Training MAE | Training r^2 |
| -------- | ---------- | -------------- | ------------- | ----------------- | ------------ | ------------ |
| 1.00000  | **1**      | **440.00000**  | **5.52831**   | **30.56226**      | **4.66010**  | **0.85030**  |
| 2.00000  | **2**      | **880.00000**  | **4.77423**   | **22.79331**      | **4.14316**  | **0.88835**  |
| 3.00000  | **3**      | **1320.00000** | **3.55286**   | **12.62279**      | **3.03413**  | **0.93817**  |
| 4.00000  | **4**      | **1760.00000** | **3.27209**   | **10.70658**      | **2.78926**  | **0.94756**  |
| 5.00000  | **5**      | **2200.00000** | **1.97516**   | **3.90125**       | **1.52041**  | **0.98089**  |
| 6.00000  | **6**      | **2640.00000** | **1.53726**   | **2.36317**       | **1.20370**  | **0.98842**  |
| 7.00000  | **7**      | **3080.00000** | **1.33786**   | **1.78986**       | **0.99880**  | **0.99123**  |
| 8.00000  | **8**      | **3520.00000** | **1.76843**   | **3.12734**       | **1.46408**  | **0.98468**  |
| 9.00000  | **9**      | **3960.00000** | **1.11574**   | **1.24488**       | **0.83433**  | **0.99390**  |
| 10.00000 | **10**     | **4400.00000** | **1.88120**   | **3.53891**       | **1.69997**  | **0.98267**  |
| 10.00000 | **10**     | **4400.00000** | **1.11574**   | **1.24488**       | **0.83433**  | **0.99390**  |

**The data is trained on RMSE, deviation, MAE, and r^2 to determine the optimal model value. The RMSE statistic is trained to select the model with the lowest value, whereas the deviance statistic is trained to evaluate the goodness-of-fit statistic of the model. The MAE is trained to find the smallest value, which gives less weight to outliers, whereas the r2** **training seeks a high value close to one, indicating a good correlation between predicted and actual values. The best model values are 1.11574 for RMSE, 1.24488 for deviance, 0.83433 for MAE, and 0.99390 for squared correlation (r^2).**

**Step 5: Test Network**

**In the final phase, the testing set is used to evaluate the performance of the network model. To assess the performance of the model, the performance operator is added to the model process.**

# FINDINGS AND DISCUSSION

Figure 5 shows a graph of actual and forecasted stock prices over a 220-day period. Despite the sharp decline in the forecasted data, the actual stock price of MPI is not significantly different from the predicted stock price, as indicated by the graph.

![](63fd7190ad276_media/media/image5.png)

**Figure 5:** Graph of actual and predicted price

From the measurement score of MSE and RMSE, we can conclude that the forecasts were reasonably accurate.

**Table 3**: Summary of the result

<table>
<thead>
<tr class="header">
<th>Data</th>
<th>MSE</th>
<th>RMSE</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Target</td>
<td>1.3604233</td>
<td>1.1663718</td>
</tr>
<tr class="even">
<td>Output</td>
<td>1.2448792</td>
<td>1.1157416</td>
</tr>
<tr class="odd">
<td><p>R^2 = 0.9939022</p>
<p>MAE = 0.8343272</p></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

Based on the results of the testing set in Table 3, the target value for MSE is 1.3604233 and the output is 1.2448792, while the values for RMSE are 1.1663718 and 1.1157416, respectively. The model is more accurate when the output value is smaller. Based on the experimental results, we can conclude that RNN is an appropriate method for time series forecasting because it has a low mean squared error and root mean squared error, as well as a low mean absolute error.

# 

# CONCLUSION AND RECOMMENDATIONS

This study proposed the use of a Recurrent Neural Network to forecast stock prices. RNN is used because it is effective at dealing with time-based problems and can predict the future. This study looked into the feasibility and performance of RNN in predicting Malaysian Pacific Industries (MPI) stock price. According to the findings, RNN may be one of the best approaches for predicting the closing price in the stock market. The experimental results also revealed that the output MSE was only 8.49 percent different from the targeted MSE. With 95.66 percent accuracy, the model demonstrated that it is reasonably efficient at identifying patterns and capable of correctly anticipating and predicting stock price even when the market is volatile. RNN is also thought to be one of the most effective methods for predicting stock prices. However, several outcomes are possible, and the outcome is determined by the forecaster’s skill and experience. Changes in stock are also not established on a consistent basis, so actuality and accuracy vary.

There are several suggestions and ideas for future study that may be used to enhance. To begin, the data should be updated in real-time using the same method. It is strongly advised that deep learning models be developed by simultaneously feeding the most recent data to anticipate the market changes. The more data there is, the more accurate the forecast. More data may be used to construct strong and reliable models, resulting in forecasts with a lower margin of logic error. Following that, multiple tests must be performed on the neural network to determine the optimal number of epochs and hidden layers. In the future, a mathematical formula for determining the optimal number of epochs and hidden layers may be developed, allowing the most appropriate neural network to be created.

# REFERENCES

*Abu-Mustofa, Y. S. & Atiya, A. F. (1996). Introduction to Financial Forecasting. Applied Intelligence, 6(3). https://doi.org/10.1007/BF00126626*

Kramer, L. (2021). *How Is a Company's Stock Price and Market Capitalization Determined?* Investopedia. https://www.investopedia.com/ask/answers/how-companys-stock-price-and-market-cap-determined/

Kurnaz, G. & Demir, A. S. (2021). Prediction of SO2 and PM10 Air Pollutants Using a Deep Learning-Based Recurrent Neural Network: Case of Industrial City Sakarya. *Urban Climate,* 41 (101051). https://doi.org/10.1016/j.uclim.2021.101051

Li, K. (2021). *Predicting Stock Price Using Machine Learning.* Neptune Blog. https://neptune.ai/blog/predicting-stock-prices-using-machine-learning

Onnen, H. (2021). *Temporal Loops: Intro to Recurrent Neural Networks for Time Series Forecasting in Python*. Towards Data Science. https://towardsdatascience.com/temporal-loops-intro-to-recurrent-neural-networks-for-time-series-forecasting-in-python-b0398963dc1f

Pinsent, W. (2021). *Understanding Stock Prices and Values.* Investopedia. https://www.investopedia.com/articles/stocks/08/stock-prices-fool.asp

Polamuri, S. R., Srinivas, K. & Mohan, A. K. (2020). A Survey on Stock Market Prediction Using Machine Learning Techniques. In: Kumar A., Paprzycki M., Gunjan V. (Eds) *ICDSMLA 2019*. Lecture Notes in Electrical Engineering, 601 (pp. 923-931). Springer. https://doi.org/10.1007/978-981-15-1420-3\_101

*Stock market prediction definition*. (2018). Capital.Com. https://capital.com/stockmarket-prediction-definition

Yates, T. (2021). *4 Ways to Predict Market Performance.* Investopedia. https://www.investopedia.com/articles/07/mean\_reversion\_martingale.asp
