At some point of time in our lives, we all come across this term ‘Stock Market.’ Well, so what really is it?
Stock Market, also known as the equity market or share market, is the aggregation of buyers and sellers of stocks, which represent ownership claims on businesses. Buying a share or stock i.e. investing, is basically you buying a portion of the company, meaning you own a certain percentage of that enterprise, and this if done the right way can prove to be very lucrative.
So then, how do we know how exactly to invest, in order to get good returns on your investment?
Various techniques have been used for decades such as observing the Stock’s momentum, Regression techniques to estimate a mathematical function of the stock, Sentiment Analysis, etc.
Recently, Artificial Intelligence and Machine Learning have taken the world by storm. Here, we apply Deep Learning techniques in order to make predictions of stock prices using the concept of Long Short Term Memory, in other words, an LSTM Model.
LSTM is type of Recurrent Neural Network (RNN), which was made as a solution to the Long-Term Dependencies of the classical RNN model.
Long Short Term Memory networks are capable of learning long-term dependencies. The main difference between a classical RNN and a LSTM model is that an RNN will have a single activation function such as the sigmoid() or the tanh() functions, and when the recurrences get too high, the correlation between the output and an input which was considered at a very early stage becomes minuscule.
The LSTM model however, has its repeating module consisting of various layers and usually comprise of a variety of activations at play, which helps in Long-Term Dependencies, which is observed during practical applications.
Hence, we will use a LSTM Model to compute the closing prices of the stock.
We will use the Amazon stock data from a website which provides such data in a csv form and feed that as an array input. The array will contain numbers, will be one dimensional, the numbers will be the stock closing prizes daily of AMZN stock.
This data will be input into a LSTM, the LSTM has memory holding capability as it is an implementation of an RNN (Recurring Neural Network) and takes its previous output as the next input (ideally with unity weight, or whatever it has been initialized to) in order to process information which works on data of the past and has impact on the future!
So basically, the input vector will be the training set as well as the test set (excluding 30% of the training set to check for independent functioning). The LSTM will take a 30 days’ cut-off price as input, from the starting 0ᵗʰ day, using the n inputs it will obtain the training output i.e. the closing price at the (n+1)ᵗʰ day.
The next loop will start from the 1ˢᵗ day and take n inputs from the new reference point, will obtain the training output i.e. the closing price at the (n+2)ᵗʰ day and so on.
In this process, all the variables at the n positions get assigned a weight by the LSTM’s weight update algorithm and hence tries to find the pattern i.e. the effect of the previous n days on the stock price of (n+1)ᵗʰ day. After the training set the weights and biases obtained from the algorithm, the LSTM model is tried on the very same dataset having only first given n days and try to predict the remaining days by shifting by steps of 1.
So, we first start off by importing the necessary modules and the functions which will be used in the program later.
Then we import AMZN stock data from tiingo, convert it to a csv format so that it can be easily operated on.
We now use the head keyword of the Pandas module in order to access stock data via their heading.
As we have access to it title-wise, now we can easily select the ‘Close’ title and take values from it to operate on the model.
Now if we plot that, we should be able to plot it directly with the matplotlib.pyplot’s plot function, as it is a 2D Array.
The data we obtained is the daily cut-off of AMZN’s stock price, but Amazon being a huge MNC has a massive stock market, and huge cut-off prices, hence it would be tough to train the model directly on it, hence we should scale it down to a value between (0,1). We will do this using the MinMaxScaler function.
Quickly checking our array, we see:
Now that we’ve scaled it down to an easier to operate range, the preprocessing is done and now we can focus on the train-test-split. We split it in the ratio: Train: Test::0.65:0.35
Train Data :
Test Data :
Now comes the most important part, we need to make the dataset in such a way it considers the ‘time_step’ number of days before it to make the prediction of the cut-off price.
Hence, we create a function which takes an input having the data from train_data and the test_data, in order to train the model accordingly.
The train data will help set up the parameters or weights, the test data will be used to check the implementation of the set weights to see the accuracy.
We obtain the X and Y train and test datasets.
To just make sure the dimensions of the output and input are correct.
Now it’s time to finally make the Neural Network layers, we implement a model as an object of Sequential class in order to start a Sequential NN.
We add 4 layers to it, the first 3 being LSTM’s, then the Last layer is a Dense Layer, and for the cost function, the error function used is Mean Square Error, along with optimizer as ADAM.
model.summary() gives us the summary of our sequential model.
Now all that’s left is to train the model, we set epochs to 100 to try and get the error as low as possible
We used scaling to normalize the values, now we have to get them back to the original scale, hence we use scaler inverse.
Now we can plot the train, which was the original dataset we had, and the test next to it, to see if the weights were able to get anything close the trends.
Well it looks like as the differentiation of the curve at every point would give us an idea that it is at least able to predict whether the stock price is going up or down, but it isn’t giving as a very good idea about the scale of bullish or bearish.
Hence it poses a risk for the stock market investor to trust his/her money on just the basis of static weights made from such a simple LSTM model.
Nevertheless, the predictions made by the model seem to be much better than a purely random guess, and hence can be considered as a success, so cheers!