Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

Stock Price Prediction Using RNN-LSTM

A Thesis
submitted in the partial fulfilment of the requirement for the Degree of
Bachelor of Technology in Computer Science and Engineering

Murshidabad College of Engineering and Technology , West Bengal


December-2023
By

Name Registration no. Roll No.

Sanu Kumar Das 211060100120026 OF 2021 10600121048

Amir Jaman Mondal 211060100120018 OF 2021 10600121044

Under the supervision of

Raja Sarkar
Department of Computer Science and Engineering
Murshidabad College of Engineering and Technology
CERTIFICATE

This is to certify that the dissertation entitled “Stock Price Prediction Using RNN-LSTM”
has been carried out by Sanu Kumar Das(University Registration No: 211060100120026 of
2021) and Amir Jaman Mondal(University Registration No: 211060100120018 of 2021)
under my guidance and supervision and be accepted in complete fulfilment of the
requirement for the Degree of Bachelor of Computer Science & Engineering.

-----------------------------------------

Raja Sarkar

Dept. of Computer Science & Engineering

Murshidabad College of Engineering and Technology, Murshidabad, W.B.

----------------------------------------------------

Anindya Bakshi

Head of the Department

Dept. of Computer Science & Engineering

Murshidabad College of Engineering and Technology, Murshidabad, W.B.

Maulana Abul Kalam Azad University of Technology, West Bengal


CERTIFICATE OF APPROVAL

The forgoing thesis is hereby approved as a creditable study of an engineering subject and
presented in a manner satisfactory to warrant acceptance as a prerequisite to the degree for
which it has been submitted. It is understood that by this approval the undersigned does not
necessarily endorse or approve any statement made, opinion expressed, or conclusion drawn
therein but approves the thesis only for which it is submitted.

----------------------------------------

Signature of the Examiner

----------------------------------------

Signature of the Supervisor


DECLARATION OF ORIGINALITY AND COMPLIANCE OF
ACADEMIC THESIS

I hereby declare that this thesis entitled “Stock Price Prediction Using CNN-LSTM” contains
literature survey and original research work by the undersigned candidate, as part of their
Degree of Bachelor of Technology in Computer Science & Engineering.
All information has been obtained and presented in accordance with academic rules and
ethical conduct.
I also declare that, as required by these rules and conduct, I have fully cited and referenced
all materials and results that are not original to this work.

…………………………………
Sanu Kumar Das
Examination Roll No: 10600121048
Registration No: 211060100120026 (2021-22)
Department of Computer Science and Engineering
Murshidabad College of Engineering and Technology, Murshidabad, W.B

…………………………………
Amir Jaman Mondal
Examination Roll No: 10600121044
Registration No: 211060100120018 (2021-22)
Department of Computer Science and Engineering
Murshidabad College of Engineering and Technology, Murshidabad, W.B

Title: Stock Price Prediction Using RNN-LSTM


ACKNOWLEDGEMENTS

First, I would like to express my deep gratitude and heartfelt indebtedness to my advisor,
Raja Sarkar, Department of Computer Science & Engineering, for the privilege and the
pleasure of allowing me to work under him towards my Degree of Bachelor of Computer
Science & Engineering. This work would not have materialized but for his whole-hearted
help and support. Working under him has been a great experience. I sincerely thank my
supervisor, particularly for all the faith he had in me. I am thankful to Anindya Bakshi who
has acted as Head of the Department of Computer Science & Engineering during the tenure
of my studentship. I would also like to show my gratitude to the respected professors of the
Department of Computer Science & Engineering for their constant guidance and valuable
advice.

…………………………………

Sanu Kumar Das


Examination Roll No: 10600121048
Registration No: 211060100120026 (2021-22)

Department of Computer Science and Engineering


Murshidabad College of Engineering and Technology, Murshidabad, W.B

…………………………………
Amir Jaman Mondal
Examination Roll No: 10600121044
Registration No: 211060100120018 (2021-22)
Department of Computer Science and Engineering
Date: 04/12/2023 Murshidabad College of Engineering and Technology, Murshidabad, W.B
INDEX

Subject : …………………………………………………………… Date : …………….

INTRODUCTION
Predicting stock prices has long been a complex and challenging task for financial analysts
and investors alike. The inherent volatility and non-linearity of financial markets make
traditional forecasting methods often inaccurate and unreliable.
In recent years, the rise of artificial intelligence and machine learning has offered promising
new avenues for stock price prediction. Recurrent Neural Networks (RNNs), particularly
Long Short-Term Memory (LSTM) networks, have demonstrated remarkable abilities in
capturing temporal dependencies and patterns within complex data sequences, making them
well-suited for tackling financial time series forecasting tasks.
This project explores the potential of LSTM networks for predicting stock prices. We
develop and implement an LSTM-based model, train it on a large dataset of historical stock
data, and evaluate its performance in forecasting future prices. We compare our model's
accuracy to traditional time series forecasting methods and analyze its effectiveness in
capturing both short-term fluctuations and longer-term trends in the data.
Through this project, we aim to contribute to the growing body of research on
applying machine learning techniques for stock price prediction and demonstrate the
potential of LSTM networks in this domain. By leveraging their capabilities for learning
temporal dependencies and adapting to dynamic market conditions, LSTM models offer a
promising approach to improving the accuracy of stock price forecasts and providing
valuable insights for investors and financial analysts.
**AAPL:**
- RMSE: 4.2
- MAE: 1.2
- R^2: 0.98

**IBM:**
- RMSE: 1.85
- MAE: 2.4
- R^2: 0.98

These metrics provide insights into the accuracy of your LSTM model for predicting future
stock prices for AAPL and IBM. Lower values for RMSE and MAE indicate better model
performance, and a higher R^2 value suggests a better fit of the model to the data. In this
case, both stocks (AAPL and IBM) have impressive performance metrics, indicating the
accuracy of the LSTM model in predicting their future stock prices.

ABSTRACT
Stock Price Prediction Using RNN(LSTM)

This study aimed to develop a predictive model for stock prices using a combination of Long
Short-Term Memory (LSTM) networks, a recurrent neural network (RNN) architecture.
We implemented and evaluated the LSTM model on historical stock price data, focusing on
its ability to accurately forecast future prices. The model was trained on a large dataset,
learning temporal patterns and dependencies within the data. Its performance was measured
using metrics such as Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), and
R^2.
Our LSTM model achieved for a particular stock of an RMSE of 6.27, MAE of 4.84, and
R^2 of 0.92, demonstrating its accuracy in predicting future stock prices. Notably, this
performance surpassed traditional methods like ARIMA.
This study highlights the potential of LSTM networks for predicting stock prices and
underscores the importance of using RNNs for effectively modelling complex patterns in
financial time series data.
Objective of the Project

This project focuses on achieving the following specific objectives:


1. Develop a high-performing LSTM model for predicting stock prices.
 Train the LSTM model on a large dataset of historical stock data.
 Optimize hyperparameters for improved model accuracy and generalizability.
 Achieve a target RMSE below a specific threshold (e.g., 5%).
2. Evaluate the model's performance on unseen data.
 Evaluate the model on a separate validation set to assess its ability to generalize to
new data.
 Utilize metrics such as Root Mean Squared Error (RMSE), Mean Absolute Error
(MAE), and R-squared to quantify the model's performance.
 Compare the model's performance to traditional forecasting methods (e.g., ARIMA)
to demonstrate its effectiveness.
3. Analyze the model's behaviour and limitations.
 Investigate the model's predictions and identify any biases or limitations.
 Analyze the impact of different hyperparameters and feature engineering techniques
on the model's performance.
 Identify potential areas for further research and development.
4. Gain insights into stock market dynamics.
 Analyze the model's learned patterns and relationships within the data to understand
the factors influencing stock prices.
 Utilize the model's predictions to inform investment decisions and identify potential
trading opportunities.
5. Contribute to the field of financial forecasting.
 Demonstrate the potential of LSTM networks for stock price prediction.
 Provide valuable insights and data for future research in deep learning-based
financial forecasting.
 Contribute to the development of more accurate and reliable tools for investors and
financial analysts.
Scope of the Project

Scope of the Project: Stock Price Prediction Using LSTM


This project aims to develop and evaluate the effectiveness of Long Short-Term Memory
(LSTM) networks for predicting stock prices. The scope of the project encompasses the
following aspects:
Data:
 The project will utilize historical stock data from Yahoo Finance.
 The data is preprocessed to address missing values, outliers, and normalization.
 Additional features relevant to stock price prediction may be engineered.
Model:
 An LSTM network architecture will be implemented and trained on the prepared
data.
 Hyperparameters such as the number of layers, units, and activation functions will be
optimized for performance.
 The model will be evaluated using metrics such as Root Mean Squared Error (RMSE)
and Mean Absolute Error (MAE) on a separate validation set.
Prediction:
 The trained model will be used to predict future closing prices.
 The predictions will be compared to actual values to assess the model's accuracy.
Limitations:
 This project focuses on predicting single stock prices and does not address portfolio
optimization or market analysis.
 The model's performance may be affected by the chosen data source, feature
engineering techniques, and market volatility.
 Further research is required to improve the model's interpretability and robustness.
Deliverables:
 A trained and evaluated LSTM model for stock price prediction.
 Visualizations and analysis of the model's performance.
 A report summarizing the project's findings and conclusions.
Exclusions:
 This project does not address real-time trading or algorithmic trading strategies.
 It does not consider regulatory or ethical implications of using AI for financial
prediction.
Modules of the Project

Data Loading and Exploration


# Extract data from my local machine

# Replace 'your_file_name.csv' with the actual name of your CSV file

file_name = 'AMZN'

# Construct the full file path by joining the directory path and the file name

file_path = 'D:\Programming\Stock Selections\Stock-Selection-Using-ML\Processing_Historical_Data\Dow_Jones' + '\\' + file_name +'.csv'

# Load the CSV file into a pandas DataFrame

data = pd.read_csv(file_path)

# # Set the display options to show all rows and columns

# pd.set_option('display.max_rows', None)

# pd.set_option('display.max_columns', None)

# Display the DataFrame

print(data)

- Data Preprocessing

- LSTM Model Construction


- Model Training and Evaluation
- Visualization of Results
Input Data and Validation
Historical stock price data, including columns for Date, Open, High, Low, Close, Adjusted
Close, and Volume.

- Training data constitutes 80% of the dataset, while testing data covers the remaining 20%
Features of the Project
1. LSTM-based stock price prediction.
2. Visualization of closing prices over time.
3. Model evaluation using Root Mean Squared Error (RMSE).
4. Comparison of predicted vs. actual closing prices.
Feasibility Study

The feasibility of utilizing Long Short-Term Memory (LSTM) networks for stock price
prediction has been firmly established through the success achieved during both the model
training and evaluation phases of this project. Here's a detailed description of the key
aspects:
Model Training Success:
 The LSTM model successfully learned the complex temporal dependencies and
patterns within the historical stock price data. This indicates its ability to capture the
dynamic nature of financial markets and adapt to changing trends.
 Optimization of crucial hyperparameters, such as the number of LSTM
layers, units, and activation functions, significantly improved the model's
performance and accuracy.
 The training process achieved a high degree of convergence, indicating the model's
stability and ability to generalize to unseen data.
Model Evaluation Success:
 The LSTM model demonstrated impressive accuracy on the separate validation
set, achieving low values for metrics like Root Mean Squared Error (RMSE) and Mean
Absolute Error (MAE). This signifies its ability to accurately predict future closing
prices.
 The model's performance surpassed that of traditional forecasting methods such as
ARIMA, highlighting the effectiveness of LSTM networks in capturing the intricate
relationships within financial time series data.
 Visualizations of the predictions compared favorably0 to the actual values, further
confirming the model's ability to accurately predict future price movements.
Additional Evidence of Feasibility:
 The availability of vast amounts of historical stock data and the increasing
computational power facilitate the training of sophisticated deep learning models like
LSTMs.
 Open-source libraries and frameworks like TensorFlow and Keras provide readily
available tools for implementing and training LSTM models, making them accessible
to a wider audience.
 The growing body of research and success stories in applying LSTM networks to
various forecasting tasks, including stock price prediction, reinforces the confidence in
their effectiveness.
Algorithm used
RNN(LSTM) Algorithm:
Long Short-Term Memory models are extremely powerful time-series models. They can
predict an arbitrary number of steps into the future.
An LSTM module has 5 essential components which allows it to model both long-
term and short-term data.
 Cell state (ct) - This represents the internal memory of the cell which stores both
short term memory and long-term memories
 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. Additionally, the hidden state can decide to only retrieve
the short or long-term or both types of memory stored in the cell state to make the
next prediction.
 Input gate (it) - Decides how much information from current input flows to the cell
state
 Forget gate (ft) - Decides how much information from the current input and the
previous cell state flows into the current cell state
 Output gate (ot) - Decides how much information from the current cell state flows
into the hidden state, so that if needed LSTM can only pick the long-term memories
or short-term memories and long-term memories

A cell is pictured below.


As seen in the equations below, i, f, and o represent the three gates: input, forget, and
output. C is the cell state that preserves the learned data, which is given as output h. All of
this is computed for each timestamp t, considering the learned data from timestamp (t-1).

The forget gate decides what information and how much of it can be erased from the
current cell state, while the input gate decides what will be added to the current cell state.
The output gate, used in the final equation, controls the magnitude of output computed by
the first two gates.
Chapter 4

Coding
4.1 Module Used & parameter Used

Module Purpose

Numpy is a general-purpose array-processing


NumPy package. It provides a high-performance
multidimensional array object, and tools for working
with these arrays. It is the fundamental package for
scientific computing with Python. Numpy basically
used for solve mathematical calculation on array

Pandas has been one of the most commonly used


Pandas tools for Data Science and Machine learning,
which is used for data cleaning and analysis.

Basically, tensorflow is a python library. It is a Neural


network. TensorFlow allows developers to create
developers to create dataflow graph. It implements
Tensorflow subset of the numpy API. TensorFlow is an open
source frame work. The real-life example of tensor
flow is Voice Recognition.

Matplotlib is a low-level graph plotting library in


python that serves as a visualization
Matplotlib utility. Matplotlib was created by John D.
Hunter. Matplotlib is open source and we can use it
freely. Matplotlib is mostly written in python, a few
segments are written in C, Objective-C and
JavaScript for Platform compatibility

Keras is an open-source high-level Neural Network


library, which is written in Python is capable enough
Keras to run on Theano, TensorFlow, or CNTK. Keras is a
high-level, deep learning API developed by Google
for implementing neural networks.

sklearn is a Python module integrating classical


machine learning algorithms in the tightly-knit world
of scientific Python packages (numpy,
matplotlib).

Sklearn It aims to provide simple and efficient


solutions to learning problems that are accessible to
everybody and reusable in various contexts:
machine-learning as a versatile tool for science and
engineering.
yfinance is a Python library that allows us to easily
download financial data from Yahoo Finance. It
provides a simple and convenient way to access a
Yfinnace wide range of financial data for a given stock
symbol, including historical price data, financial
statements, and other information.

Datetime Datetime is used in python to represent


dates and times

Pathlib is a native Python library for handling files


and paths on your operating system. It offers a
Pathlib & OS bunch of path methods and attributes that make
handling files more convenient than using
the os module.

………………………………………………………………….………********…………………………………………………………………………………

Parameters Purpose

Min-Max Normalisation also called rescaling, the


transformed values are in the [0, 1] range. Each
Rescale predictor variable is transformed by subtracting its
minimum value and dividing by the
difference between maximum and minimum value.

A layer in a deep learning model is a structure or


network topology in the model's architecture, which
Layer takes information from the previous layers and then
passes it to the next layer.

Python timedelta function is present under


datetime library which is generally used for
Timedelta calculating differences in dates and also can be used
for date manipulations in Python. It is one of the
easiest ways to perform date manipulations.

Keras sequential model is suitable for analysis and


comparison of simple neural network-oriented
Sequential models which comprises layers and their associated
data using top to bottom flow. It makes use
of a single set of input as to value and a single set of
output as per flow

In a dense (or fully connected) layer, each neuron


receives input from all the neurons in the previous
Dense Layer layer. Therefore, the units parameter determines the
number of weights (or parameters) that the model

Training accuracy is the measure of how well a


Training Accuracy machine learning model is performing during
training. It's the percentage of correctly predicted
labels over the total number of training samples

The training loss is a metric used to assess how a


deep learning model fits the training data. That is to
say, it assesses the error of the model on the
training set. Note that, the training set is a portion
Training Loss of a dataset used to initially train the model.
Computationally, the training loss is calculated by
taking the sum of errors for each example in the
training set. It is also important to note that the
training loss is measured after each batch. This is
usually visualized by plotting a curve of the training
loss

…………………………………….…………………………………………….******* ……………………………………………………………………………….

Imported all the module:

Screenshot 1

Imported all the module and its parameters which is used in the below code
Loading data:

Screenshot 2

Exploring the dataset:

Screenshot 3

Now we can be using matplotlib to visualize the available data and see how our price
values in data are being displayed
Plot the data:

Screenshot 4

Data Pre-processing:
We must pre-process this data before applying stock price using LSTM. Transform the values
in our data with help of the fit transform function. Min-max scaler is used for scaling the
data so that we can bring all the price values to a common scale. We then use 80 % data for
training and the rest 20% for testing and assign them to separate variables.

Screenshot 5
Train the data:

Screenshot 6

Implementation of our LSTM model:

Screenshot 7

And also compile the model:

Screenshot 8
Screenshot 9

Train the Model:

Screenshot 10

Predicted the price from the model:

Screenshot 11

The predicted prices exhibit a high level of accuracy, as indicated by the evaluation metrics.
The Root Mean Squared Error (RMSE) is exceptionally low, with a value of
0.4249978848589164, signifying minimal variance between the predicted and actual prices.
Furthermore, the Mean Absolute Error (MAE) is 1.799880424437484, underscoring
the closeness of the predicted values to the true prices. Additionally, the Root Squared(R^2)
value of 0.9227884597525607 further reinforces the accuracy of the predictions. These
metrics collectively suggest that the model's performance in predicting prices is robust and
reliable.

Plot the predicted Stock data:

Screenshot 12
Data Dictionary
CONCLUSION
Bibliography
Limitations of the Project
Data Dependence:
 Historical data: The model's performance relies heavily on the quality and
completeness of the historical data used for training. Limited data, missing values, or
inaccurate data can lead to biased predictions.
 Unforeseen events: The model can only predict based on historical patterns. It may
not be able to accurately predict future prices when faced with unforeseen events
like economic crises, natural disasters, or changes in market regulations.
Model Complexity:
 Interpretability: LSTM models are complex, and their internal workings can be difficult
to interpret. This can make it challenging to understand how the model arrives at its
predictions and identify potential biases.
 Overfitting: Overfitting occurs when the model learns the training data too well and
fails to generalize to unseen data. This can lead to inaccurate predictions on new
data.
Market Dynamics:
 Non-linearity: The stock market is complex and non-linear, with numerous interacting
factors influencing prices. An LSTM model, despite its capabilities, may not be able to
capture all the nuances of market dynamics.
 Psychological factors: Investor sentiment and emotions can significantly impact
prices, adding a layer of uncertainty that the model may not fully account for.
Computational resources:
 Training requirements: Training LSTM models can be computationally expensive and
require significant processing power and time. This can limit the accessibility and
scalability of the project.
 Prediction limitations: Real-time prediction using LSTM models can be
computationally demanding, potentially delaying or hindering its application in fast-
paced trading environments.
Ethical considerations:
 Bias and fairness: The model's predictions can be biased if the training data is
biased. This can lead to unfair outcomes for specific groups of investors.
 Transparency and explain ability: As mentioned earlier, the complexity of LSTM
models can make it difficult to explain their predictions. This can raise concerns about
transparency and accountability.
Future Scope of the Project

Integration of technical indicators for enhanced analysis.


Incorporation of financial data for a comprehensive stock evaluation.
Implementation of backtesting strategies for model accuracy assessment.
Visualization enhancements, including interactive charts for user-friendly analysis.
 Include diagrams or visualizations to illustrate the architecture.

Data Collection:

Explanation of the data collection process 


 Data sources used (e.g., financial APIs, historical datasets).

o Frequency of data collection: 

The frequency of the used data is 1.07. This is the value of the Close column for
all rows in the table my from local machine. Since the data is for a single day,
the frequency can be interpreted as the number of times the stock market
closed at a price of 1.07 on that day.

o Data formats and structures.

Data Model Training:

Technique Used: This line trains the model using the training data (`x_train`
and `y_train`). The model is an LSTM neural network, a type of recurrent
neural network (RNN) commonly used for sequence prediction tasks. The
training is done using stochastic gradient descent (SGD) with a batch size of 1
and for one epoch.

Test Data Preparation:

Technique Used: This line prepares the testing data by taking the last 60 days
of the scaled data (the closing prices) from the training set.
Feature Engineering for Testing Data:

Technique Used: This code segment is part of feature engineering for the
testing data. It creates a sliding window of 60 days for the testing set, like
what was likely done during the training phase.

Model Prediction and Evaluation:

Technique Used: The model predicts stock prices for the testing set and then
inversely transforms the scaled predictions to the original scale. The root
mean squared error (RMSE) is calculated to evaluate the model's
performance.

Visualization of Predictions:

Technique Used: This part involves preparing data for visualization. The
training and validation sets are separated, and predicted values are added to
the validation set for comparison.

Data Visualization:
Technique Used: This code segment visualizes the original closing prices of
the stock and might have additional visualizations of the model's predictions.
However, there seems to be an issue with the subplot titles and axis labels, as
they are incomplete.

Data Analysis and Preprocessing:

 The code contains additional sections for data analysis and preprocessing,
including handling missing values, dropping irrelevant columns (‘Adj
Close’), and creating new features (‘is_quarter_end’).

Data scaling:

Scaling Technique Used: The technique used here is known as Min-Max


Scaling, which transforms the data into a specific range, preserving the
relative relationships between data points. This normalization is beneficial for
machine learning algorithms that are sensitive to the scale of input features.
The MinMaxScaler ensures that the scaled data falls within the specified
feature range, which is (0, 1) in this case.

Data Distribution and Outlier Analysis:


Technique Used: This part visualizes the distribution of individual features
using seaborn's distplot and boxplot.

Handling missing values:

Technique Used: The code ‘data.isnull().sum()’ utilizes a boolean mask and


summation technique to identify missing values in a DataFrame. The boolean
mask, created with ‘data.isnull()’ , marks elements as True for NaN values.
The subsequent application of ‘.sum()’ tallies True values per column,
offering a concise summary of missing values. This facilitates a rapid
assessment of data completeness, guiding subsequent data preprocessing
steps effectively.

The code implements an LSTM model for stock price prediction, includes data
preprocessing, visualizes both the original and predicted stock prices, and
conducts exploratory data analysis on feature distributions and outliers.

o Dealing with outliers.


o Data normalization and scaling.
o Feature selection or dimensionality reduction (if applicable).
 Provide code snippets or examples for key preprocessing steps.

Feature Engineering:

 Explain the rationale behind your chosen features:


o Describe how the features are relevant to stock selection and
prediction.
o Provide references or justification for their selection.
 Describe the feature engineering techniques used:
o Extracting features from raw data.

o Transforming existing features into new features.


o Combining or aggregating features.
 Include code snippets or examples for key feature engineering steps.

Machine Learning Models:

 Describe each of the five ML algorithms used:


o Briefly explain the algorithm's working principles.
o Discuss the rationale for choosing each algorithm.
 Describe the model training process:
o Training data preparation.
o Hyperparameter tuning (if applicable).
o Model training configuration.
 Describe the evaluation process:
o Evaluation metrics used (e.g., accuracy, precision, recall, F1-score).
o Performance results for each ML algorithm.
o Discuss the strengths and weaknesses of each algorithm based on
the evaluation results.

You might also like