How Machine Learning is used in Predicting Stock Prices - LSTM (2024)

Algorithms can predict future stock prices. These models analyze huge amounts of data to detect complex patterns in the market and figure out how prices might change. Machine learning is increasingly being used by major financial institutions to gain an edge in the stock market. Top companies like Goldman Sachs and JP Morgan are leveraging sophisticated algorithms to predict future stock prices and make profitable trades.

Some of the machine learning models used for stock price forecasting include:

  • Long short-term memory (LSTM) networks
  • Support vector machines (SVMs)
  • Random forests
  • Gradient boosting machines (GBMs)
  • Ensemble learning

In this article, we'll explore one of the Machine Learning algorithms used for stock price prediction which is LSTM. We'll delve into how it works with an example

Long short-term memory (LSTM) networks

LSTMs are a type of neural network that can learn long-term dependencies and are useful for predicting stock prices. They examine a sequence of stock prices over time to detect patterns and predict future prices.

How Machine Learning is used in Predicting Stock Prices - LSTM (1)

Understanding LSTM

Imagine how humans process language. When we listen to someone speak, we comprehend each word within the context of preceding words and the overall conversation. Likewise, when predicting stock prices, LSTMs rely on their past experiences, just as we rely on our knowledge to understand the meaning of words in a sentence.

To illustrate LSTM's concept, let's look at predicting the next number in a sequence:

  • In the sequence [6 - 7 - 8 - ], we expect the next number to be 9.
  • However, in the sequence [2 - 4 - 8 - ], we anticipate the next number to be 16.

This is where LSTM networks shine. They excel at capturing dependencies over time, just like our brains adapt to changing contexts during a conversation. As time progresses, older inputs in a sequence have less impact on predicting the next value. LSTM networks are designed to learn when to remember important information and when to forget less relevant data, a critical feature when dealing with the ever-changing landscape of stock market data.

How LSTM Networks Work

At the heart of LSTM networks are memory cells that store and update information over time. These memory cells are connected through a network of gates that control the flow of information. The three fundamental gates in an LSTM network are:

How Machine Learning is used in Predicting Stock Prices - LSTM (2)

  1. Forget Gate: This gate determines what information from the previous time step should be discarded or forgotten.
  2. Input Gate: The input gate decides which new information from the current time step should be stored in the memory cell.
  3. Output Gate: The output gate selects what information from the memory cell should be used to make the prediction for the current time step.

These gates work in tandem to capture patterns and dependencies in the data. By adjusting the weights and biases associated with each gate during training, LSTM networks can effectively learn to remember and forget information as needed to make accurate predictions.

Example

I have done a team project using Machine Learning Algorithms at my University for Stock Price Prediction. we employed LSTM (Long Short-Term Memory) neural networks as one of the algorithms to predict Tesla stock prices.

The Process involved,

  • Data preparation
  • Visualization of historical price trends
  • Feature engineering using a sliding window approach
  • Building a Sequential neural network model with multiple LSTM layers and dropout regularization
  • Training the model to predict the next day's stock price
  • Visualizing the model's performance by comparing real and predicted prices over time

This approach allowed for effective forecasting of Tesla stock prices and showcased the capabilities of LSTM in financial time series prediction.

Recommended by LinkedIn

Analysis Paralysis: Which Machine Learning Model to… Joseph Jose 3 years ago
🚀 Unlocking Model Black Boxes: Understanding SHAP… Mouhssine AKKOUH 6 months ago
Gradient Descent + Matrix Determinants Yeshwanth N 11 months ago

In our first attempt at using LSTM (Long Short-Term Memory) for stock price prediction, we want to be upfront about our results. Please don't expect perfect accuracy just yet. While we'll show you what we've done, keep in mind that improving predictions takes time, data, and experience.

Code

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom datetime import datetimefrom keras.models import Sequentialfrom keras.layers import Dense, LSTM, Dropoutfrom sklearn.preprocessing import MinMaxScalerfrom sklearn.metrics import mean_squared_error, mean_absolute_errorfrom sklearn.model_selection import train_test_splitfrom keras.callbacks import EarlyStoppingfrom tensorflow.keras.optimizers import Adam, SGD# Function to convert date formatdef to_datetime(df): date = datetime.strptime(df, '%d.%m.%Y') return date.strftime("%Y-%m-%d")# Read and preprocess the datasetdf = pd.read_csv('Price Tesla.csv', sep=';')df['Date'] = df['Date'].apply(lambda x: to_datetime(x))df = df.sort_values('Date').reset_index(drop=True)df['Price'] = df['Price'].astype(float)# Visualize Tesla stock price dataplt.figure(figsize=(20, 7))plt.plot(df['Date'].values, df['Price'].values, label='Tesla Stock Price', color='red')plt.xticks(np.arange(100, df.shape[0], 200))plt.xlabel('Date')plt.ylabel('Price ($)')plt.legend()plt.show()# Prepare training dataX_train = []y_train = []window = 60for i in range(window, num_shape): # num_shape needs to be defined X_train_ = np.reshape(train_scaled[i-window:i, 0], (window, 1)) # train_scaled should be defined X_train.append(X_train_) y_train.append(train_scaled[i, 0])X_train = np.stack(X_train)y_train = np.stack(y_train)# Initialize the Recurrent Neural Network (RNN) modelmodel = Sequential()model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))model.add(Dropout(0.2))model.add(LSTM(units=50, return_sequences=True))model.add(Dropout(0.2))model.add(LSTM(units=50, return_sequences=True))model.add(Dropout(0.2))model.add(LSTM(units=50))model.add(Dropout(0.2))model.add(Dense(units=1))model.summary()# Visualize real and predicted Tesla stock pricesplt.figure(figsize=(20, 7))plt.plot(df['Date'].values[1800:], df_volume[1800:], color='red', label='Real Tesla Stock Price') # df_volume needs to be definedplt.plot(df['Date'][-predict.shape[0]:].values, predict, color='blue', label='Predicted Tesla Stock Price') # predict needs to be definedplt.xticks(np.arange(100, df[1800:].shape[0], 200))plt.title('Tesla Stock Price Prediction')plt.xlabel('Date')plt.ylabel('Price ($)')plt.legend()plt.show()# 20 days predictionpred_ = predict[-1].copy() # predict needs to be definedprediction_full = []window = 60df_copy = df.iloc[:, 1:2][1:].valuesfor j in range(20): df_ = np.vstack((df_copy, pred_)) train_ = df_[:num_shape] # num_shape needs to be defined test_ = df_[num_shape:] # num_shape needs to be defined df_volume_ = np.vstack((train_, test_)) inputs_ = df_volume_[df_volume_.shape[0] - test_.shape[0] - window:] inputs_ = inputs_.reshape(-1, 1) inputs_ = sc.transform(inputs_) # sc needs to be defined X_test_2 = [] for k in range(window, num_2): # num_2 needs to be defined X_test_3 = np.reshape(inputs_[k-window:k, 0], (window, 1)) X_test_2.append(X_test_3) X_test_ = np.stack(X_test_2) predict_ = model.predict(X_test_) pred_ = sc.inverse_transform(predict_) # sc needs to be defined prediction_full.append(pred_[-1][0]) df_copy = df_[j:]# Visualize real and predicted Tesla stock pricesplt.figure(figsize=(20, 7))plt.plot(df['Date'].values[1700:], df_volume[1700:], color='red', label='Real Tesla Stock Price') # df_volume needs to be definedplt.plot(df_date['Date'][-prediction_full_new.shape[0]:].values, prediction_full_new, color='blue', label='Predicted Tesla Stock Price') # prediction_full_new needs to be definedplt.xticks(np.arange(100, df[1700:].shape[0], 200))plt.title('Tesla Stock Price Prediction')plt.xlabel('Date')plt.ylabel('Price ($)')plt.legend()plt.show() 

Output:

LSTM

MSE: 4840.804172281035

MAE: 63.15895608084543

RMSE: 69.5758878655604

How Machine Learning is used in Predicting Stock Prices - LSTM (6)
How Machine Learning is used in Predicting Stock Prices - LSTM (7)

The performance can certainly be improved by tuning the model further.

Limitations of LSTM stock price prediction

There are a number of limitations to using LSTM neural networks for stock price prediction. One limitation is that LSTM models require a large amount of historical data to train. Another limitation is that LSTM models are complex and can be difficult to tune. Additionally, LSTM models are susceptible to overfitting, which can lead to inaccurate predictions on new data.

Other Popular Algorithms for Price Prediction:

Support vector machines (SVMs)

SVMs are supervised learning models that analyze data and recognize patterns. They can classify stock price data into groups to predict if a stock price will increase or decrease.

Random forests

Random forests are an ensemble learning method that combines the predictions of multiple decision trees. They analyze many variables to determine which factors influence stock price changes the most.

Gradient boosting machines (GBMs)

GBMs are also ensemble methods that combine the predictions of multiple models to improve accuracy. They incrementally build new models to predict stock prices based on the errors of prior models.

Ensemble learning

Ensemble learning combines multiple ML models, like the ones above, into a meta-model that can make even more accurate predictions. For stock trading, ensemble models that incorporate both technical analysis and fundamental analysis tend to perform the best.

Conclusion

In the future, machine learning will become even more widespread in finance. Algorithms will continue to become more sophisticated, using techniques like deep learning, reinforcement learning and transfer learning. Firms will have AI systems that can analyze huge amounts of data, detect complex patterns and relationships, and make extremely accurate predictions about the market. Overall, machine learning has the potential to vastly improve how investments are made and how the finance industry operates.

Disclaimer:The material is purely for educational and should not be taken as professional investment advice. Invest at your own discretion.

How Machine Learning is used in Predicting Stock Prices - LSTM (2024)

FAQs

How Machine Learning is used in Predicting Stock Prices - LSTM? ›

Long short-term memory (LSTM) networks

How to use LSTM to predict stock prices? ›

Predicting Stock Prices with LSTM and GRU: A Step-by-Step Guide
  1. Getting the Data. To get started, we need historical stock price data. ...
  2. Data Visualization. ...
  3. Data Preprocessing. ...
  4. Creating the Training Data. ...
  5. Building the LSTM Model. ...
  6. Training the Model. ...
  7. Making Predictions. ...
  8. Visualizing the Predictions.

How does machine learning predict stock prices? ›

With recent research trends, a popular approach is to apply machine learning algorithms to learn from historical price data, thereby being able to predict future prices. The scale demonstrates predictive power on historical stock price data that outperforms other methods due to its suitability for this data type.

How does LSTM work for prediction? ›

An LSTM module (or cell) has 5 essential components which allows it to model both long-term and short-term data. Hidden state (ht) - This is output state information calculated w.r.t. current input, previous hidden state and current cell input which you eventually use to predict the future stock market prices.

What is LSTM in machine learning? ›

Long short-term memory (LSTM) is a type of recurrent neural network (RNN) aimed at dealing with the vanishing gradient problem present in traditional RNNs. Its relative insensitivity to gap length is its advantage over other RNNs, hidden Markov models and other sequence learning methods.

What is the best algorithm for predicting stock prices? ›

Q2. What can you use to predict stock prices in Deep Learning? A. Moving average, linear regression, KNN (k-nearest neighbor), Auto ARIMA, and LSTM (Long Short Term Memory) are some of the most common Deep Learning algorithms used to predict stock prices.

What is the difference between CNN and LSTM for stock prediction? ›

Based on the results of the experiment, it has been observed that it is more reliable to use LSTM which gives an accuracy of 99.68% for open price and 99.89% for close price prediction than using CNN which gives 99.07% for open price and 97.89% for close price prediction.

How does LSTM work step by step? ›

The LSTM cell also has a memory cell that stores information from previous time steps and uses it to influence the output of the cell at the current time step. The output of each LSTM cell is passed to the next cell in the network, allowing the LSTM to process and analyze sequential data over multiple time steps.

How prediction works in machine learning? ›

What does Prediction mean in Machine Learning? “Prediction” refers to the output of an algorithm after it has been trained on a historical dataset and applied to new data when forecasting the likelihood of a particular outcome, such as whether or not a customer will churn in 30 days.

How accurate is the LSTM algorithm? ›

Occasionally, training of an identical setup LSTM model ( using Keras ), on the same training data, results in a much greater achieved validation accuracy. E.g., a common scenario would be out of 10 training runs, it'd achieve a validation accuracy of 63% 8 out of 10 times, and 79% 2 out of 10 times.

What is LSTM best used for? ›

LSTMs are predominantly used to learn, process, and classify sequential data because these networks can learn long-term dependencies between time steps of data. Common LSTM applications include sentiment analysis, language modeling, speech recognition, and video analysis.

What are the disadvantages of LSTM? ›

Disadvantages: Computational Complexity: LSTMs are computationally more intensive compared to other neural network architectures like feedforward networks or simple RNNs. Training LSTMs can be slower and may require more resources.

What are the 4 gates of LSTM? ›

It is an unit structure of LSTM, including 4 gates: input modulation gate, input gate, forget gate and output gate. We describe recurrent neural networks (RNNs), which have attracted great attention on sequential tasks, such as handwriting recognition, speech recognition and image to text.

Which is better LSTM or GRU for stock price prediction? ›

GRU has two gates, reset and update gates compared to LSTM has three gates, input, forget and output. GRU does not have an output gate and the update gate in GRU does the work of input and forget gate of LSTM. GRU is computationally more efficient considering fewer parameters and need less data to generalize.

What is the most accurate stock predictor? ›

AltIndex – We found that AltIndex is the most accurate stock predictor for 2024. Unlike other providers in this space, AltIndex relies on alternative data points, such as social media sentiment and website analytics. It also uses artificial intelligence to convert its findings into risk-averse stock picks.

Is Arima better than LSTM for stock market prediction? ›

The longer the data window period, the better ARIMA performs, and the worse LSTM performs. The comparison of the models was made by comparing the values of the MAPE error. When predicting 30 days, ARIMA is about 3.4 times better than LSTM. When predicting an averaged 3 months, ARIMA is about 1.8 times better than LSTM.

What is the formula for predicting stock price? ›

For a beginning investor, an easier task is determining if the stock is trading lower or higher than its peers by looking at the price-to-earnings (P/E) ratio. The P/E ratio is calculated by dividing the current price per share by the most recent 12-month trailing earnings per share.

References

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 5643

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.