by Fazal Mittu (’23) | May 10, 2021
As part of Saint Francis’s new Machine Learning Club, the project team of Fazal Mittu (’23), Varun Nair (’23), Michael Puell (’23), Swathi Badrinarayanan (’23), Koena Jaware (’23), Sandeep Bajamahal (’24), Ruhi Yusuf (’24), and Siddhant Hullur (’23) developed a model of future stock prices using python libraries to identify trends and predict future prices from Apple’s stock data. The practical application of this project is to assist stock traders in their buying decisions by using historical data of stock pricing to identify trends and predict future prices. This product was built to provide increased assurance when making financial decisions.
This project utilizes common machine learning python libraries as well as public data to produce a trained model able to accurately predict stock prices. Getting into the nitty-gritty details, the Python frameworks and libraries used were SciKitLearn, Keras, Matplotlib, Numpy, and Pandas, which are all available for free through code hosting platforms like Github or Google Colab. In these platforms, the code is divided into individual cells that can run independently to make up the output of the program as a whole. Cells 1-2 imported necessary data libraries, with SciKitLearn and Keras to build, train, and test the model and Matplotlib, Numpy, and Pandas to preprocess and view the data. Cells 3-8 pulled the dataset of Apple’s stock prices stored in a csv file. code then extracted the necessary data in order to train and test the model as well as split them into smaller test arrays to be individually tested. Cells 9-11 reshaped the data to prepare it for training in a preprocessing step known as feature scaling, which essentially normalizes the data with a value between 0 and 1. Cells 12-13 trained the model to fit the data, essentially building the data and running it. Lastly, Cells 15-17 created the test dataset to plot the final result, with Matplotlib graphing the prediction alongside the real value.
In terms of more general applications, Long Term Short Term Memory networks are an extremely powerful time-series model. They are a type of Recurrent Neural Network (RNN) that are able to learn long-term dependencies. LTSMs have been used for applying RNN’s in a variety of problems including image captioning, speech recognition, language modeling, and translation. LSTMs solve the problem of short-term memory or vanishing and exploding gradients and gradient shrinking as a model back-propagates through time. The LSTM has gates that are able to regulate what to keep or forget/throw away. By doing so, it learns to use relevant information to make predictions. The LSTM is able to store information from the past which helps especially predict stock price fluctuations based on past prices. The market changes constantly, so making timely accurate decisions and predictions is extremely crucial for accurate results.
LSTM processes data sequentially. The LSTM passes data as it propagates forward and uses gates to decide whether to keep or forget information. Each LSTM cell contains a forget state, input gate, output gate, and cell state, along with two activation functions, Sigmoid and Tanh. The cell state contains the long-term memory, with input gates updating the cell state. The forget gate decides what information needs to be thrown away. The value 0 indicates nonessential, while 1 indicates important. The output (hidden state) is used as the prediction.
Sigmoid activation ensures that values stay between 0 and 1. Any value multiplied by 0 is forgotten, while any value multiplied by 1 is kept. Tanh is used to determine candidate values to be added to the cell state. Tanh activation ensures that the values stay between a positive (1) and negative (-1) number, allowing for an increase or decrease in value for cell state.
Here we have our final prediction! The green line is our prediction while the orange line represents the true values. We have learned the structure of LSTM’s and how they can be applied in real life. Further, we implemented the model using the SciKitLearn library and Keras framework. To check out the code for this project, click on the link!
Adapted from this Medium article, originally published April 16, 2021. https://sfhsmlclub.medium.com/using-lstms-to-predict-future-stock-prices-61f4458fc860