# Stock Prediction Based on Optimized LSTM and GRU Models

Authors: Ya Gao; Rong Wang; Enmin Zhou
Journal: Scientific Programming
 (2021)

---

## Abstract

Stock market prediction has always been an important research topic in the financial field. In the past, inventors used traditional analysis methods such as K-line diagrams to predict stock trends, but with the progress of science and technology and the development of market economy, the price trend of a stock is disturbed by various factors. The traditional analysis method is far from being able to resolve the stock price fluctuations in the hidden important information. So, the prediction accuracy is greatly reduced. In this paper, we design a new model for optimizing stock forecasting. We incorporate a range of technical indicators, including investor sentiment indicators and financial data, and perform dimension reduction on the many influencing factors of the retrieved stock price using depth learning LASSO and PCA approaches. In addition, a comparison of the performances of LSTM and GRU for stock market forecasting under various parameters was performed. Our experiments show that (1) both LSTM and GRU models can predict stock prices efficiently, not one better than the other, and (2) for the two different dimension reduction methods, both the two neural models using LASSO reflect better prediction ability than the models using PCA.


---

## Body

## 1. Introduction

The financial market is quite volatile and experiences periods of contraction as well as expansion. The stock market, as a major financial market, is likewise highly volatile. The stock market has the characteristics of high return which has attracted the majority of investors and high risk which puts pressure on investors to sell out at the wrong time. In order to reduce unnecessary losses and obtain higher trading profits, the investors usually except to predict the stock price trend. As a result, stock market forecasting has been a major research topic in the financial area and attracts the attention of investors. In the stock market, the factors affecting the rise and fall of stock prices are complex and diverse. It includes not only the impact of economic factors such as price indicator, circulation indicator, activity degree, and economic uncertainty but also the impact of noneconomic factors such as traders’ expectations, traders’ psychological factors, and political environment. Therefore, the prediction of stock price has always been a challenging task.According to the efficient market hypothesis [1], the stock price can be predicted according to the data of historical stocks. Furthermore, in recent years, since the increasing computing power and the decreasing data storage costs, especially the rise and development of innovative technologies such as big data, machine learning, reinforcement learning, and other optimization technologies, researchers have developed various models for predicting stock prices. Machine learning has been widely used in the capital market and plays an indispensable role in predicting future stock prices based on historical data. Traditional stock price forecasting models are mainly linear models, including autoregressive integrated moving average (ARIMA) model [2], multiple linear regression model, and exponential smoothing model [3, 4]. However, those (autoregressive integrated moving average,multiple linear regression model, and exponential smoothing model) linear models play an important role in promoting the progress and development of stock forecasting. Stock prices are typically noisy, fluctuating, and nonparametric, resulting in nonlinear and nonstationary characteristics in the stock market. The standard linear prediction model is unable to produce reliable stock predictions. With the development of deep learning methods, nonlinear neural networks are increasingly employed to predict the stock price for their higher accuracy.The artificial neural network (ANN) includes MP neural network and back propagation (BP) neural network. However, the structure of ANN model is too single and there are some problems: (1) over fitting leads to the weak ability of the model generalization, (2) local extrema leads to the decline of the prediction ability of the model, and (3) the gradient disappears or explodes due to the excessive weight of neurons in the optimization process, resulting in the failure of prediction. Therefore, relevant scholars introduce deep neural networks (DNN), including convolutional neural network (CNN), recurrent neural network (RNN), long-term and short-term memory neural network (LSTM), and gated recurrent neural network (GRU), to improve the problems existing in the ANN model, so as to improve the accuracy and efficiency of prediction.CNN is a type of neural network that has been increasingly popular in recent years. A one-dimensional CNN is a neural network that is designed to analyse image data efficiently. CNN can read and automatically extract the most significant features from the original input data for learning. This method feeds the network observed time series value as input and uses a multilayer network to predict the unobserved value. For example, Xu et al. [5] employed CNN to extract important stock features from stock market returns for forecasting stock market trends. Recurrent neural networks (RNN) such as long-term and short-term memory neural networks (LSTM) are another tool for predicting time series [6, 7]. LSTM accurately estimates time series data by using both the historical and the present stock data. In recent years, LSTM has been applied to stock market forecasting in different stock markets around the world. Chen et al. [8] used an LSTM model to predict China’s Shanghai and Shenzhen stock markets. Li et al. [9] introduced the stock indicator with investor sentiment based on the LSTM model to predict the CS1300 index value, and the research results showed that the model was better than the support vector machine method in prediction accuracy. However, this model does not reduce the dimension of stock indicator. Jiawei and Murata [10] attempted to identify the influencing factors of stock market trend prediction through the LSTM model, which used a preprocessing algorithm to reduce the dimension of stock features and a sentiment analyzer to present financial news for stock trend prediction. However, only one dimension reduction method is used, and there is no comparison with other methods. Hu [11] reduced the dimension of stock technical analysis indicators by PCA and LASSO methods before using the LSTM model to predict. The results demonstrated that compared with the LASSO-LSTM model, the PCA-LSTM model can significantly reduce data redundancy and enhance prediction accuracy. Although this work used different dimension reduction methods, it only used one model and did not compare with other models.Cho et al. [12] reduced the LSTM structure and created GRU, a new deep learning architecture that integrates long-term and short-term memory. GRU solves the problem of gradient disappearance and explosion in classic recurrent neural networks (RNNs) when learning long-term reliance. GRU has also been widely used in recent stock forecasting. Shen et al. [13] compared and predicted the trading signals of stock indicator based on the GRU model and SVM. The results demonstrated that the prediction accuracy of the two GRU models is higher than that of other models. However, the emotion indicator was not included in this study. Rahman et al. [14] used the stock data of Yahoo Finance mobile phone and GRU model to predict the stock price. The emotional indicators were not considered in this study, nor were compared with the performance of other models [15].In this paper, we integrate a variety of technical indicators, such as investor sentiment indicators and financial data based on the Shanghai Composite Index data. We use LASSO and PCA methods to perform dimension reduction on the multiple influencing factors of the extracted stock price. The LSTM and GRU models are then utilized in this paper to forecast the stock price. Most importantly, by comparing the accuracy and stability of the LASSO-LSTM, LASSO-GRU, PCA-LSTM, and PCA-GRU models, the optimal forecasting model may be recommended.


## 2. Methodology

### 2.1. LASSO

In empirical analysis, in order to minimize the model deviation due to the lack of important independent variables, we set multidimensional variables. The models need to find the set of independent variables with the strongest explanatory power to the dependent variables. That is, the models need to improve the interpretability and prediction accuracy through independent variable selection (indicator selection and field selection). Indicator selection is an extremely important problem in statistical modelling. LASSO is an estimation method that can simplify the indicator set. It is a compressed estimation. It gets a more refined model by constructing a penalty function, which makes it compress some coefficients and set some coefficients to zero. Therefore, it retains the advantage of subset contraction and is a biased estimation for dealing with complex collinear data.LASSO’s basic idea is to minimize the sum of squares of residuals under the constraint that the sum of absolute values of regression coefficients is less than a constant, so as to produce some regression coefficients strictly equal to 0 and obtain an interpretable model. LASSO adds penalty term to the ordinary linear regression model, and the LASSO estimation of the ordinary linear model is(1)βLasso^=argminβɛRdY−Xβ2,s.t.∑j=1dβj≤t,t≥0,which is equivalent to(2)βLasso^=argminβɛRdY−Xβ2+λ∑j=1dβj,where t and λ are said to be in one-to-one correspondence and they are the adjustment coefficients.Lett0=∑j=1dβj^OLS, and when t<t0, some coefficients will be compressed to 0, so as to reduce the dimension of X and the complexity of the model. Finally, the variable selection can be realized by controlling the adjustment coefficient through the λ.


### 2.2. PCA

Principal component analysis (PCA) is a dimension reduction statistical method. With the help of an orthogonal transformation, it transforms the original random vector whose components are related into a new random vector whose components are not related. This is expressed algebraically as transforming the covariance matrix of the original random vector into a diagonal matrix and geometrically as transforming the original coordinate system into a new orthogonal coordinate system. Then, the multidimensional variable system is reduced, so that it can be transformed into low-dimensional variable system with a high accuracy, and the low-dimensional system can be further transformed into one-dimensional system by constructing an appropriate value function.(1)
Standardized collection of original indicator datap-dimensional random vector x=x1,x2,x3,……,xpT and n samples xi=xi1,xi2,xi3,……,xipT, where i=1,2,…,nn>p. Then, we construct the sample array and carry out the following standardized transformation on the sample array elements: Zij=xij−xj¯/sj, where i=1,2,…,n,j=1,2,…,p, xj¯=∑i=1nxij/n, and Sj2=∑i=nnxij−xj¯2/n−1. Thus, the standardized matrix Z is obtained.(2)
Find the correlation coefficient matrix for the standardized matrixZ as(3)R=rijp×p=ZTZn−1,whererij=∑​zkj.zkj/n−1;i,j=1,2,…,p.(3)
Solve the characteristic equation of sample correlation matrixR by R−λIp=0 to get p-characteristic roots, thus determining the principal component. Determine the value of m according to ∑j=1mλj/∑j=1pλj≥0.85, so that the utilization rate of information can reach more than 85%. For each λj,j=1,2,…,m, we solve the equation Rb=λjb to obtain the unit eigenvector bjo.(4)
Convert the standardized indicator variable into the main componentUij=ziTbjo,j=1,2,…,m, where U1 is called the first principal component, U2 is called the second principal component, and so on. Up is called the p principal component.(5)
Evaluatem principal components comprehensively. The final evaluation value is obtained by weighted sum of m principal components, and the weight is the variance contribution rate of each principal component.


### 2.3. LSTM and GRU

LSTM is a special type of recurrent neural network (RNN). The RNN neural network model can recycle the weight parameters of neurons and can effectively employ past data information for prediction. However, RNN can only deal with certain short-term dependence and is prone to gradient explosion and gradient disappearance, that is, long-term dependence on historical data. In order to solve these problems, LSTM was proposed by Hochreiter and Schmidhuber [6] and then improved and promoted by Graves [16]. It has been widely used in a variety of challenges and has yielded impressive outcomes.Compared with the RNN model, the LSTM model introduces a cell stateCt and uses the input gate it, forget gate ft, and output gate Ot. The three gates are used to maintain and control information. At time t, xt is the input data, ht represents the current output, ct is the value from the input gate, tanh is hyperbolic tangent function, σ is the sigmoid function, W represents the matrix weight, and b is the bias. The operation formula of LSTM is as follows.Forget gate:(4)ft=σWf∗ht−1,xt+bf.Input gate:(5)it=σWi∗ht−1,xt+bi,c˜t=tanhWc∗ht−1,xt+bc,ct=ft∗ct−1+it∗c˜t.Output gate:(6)ot=σWo∗ht−1,xt+bo,ht=ot∗tanhct.The LSTM model is especially popular in the field of financial forecasting because it effectively deals with the redundancy of relevant information in historical data.GRU is one of the variants of RNN which is introduced by Cho et al. [12]. By introducing gating structure, it solves the problem that RNN is difficult to deal with long-distance information acquisition. Compared with LSTM, GRU is simplified and only update gate zt and reset gate rt are introduced. In GRU, the update (or input) gate decides how much input xt and previous output ht−1 to be passed to the next cell and the reset gate is used to determine how much of the past information to forget. The current memory content ensures that only the relevant information needs to be passed to the next iteration, which is determined by the weight W. The main operations in GRU are governed by the following formulae.Update gate:(7)zt=σWz∗ht−1,xt.Reset gate:(8)rt=σWr∗ht−1,xt.After resetting the gate and updating the gate, the candidate status value of GRU unit isht˜ and the final output status value is ht:(9)ht˜=tanhWh˜∗rt∗ht−1,xt,ht=1−zt∗ht−1+zt∗ht˜.


## 2.1. LASSO

In empirical analysis, in order to minimize the model deviation due to the lack of important independent variables, we set multidimensional variables. The models need to find the set of independent variables with the strongest explanatory power to the dependent variables. That is, the models need to improve the interpretability and prediction accuracy through independent variable selection (indicator selection and field selection). Indicator selection is an extremely important problem in statistical modelling. LASSO is an estimation method that can simplify the indicator set. It is a compressed estimation. It gets a more refined model by constructing a penalty function, which makes it compress some coefficients and set some coefficients to zero. Therefore, it retains the advantage of subset contraction and is a biased estimation for dealing with complex collinear data.LASSO’s basic idea is to minimize the sum of squares of residuals under the constraint that the sum of absolute values of regression coefficients is less than a constant, so as to produce some regression coefficients strictly equal to 0 and obtain an interpretable model. LASSO adds penalty term to the ordinary linear regression model, and the LASSO estimation of the ordinary linear model is(1)βLasso^=argminβɛRdY−Xβ2,s.t.∑j=1dβj≤t,t≥0,which is equivalent to(2)βLasso^=argminβɛRdY−Xβ2+λ∑j=1dβj,where t and λ are said to be in one-to-one correspondence and they are the adjustment coefficients.Lett0=∑j=1dβj^OLS, and when t<t0, some coefficients will be compressed to 0, so as to reduce the dimension of X and the complexity of the model. Finally, the variable selection can be realized by controlling the adjustment coefficient through the λ.


## 2.2. PCA

Principal component analysis (PCA) is a dimension reduction statistical method. With the help of an orthogonal transformation, it transforms the original random vector whose components are related into a new random vector whose components are not related. This is expressed algebraically as transforming the covariance matrix of the original random vector into a diagonal matrix and geometrically as transforming the original coordinate system into a new orthogonal coordinate system. Then, the multidimensional variable system is reduced, so that it can be transformed into low-dimensional variable system with a high accuracy, and the low-dimensional system can be further transformed into one-dimensional system by constructing an appropriate value function.(1)
Standardized collection of original indicator datap-dimensional random vector x=x1,x2,x3,……,xpT and n samples xi=xi1,xi2,xi3,……,xipT, where i=1,2,…,nn>p. Then, we construct the sample array and carry out the following standardized transformation on the sample array elements: Zij=xij−xj¯/sj, where i=1,2,…,n,j=1,2,…,p, xj¯=∑i=1nxij/n, and Sj2=∑i=nnxij−xj¯2/n−1. Thus, the standardized matrix Z is obtained.(2)
Find the correlation coefficient matrix for the standardized matrixZ as(3)R=rijp×p=ZTZn−1,whererij=∑​zkj.zkj/n−1;i,j=1,2,…,p.(3)
Solve the characteristic equation of sample correlation matrixR by R−λIp=0 to get p-characteristic roots, thus determining the principal component. Determine the value of m according to ∑j=1mλj/∑j=1pλj≥0.85, so that the utilization rate of information can reach more than 85%. For each λj,j=1,2,…,m, we solve the equation Rb=λjb to obtain the unit eigenvector bjo.(4)
Convert the standardized indicator variable into the main componentUij=ziTbjo,j=1,2,…,m, where U1 is called the first principal component, U2 is called the second principal component, and so on. Up is called the p principal component.(5)
Evaluatem principal components comprehensively. The final evaluation value is obtained by weighted sum of m principal components, and the weight is the variance contribution rate of each principal component.


## 2.3. LSTM and GRU

LSTM is a special type of recurrent neural network (RNN). The RNN neural network model can recycle the weight parameters of neurons and can effectively employ past data information for prediction. However, RNN can only deal with certain short-term dependence and is prone to gradient explosion and gradient disappearance, that is, long-term dependence on historical data. In order to solve these problems, LSTM was proposed by Hochreiter and Schmidhuber [6] and then improved and promoted by Graves [16]. It has been widely used in a variety of challenges and has yielded impressive outcomes.Compared with the RNN model, the LSTM model introduces a cell stateCt and uses the input gate it, forget gate ft, and output gate Ot. The three gates are used to maintain and control information. At time t, xt is the input data, ht represents the current output, ct is the value from the input gate, tanh is hyperbolic tangent function, σ is the sigmoid function, W represents the matrix weight, and b is the bias. The operation formula of LSTM is as follows.Forget gate:(4)ft=σWf∗ht−1,xt+bf.Input gate:(5)it=σWi∗ht−1,xt+bi,c˜t=tanhWc∗ht−1,xt+bc,ct=ft∗ct−1+it∗c˜t.Output gate:(6)ot=σWo∗ht−1,xt+bo,ht=ot∗tanhct.The LSTM model is especially popular in the field of financial forecasting because it effectively deals with the redundancy of relevant information in historical data.GRU is one of the variants of RNN which is introduced by Cho et al. [12]. By introducing gating structure, it solves the problem that RNN is difficult to deal with long-distance information acquisition. Compared with LSTM, GRU is simplified and only update gate zt and reset gate rt are introduced. In GRU, the update (or input) gate decides how much input xt and previous output ht−1 to be passed to the next cell and the reset gate is used to determine how much of the past information to forget. The current memory content ensures that only the relevant information needs to be passed to the next iteration, which is determined by the weight W. The main operations in GRU are governed by the following formulae.Update gate:(7)zt=σWz∗ht−1,xt.Reset gate:(8)rt=σWr∗ht−1,xt.After resetting the gate and updating the gate, the candidate status value of GRU unit isht˜ and the final output status value is ht:(9)ht˜=tanhWh˜∗rt∗ht−1,xt,ht=1−zt∗ht−1+zt∗ht˜.


## 3. Experiment Settings and Results

### 3.1. Data Source and Indicator Selection

In this paper, the data of the Shanghai Composite Index (000001) from April 11, 2007, to August 3, 2021, are selected as the experimental data. The data comes from NetEase Finance and Economics website, with a total of 3,481 days. In order to evaluate the training effect of the model, we divide the experimental data into training set and test set, of which 80% are used as one training set to train the stock prediction model and the other 20% are used as test sets to verify the prediction effect of the model. In addition, we use Intel Core i9-9900K CPU with memory 64 GB to finish the experiments.In the selection process of stock technical indicators, this paper considers the factors affecting the stock price as much as possible. Compared with other studies, this paper selects the open price, highest price, lowest price, trading volume, and other common technical indicators, such as OBV, KDJ, BIAS, RSI, CCI, and MFI, as well as other stock price judgment technical indicators and PSY indicators reflecting investors’ psychological mood. These indicators comprehensively reflect the information affecting stock price fluctuations and have the strong explanatory power for stock price fluctuations. The selected indicators are described in detail in Table1.Table 1
Explanation of technical indicator variables.
Primary indicatorSecondary indicatorVariableMomentum indicatorsADXZ1ADXRZ2BOPZ3CCIZ4DXZ5MACDZ6MACDEXTZ7MACDFIXZ8TRIXZ9MFIZ10MOMZ11PPOZ12ROCZ13RSIZ14WILLRZ15DPOZ16DMAZ17EXPMAZ18DMIZ19Volume indicatorOBV.OBVZ20OBV.MAOBVZ21ADOSCZ22Volatility indicatorsATRZ23NATRZ24TRANGEZ25Cycle indicatorsHT_DCPERIODZ26HT_DCPHASEZ27HT_ PHASORZ28HT_ SINEZ29Moving average indicatorBBIZ30SMAZ31Overbought and oversoldKDJ.KZ32KDJ.DZ33KDJ.JZ34BOLLZ35BIAS.BIAS1Z36BIAS.BIAS2Z37BIAS.BIAS3Z38WR.WR1Z39WR.WR2Z40Volume price indicatorASIZ41EMVZ42Energy indicatorPSYZ43MASS.MASSZ44MASS.MAMASSZ45MTMZ46BRAR-BRZ47BRAR-ARZ48VRZ49Stock price basic typeOpen priceZ50High priceZ51Low priceZ52VolumeZ53


### 3.2. Experimental Setup

Different superparameters have a significant impact on the prediction ability of LSTM and GRU models. Therefore, different superparameter data are set in the prediction to compare the prediction results. The number of neuron layers is set to 2 and 3, the number of neurons is set to 8, 16, and 32, the learning rate is usually set to 0.001, and the number of iterations is set to 1000. We can determine the most accurate prediction method by analyzing the prediction accuracy of the experimental results and the degree of fit of the trend between the predicted stock price and the historical stock price. The prediction accuracy is evaluated by mean square error function (MSE), root mean square error (RMSE), and mean absolute error (MAE) at different look-back values. The smaller the value of the three, the more accurate the forecast result is. The full specification of parameters used in these models is listed in Tables2–5.Table 2
Learning results of LASSO-LSTM.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101904.541043.641029.6357979.39331.295323.7622132.878038201272.188435.667824.2016904.954330.082522.6913307.999038302142.919246.291736.9945957.245430.939422.6934430.992838404629.9638868.043861.95415342.139673.089967.8341576.796638501214.774534.853623.3589881.026529.682121.0112766.215728101248.650835.336323.4574777.520827.884119.655495.182528201608.067640.100727.8215991.324431.485321.8147213.350028301251.198935.372322.9057858.066929.292819.7988320.693928401202.156134.672123.1705883.710129.727321.3382411.659528501206.704234.737623.1075850.248329.159020.6415490.2418316101031.035832.109721.8451778.945927.909619.9938376.8092316206555.537680.966375.54809468.341897.305493.4085779.2415316301254.391535.417423.0273733.877327.090218.65171189.4983316401015.528531.867421.3504872.576429.539420.94251603.382631650984.689131.379822.6411892.326329.871820.58842065.8919216101051.316932.424021.6973786.579628.046019.8271212.8883216201100.543033.174421.7411734.012227.092718.7960442.2047216301854.974543.069434.89381331.527636.490130.3015725.783021640984.731631.380420.8187803.866728.352520.93271080.9957216501006.728931.729021.1893846.484929.094421.38911346.372033210787.552228.063420.2811852.459029.196920.1759306.426533220816.338328.571620.29251199.147534.628723.9940636.017833230908.517030.141620.89151850.994843.023230.6120970.829333240844.316529.057120.14082720.098152.154632.72571291.5366332501057.641632.521421.60942061.006145.398329.48761356.4536232101117.402533.427622.2174799.080128.268019.8247454.650423220919.862630.329221.2240751.914627.421119.1840929.6708232301096.116233.107623.05842220.707847.124431.13581358.4529232401124.639533.535623.59111125.732233.551925.27331385.7139232501442.568837.981229.58021449.245938.069029.88981497.0255The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.Table 3
Learning results of PCA-LSTM.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101690.917141.120828.139710971.7773104.746372.824294.660238202939.533254.217536.524242278.7188205.6179176.8455198.658538303896.070162.418550.71395464.553773.922654.9697286.604138402811.640153.024935.259137730.9688194.2446160.7494410.722838502484.060849.840438.534530902.8711175.7921154.2262541.616428101619.030840.237227.521519627.638140.0987116.243260.606128202298.589147.943634.540317482.4219132.2211102.2909128.581528302054.780345.329732.686724208.5762155.5911125.7793191.262628402300.363547.962134.428127250.1621165.0762143.2569252.301428501614.692640.183226.503127625.6934166.2098140.6607313.6309316101022.417831.975323.600219104.1875138.2179105.8687278.2536316201744.881341.771831.4407100281.7656316.6730261.6820543.416831630932.114830.530623.07095806.194376.198458.5185846.4980316401688.805341.095131.048372460.7422269.1853225.78331090.1818316501101.541433.189525.58794054.689563.676442.89041654.781321610755.393627.484419.61687201.130484.859550.935393.157621620642.614025.349818.25083843.063761.992448.0259207.667221630734.115127.094619.650613394.8955115.736389.6245311.566821640737.102227.149619.18605955.741277.173459.1523427.408721650941.917530.690721.12894154.140664.452650.9237666.041633210599.829524.491419.429835741.8242189.0551124.2747514.523333220594.435124.381018.659745484.7734213.2716169.47781064.258933230455.746021.348216.178317365.6328131.778794.39151647.602033240805.406728.379723.09457770.926888.152958.47162789.528133250672.488725.932419.277591975.9453303.2754242.90422610.520923210495.863622.268016.660315152.0000123.093590.1184347.511523220627.717025.054318.43746696.089481.829650.5065721.567623230454.680121.323215.71415454.822873.856851.72491089.625723240548.592723.422117.234117594.1758132.6430105.71551437.524623250407.017520.174714.66088100.046490.000366.11211808.6861The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.Table 4
Learning results of LASSO-GRU.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101293.780235.969223.8084831.706228.839320.3225117.736938201444.412538.005425.5757910.988630.182621.6005249.311438301123.823233.523522.7495826.198628.743720.0954402.094738401482.583638.504327.8070943.866830.722420.9493508.539138501280.065735.778024.5826978.312731.278024.1942657.011328101377.298537.112024.9828834.275328.883820.080390.393628201136.973133.719022.8381799.183428.269819.5676200.843528301245.380935.290023.5962864.609129.404220.8457276.347928401500.872638.741126.32631093.249833.064323.7652350.204328501239.857535.211623.4242845.882529.084120.5453431.4074316101138.961533.748522.0896753.700427.453619.1470378.2382316201192.851434.537724.79561015.763931.871123.3095783.471731630985.859731.398421.1888883.428529.722522.01791198.1594316401038.719132.229221.6453802.512128.328619.86181622.0595316501116.042033.407222.7048792.447328.150420.00952032.0963216101233.546735.121923.9346833.034228.862319.9071207.6725216201104.472233.233623.2089869.426929.486021.8385433.1125216301114.160233.379022.0850796.863028.228820.7113668.5479216401072.471732.748623.0368802.499828.328419.72361073.651621650979.388231.295221.5892844.809529.065619.70651373.041933210924.589130.407120.6728831.014628.827320.2939277.719433220862.205629.363320.48951081.112332.880324.4835526.145733230907.103130.118221.28141127.495733.578223.5790797.842433240847.783429.116721.02601012.236531.815721.72141066.693633250810.523228.469720.33461196.492734.590424.47211349.854723210969.957631.144120.7674777.385127.881619.5336367.932123220985.321531.389821.8014829.021628.792720.1274890.587123230861.635729.353620.5396952.953430.869922.59851362.8664232401149.702733.907324.3771821.375728.659720.07521392.184323250883.307129.720520.3320833.843928.876420.48381596.1727The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.Table 5
Learning results of PCA-GRU.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101369.672737.009127.380029235.9590170.9853152.253872.125338202434.989549.345635.668312723.6592112.799293.6385168.968638302101.954145.847133.884734108.7148184.6854166.6717254.580238402574.525650.739837.520521786.1641147.6014127.3568333.745138501194.850534.566625.131229938.3730173.0271151.4763458.276028101196.423134.589323.042410500.0391102.469782.429054.225628202299.980247.958136.054229103.7676170.5983151.9113112.477728301426.401937.767726.662921631.0195147.0749121.6393167.518828401835.961842.848129.40479878.101699.388680.6860225.157228501503.736238.778026.767210440.7920102.180286.5779275.053931610943.231930.712121.840129209.2168170.9070148.9597116.738431620816.022628.566120.891512768.4766112.997799.0165255.290631630965.208131.067823.174714524.5684120.517999.9182815.159731640980.007231.305122.147712230.3311110.590884.34231096.213631650803.942028.353920.79949752.191498.753281.75051354.411021610776.476827.865319.34897601.507387.186674.742470.510321620784.206228.003721.068618093.4902134.5120119.4448176.998421630747.755327.345120.06758023.935189.576477.0783236.343521640720.655826.845019.340418241.6797135.0617117.5537378.046721650713.909426.719119.00319412.277397.016979.6811484.166433210453.667021.299515.65196122.331178.245361.3247469.718433220456.486821.365615.916911951.0684109.320990.6603977.371233230626.647225.032919.338411865.4746108.928886.89481483.567933240529.360423.007817.05539151.210995.662076.85831991.793033250504.711022.465816.904930787.4473175.4635153.60523289.747523210425.421020.625715.02016595.335981.211763.7287268.069223220653.841225.570318.744210183.2344100.912084.6054539.526323230508.943022.559816.23666050.969277.788061.9705856.654723240630.057525.100918.99567276.745685.303869.02451345.952223250623.701524.974018.07299594.841897.953380.54111678.2588The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.


### 3.3. Experimental Results

The experimental results of stock prediction of four models are shown in Tables2–5. Two different feature sets were obtained in this experiment. Set I is the data obtained from the LASSO dimension reduction method, and set II is the data obtained from the PCA dimension reduction method. These characteristic data are used to train LSTM and GRU models. In the experiment, different backtracking values were set. All parameter specifications used by the four models are shown in Tables 2–5. The results show that, through MAS, RMSE, and MAE indicators, both LSTM and GRU models can predict stock prices effectively, not one is more efficient than the other. However, for different dimension reduction methods, we find that all indicators (except the training time) show that the prediction results of the two neural network models using LASSO dimension reduction are mostly better than those using PCA dimension reduction data. In other words, under the same network model, the prediction performance of LASSO-LSTM model is better than PCA-LSTM and the prediction performance of LASSO-GRU is better than PCA-GRU.


## 3.1. Data Source and Indicator Selection

In this paper, the data of the Shanghai Composite Index (000001) from April 11, 2007, to August 3, 2021, are selected as the experimental data. The data comes from NetEase Finance and Economics website, with a total of 3,481 days. In order to evaluate the training effect of the model, we divide the experimental data into training set and test set, of which 80% are used as one training set to train the stock prediction model and the other 20% are used as test sets to verify the prediction effect of the model. In addition, we use Intel Core i9-9900K CPU with memory 64 GB to finish the experiments.In the selection process of stock technical indicators, this paper considers the factors affecting the stock price as much as possible. Compared with other studies, this paper selects the open price, highest price, lowest price, trading volume, and other common technical indicators, such as OBV, KDJ, BIAS, RSI, CCI, and MFI, as well as other stock price judgment technical indicators and PSY indicators reflecting investors’ psychological mood. These indicators comprehensively reflect the information affecting stock price fluctuations and have the strong explanatory power for stock price fluctuations. The selected indicators are described in detail in Table1.Table 1
Explanation of technical indicator variables.
Primary indicatorSecondary indicatorVariableMomentum indicatorsADXZ1ADXRZ2BOPZ3CCIZ4DXZ5MACDZ6MACDEXTZ7MACDFIXZ8TRIXZ9MFIZ10MOMZ11PPOZ12ROCZ13RSIZ14WILLRZ15DPOZ16DMAZ17EXPMAZ18DMIZ19Volume indicatorOBV.OBVZ20OBV.MAOBVZ21ADOSCZ22Volatility indicatorsATRZ23NATRZ24TRANGEZ25Cycle indicatorsHT_DCPERIODZ26HT_DCPHASEZ27HT_ PHASORZ28HT_ SINEZ29Moving average indicatorBBIZ30SMAZ31Overbought and oversoldKDJ.KZ32KDJ.DZ33KDJ.JZ34BOLLZ35BIAS.BIAS1Z36BIAS.BIAS2Z37BIAS.BIAS3Z38WR.WR1Z39WR.WR2Z40Volume price indicatorASIZ41EMVZ42Energy indicatorPSYZ43MASS.MASSZ44MASS.MAMASSZ45MTMZ46BRAR-BRZ47BRAR-ARZ48VRZ49Stock price basic typeOpen priceZ50High priceZ51Low priceZ52VolumeZ53


## 3.2. Experimental Setup

Different superparameters have a significant impact on the prediction ability of LSTM and GRU models. Therefore, different superparameter data are set in the prediction to compare the prediction results. The number of neuron layers is set to 2 and 3, the number of neurons is set to 8, 16, and 32, the learning rate is usually set to 0.001, and the number of iterations is set to 1000. We can determine the most accurate prediction method by analyzing the prediction accuracy of the experimental results and the degree of fit of the trend between the predicted stock price and the historical stock price. The prediction accuracy is evaluated by mean square error function (MSE), root mean square error (RMSE), and mean absolute error (MAE) at different look-back values. The smaller the value of the three, the more accurate the forecast result is. The full specification of parameters used in these models is listed in Tables2–5.Table 2
Learning results of LASSO-LSTM.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101904.541043.641029.6357979.39331.295323.7622132.878038201272.188435.667824.2016904.954330.082522.6913307.999038302142.919246.291736.9945957.245430.939422.6934430.992838404629.9638868.043861.95415342.139673.089967.8341576.796638501214.774534.853623.3589881.026529.682121.0112766.215728101248.650835.336323.4574777.520827.884119.655495.182528201608.067640.100727.8215991.324431.485321.8147213.350028301251.198935.372322.9057858.066929.292819.7988320.693928401202.156134.672123.1705883.710129.727321.3382411.659528501206.704234.737623.1075850.248329.159020.6415490.2418316101031.035832.109721.8451778.945927.909619.9938376.8092316206555.537680.966375.54809468.341897.305493.4085779.2415316301254.391535.417423.0273733.877327.090218.65171189.4983316401015.528531.867421.3504872.576429.539420.94251603.382631650984.689131.379822.6411892.326329.871820.58842065.8919216101051.316932.424021.6973786.579628.046019.8271212.8883216201100.543033.174421.7411734.012227.092718.7960442.2047216301854.974543.069434.89381331.527636.490130.3015725.783021640984.731631.380420.8187803.866728.352520.93271080.9957216501006.728931.729021.1893846.484929.094421.38911346.372033210787.552228.063420.2811852.459029.196920.1759306.426533220816.338328.571620.29251199.147534.628723.9940636.017833230908.517030.141620.89151850.994843.023230.6120970.829333240844.316529.057120.14082720.098152.154632.72571291.5366332501057.641632.521421.60942061.006145.398329.48761356.4536232101117.402533.427622.2174799.080128.268019.8247454.650423220919.862630.329221.2240751.914627.421119.1840929.6708232301096.116233.107623.05842220.707847.124431.13581358.4529232401124.639533.535623.59111125.732233.551925.27331385.7139232501442.568837.981229.58021449.245938.069029.88981497.0255The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.Table 3
Learning results of PCA-LSTM.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101690.917141.120828.139710971.7773104.746372.824294.660238202939.533254.217536.524242278.7188205.6179176.8455198.658538303896.070162.418550.71395464.553773.922654.9697286.604138402811.640153.024935.259137730.9688194.2446160.7494410.722838502484.060849.840438.534530902.8711175.7921154.2262541.616428101619.030840.237227.521519627.638140.0987116.243260.606128202298.589147.943634.540317482.4219132.2211102.2909128.581528302054.780345.329732.686724208.5762155.5911125.7793191.262628402300.363547.962134.428127250.1621165.0762143.2569252.301428501614.692640.183226.503127625.6934166.2098140.6607313.6309316101022.417831.975323.600219104.1875138.2179105.8687278.2536316201744.881341.771831.4407100281.7656316.6730261.6820543.416831630932.114830.530623.07095806.194376.198458.5185846.4980316401688.805341.095131.048372460.7422269.1853225.78331090.1818316501101.541433.189525.58794054.689563.676442.89041654.781321610755.393627.484419.61687201.130484.859550.935393.157621620642.614025.349818.25083843.063761.992448.0259207.667221630734.115127.094619.650613394.8955115.736389.6245311.566821640737.102227.149619.18605955.741277.173459.1523427.408721650941.917530.690721.12894154.140664.452650.9237666.041633210599.829524.491419.429835741.8242189.0551124.2747514.523333220594.435124.381018.659745484.7734213.2716169.47781064.258933230455.746021.348216.178317365.6328131.778794.39151647.602033240805.406728.379723.09457770.926888.152958.47162789.528133250672.488725.932419.277591975.9453303.2754242.90422610.520923210495.863622.268016.660315152.0000123.093590.1184347.511523220627.717025.054318.43746696.089481.829650.5065721.567623230454.680121.323215.71415454.822873.856851.72491089.625723240548.592723.422117.234117594.1758132.6430105.71551437.524623250407.017520.174714.66088100.046490.000366.11211808.6861The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.Table 4
Learning results of LASSO-GRU.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101293.780235.969223.8084831.706228.839320.3225117.736938201444.412538.005425.5757910.988630.182621.6005249.311438301123.823233.523522.7495826.198628.743720.0954402.094738401482.583638.504327.8070943.866830.722420.9493508.539138501280.065735.778024.5826978.312731.278024.1942657.011328101377.298537.112024.9828834.275328.883820.080390.393628201136.973133.719022.8381799.183428.269819.5676200.843528301245.380935.290023.5962864.609129.404220.8457276.347928401500.872638.741126.32631093.249833.064323.7652350.204328501239.857535.211623.4242845.882529.084120.5453431.4074316101138.961533.748522.0896753.700427.453619.1470378.2382316201192.851434.537724.79561015.763931.871123.3095783.471731630985.859731.398421.1888883.428529.722522.01791198.1594316401038.719132.229221.6453802.512128.328619.86181622.0595316501116.042033.407222.7048792.447328.150420.00952032.0963216101233.546735.121923.9346833.034228.862319.9071207.6725216201104.472233.233623.2089869.426929.486021.8385433.1125216301114.160233.379022.0850796.863028.228820.7113668.5479216401072.471732.748623.0368802.499828.328419.72361073.651621650979.388231.295221.5892844.809529.065619.70651373.041933210924.589130.407120.6728831.014628.827320.2939277.719433220862.205629.363320.48951081.112332.880324.4835526.145733230907.103130.118221.28141127.495733.578223.5790797.842433240847.783429.116721.02601012.236531.815721.72141066.693633250810.523228.469720.33461196.492734.590424.47211349.854723210969.957631.144120.7674777.385127.881619.5336367.932123220985.321531.389821.8014829.021628.792720.1274890.587123230861.635729.353620.5396952.953430.869922.59851362.8664232401149.702733.907324.3771821.375728.659720.07521392.184323250883.307129.720520.3320833.843928.876420.48381596.1727The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.Table 5
Learning results of PCA-GRU.
Number of layersNumber of neuronsLook-back valueTrain MSETrain RMSETrain MAETest MSETest RMSETest MAETrain time38101369.672737.009127.380029235.9590170.9853152.253872.125338202434.989549.345635.668312723.6592112.799293.6385168.968638302101.954145.847133.884734108.7148184.6854166.6717254.580238402574.525650.739837.520521786.1641147.6014127.3568333.745138501194.850534.566625.131229938.3730173.0271151.4763458.276028101196.423134.589323.042410500.0391102.469782.429054.225628202299.980247.958136.054229103.7676170.5983151.9113112.477728301426.401937.767726.662921631.0195147.0749121.6393167.518828401835.961842.848129.40479878.101699.388680.6860225.157228501503.736238.778026.767210440.7920102.180286.5779275.053931610943.231930.712121.840129209.2168170.9070148.9597116.738431620816.022628.566120.891512768.4766112.997799.0165255.290631630965.208131.067823.174714524.5684120.517999.9182815.159731640980.007231.305122.147712230.3311110.590884.34231096.213631650803.942028.353920.79949752.191498.753281.75051354.411021610776.476827.865319.34897601.507387.186674.742470.510321620784.206228.003721.068618093.4902134.5120119.4448176.998421630747.755327.345120.06758023.935189.576477.0783236.343521640720.655826.845019.340418241.6797135.0617117.5537378.046721650713.909426.719119.00319412.277397.016979.6811484.166433210453.667021.299515.65196122.331178.245361.3247469.718433220456.486821.365615.916911951.0684109.320990.6603977.371233230626.647225.032919.338411865.4746108.928886.89481483.567933240529.360423.007817.05539151.210995.662076.85831991.793033250504.711022.465816.904930787.4473175.4635153.60523289.747523210425.421020.625715.02016595.335981.211763.7287268.069223220653.841225.570318.744210183.2344100.912084.6054539.526323230508.943022.559816.23666050.969277.788061.9705856.654723240630.057525.100918.99567276.745685.303869.02451345.952223250623.701524.974018.07299594.841897.953380.54111678.2588The number of epochs is 1000, learning rate is 0.001, and the activation function istanh.


## 3.3. Experimental Results

The experimental results of stock prediction of four models are shown in Tables2–5. Two different feature sets were obtained in this experiment. Set I is the data obtained from the LASSO dimension reduction method, and set II is the data obtained from the PCA dimension reduction method. These characteristic data are used to train LSTM and GRU models. In the experiment, different backtracking values were set. All parameter specifications used by the four models are shown in Tables 2–5. The results show that, through MAS, RMSE, and MAE indicators, both LSTM and GRU models can predict stock prices effectively, not one is more efficient than the other. However, for different dimension reduction methods, we find that all indicators (except the training time) show that the prediction results of the two neural network models using LASSO dimension reduction are mostly better than those using PCA dimension reduction data. In other words, under the same network model, the prediction performance of LASSO-LSTM model is better than PCA-LSTM and the prediction performance of LASSO-GRU is better than PCA-GRU.


## 4. Conclusion

This study innovatively integrates a variety of technical indicators such as investor sentiment indicators and financial data and carries out dimension reduction on the multiple influencing factors of the extracted stock price through LASSO and PCA analysis approaches. This work carries out a comparison on the performances of LSTM and GRU for stock market forecasting under the different parameters. Our experimental results show that (1) both LSTM and GRU models can be used to predict stock prices effectively and (2) for different dimension reduction methods, the prediction results of the two neural network models using LASSO dimension reduction are mostly better than those using PCA dimension reduction data.



---

*Source: 4055281-2021-09-29.xml*