10.1 Introduction
The goal of a stock prediction algorithm is to recommend a portfolio of stocks that will maximize an investor’s return. The investor has a finite amount of money and wants to create a portfolio to maximize her or his return on investment. The neural network in this chapter will predict the behavior of a portfolio of stocks given its history. This could then be used to select a portfolio of stocks with some idea of the future performance. The stock market model used in this chapter is based on Geometric Brownian Motion. Given that, we could do statistical analysis that would allow us to pick stocks. We’ll show that a neural net, which does not have any knowledge of the model, can do as well in modeling the stocks.
10.2 Generating a Stock Market
10.2.1 Problem
We want to create an artificial stock market that replicates real stocks.
10.2.2 Solution
Implement Geometric Brownian Motion. This was invented by Paul Samuelson, Nobel Laureate [36].
10.2.3 How It Works
The results are shown in Figure 10.1. They look like a real stock. Changing the drift or volatility will change the overall shape. For example, if you set the volatility, σ = 0, you get the very nice stock shown with the red line. Increasing r makes the stock grow faster. This gives us the general rule that we want high r and low σ. See Figure 10.2.
10.3 Creating a Stock Market
10.3.1 Problem
We want to create a stock market.
10.3.2 Solution
Use the stock price function to create 100 stocks with randomly chosen parameters.
10.3.3 How It Works
Two runs are shown in Figure 10.3.
10.4 Training and Testing
10.4.1 Problem
We want to build a deep learning system to predict the performance of a stock. This can be applied to the stock market created earlier to predict the performance of a portfolio.
10.4.2 Solution
The history of a stock is a time series. We will use a Long Short-Term Memory (LSTM) network to predict the future performance of the stock based on past data. Past performance is not necessarily indicative of future results. All investments carry some amount of risk. You are encouraged to consult with a certified financial planner before making any investment decisions. This utilizes the Deep Learning Toolbox’s lstmLayer layer. We will use part of the time series to test the results.
10.4.3 How It Works
An LSTM layer learns long-term dependencies between time steps in a time series. It automatically deweights past data. LSTMs have replaced recursive neural nets (RNNs) in many applications.
The stock price is shown in Figure 10.5.
- 1.
sequenceInputLayer(inputSize) defines a sequence input layer. inputSize is the size of the input sequence at each time step. In our problem, the sequence is just the last value in the time sequence, so inputSize is 1. You could have longer sequences.
- 2.
lstmLayer(numHiddenUnits) creates a Long Short-Term Memory layer. numHiddenUnits is the number of hidden units in the layer. The number of hidden units is the number of neurons in the layer.
- 3.
fullyConnectedLayer(outputSize) creates a fully connected layer with specified output size.
- 4.
regressionLayer creates a regression output layer for a neural network. Regression is data fitting.
We let “patience” be inf. This means the learning will continue to the last epoch even if no progress is made. The training window is shown in Figure 10.7. The top plot shows the root-mean-squared error (RMSE) calculated from the data and the bottom plot the loss. We are also using the test data for validation. Note that the validation data needs to be normalized with its own mean and standard deviation.
Results for the bilstm layer and two lstm layers are shown in Figure 10.9 and Figure 10.10. All produce acceptable models. The bilstm looks like it predicts the trends better.