AAPL Stock Price Forecast using LSTM

by Emily Khor | April 22, 2023

AAPL Stock Actual and Predicted Open Price for 2023

Brief Summary of LSTM

The LSTM model is a type of artificial neural network and stands for Long Short-Term Memory network. It’s an improvement over RNN (Recurrent Neural Network), which often struggles with long-term dependencies in sequential data and are prone to the vanishing gradient problem.
The LSTM model addresses this issue with the integration of a memory cell with gated connections that can selectively remember or forget information from previous time steps. This allows the model to capture important long-term patterns or information and make predictions for future time steps.
As a result, this makes LSTM suitable for modeling time-dependent data with long-term dependencies and forecasting time-series data like stock prices.


Data Acquisition and Model Building

This project focuses on predicting the stock prices of Apple Inc. (AAPL) , but an LSTM model should work on any stocks by simply switching up the input data. The historical AAPL stock prices going all the way back to 2000 were extracted from Yahoo Finance and used to train the model.
The model was built using the Pytorch package in Python. Two different LSTM models were built and evaluated - one with a single LSTM layer, and another with two LSTM layers. The intention was to see how model complexity affects the accuracy of the prediction.


Hyperparameter Tuning and Model Training

R2 Plot and RMSE Plot with the Optimal Hyperparameters for Both Models Indicated

The models were trained using the historical stock price data up to 2022, and different combinations of hyperparameters were tuned to determine the optimal set of hyperparameters. The combination of hyperparameters for each model was evaluated by looking at RMSE (Root Mean Square Error) and R2 (Coefficient of Determination). The models were then validated using stock prices from 2023.
The best hyperparameters for both models were used to predict stock prices from January 2023 to now. The results showed that the single-layer LSTM model was better for forecasting stock prices than the multi-layer LSTM, with lower RMSE and higher R2 values.


Key Takeaways

Based on the findings from the training stage, it seems that a simpler model like the single-layer LSTM is effective for modeling and predicting time-dependent data such as stock prices. For this case, the single-layer LSTM model outperformed the multi-layer LSTM model and produced predictions that were closest to the actual stock price. Overall, the LSTM model can be an effective tool for forecasting stock prices or other time-dependent data after careful consideration of model architecture and hyperparameters.

Comparison of LSTM Models

Interactive Plot of AAPL Stock Open Price




Next Stage of the Project

The model could be trained with fine-tuned hyperparameters using all historical data from 2000 until today. Once trained, the model can then be used to predict only the stock price for the following day.