Comparative Analysis of Recurrent Neural Networks in Stock Price Prediction for Different Frequency Domains

: Investors in the stock market have always been in search of novel and unique techniques so that they can successfully predict stock price movement and make a big proﬁt. However, investors continue to look for improved and new techniques to beat the market instead of old and traditional ones. Therefore, researchers are continuously working to build novel techniques to supply the demand of investors. Different types of recurrent neural networks (RNN) are used in time series analyses, especially in stock price prediction. However, since not all stocks’ prices follow the same trend, a single model cannot be used to predict the movement of all types of stock’s price. Therefore, in this research we conducted a comparative analysis of three commonly used RNNs—simple RNN, Long Short Term Memory (LSTM), and Gated Recurrent Unit (GRU)—and analyzed their efﬁciency for stocks having different stock trends and various price ranges and for different time frequencies. We considered three companies’ datasets from 30 June 2000 to 21 July 2020. The stocks follow different trends of price movements, with price ranges of $30, $50, and $290 during this period. We also analyzed the performance for one-day, three-day, and ﬁve-day time intervals. We compared the performance of RNN, LSTM, and GRU in terms of R 2 value, MAE, MAPE, and RMSE metrics. The results show that simple RNN is outperformed by LSTM and GRU because RNN is susceptible to vanishing gradient problems, while the other two models are not. Moreover, GRU produces lesser errors comparing to LSTM. It is also evident from the results that as the time intervals get smaller, the models produce lower errors and higher reliability.


Introduction
A stock market is a place where companies issue their stocks to enlarge their business and investors can buy/sell the stocks to each other at specific prices. Investors around the world can buy a company's stock and enjoy yearly dividends for their shares. They can also sell their stocks at any time and can make a profit by selling at a price higher than their buying. The stock market is becoming a crucial investment venue, and the size of the market is growing every day. As of January 2021, the top 10 stock exchanges have $81.68 trillion worth of total capitalization [1]. Although stock market investment seems lucrative, predicting stock movements in competitive financial markets is a challenge, even for experienced traders and stock experts. However, stock price forecasting theory is often controversial: within a fraction of a second, the price of a stock can fluctuate so drastically that some individuals make huge sums of money, leaving the rest of the shareholders in financial ruin. Even after that, many experts and economists have been continuously trying to make stock predictions using a variety of methods for the past few decades. However, manually predicting stock price trends from stock data is a tedious task. However, now, with the advent of artificial intelligence, the automated method of predicting the stock market through big data and enhanced computing capabilities has become possible.
The main objective of this research is to predict the stock price using Recurrent Neural Networks (RNN) for three types of stocks: stocks for which price fluctuates significantly, stocks for which the price fluctuates moderately, and stocks for which the price fluctuates slightly. We did our research for three frequency domains: one-day, three-day, and five-day intervals. The motivation for using RNNs compared to other machine learning and statistical approaches is due to their effective prediction capability in time series analysis [2][3][4] and their efficient representation-learning capabilities; raw input transformation can be useful while learning complex functions [5]. The multiple levels of RNN can be utilized for multiple levels of features, which can represent abstract features derived from previous levels, and thus, the level of abstraction is increased. Compared to typical networks with one hidden layer, RNN can achieve a higher level of feature extraction by adding extra hidden layers [6,7]. The adaptability of RNNs in a complex financial market acts as the primary motivation to analyze the performance of RNNs with stock market data.
The outline of this article is as follows: Section 2 discusses related works in stock price prediction and briefly introduces RNN, LSTM, and GRU models; Section 3 explains the proposed methodology, Section 4 discusses the outcomes of the research, and Section 5 concludes with the overall findings and discusses future research directions.

Related Works
This research lies in the wide research area of Efficient Market Hypothesis proposed by Eugena F. Fama [8,9]. Fama said that we can just test whether data are appropriately reflected in costs with regards to an evaluating model. A 1970 survey isolates work on market productivity into three categories: (i) weak-form tests (how well do past returns foresee future returns?), (ii) semi strong-form tests (how rapidly do security costs reflect public data declarations?), and (iii) strong-form tests (do any financial backers have private data that is not completely reflected in market costs?). This work can also be categorized as big data research, as we have considered stock data of the timeframe 2000-2020 [10].
There are several methods in computer science as well as in economics to predict the future behavior of the market, which includes the direction of stock trend (up or down, i.e., bull market or bear market, respectively), costing of stock on intraday or interday, related risk and return, etc. A finite sequence or a collection of data points gathered at explicit periods indicates the time-series data of the stock market. It refers to information about the stock during the specific trading cycle of a stock exchange; such recorded information in its crude structure incorporates the starting and ending prices, the most noteworthy and least costs achieved, and the complete number of exchanged stocks, i.e., volume, for the specified period of trading. Several machine learning techniques have been applied to predict stock price movement [11][12][13]. In order to obtain appropriate predictions, this materialistic stock data have been merged with computational intellectual-based procedures [14][15][16] and different econometrics-based factual strategies [17][18][19]. The numerical techniques are probably going to be subject to the underlying presumptions; on the contrary, the AI approaches experience controlled interoperability, performing based on manually selected features, and over-fitting issues; this supports a mix of neural network (NN)-based deep learning strategies to upgrade the predictions of the stock market [20][21][22]. By using such NNs, the main characteristics of the supremely unstructured data can be extracted, which is useful in studying the hidden patterns of the movement of the stock price [23]. The stock market is affected by a number of events, and their impact is difficult to identify [24]; economic markets can be assessed by studying and analyzing such phenomena. The study provides evidence on the impact of political influence [25], data protection events [26], specific news and/or announcements [27], national policies [28], and other factors. The investigation through this angle is critical; likewise, the potential security perspectives on domains related to the monetary business sectors are urgent to keep up the integrity of the gathered data as well as their analysis [29,30]. In addition, it is important to understand the potential effects of several domains on financial market volatility.
In this research, we want to justify the use of recurrent neural networks using three popular versions of RNN: Simple RNN, Long Short Term Memory (LSTM), and Gated Recurrent Unit (GRU). Although many approaches are used to predict stock price, a model loses its viability when everyone starts using the same method. Therefore, investors are always in search of new approaches to outperform previous techniques. We do not only focus on the viability of neural networks in stock price forecasting, but also aim to show how different versions of RNN behave with different types of stocks. To serve that purpose, we considered Honda Motor Company's dataset as slightly fluctuating data whose stock price are in between USD 15 to USD 45 in the duration 2000-2020. We also considered Oracle Corporation as the moderately fluctuating data (price range USD 10 to USD 60), and Intuit Incorporation as the highly fluctuating data which had the lowest price of USD 20 and the highest price of USD 310. These three datasets were considered as proofs of concept, and the models can be applied to any other datasets as well. All datasets are collected from Yahoo Finance.

Overview of Recurrent Neural Networks
A Recurrent Neural Network (RNN) is an advanced form of neural networks that has internal memory that makes RNN capable of processing long sequences [31]. This makes RNN very suitable for stock price prediction, which involves long historical data. The following three subsections briefly discuss simple RNN, LSTM, and GRU models.

Simple RNN Model
RNN can provide considerably good prediction for the temporal stock data. The hidden states of RNN are given by Equations (1) and (2) [32].
where x t is the input vector at time t; b and c are bias values; W, U, and V denote inputto-hidden, hidden-to-hidden, and hidden-to-output weight matrices, respectively. While working with time-series data (like the stock market), an attention mechanism can be utilized that can divide the given data into parts so that decoder can utilize specific parts while generating new values. Figure 1 shows the generalized RNN architecture [33].

LSTM Model
LSTM network is a modified version of recurrent neural networks, which makes it easier to remember past data in memory. The vanishing gradient problem of RNN is resolved here. LSTM is well-suited to classification process and time series prediction given time lags of unknown duration. It trains the model using back-propagation. LSTM architecture consists of five main parts ( Figure 2) [34,35]: Cell state (c t )-1D vector of fixed shape with random value initialization. It contains the information that was present in the memory after the previous time step. • Forget gate ( f t )-changes the cell state, intending to eliminate non-important values from previous time steps. This helps the LSTM network to forget the irrelevant information that does not have any impact on the future price prediction. The values of these vectors are calculated by the Equations (3)- (7).
where x t is input vector, c t−1 is previous cell state, h t−1 is previous hidden state, W and U are input-to-hidden and hidden-to-hidden weight matrices, σ is the logistic sigmoid function, and denotes the element-wise multiplication.

GRU Model
Like LSTM, GRU is another improved version of the standard RNN. To solve the vanishing gradient problem of a standard RNN, GRU uses an update gate and reset gate. These are two vectors that decide what information should be passed to the output. The special thing about them is that they can be trained to keep information from long ago without washing it through time or to remove information that is irrelevant to the prediction. To explain the mathematics behind that process, we will examine a single unit of GRU given in Figure 3 [36,37]. The update gate helps the model to determine how much of the past information (from previous time steps) needs to be passed along to the future. This is very powerful because the model can decide to copy all the information from the past and eliminate the risk of the vanishing gradient problem. It is calculated by Equation (8).
A reset gate is used to decide how much of the past information to forget and is calculated by Equation (9).
The final output of the cell is calculated by the Equation (10).

Proposed Methodology
In this research, we have analyzed the effectiveness of Recurrent Neural Networks (Simple RNN, LSTM, and GRU) while predicting different types of stocks' price movements. Specifically, we considered three types of datasets-highly fluctuated, moderately fluctuated, and slightly fluctuated-and the prediction was done for three different time intervals: one day, three days, and five days. Figure 4 shows the system architecture of our proposed model.

Data Collection
As slightly fluctuating data, we considered Honda Motor Company (HMC), and the dataset was collected from Yahoo Finance [38]. We considered Orcale Corporation (ORCL)'s data as a moderately fluctuating dataset, and Intuit Inc. (INTU)'s data as a highly fluctuated dataset, which was also collected from Yahoo Finance [39,40]. For all of these datasets, we collected the data for 20 years (from 30 June 2000 to 21 July 2020) and have 5044 instances in each dataset. Each dataset contains seven attributes: date, open price, high price, low price, close price, adjusted close price, and traded volume.

Data Preprocessing
There were few null values in the collected datasets. As null value can disrupt the actual pattern of the price movement, it can handle the null values efficiently. A widely employed method to replace the null value is to use the mean of the previous 30 days' price. We used this method to solve this issue. Moreover, we collected data in the format of one-day time intervasl. Therefore, for our one-day proposed model, there was no preprocessing needed. However, for three-day and five-day models, we had to convert one-day time-interval data into three-day and five-day time-interval datasets. For this purpose, we made the following adjustments:

Model Design
To reiterate, we have predicted the stock price movements for three different stocks (HMC, ORCL, and INTU) of various fluctuation for three different frequencies (1 day, 3 days, and 5 days) using three neural networks models (Simple RNN, LSTM, and GRU). Therefore, we built a total of 27 models for our analysis. Each model was trained on 80% of the total data and was tested on 20% data. We used the corresponding neural network (RNN, LSTM, or GRU) as the first hidden layer, followed by a 20% dropout layer. Adam optimizer was used as the model optimizer, and MSE was used as loss function. After analyzing the effect of various neuron combinations in the hidden layers, we propose that the following neuron combinations work best on different trends in the three frequency domains. Tables 1-3 show the number of neurons used in each model.

Model Validation
To validate our model, we used four performance metrics: Mean Absolute Error (MAE), Mean Absolute Percentage Error (MAPE), Root Mean Squared Error (RMSE), and R squared value (R 2 ). MAE and MAPE only consider the amount of the error, not the sign. Therefore, it eliminates the possibility of positive and negative errors canceling each other otu. RMSE takes the square of the error before averaging it, and therefore, it gives more weight to the large error. It is therefore more useful when the high error is undesirable like stock price prediction. Finally, the R 2 value indicates the risk associated with a model while predicting a financial asset's price.
Mean Absolute Error (MAE) measures the absolute average error between the real data and predicted data and is calculated using the Equation (11).
where y j = actual value, y j = predicted value, N = total number test cases, and j = value ranging from 1 to N.

Mean Absolute Percentage Error (MAPE)
is the mean of absolute percentage error where the error is defined as the absolute difference between actual and predicted value [41]. MAPE is easy to understand as the error is represented in terms of percentage. It is calculated using Equation (12).
where A t = actual value, P t = predicted value, N = total number of test cases, and t = value ranging from 1 to N.

Root Mean Squared Error (RMSE)
is the rooted value of the squared average distance between the real data and the predicted data and is calculated using Equation (13).
where A t = actual value, P t = predicted value, N = total number of test cases. R Squared Value or the coefficient of determination, is an indicator of goodness of fit of a model. It indicates how close the regression line (i.e., the predicted value curve) is to the actual data values. The R squared value lies between 0 and 1, where 0 indicates that the model cannot capture the correlation among the input and output data, while 1 indicates that the model is perfectly fitted by the dataset. In the financial market, the R 2 value is widely used to determine the risk-adjusted return of a financial asset [3]. A higher R 2 value indicates lower risk associated with the model and vice versa.

Results and Discussions
In this section, we discuss the performance of RNN, LSTM, and GRU on the three datasets for the three different time-intervals. If anyone wants to replicate the results of this research, the authors are willing to share the codes and results. Full codes and results can be downloaded from GitHub (GitHub link: https://github.com/ehfahad/Comparative-Analysis-of-RNNs-in-Stock-Price-Prediction (accessed on 10 July 2021). Figures 5-7 show the actual value vs. predicted value curves for one-day timeinterval of HMC using RNN, LSTM, and GRU, respectively. All of the models predict very well; however, GRU performs slightly better than the other two models. The results are summarized in Table 4.   Actual value vs. predicted value curves for a three-day time-interval of HMC are shown in Figures 8-10 where GRU performs better than RNN and LSTM. However, all of them produced slightly larger errors than one-day time-interval, as shown in Table 5.      Figures 11-13 show the actual vs. predicted curves for five-day time intervals of HMC. GRU outperformed the other two models in this timeframe as well. The results are summarized in Table 6.

Performance Evaluation of ORCL
In case of ORCL dataset, LSTM performs better than RNN and GRU models. The actual vs. predicted value curves of a one-day interval are shown by Figures 14-16, of a three-day interval are shown by Figures 17-19, and of a five-day interval are shown by Figures 20-22. Tables 7-9 summarize the performance of one-day, three-day, and five-day intervals, respectively.              Tables 10-12 for one-day, three-day, and five-day intervals, respectively.

Performance Analysis
Based on the results of the previous three subsections, it is evident that RNN is outperformed by LSTM and GRU in stock price prediction. The reason is that both LSTM and GRU are advanced versions of traditional RNN and provide more controlling knobs, which control the flow input based on the training weights. This gives LSTM and GRU more flexibility to control the output and thus improve the performance. Another reason is that RNN suffers from the vanishing gradient problem, which may cause RNN to stop being trained if the learning rate is too low. However, with the help of update gate, forget gate, and reset gate, LSTM and GRU can avoid this problem [42]. Moreover, in terms of highly fluctuating data (INTU), although LSTM can closely identify the trend (R 2 value is close to GRU), it produces very large errors compared to GRU for all timeframes. Furthermore, in our research, we applied our methods on three types of data (highly fluctuating, moderately fluctuating, and slightly fluctuating) where highly fluctuating data are of the non-stationary type. High R 2 value indicates better fit to the regression model, and thus better prediction can be made, which is our ultimate goal in this research work. For non-stationary-type data in our analysis, we achieved very high rate of R 2 value for every method in each time frame, which is shown in Tables 10, 11, and 12. It can therefore be said our methods are robust to non-stationary data as well.

Conclusions and Future Works
Investors in the stock market are always searching for new techniques to outperform the stock market and make a good profit. Researchers around the world continue to conduct research in this area to meet the demand of the investors. In this research, we have tried to analyze the performance of different versions of recurrent neural networks (simple RNN, LSTM, and GRU) in time series analysis in the domain of stock price forecasting. We have experimented with three different types of stocks. One stock's price fluctuates very little, another one's price fluctuates moderately, and the other one has a very high fluctuation. We have also considered three timeframes to identify how these RNN models behave in each of these situations. GRU performs better in terms of slightly fluctuating and highly fluctuating data, while LSTM produces better results when the pattern is not too flat or too sharp. From the results, we can say that both LSTM and GRU give better performance than RNN because RNN suffers from vanishing gradient problem and also has less control on the input and output. Investors should therefore try updated versions of RNN instead of traditional RNN to predict stock price movement. Investors can also have up to 5 days to figure out their buying and selling points. On the other hand, policymakers can use this model to monitor the stock market and take any actions to control the market if there is any chance of chaos in it. More interestingly, our model can also be applied in the foreign exchange currency market as both markets are similar in nature.
In the future, an application can be built that can take the historical data of stock as input, and the user will have the options to predict future price directly from the application. We also plan to do the prediction for other timeframes (such as 7 days, 10 days, 14 days, etc.) as well. Moreover, we will explore how the prediction works when a combination of multiple deep learning models is used.

Conflicts of Interest:
The authors declare no conflict of interest.