# An Economic Forecasting Method Based on the LightGBM-Optimized LSTM and Time-Series Model

Authors: Jiehua Lv; Chao Wang; Wei Gao; Qiumin Zhao
Journal: Computational Intelligence and Neuroscience
 (2021)

---

## Abstract

Stock price prediction is very important in financial decision-making, and it is also the most difficult part of economic forecasting. The factors affecting stock prices are complex and changeable, and stock price fluctuations have a certain degree of randomness. If we can accurately predict stock prices, regulatory authorities can conduct reasonable supervision of the stock market and provide investors with valuable investment decision-making information. As we know, the LSTM (Long Short-Term Memory) algorithm is mainly used in large-scale data mining competitions, but it has not yet been used to predict the stock market. Therefore, this article uses this algorithm to predict the closing price of stocks. As an emerging research field, LSTM is superior to traditional time-series models and machine learning models and is suitable for stock market analysis and forecasting. However, the general LSTM model has some shortcomings, so this paper designs a LightGBM-optimized LSTM to realize short-term stock price forecasting. In order to verify its effectiveness compared with other deep network models such as RNN (Recurrent Neural Network) and GRU (Gated Recurrent Unit), the LightGBM-LSTM, RNN, and GRU are respectively used to predict the Shanghai and Shenzhen 300 indexes. Experimental results show that the LightGBM-LSTM has the highest prediction accuracy and the best ability to track stock index price trends, and its effect is better than the GRU and RNN algorithms.


---

## Body

## 1. Introduction

As of December 16, 2019, there are 3765 listed companies in China’s Shanghai and Shenzhen stock markets, with a total market value of 57779.362 billion yuan [1]. Investors are more and more involved in the financial market. However, due to the uncertainty of the stock market, the lack of professional skills of individual investors, and the over-professional technical analysis methods, the return of investors’ investment cannot meet the expectations. The first challenge is to select the correct features from many features that have a significant impact on stock price volatility. In the existing research, gray correlation analysis, correlation analysis, and other methods are commonly used to screen out the important features of the model [2]. Among them, gray relational analysis needs to determine the optimal value of the characteristics of the model, but it is difficult to be widely used because of its strong subjectivity and the difficulty in determining the optimal value of some characteristics. The second challenge is how to build a stock price forecasting model with high efficiency and accuracy. Stock price forecasting needs a huge amount of information in the process of modeling and forecasting, which puts forward higher requirements for the ability of the algorithm to process massive data [3]. At present, according to different theories of building stock price forecasting models, forecasting models can be divided into three categories: time-series model, machine learning model, and deep learning model [4]. Time-series models are such as exponential smoothing method, autoregressive moving average model, and autoregressive conditional heteroscedasticity model arch [5]. The model based on machine learning has been gradually applied to the study of stock price and constantly put forward some new models to predict the future trend of stock or some specific stock portfolio, such as index price. Machine learning models in data mining methods are such as random forest and support vector machine model [6]. Deep learning is a modern tool for automatic feature extraction and prediction. It has strong adaptability and self-learning ability and does not need to show specific network relationships and mathematical models. It has made some progress in intelligent speech and image classification technology [7]. There are a lot of schemes for the application of deep learning model in stock price forecasting model [8]. At present, researchers have applied deep learning theory to financial time-series forecasting [9]. However, the traditional model is prone to the problem of overfitting and time-series dependence of data, and RNN recurrent neural network can solve the problem. However, RNN has some problems such as gradient explosion and it cannot converge to the optimal solution [10]. Researchers are committed to applying deep learning to stock price forecasting [11]. Compared with traditional neural networks and machine learning models, deep learning has higher accuracy, more comprehensive explanation ability, and stronger learning ability to abstract problems [12].The contributions made by this paper are as follows. (1) This paper designs a LightGBM-optimized LSTM model to realize short-term stock price prediction. (2) The designed model can output a better result in predicting short-term stock price. (3) In order to verify its effectiveness compared with other deep network models such as RNN (Recurrent Neural Network) and GRU (Gated Recurrent Unit), the LightGBM-LSTM, RNN, and GRU are respectively used to predict the Shanghai and Shenzhen 300 indexes. Experimental results show that the LightGBM-LSTM has the highest prediction accuracy and the best ability to track stock index price trends, and its effect is better than the GRU and RNN algorithms.This article is divided into five parts. The first part is an introduction to the research background; the second part is an introduction to the current research status; the third part is an introduction to the LightGBM-LSTM model algorithm; the fourth part shows the prediction effect of the LightGBM-LSTM algorithm on stock prices, compared with the prediction effect of RNN and GRU algorithm; the fifth part is the conclusion of the article.


## 2. Related Work

Samreen et al. used hybrid financial systems (HFS) to model Karachi Stock Exchange index data kse100 for short-term forecasts [13]. The ANN is better in prediction than ARIMA and arch family models [14]. Researchers proposed series problems: lack of persuasion and the length of language interval, and multiple attributes are not used in prediction. In the verification, the actual transaction is used as the experimental data set. The research shows list method based on the average error percentage [15]. Many researches have been made to predict stock market prices using machine learning technology, including naive Bayesian, SVM, and random forest in the past 30 years. Jae and Young design a hybrid feature extraction algorithm and SVM combination and predict the trend of stock index showing that the prediction effect is better [16]. Based on the online news data, Vaishali and Sachin analysed and forecast the stock market status [17]. Datao et al. proposed that the traditional integrated learning model has the problem [18]. By short-term forecasts of the daily earnings of the standard, it is possible to provide considerable net profit for reasonable decision-making [19]. Co et al. used two methods to predict VN index of Vietnam stock exchange (macroeconomic indicators for developing economies) by using two methods: time-series model ARIMA and LSTM RNN model in-depth learning method [20]. Cheng compared the prediction effect of ARIMA model and arch model in Hong Kong stock index. The research shows that there is no significant difference between the two models in application, but the better model should be selected in different periods [21]. Luo and Sattayatham studied the yield series of Shanghai Composite Index, proposed fuzzy GARCH model, and compared the influence of distribution model and asymmetric model on the prediction accuracy of nonlinear return rate. The results show that volatility has greater influence on prediction effect of fuzzy GARCH model than distribution hypothesis [22]. Dai and Lan built the Shanghai stock market sentiment composite index by combining the text data of stock market forum and transaction data and used neural network to predict the price change of stock market. The research shows that the accuracy of stock index trend prediction has been significantly improved after the introduction of sentiment index [23]. Deng and Li optimized the random forest algorithm by using grid search parameter optimization method and constructed a stock prediction model based on pure technical indexes and parameters optimization random forest. With the original random forest, the comparison of decision tree and SVM classification model shows that the accuracy and AUC value of the model evaluation of the stochastic forest stock prediction model after parameter optimization are improved compared with other models [24]. Han et al. proposed an improved differential evolution algorithm and introduced local operators and mixed mutation strategies to accelerate the convergence rate and enhance the local search ability of the algorithm. The paper designs the RBF neural network as stock index prediction model. [25].


## 3. Construction of Stock Forecasting Model Based on LSTM and Time-Series Model

### 3.1. Introduction of Variable LSTM Model

The data flow direction and calculation process of the three-gate structure of the neural module of LSTM are analysed in detail as follows.


#### 3.1.1. Forget the Door

LSTM network calculates a valuef from 0 to 1 for H−1 and X and uses the value f to decide whether to “forget” the information value of C−1 (0 means to discard completely, 1 means to save completely). The control function of “forget gate” is as follows:(1)ft=σWf·ht−1,xt+bf.


#### 3.1.2. Input Gate

The control function of “input gate” is as follows:(2)it=σWt·ht−1,xt+bi,Ct˜=tanhWc·ht−1,xc+bc.The sigmoid layer and tanh layer are combined to generate a new update state.


#### 3.1.3. Cellular State

The update function of cell state is as follows:(3)Ct=ftCt−1+ic·Ct˜.The specific update method is to multiplyf by the old cell state C−1. C is the new candidate value to determine how many state values need to be updated.


#### 3.1.4. Output Door

The output resulto is calculated by the output gate of LSTM, and the cell state C at t time is processed by tanh. It determines which information in OT is finally output, and the number of control functions of output gate is as follows:(4)ot=σWo·ht−1,xt+bo,ht=ottanhCt.Among them, the function of output gate is to output the StateC of control unit and transfer it to the next neural unit.


#### 3.1.5. Univariate Long-Term and Short-Term Memory Network

(5)Xt=st−W,st−W+1,…,st−2,st−1,where W is the width of the observation window.


#### 3.1.6. Multivariable Long-Term and Short-Term Memory Network

If the number of variables isn and the original stock price sequence is added, let the input time-series be(6)Si=s1i,s2i,…,sTi,i=0,1,2,…,N,where i is the factor serial number, assuming that 0 represents the stock price sequence and 1 to n represents the multivariable sequence, the matrix form of the input at time t is expressed as follows:(7)Xt=st−W0st−W+10⋯st−10st−W0st−W+11⋯st−11⋮⋮⋱⋮st−W0st−W+1N⋯st−1N.In multivariable LSTM, a variable sequence has a mapping relationship not only with its own hidden layer, but also with other multiple variable hidden layers. The network mapping is more abundant, and the performance of the model is improved. However, the network structure is more complex than single variable LSTM model.


### 3.2. Overview of Research Framework

The LSTM model is used to capture the time autocorrelation of stock price. In addition, according to the characteristics of prediction task, a deep random subspace learning data mining model is proposed. Finally, on the basis of the above, this paper proposes a stock forecasting framework using multitask deep learning model. The framework of this method is shown in Figure1.Figure 1
Stock forecast framework.


### 3.3. Data Collection and Preprocessing

The data used in this paper are collected from the CSI 300 index of wind financial terminal, with the code of 000300. HSO data interval is 3605 trading days from April 8, 2005, to February 6, 2020. Among them, the training sample interval is from April 8, 2005, to December 31, 2014, with a total of 2366 samples. The forecast sample range is from January 1, 2015, to February 6, 2020, with a total of 1239 samples.In the process of missing data interpolation, the data filling value will be calculated according to the following formula:(8)Inputh=α1avgh+α2β1Vbackw+β2Vforw.Among them,α is an experimental weight, which can be used to optimize the filling effect. The weight is roughly set in this experiment to make α1=α2=0.5. It is worth noting that the interpolation process has limited the prediction performance compared with other models. During the experiment, when other data interpolation methods are implemented by changing the value of α, the results are similar.


### 3.4. Stock Time Prediction Model Based on LSTM

This paper mainly studies the prediction of stock price, that is, to establish the relationship model between stock and other variables. Suppose that it is necessary to predict the price of the specified coordinate position (x, y) at time t, which can be expressed by (9)Fx,y,z=fXx,y,t∘.After the training set organization process, the training samples with time stamp will automatically form a time-series. Based on the limited short memory of previous input information, RNN model realizes the mechanism of predicting what will happen in the next step by using newly learned knowledge and new input information. Therefore, RNN model has more advantages than other artificial neural network models in dealing with air quality prediction.However, the traditional RNN models often encounter the problem of gradient explosion or gradient disappearance, because the gradient vector components may grow or decay exponentially in the long sequence training process. As a variant of RNN model, LSTM model is designed to solve the problems of RNN model through gating mechanism.The architecture of LSTM neural network model used in this study is shown in Figure2. Among them, memory cell layer is the main difference between LSTM model and traditional RNN model. It plays the role of connecting information conveyor belt, which means LSTM model can memorize information “for a long time”. “The introduction of memory cell layer improves the gradient training process by using memory cells to determine the receiving degree of previously acquired knowledge and the updating degree of hidden state.” The “gate” mechanism is designed to adjust the extent to which information is added or removed. The LSTM model in Figure 2 has three gates.Figure 2
Long- and short-time memory model.As Elman [5] introduced, in the traditional RNN network, the previous hidden state can be simply updated by using the following:(10)Ht+1=fHt,It+1,(11)Mt˜=tanhUMIt+VMHt−1.The input state is optimized by training and the coefficient matrix of the last hidden state. According to the values of the two gate vectors at timet, the final value of Mt can be calculated according to the following:(12)Mt=Gf,t⊙Mt−1+Gi,t⊙Mt˜.Among them, operation 0 represents the multiplication element by element between the state of memory cells and the “gate” vector.Then, the hidden stateHt can be obtained through Mt (13):(13)Ht=Go,t⊙tanhMt.In addition, the transformation equations of the above three “gate” vectors are as follows:(14)Gi,t=σUiXt+ViHt−1+PiMt−1,(15)Gf,t=σUfXt+VfHt−1+PfMt−1,(16)Go,t=σUoXt+VoHt−1+PoMt,Sigmoid function fσ, namely,(17)fσx=11+e−x.So far, the LSTM model unit at timet consists of a hidden state Ht, a memory cell Ct, and three “gate” vectors, namely an output gate Go, t, a forgetting gate Gf, t, and an input gate Gi, t.In the process of model training, the back time propagation mechanism is used to adjust the parametersM, N, and P based on the loss function constructed by minimizing the mean square error(18)LMSE=1N∑t=tot+NFt−TrueVt22,(19)Ft=fFHt=WFHt+bF.On this basis, a data mining model called LSTM-drsl is developed, which integrates LSTM model and stochastic subspace integration method. The RSE method is introduced into the task of air quality prediction. In this framework, we use bootstrap methods: the random space is constructed by randomly samplingnfnf≤NF emission features in the feature selection process, and N random subspaces can be obtained by repeating the feature sampling process n times. For each random subspace, emission characteristics can be combined with air quality and meteorological characteristics to train LSTM predictors.


### 3.5. Stock Forecasting Model Based on Multitask Learning Method

At present, the purpose is to achieve the goal in a single task; in a single task, through the data input of a specific task, specific results can be obtained. Different from these methods, multitask learning combines input shared data. The comparison of the two learning structures is shown in Figure3.Figure 3
Structural diagram of single-task learning and multitask learning.This paper combines multitask learning with LSTM to realize stock forecasting. In this process, MT-LSTM extended framework based on multitask sharing mechanism organized by spatiotemporal characteristics is adopted. Multitask sharing mechanism enables input layer and LSTM layer to share information among multiple prediction tasks. At timet, for each task T ∈ {A, B, C, D}, the input information represented by XtT is composed of a shared part XtS and a specific part (such as XtA) of task T, which are combined by connection operation ⊙. Then, the connected input XtT and the latest hidden state Ht−1T and the next hidden state XtT are obtained through formulas (18) and (19) using the shared weight matrix displayed in the black rectangle. Finally, the predicted value FtT of each target task is determined by the hidden state just calculated and the input from the combined connection. The most critical change is to transform the training objective into optimizing multiple prediction values at the same time. The loss function of mt-LSTM framework consists of its expression (20) as follows:(20)LMSEMT=∑n=1NtaskλnLMSEForecastn,TrueValuen.When training the training samples under the same time stamp, it will cycle in the task set in a random way until the traversal is completed.


## 3.1. Introduction of Variable LSTM Model

The data flow direction and calculation process of the three-gate structure of the neural module of LSTM are analysed in detail as follows.


### 3.1.1. Forget the Door

LSTM network calculates a valuef from 0 to 1 for H−1 and X and uses the value f to decide whether to “forget” the information value of C−1 (0 means to discard completely, 1 means to save completely). The control function of “forget gate” is as follows:(1)ft=σWf·ht−1,xt+bf.


### 3.1.2. Input Gate

The control function of “input gate” is as follows:(2)it=σWt·ht−1,xt+bi,Ct˜=tanhWc·ht−1,xc+bc.The sigmoid layer and tanh layer are combined to generate a new update state.


### 3.1.3. Cellular State

The update function of cell state is as follows:(3)Ct=ftCt−1+ic·Ct˜.The specific update method is to multiplyf by the old cell state C−1. C is the new candidate value to determine how many state values need to be updated.


### 3.1.4. Output Door

The output resulto is calculated by the output gate of LSTM, and the cell state C at t time is processed by tanh. It determines which information in OT is finally output, and the number of control functions of output gate is as follows:(4)ot=σWo·ht−1,xt+bo,ht=ottanhCt.Among them, the function of output gate is to output the StateC of control unit and transfer it to the next neural unit.


### 3.1.5. Univariate Long-Term and Short-Term Memory Network

(5)Xt=st−W,st−W+1,…,st−2,st−1,where W is the width of the observation window.


### 3.1.6. Multivariable Long-Term and Short-Term Memory Network

If the number of variables isn and the original stock price sequence is added, let the input time-series be(6)Si=s1i,s2i,…,sTi,i=0,1,2,…,N,where i is the factor serial number, assuming that 0 represents the stock price sequence and 1 to n represents the multivariable sequence, the matrix form of the input at time t is expressed as follows:(7)Xt=st−W0st−W+10⋯st−10st−W0st−W+11⋯st−11⋮⋮⋱⋮st−W0st−W+1N⋯st−1N.In multivariable LSTM, a variable sequence has a mapping relationship not only with its own hidden layer, but also with other multiple variable hidden layers. The network mapping is more abundant, and the performance of the model is improved. However, the network structure is more complex than single variable LSTM model.


## 3.1.1. Forget the Door

LSTM network calculates a valuef from 0 to 1 for H−1 and X and uses the value f to decide whether to “forget” the information value of C−1 (0 means to discard completely, 1 means to save completely). The control function of “forget gate” is as follows:(1)ft=σWf·ht−1,xt+bf.


## 3.1.2. Input Gate

The control function of “input gate” is as follows:(2)it=σWt·ht−1,xt+bi,Ct˜=tanhWc·ht−1,xc+bc.The sigmoid layer and tanh layer are combined to generate a new update state.


## 3.1.3. Cellular State

The update function of cell state is as follows:(3)Ct=ftCt−1+ic·Ct˜.The specific update method is to multiplyf by the old cell state C−1. C is the new candidate value to determine how many state values need to be updated.


## 3.1.4. Output Door

The output resulto is calculated by the output gate of LSTM, and the cell state C at t time is processed by tanh. It determines which information in OT is finally output, and the number of control functions of output gate is as follows:(4)ot=σWo·ht−1,xt+bo,ht=ottanhCt.Among them, the function of output gate is to output the StateC of control unit and transfer it to the next neural unit.


## 3.1.5. Univariate Long-Term and Short-Term Memory Network

(5)Xt=st−W,st−W+1,…,st−2,st−1,where W is the width of the observation window.


## 3.1.6. Multivariable Long-Term and Short-Term Memory Network

If the number of variables isn and the original stock price sequence is added, let the input time-series be(6)Si=s1i,s2i,…,sTi,i=0,1,2,…,N,where i is the factor serial number, assuming that 0 represents the stock price sequence and 1 to n represents the multivariable sequence, the matrix form of the input at time t is expressed as follows:(7)Xt=st−W0st−W+10⋯st−10st−W0st−W+11⋯st−11⋮⋮⋱⋮st−W0st−W+1N⋯st−1N.In multivariable LSTM, a variable sequence has a mapping relationship not only with its own hidden layer, but also with other multiple variable hidden layers. The network mapping is more abundant, and the performance of the model is improved. However, the network structure is more complex than single variable LSTM model.


## 3.2. Overview of Research Framework

The LSTM model is used to capture the time autocorrelation of stock price. In addition, according to the characteristics of prediction task, a deep random subspace learning data mining model is proposed. Finally, on the basis of the above, this paper proposes a stock forecasting framework using multitask deep learning model. The framework of this method is shown in Figure1.Figure 1
Stock forecast framework.


## 3.3. Data Collection and Preprocessing

The data used in this paper are collected from the CSI 300 index of wind financial terminal, with the code of 000300. HSO data interval is 3605 trading days from April 8, 2005, to February 6, 2020. Among them, the training sample interval is from April 8, 2005, to December 31, 2014, with a total of 2366 samples. The forecast sample range is from January 1, 2015, to February 6, 2020, with a total of 1239 samples.In the process of missing data interpolation, the data filling value will be calculated according to the following formula:(8)Inputh=α1avgh+α2β1Vbackw+β2Vforw.Among them,α is an experimental weight, which can be used to optimize the filling effect. The weight is roughly set in this experiment to make α1=α2=0.5. It is worth noting that the interpolation process has limited the prediction performance compared with other models. During the experiment, when other data interpolation methods are implemented by changing the value of α, the results are similar.


## 3.4. Stock Time Prediction Model Based on LSTM

This paper mainly studies the prediction of stock price, that is, to establish the relationship model between stock and other variables. Suppose that it is necessary to predict the price of the specified coordinate position (x, y) at time t, which can be expressed by (9)Fx,y,z=fXx,y,t∘.After the training set organization process, the training samples with time stamp will automatically form a time-series. Based on the limited short memory of previous input information, RNN model realizes the mechanism of predicting what will happen in the next step by using newly learned knowledge and new input information. Therefore, RNN model has more advantages than other artificial neural network models in dealing with air quality prediction.However, the traditional RNN models often encounter the problem of gradient explosion or gradient disappearance, because the gradient vector components may grow or decay exponentially in the long sequence training process. As a variant of RNN model, LSTM model is designed to solve the problems of RNN model through gating mechanism.The architecture of LSTM neural network model used in this study is shown in Figure2. Among them, memory cell layer is the main difference between LSTM model and traditional RNN model. It plays the role of connecting information conveyor belt, which means LSTM model can memorize information “for a long time”. “The introduction of memory cell layer improves the gradient training process by using memory cells to determine the receiving degree of previously acquired knowledge and the updating degree of hidden state.” The “gate” mechanism is designed to adjust the extent to which information is added or removed. The LSTM model in Figure 2 has three gates.Figure 2
Long- and short-time memory model.As Elman [5] introduced, in the traditional RNN network, the previous hidden state can be simply updated by using the following:(10)Ht+1=fHt,It+1,(11)Mt˜=tanhUMIt+VMHt−1.The input state is optimized by training and the coefficient matrix of the last hidden state. According to the values of the two gate vectors at timet, the final value of Mt can be calculated according to the following:(12)Mt=Gf,t⊙Mt−1+Gi,t⊙Mt˜.Among them, operation 0 represents the multiplication element by element between the state of memory cells and the “gate” vector.Then, the hidden stateHt can be obtained through Mt (13):(13)Ht=Go,t⊙tanhMt.In addition, the transformation equations of the above three “gate” vectors are as follows:(14)Gi,t=σUiXt+ViHt−1+PiMt−1,(15)Gf,t=σUfXt+VfHt−1+PfMt−1,(16)Go,t=σUoXt+VoHt−1+PoMt,Sigmoid function fσ, namely,(17)fσx=11+e−x.So far, the LSTM model unit at timet consists of a hidden state Ht, a memory cell Ct, and three “gate” vectors, namely an output gate Go, t, a forgetting gate Gf, t, and an input gate Gi, t.In the process of model training, the back time propagation mechanism is used to adjust the parametersM, N, and P based on the loss function constructed by minimizing the mean square error(18)LMSE=1N∑t=tot+NFt−TrueVt22,(19)Ft=fFHt=WFHt+bF.On this basis, a data mining model called LSTM-drsl is developed, which integrates LSTM model and stochastic subspace integration method. The RSE method is introduced into the task of air quality prediction. In this framework, we use bootstrap methods: the random space is constructed by randomly samplingnfnf≤NF emission features in the feature selection process, and N random subspaces can be obtained by repeating the feature sampling process n times. For each random subspace, emission characteristics can be combined with air quality and meteorological characteristics to train LSTM predictors.


## 3.5. Stock Forecasting Model Based on Multitask Learning Method

At present, the purpose is to achieve the goal in a single task; in a single task, through the data input of a specific task, specific results can be obtained. Different from these methods, multitask learning combines input shared data. The comparison of the two learning structures is shown in Figure3.Figure 3
Structural diagram of single-task learning and multitask learning.This paper combines multitask learning with LSTM to realize stock forecasting. In this process, MT-LSTM extended framework based on multitask sharing mechanism organized by spatiotemporal characteristics is adopted. Multitask sharing mechanism enables input layer and LSTM layer to share information among multiple prediction tasks. At timet, for each task T ∈ {A, B, C, D}, the input information represented by XtT is composed of a shared part XtS and a specific part (such as XtA) of task T, which are combined by connection operation ⊙. Then, the connected input XtT and the latest hidden state Ht−1T and the next hidden state XtT are obtained through formulas (18) and (19) using the shared weight matrix displayed in the black rectangle. Finally, the predicted value FtT of each target task is determined by the hidden state just calculated and the input from the combined connection. The most critical change is to transform the training objective into optimizing multiple prediction values at the same time. The loss function of mt-LSTM framework consists of its expression (20) as follows:(20)LMSEMT=∑n=1NtaskλnLMSEForecastn,TrueValuen.When training the training samples under the same time stamp, it will cycle in the task set in a random way until the traversal is completed.


## 4. Empirical Analysis of Stock Forecasting Based on LSTM and Time-Series Model

### 4.1. Deep Learning Model Based on Long Short Memory Network

In this paper, through many experiments, as shown in Figure4, when sequences are set to true, the uniform function initializes the weight. Finally, the model output layer is set. The output layer consists of two fully connected neural network layers, density, which outputs the prediction data of LSTM neural network layer, that is, the closing price of the stock index. Therefore, the number of neurons in the last density output layer is set to 1. The above model building process uses the sequential model of Keras. After building, the model is compiled. When training the model, we use Adam optimization algorithm, loss function as mean square error training model, two full-connection layers, and two activation functions. The activation function uses the default tanh and uses small batch training mode, the initial learning rate is set to 0.001, the number of iterations epoch is 300, and the batch size is 50. Due to the different factors of different stock markets, in order to ensure the rationality of the experiment and the stability of the prediction effect of the research model on different data sets, this paper makes an empirical study on the data of Shanghai Composite Index (00000 1. SH, pp. China Securities 100 (399 903. SZ) and Shanghai Shenzhen 300 index. According to the experimental results of variable importance score in Chapter 4, the first four input features with the highest importance, trading market OHLC, are selected.Figure 4
LSTM network structure map.


### 4.2. Optimization of Structural Design Parameters for Long- and Short-Time Memory Networks

In order to study the influence of the length of prediction window on the prediction accuracy, 32 neurons are connected in the full-connection layer; the dropout layer parameter is 0.2, the epoch number is 300, the loss function is the average error loss function, the optimization algorithm selects Adam, and the batch size is set to 50. And try to use different forecast period seqjen, including 7 days, 14 days, 21 days, 30 days, and 60 days to forecast the stock price of the next trading day. The change trend of verification set RMSE with window seqjen is as follows.As can be seen from Figure5, with the length of the data window, SEQ_, and with the increase of length, the RMSE of the test set increases gradually and decreases at 21 days. The prediction error of the test set is in the data window length SEQ_. When length is 7, it is the minimum, so the window length of the model is SEQ_. Len is set to 7 days. Table 1 shows the combination of different activation functions and optimization algorithms.Figure 5
Trend of validation set RMSE with WindowW.Table 1
Combination of different activation functions and optimization algorithms.
Activation functionOptimization algorithmClosing priceRMSEMaeTraining errorTest errorTraining errorTest errorReLURMSprop0.012780.012720.009760.00970ReLUAdagrad0.013070.013670.009040.00867ReLUAdadelta0.014700.015290.010130.00968ReLUAdam0.010820.010490.007260.00647ReLUAdamax0.010800.011070.007110.00692SigmiodRMSprop0.012990.014640.008890.01168SigmiodAdagrad0.015660.017930.011070.01368SigmiodAdadelta0.025340.023830.021930.02047SigmiodAdam0.011880.010830.008990.00693SigmiodAdamax0.013200.013220.010050.01002TanhRMSprop0.011760.010540.008340.00658TanhAdagrad0.012530.013140.008550.00835TanhAdadelta0.015390.016660.010500.01089TanhAdam0.010560.011080.006860.00743TanhAdamax0.010690.010750.007020.00653SoftsignRMSprop0.011080.010760.007700.00651SoftsignAdagrad0.012790.013550.008660.00869SoftsignAdadelta0.015000.015320.010520.00994SoftsignAdam0.011040.011660.007110.00772SoftsignAdamax0.010780.011100.006840.00664SoftplusRMSprop0.028300.034560.023950.03233SoftplusAdagrad0.014530.015340.009910.00975SoftplusAdadelta0.044570.046990.041060.04471SoftplusAdam0.013370.014760.010000.01216SoftplusAdamax0.011650.012140.008130.00871Through the comprehensive consideration of the prediction effect on the training set and the test set, the best combination of the optimization algorithm and the activation function is selected: the combination of the activation function ReLU and Adam with RMSE of 0.01049 and Mae of 0.00647 on the test set, which also shows that Adam algorithm with learning rate attenuation can improve the training effect of the model.As shown in Figure6, HS300 training set and verification set LSTM can converge rapidly. After about 250 times, RMSE of training set and test set reaches the lowest point, and RMSE of test set declines faster.Figure 6
Shanghai and Shenzhen 300 training set and verification set.


## 4.1. Deep Learning Model Based on Long Short Memory Network

In this paper, through many experiments, as shown in Figure4, when sequences are set to true, the uniform function initializes the weight. Finally, the model output layer is set. The output layer consists of two fully connected neural network layers, density, which outputs the prediction data of LSTM neural network layer, that is, the closing price of the stock index. Therefore, the number of neurons in the last density output layer is set to 1. The above model building process uses the sequential model of Keras. After building, the model is compiled. When training the model, we use Adam optimization algorithm, loss function as mean square error training model, two full-connection layers, and two activation functions. The activation function uses the default tanh and uses small batch training mode, the initial learning rate is set to 0.001, the number of iterations epoch is 300, and the batch size is 50. Due to the different factors of different stock markets, in order to ensure the rationality of the experiment and the stability of the prediction effect of the research model on different data sets, this paper makes an empirical study on the data of Shanghai Composite Index (00000 1. SH, pp. China Securities 100 (399 903. SZ) and Shanghai Shenzhen 300 index. According to the experimental results of variable importance score in Chapter 4, the first four input features with the highest importance, trading market OHLC, are selected.Figure 4
LSTM network structure map.


## 4.2. Optimization of Structural Design Parameters for Long- and Short-Time Memory Networks

In order to study the influence of the length of prediction window on the prediction accuracy, 32 neurons are connected in the full-connection layer; the dropout layer parameter is 0.2, the epoch number is 300, the loss function is the average error loss function, the optimization algorithm selects Adam, and the batch size is set to 50. And try to use different forecast period seqjen, including 7 days, 14 days, 21 days, 30 days, and 60 days to forecast the stock price of the next trading day. The change trend of verification set RMSE with window seqjen is as follows.As can be seen from Figure5, with the length of the data window, SEQ_, and with the increase of length, the RMSE of the test set increases gradually and decreases at 21 days. The prediction error of the test set is in the data window length SEQ_. When length is 7, it is the minimum, so the window length of the model is SEQ_. Len is set to 7 days. Table 1 shows the combination of different activation functions and optimization algorithms.Figure 5
Trend of validation set RMSE with WindowW.Table 1
Combination of different activation functions and optimization algorithms.
Activation functionOptimization algorithmClosing priceRMSEMaeTraining errorTest errorTraining errorTest errorReLURMSprop0.012780.012720.009760.00970ReLUAdagrad0.013070.013670.009040.00867ReLUAdadelta0.014700.015290.010130.00968ReLUAdam0.010820.010490.007260.00647ReLUAdamax0.010800.011070.007110.00692SigmiodRMSprop0.012990.014640.008890.01168SigmiodAdagrad0.015660.017930.011070.01368SigmiodAdadelta0.025340.023830.021930.02047SigmiodAdam0.011880.010830.008990.00693SigmiodAdamax0.013200.013220.010050.01002TanhRMSprop0.011760.010540.008340.00658TanhAdagrad0.012530.013140.008550.00835TanhAdadelta0.015390.016660.010500.01089TanhAdam0.010560.011080.006860.00743TanhAdamax0.010690.010750.007020.00653SoftsignRMSprop0.011080.010760.007700.00651SoftsignAdagrad0.012790.013550.008660.00869SoftsignAdadelta0.015000.015320.010520.00994SoftsignAdam0.011040.011660.007110.00772SoftsignAdamax0.010780.011100.006840.00664SoftplusRMSprop0.028300.034560.023950.03233SoftplusAdagrad0.014530.015340.009910.00975SoftplusAdadelta0.044570.046990.041060.04471SoftplusAdam0.013370.014760.010000.01216SoftplusAdamax0.011650.012140.008130.00871Through the comprehensive consideration of the prediction effect on the training set and the test set, the best combination of the optimization algorithm and the activation function is selected: the combination of the activation function ReLU and Adam with RMSE of 0.01049 and Mae of 0.00647 on the test set, which also shows that Adam algorithm with learning rate attenuation can improve the training effect of the model.As shown in Figure6, HS300 training set and verification set LSTM can converge rapidly. After about 250 times, RMSE of training set and test set reaches the lowest point, and RMSE of test set declines faster.Figure 6
Shanghai and Shenzhen 300 training set and verification set.


## 5. Comparison of Experimental Results of Deep Learning Model

The structure of GRU network and RNN neural network is completely consistent with LSTM neural network mentioned above, except that the LSTM layer is replaced by GRU layer and simple RNN layer. Table2 shows the parameter settings of three deep learning neural networks.Table 2
Deep learning network parameter setting.
Network structureNumber of L1 nodesNumber of L2 nodesNumber of nodes in full-connection layerOptimizerActivation functionIterationRNN12812832AdamReLU300LSTM12812832AdamReLU300GRU12812832AdamReLU300The results of CSI 300 index experiment are as follows.From the experimental results of the above model comparison, the comparison of the effect of LSTM neural network and RNN, GRU neural network on the prediction of stock closing price can be concluded as follows: the Shanghai and Shenzhen 300, China Securities 100, and Shanghai Composite Index networks in Figures7 and 8 can grasp the change trend of stock closing price, and the curve of the price predicted by LSTM model is basically consistent with the real price curve. The forecast results are the best and can accurately capture the price mutation. The prediction accuracy of GRU model is slightly lower than that of LSTM, and the worst prediction result is RNN model, so the deviation between the predicted price and the real price is large, which leads to the large prediction error of the final prediction model.Figure 7
Comparison between forecast value and real value of LSTM CSI 300 test set.Figure 8
The LSTM SSE test set forecast values are compared with the real values.Research on the influencing factors of stock price trend calculation is based on the fusion of LSTM and time-series model and the final effect comparison.To study the uncertainty of the stock price index, it is necessary to consider the impact of influencing factors on stock prices. Here, the interest rate in the macro factors is taken as the representative factor, and the currency growth rate in the micro factors is taken as the representative factor, as shown in Figure9. The impact of interest rates on stock prices is mainly manifested as a side effect; that is, lower interest rates will have a negative impact on stock prices in the first three periods. The impact of currency growth on stock prices is also negative, but the lag is smaller than that of interest rates.Figure 9
LightGBM-LSTM-based stock price index impulse response function analysis diagram.The sensitivity analysis of stock price forecasts is carried out again. Here, we still take repetition as the representative of the macro factors and the currency growth rate as the representative of the micro factors. The sensitivity coefficient of the stock price forecast is shown in Table3. When the interest rate drops by 1%, the predicted stock price drops by 1.43% and the predicted stock price rises by 1.28% for every 1% increase in currency growth. It can be found that the selected macro and micro factors have some influence on the stock price forecast.Table 3
Analysis of the impact of macro and micro representative factors on LightGBM-LSTM stock forecast prices.
Range of changeSensitivity coefficient of stock price to interest rateSensitivity coefficient of stock price to currency growth−10−1.43−1.28−5−1.43−1.2851.431.28101.431.28It can be seen from Table4 that LightGBM-LSTM has the highest ACC value among the four models implemented in the paper, but the F1 value is the lowest. There are two main reasons for this. From the sample, the classifier is overfocused during the training process. A large class with a large number of samples is ignored, which results in a decrease in prediction accuracy. In our paper, we use thresholds to divide the training set and determine the categories to avoid such problems. Secondly, from the perspective of the model, because GRU and RNN predict the rise and fall of the next day by analyzing the relationship before and after the time series, the overall prediction accuracy is excellent, but due to the high noise and random walk characteristics of stock prices, it may mislead the direction of prediction. The recognition degree for flat classes is 0, and LightGBM-LSTM is much better in this regard.Table 4
Comparison table of LightGBM-LSTM and other models.
ModelACCF1 valueF1 averageRiseFallFlatGRU0.4500.2830.5840.0000.289RNN0.4150.3420.5280.0500.307LightGBM-LSTM0.4730.3720.5920.0000.321


## 6. Conclusion

The architecture of deep learning framework Keras is studied experimentally, uses the Keras framework based on TensorFlow to build the LSTM composite model after the operation of the original data standardization, builds the model structure, then makes continuous experiments on the CSI 300 index, selects the optimal model structure from the test model according to the experimental results, and applies the deep learning theory. The prediction results of GRU structure and RNN model based on LSTM structure variant on different stock index data sets are compared. In the end, GRU network can greatly improve the training speed, but the accuracy is decreased. The LSTM combination model has a good prediction effect on the problem of multivariate and nonlinear stock price prediction. Compared with the integrated model, GRU and RNN, the LSTM model can greatly enhance the accuracy of stock price prediction.



---

*Source: 8128879-2021-09-28.xml*