M3 Deep Learning & Artificial intelligence
1. EDA
In addition, it could be relevant to see where, during the period, the biggest loss and profit came. Here we can conclude that biggest loss was when the COVID-10 lockdown started.
2. LSTM
2.1 Preprocessing for LSTM
1. Here we split the data into training and test set.
2. Normalize data
Now we move on to normalization of the data, which is the process of making the properties more consistent. This will in turn allow the model to predict more accuratly.
3. X_train - y_train & 4. Reshape
Next is separating the normalized data into x_train and y_train. In the same step we reshape the data. This is done because we want the input to have 3 dimensions - which is typically: data-size, number of steps and number of features.
2.2 LSTM-model with Keras
1. Create model:
First we create and initialize the model, which is a sequential model - meaning it's a stack of layers, using previous observations to predict the next.We add layers and dense it twice in the end.
2. Compile model
Next step is to compile the model:optimizer: The optimizer does the process of updating our parameters for us here. For this adam is chosen. loss: It is a number that indicates how good or bad the model is to its prediction. As it approaches 0, the error starts to decrease.
3 & 4 Fit the model, and choose the number of epochs and the size og batch
To choose the correct number of epochs (to minimize the loss), we will create a visualization of the right amount of epochs.
Here we combine the model that we just created with the training data (x and y):
2.3 Prediction using LSTM and model evaluation
Now we move on to predicting how well our model can predict the stock price of Microsoft
The root-mean-square error (RMSE), as seen above is a frequently used measure of the differences between values predicted by a model and the values observed. RMSE depends on the scale on which the model is measured - it is just the average of the squared difference between the predicted and the actual data points - in our case 9.2$, which is pretty ok! Now we plot our result of the prediction on the 20% test set.
As we see above the predictions is pretty good! the predicted line follows the valid line, which is the real stock price.
Below is a dataframe where the real prices and the predicted prices can be seen:
On the plot below, we zoomed in to the test set only, so we better can see how the valid line and the predicted line follows each other:
In the above, we see how the model would have predicted previous dates, where we can validate against the actual stock price.
2.4 Predicting one day ahead
Now we will try to predict tomorrows price - so we can be rich, sort of.
2.5 Multi-step prediction
3. RNN Model
In addition to the LSTM, we created a RNN model to compare the models, to conclude which one would be the best for the purpose of predicting the Microsoft stock price.
3.1 Preprocessing
We normalize the data, which is the process of making the properties more consistent. This will in turn allow the model to predict more accurately.
Split the dataset into a training- and a testset, when that is done we convert the x & y-train into Numpy arrays. At the end we reshape the data.
3.2 Build the RNN model with Keras
Next step would be to build the model with Keras. When the model is build, we compile it right away.
When the model is build, we train the model. In this case we have actually just used a batch size of 1 and with 1 epoch.
3.3 Predicting using RNN and model evaluation
To predict and ealuate the model, we calculate the root-mean-square error (RMSE)
The root-mean-square error (RMSE), as seen above is a frequently used measure of the differences between values predicted by a model and the values observed. RMSE depends on the scale on which the model is measured - it is just the average of the squared difference between the predicted and the actual data points - in our case 9.2$, which is pretty ok! Now we plot our result of the prediction on the 20% test set.
Now we plot the RNN model, to give a visualization
4. Multivariate LSTM (Microsoft)
4.2 Model creation
After we ran the model training the first time we saved the result and suppresses the training and just load the trained model
We look at loss for each of the epoch iteration to the determent how the training of the model went.
Calculate the error ratings of the model
Now we visualise the models prediction and compare it to the real data.
at the bottom we show how big the difference is between the data is and if it positiv or negativ
Now we use the model to tell us what the closing value of the Microsoft stock would be tomorrow according to the stock
5. Multivariate LSTM (Multiple Stocks)
4.2 Model creation
Model evaluation
Calculate the error ratings of the model