Stock Price Prediction Using Deep Learning - LSTM Network

Stock Price Prediction Using Deep Learning - LSTM Network


The stock market's recent surge to all-time highs leaves many wondering: can it last?

This project explores the potential future of the Magnificent 7 stocks - the current leaders of the S&P 500. We'll leverage Long Short-Term Memory (LSTM) networks to forecast their stock prices and computationally figure out potential shifts in the market trend.


TL;DR

  • Scraped the historical stock prices of the Magnificent 7 using Selenium WebDriver and Beautifulsoup
  • Trained a deep learning model using Long Short-Term Memory (LTSM) layers and predicted stock prices
  • Achieved a margin of error between 0.87% and 8.94% - depending on the timing of training and the volatility of the specific stock prices



Why LSTMs?

LSTMs are a type of neural network that can learn long-term dependencies by detecting patterns from the sequence of data in the time series and predicting future values. For example -

  • [ a, b, c, XX ] --- XX is expected to be 'd'
  • [ 10, 100, 1,000, XXX ] --- XXX is expected to be 10,000

LSTM can capture dependencies over time - and most importantly, in the process, they can learn when to remember and forget information ir/relevant to future prediction.


So, LSTMs are attractive for stock price prediction for several reasons:

  • Capturing Trends: LSTMs excel at identifying patterns and relationships within sequences. This allows them to learn complex historical trends in stock prices, potentially uncovering hidden insights missed by simpler models.
  • Volatility Handling: The stock market is inherently volatile. LSTMs can handle this volatility by considering past price fluctuations to inform future predictions.
  • Adaptability: Markets are constantly evolving. LSTMs can adapt to changing market dynamics by continuously learning from new data.


Let's Dive Into Technical Steps

Step 1. Data Preparation

1) Scrape the data from Yahoo Finance using Selenium Webdriver

*Alternatively, we can use public API or manually copy and paste the data to a CSV file.


2) Take adj. close price as a dataset, and normalize it from 0 to 1 using the MinMaxScaler preprocessing class from the scikit-learn ML library

We need to normalize the data as the scale of the input data can affect the performance of LSTMs.


3) Split the ordered dataset into train and test datasets

This straightforward method ensures maintaining the sequence of the values - as we address the time series data.


4) Create feature (X) and forecast (Y) datasets and convert them into the NumPy array to model with a neural network

Create a function - the loo-back value represents a given time (t) and next time (t+1)


5) Form the input dataset X as [samples, time steps, features] to feed into the LSTM network


Step 2. Model Training

LTMS networks are memory cells that store data over time with input/output layers and hidden layers that capture patterns and dependencies in the data.

By adjusting the weights and biases during the training, LSTM networks can effectively learn to remember and forget information as needed to make accurate predictions.

"The neural network architecture consists of a visible layer with one input, a hidden layer with four LSTM blocks (neurons), and an output layer that predicts a single value"


1) Create a neural network using the Keras library - an API for building and training deep learning models with 5 dense layers


2) Train the model by passing the train data


Step 3. Result Visualization

The predicted stock prices are displayed here:

Predicted stock prices


As we train the LSTM model for a longer period, you can see a noticeable improvement in the accuracy of the predictions compared to the initial values.

Training Loss - APPL (Epochs = 10, 100, 1,000)


Step 4. Performance Evaluation

After training the model, evaluate the performance using Root Mean Squared Error (RMSE) and Mean Absolute Percentage Error (MAPE).

LSTM predictions for volatile stocks like NVDA often show larger error margins:

  • NVDA (epochs = 1,000) - MSE: 73.19, MAPE: 0.08937
  • APPL (epochs = 1,000) - MSE: 2.064, MAPE: 0.00866

This can be attributed to the difficulty of capturing rapid price movements in the model.



In Conclusion - Navigating a Bullish Market

LSTMs are excellent at capturing long-term dependencies if we expect the bullish market to continue for a while. However, we need to consider several limitations:

  • Data Dependence: The accuracy of LSTMs heavily relies on the quality and quantity of data used for training. Insufficient or low-quality data can lead to unreliable predictions.
  • Black Box Nature: LSTMs are complex models. While they can produce impressive results, understanding their internal reasoning behind predictions can be challenging. This makes it difficult to interpret and debug errors.
  • Market Incompleteness: The stock market is influenced by multiple factors beyond historical price data, including news, economic conditions, and investor sentiment. LSTMs may struggle to account for these external forces.

Integrating LSTM predictions with fundamental analysis and technical indicators is ideal for getting more comprehensive stock market views.


Go deeper...?

Although training the model might be computationally expensive, we can expect more complex and accurate predictions using other deep-learning models such as:

  • Random Forests - to identify key factors influencing stock prices and make predictions based on those factors.
  • Gradient Boosting Machines (GBMs) - to capture complex, non-linear relationships in stock prices, potentially leading to more accurate forecasts.



Reference:

Dense Layers in Neural Networks

The Concepts of Dense and Spares in the Context of Neural Networks

Keras documentation

Yahoo! Finance


Disclaimer: The predictions are for experiment purposes and don't guarantee future performance.

Marc Deveaux

Founder @ Marcus Lion LLC | Mathematics and Computing

1 个月

is this correct? train_data, test_data = dataset[test_size: len(dataset), :], dataset[0:test_size, :] or should the assignments be flipped around? train_data, test_data = dataset[0:test_size, :], dataset[test_size: len(dataset), :]

回复
Karan Khara

Global Managing Director at Meta | Strategic Thinker & Partner| AI Leader

8 个月

Was just thinking about this topic the other day. You might like this ebook - “Deep Learning for Time Series Forecasting” by Jason Brownlee

回复
Stanley Russel

??? Engineer & Manufacturer ?? | Internet Bonding routers to Video Servers | Network equipment production | ISP Independent IP address provider | Customized Packet level Encryption & Security ?? | On-premises Cloud ?

8 个月

Forecasting stock prices using deep learning models like LSTM (Long Short-Term Memory) is a fascinating application of AI in finance. These models can capture complex patterns and dependencies in historical stock data, enabling them to make predictions about future price movements. However, it's essential to remember that stock market forecasting is inherently challenging due to its non-linear and unpredictable nature. While LSTM models can provide valuable insights, they also come with limitations and uncertainties. What are your thoughts on the reliability and accuracy of deep learning models in predicting stock prices, especially in volatile market conditions? Additionally, how do you incorporate factors like market sentiment, macroeconomic indicators, and geopolitical events into your forecasting models to enhance their robustness?

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了