Whether LSTM neural network can make (linear prediction) stock prediction (code interpretation)

          Recently, someone has been asking me whether LSTM can make stock predictions. So many people write codes to predict stocks. Why is there no specific application to buy? Whether the LSTM method can make accurate linear predictions. For the solution of the LSTM prediction problem, I will outline the current thinking of some codes on the Internet from the solution of stock prediction and linear prediction problems (for example: PM2.5 prediction, traffic flow prediction) (this is just some personal insights. Leave a comment)

table of Contents

Stock forecasting issues:

Linear prediction problem:

to sum up:


Stock forecasting issues:

        Summarizing the current code for stock prediction problems, we can find that there are two types of prediction methods for stock prediction (or two different prediction problems to use LSTM to solve). 

        One is to use the relevant characteristics of the opening price, volume, etc. to predict the closing price (also called multivariate time series forecasting). The data and forecast results are shown in the figure:

              

           This is the type of data I am looking for on the Internet, and it is also the first method and data type that I first started learning about LSTM stock prediction. The use of high-dimensional features for stock forecasting certainly has a high accuracy (in theory), but the method still has accurate errors. At present, when solving such data sets for closing price prediction, people often use two methods to construct input feature variables and prediction labels. In order to facilitate everyone's understanding, I have drawn the form of the following figure: (for example, use three days of data to go to Predict the closing price of the fourth day)

                                                                

                         

       The above method of dynamically constructing features is often referred to as a sliding window. That is to use the characteristics of historical data to predict the future label output. Of course, this method can only predict the output of one day through historical data. The commonly used way codes to construct this type of data can be described as:

def multivariate_data(dataset, target, start_index, end_index, history_size,target_size, step, single_step=False):
  data = []
  labels = []
  start_index = start_index + history_size
  if end_index is None:
    end_index = len(dataset) - target_size
  for i in range(start_index, end_index):
    indices = range(i-history_size, i, step)
    data.append(dataset[indices])
    if single_step:
      labels.append(target[i+target_size])
    else:
      labels.append(target[i:i+target_size])
  return np.array(data), np.array(labels)

      The other is to linearly predict the future price trend based on the stock's closing price (also called forecasting univariate time series). For example, I randomly collected a batch of data, and the normalized style is shown in the figure. On this figure, I drew how to construct input feature variables and prediction labels.

                                                                         

         That is, a feature is used to train the model, and the trained model is used again to make predictions for the future value. The construction of this feature is also often constructed using the sliding window method, and its implementation code is as follows:

def univariate_data(dataset, start_index, end_index, history_size, target_size):
  data = []
  labels = []
  start_index = start_index + history_size
  if end_index is None:
    end_index = len(dataset) - target_size
  for i in range(start_index, end_index):
    indices = range(i-history_size, i)
    data.append(np.reshape(dataset[indices], (history_size, 1)))
    labels.append(dataset[i+target_size])
  return np.array(data), np.array(labels)

       The above two methods to predict stocks only use some historical data to learn to predict a certain day in the future, or it can be said to predict data that is delayed by one day in the future. The structure of the input data entering the LSTM neural network is shown in the figure:

                                                 

               The output of the actual prediction indicates that the structure should be like this:

                                                 

              So far, the input forms constructed by the two different data types of stock predictions that I know have been described. Judging from the data input form, the trained model does have the ability to predict stock prices, that is to say, the trained LSTM neural network can indeed make linear predictions, but why do everyone have such high accuracy in code prediction, but It's not working in real application? My understanding is that the stock price problem includes the influence of various characteristic factors, such as political factors, market environment factors, etc. These characteristics are difficult to accurately reflect in the form of vectors, so the model we trained cannot be applied. Another reason may be that our model trained based on historical limited feature data will have the problem of overfitting historical data, resulting in various reasons such as inaccurate application.

Linear prediction problem:

     Based on the above discussion of stock forecasting problems, it is not difficult to find that the LSTM network model can indeed predict linear problems. We only need to use the so-called sliding window method to construct input and output features.

      However, how to make multi-point predictions, that is to say, to be able to predict data values ​​for a period of time based on the given point data, this requires us to make changes in the input feature structure and model output.

     For example, the output of the last layer can be set to the following form: Based on the multi-point prediction problem (predicting data for a period of time in the future), I will outline the implementation code in specific applications.

                                                     

multi_step_model.add(tf.keras.layers.Dense(72))

to sum up:

     If you are interested in this type of forecasting problem, you can also reproduce the stock forecast based on my discussion. If there is a problem or error in my previous understanding, you are welcome to add Q525894654 for criticism, correction or discussion. As we know, the stock market is very volatile and unpredictable. However, the use of deep learning methods for price prediction certainly has a certain potential for use. Therefore, we believe that the algorithm model can (not always) correctly predict stock price movements rather than specific values ​​(it is best to do classification problems). Therefore, I guess based on a probabilistic time series model (I have seen someone put forward a probabilistic time-series model before). Perhaps a more metaphysical method is more suitable for stock forecasting.

Published 40 original articles · praised 203 · 190,000 views

Guess you like

Origin blog.csdn.net/weixin_40651515/article/details/105571639