Stock Price Prediction Using LSTM

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

MINOR PROJECT REPORT

ON
Stock Price Prediction Using LSTM
Submitted By:
Kunal Garg(00155102719)
Vipin(01655102719)

BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE AND ENGINEERING
At
MAHAVEER SWAMI INSTITUTE OF
TECHNOLOGY

JAGDISHPUR, SONEPAT

AFFILIATED TO GGSIPU DWARKA, DELHI


Table of Contents

1. PROBLEM DEFINITION AND SCOPE OF PROJECT

1.1 Introduction

1.2 Motivation for Work

1.3 Project Scope

1.4 Technologies and hardware requirements

2. LITERATURE SURVEY

2.1 Existing System

3. METHODOLOGY OF THE PROJECT

3.1 Proposed System

4. Design

4.1 System Architecture

4.2 UML Diagrams

4.3 DFD

5. Experiment Analysis and Sample Code.

6. Bibliography
Declaration

We, Kunal Garg (02955202719) and Vipin(01655102719)

students of Fourth Year B.Tech., in the Department of Computer

Science and Engineering from MVSIT hereby declare that the work

presented in this report entitled “ Stock Price Prediction using

LSTM” in fulfillment of the requirement for the award of the degree

Bachelor of Technology in Computer Science Engineering

submitted in CSE Department, Mahavir Swami Institute of

Technology affiliated to Guru Gobind Singh Indraprastha

University, New Delhi, during the academic year 2019-2023 is an

authentic record of our Work carried out during my degree under the

guidance of MS. SHRUTI MAM. The work reported in this has not

been submitted by me for the award of any other degree or diploma.

Kunal Garg (00155102719)

Vipin (01655102719)
Acknowledgement

We would like to express our deep gratitude to our guide Ms. SHRUTI for her

valuable guidance, faculty of computer science and engineering, BMCEM and

timely suggestions during the entire duration of our dissertation work, without

which this work would not have been possible. We would also like to convey our

deep regards to all other faculty members of BMCEM, who have bestowed their

great effort and guidance at appropriate times without which it would have been

very difficult on our part to finish this work. Finally, we would also like to thank

our friends for their advice and pointing out our mistakes, parents, and classmates

for their encouragement throughout our project period. And last but not least, we

thank everyone for supporting us directly or indirectly in completing this project

successfully.
1. Problem Definition and Scope of Project
1.1 INTRODUCTION

The stock market is defined as a collection of markets and exchanges where


regular activities such as buying and selling shares of publicly traded
companies occur. It is a market for trading shares of publicly traded
companies. The primary market is where companies sell their stock to the
general public in an initial public offering (IPO). This is done to raise funds.

People primarily buy stocks in the hope that their value will rise in the
future. However, there is always uncertainty in the stock market, which
makes people reluctant to invest their money in it. As a result, we require a
technique that can forecast stock market prices so that people can invest
their money.

Time-series prediction is a widely used technique in many real-world


applications, including weather forecasting and financial market prediction.
It predicts the next time unit using continuous data collected over a period of
time. Many time-series prediction algorithms have been demonstrated to be
effective in practice. The most widely used algorithms today are based on
Recurrent Neural Networks (RNN) and their variants, Long-short Term
Memory (LSTM), and Gated Recurrent Units (GRU). The stock market is a
common example of time-series data, and many researchers have studied it
and proposed various models. The LSTM model is used in this project to
forecast the stock price.

The operation of the LSTM is divided into four steps:

1) The first step in the LSTM is to identify unnecessary information that will
be discarded from the cell state. A sigmoid layer makes this decision. This
layer is also known as the forget gate layer.
2) The following step determines what new information will be stored in the
cell state. The cell state is critical to the LSTM. This consists primarily of
two steps:
a) The input gate layer, a sigmoid layer, determines which values will
be updated.
b) Tanh layer generates a vector of new candidate values for that state.
The tanh function is a squashing function, which means it converts the
value to a value between -1 and +1.
3) To update the old cell state Ct-1 into the new cell state Ct, first multiply it
by Ft while forgetting the things we decided to forget earlier. Then we
multiply it by *Ct. This is the new candidate value, which is scaled
according to how much we decided to update each state value.
4) We will run a sigmoid layer to determine which part of the cell state we
will output. Then we run the cell state through tanh and multiply it by the
sigmoid state output to output only the parts we want.
1.2 MOTIVATION FOR WORK
Businesses are primarily concerned with customer satisfaction and product
reviews. Changes in social media sentiment have been shown to correlate with
changes in stock markets. Identifying and resolving customer complaints increases
customer satisfaction and an organization's trustworthiness. As a result, an
unbiased automated system is required to classify customer reviews about any
problem. Companies may have mountains of customer feedback collected in
today's environment, where we are understandably suffering from data overload
(though this does not imply better or deeper insights), but it is still impossible for
mere humans to analyze it manually without error or bias.
Companies with the best intentions are frequently trapped in an information
vacuum. You know you need insights to help you make decisions, and you know
you're lacking them, but you're not sure how to get them.
Sentiment analysis provides insights into the most important issues from
customers’ perspectives. Because sentiment analysis can be automated, decisions
can be based on a large amount of data rather than pure intuition.

1.3 Project Scope


Time Series forecasting and modeling are critical in data analysis. Time series
analysis is a subset of statistics that are widely used in fields such as econometrics
and operations research. Time series are commonly used in analytics and data
science. Stock prices are volatile, and their value is determined by a variety of
factors. This project's main goal is to forecast stock prices using long short-term
memory (LSTM).
1.4 Technologies and hardware requirements
System Configuration
This project can run on commodity hardware. We ran entire project on an Ryzen 7
processor with 16 GB Ram, 2 GB Nvidia Graphic Processor, It also has 2 cores
which runs at 1.7 GHz, 2.1 GHz, respectively. The first part of the is training phase
which takes 10-15 mins of time, and the second part is the testing part which only
takes a few seconds to make predictions and calculate accuracy.
Hardware Requirements:
• RAM: 4 GB
• Storage: 500 GB
• CPU: 2 GHz or faster
• Architecture: 32-bit or 64-bit 5.1.2 Software requirements
• Python 3.5 in Jupyter-Notebooks is used for data pre-processing, model training,
and prediction.
• Operating System: Windows 7 and above or Linux-based OS or MAC OS.
• Deep Learning Environment needs to be set up. Installation of TensorFlow is
required.
2. LITERATURE SURVEY

2.1 Existing System

2.1.1 Stock Market Prediction Using Machine Learning


The research work done by V Kranti Sai Reddy Student, ECM, Srinidhi
Institute of Science and Technology, Hyderabad, India. In the finance world
stock trading is one of the most important activities. Stock market prediction
is an act of trying to determine the future value of a stock other financial
instrument traded on a financial exchange. This paper explains the prediction
of a stock using Machine Learning. The technical and fundamental or the
time series analysis is used by the most of the stockbrokers while making the
stock predictions. The programming language is used to predict the stock
market using machine learning is Python. In this paper we propose a
Machine Learning (ML) approach that will be trained from the available
stocks data and gain intelligence and then uses the acquired knowledge for
an accurate prediction. In this context this study uses a machine learning
technique called Support Vector Machine (SVM) to predict stock prices for
the large and small capitalizations and in the three different markets,
employing prices with both daily and up-to-the-minute frequencies.

2.1.2 Automated Stock Price Prediction Using Machine Learning


The research work done by Mariam Moukalled Wassim El-Hajj Mohamad
Jaber Computer Science Department American University of Beirut.
Traditionally and to predict market movement, investors used to analyse the
stock prices and stock indicators in addition to the news related to these
stocks. Hence, the importance of news on the stock price movement. Most of
the previous work in this industry focused on either classifying the released
market news as (positive, negative, neutral) and demonstrating their effect
on the stock price or focused on the historical price movement and predicted
their future movement. In this work, we propose an automated trading
system that integrates mathematical functions, machine learning, and other
external factors such as news’ sentiments for the purpose of achieving better
stock prediction accuracy and issuing profitable trades. Particularly, we aim
to determine a certain stock’s price or trend for the coming end-of-day
considering the first several trading hours of the day. To achieve this goal,
we trained traditional machine learning algorithms and created/trained
multiple deep learning models taking into consideration the importance of
the relevant news. Various experiments were conducted, the highest
accuracy (82.91%) of which was achieved using SVM for Apple Inc.
(AAPL) stock.

2.2.6 Stock Price Correlation Coefficient Prediction with ARIMALSTM


Hybrid Model
The research work done by Hyeong Kyu Choi, B.A Student Dept. of
Business Administration Korea University Seoul, Korea. Predicting the price
correlation of two assets for future time periods is important in portfolio
optimization. We apply LSTM recurrent neural networks (RNN) in
predicting the stock price correlation coefficient of two individual stocks.
RNNs are competent in understanding temporal dependencies. The use of
LSTM cells further enhances its long-term predictive properties. To
encompass both linearity and nonlinearity in the model, we also adopt the
ARIMA model. The ARIMA model filters linear tendencies in the data and
passes on the residual value to the LSTM model. The ARIMA-LSTM hybrid
model is tested against other traditional predictive financial models such as
the full historical model, constant correlation model, single-index model,
and multi-group model. In our empirical study, the predictive ability of the
ARIMA-LSTM model turned out superior to all other financial models by a
significant scale. Our work implies that it is worth considering the
ARIMALSTM model to forecast the correlation coefficient for portfolio
optimization.

2.2.6 Stock Price Correlation Coefficient Prediction with ARIMALSTM


Hybrid Model
The research work done by Hyeong Kyu Choi, B.A Student Dept. of
Business Administration Korea University Seoul, Korea. Predicting the price
correlation of two assets for future time periods is important in portfolio
optimization. We apply LSTM recurrent neural networks (RNN) in
predicting the stock price correlation coefficient of two individual stocks.
RNNs are competent in understanding temporal dependencies. The use of
LSTM cells further enhances its long-term predictive properties. To
encompass both linearity and nonlinearity in the model, we also adopt the
ARIMA model. The ARIMA model filters linear tendencies in the data and
passes on the residual value to the LSTM model. The ARIMA-LSTM hybrid
model is tested against other traditional predictive financial models such as
the full historical, constant correlation, single-index, and multi-group
models. In our empirical study, the predictive ability of the ARIMA-LSTM
model turned out superior to all other financial models by a significant scale.
3. METHODOLOGY OF THE PROJECT

3.1 Proposed System

Prediction methods are broadly classified into two types: statistical methods and artificial
intelligence methods. Statistical techniques include the logistic regression model, the ARCH
model, and others. Examples include multi-layer perceptron’s, convolutional neural networks,
naïve backpropagation networks, single-layer LSTM, support vector machines, recurrent neural
networks, and other artificial intelligence methods. They made use of a Long Short-Term
Memory Network (LSTM).

3.1.1 Long-short term memory


Long short-term memory (LSTM) is a type of recurrent neural-network architecture in which
the vanishing gradient problem is solved. LSTMs are capable of learning very long-term
dependencies, and they work tremendously well on a large variety of problems. LSTMs are
first introduced by Hochester et al. in 1997. In addition to the original authors, many researchers
contributed to the architecture of modern LSTM cells. Recurrent neural networks are generally
designed in a chain-like structure, looping back to the previous layers. In standard RNNs, this
looping module will have a very simple structure, as shown. This structure can be a simple tanh
layer controlling the flow.

Simple structure of RNNs looping module

3.1.2 Working of LSTM


LSTM is a special network structure with three “gate” structures. Three gates are placed in an
LSTM unit, called input gate, forgetting gate and output gate. While information enters the
LSTM’s network can be selected by rules. Only the information that conforms to the algorithm
will be left, and the information that does not conform will be forgotten through the forgetting
gate.

3.1.3 LSTM Architecture

Forget Gate: A forget gate is responsible for removing information from the cell state.
• The information that is no longer required for the LSTM to understand things or information
that is less important is removed via a filter's multiplication.
• This is required for optimizing the performance of the LSTM network.
• This gate takes in two inputs; h_t-1 and x_t. h_t-1 is the hidden state from the previous cell, or
the output of the previous cell and x_t is the input at that particular time step

Input Gate:
1. Regulating what values need to be added to the cell state by involving a sigmoid function.
This is basically very similar to the forget gate and acts as a filter for all the information from hi-
1 and x_t.
2. Creating a vector containing all possible values that can be added (as perceived from h_t-1
and x_t) to the cell state. This is done using the tanh function, which outputs values from -1 to
+1.
3. Multiplying the value of the regulatory filter (the sigmoid gate) to the created vector (the tanh
function) and then adding this useful information to the cell state via addition operation.

Output Gate: The functioning of an output gate can again be broken down into three steps:

• Creating a vector after applying tanh function to the cell state, thereby scaling the values to the
range -1 to +1.
• Making a filter using the values of h_t-1 and x_t to regulate the values that need to be output
from the vector created above. This filter again employs a sigmoid function.
• Multiplying the value of this regulatory filter to the vector created in step 1 and sending it out
as output and to the next cell’s hidden state.

3.1.4 Pseudo-Code for implementation of the LSTM Model


# LSTM
• Inputs: dataset
• Outputs: RMSE of the forecasted data

• # Split dataset into 75% training and 25% testing data
• size = length(dataset) * 0.75
• train = dataset [0 to size]
• test = dataset [size to length(dataset)]

• # Procedure to fit the LSTM model
• Procedure LSTM Algorithm (train, test, train_size, epochs)
• X = train
• y = test
• model = Sequential ()
• model.add(LSTM(50), stateful=True)
• model.compile(optimizer='adam', loss='mse')
• model.fit(X, y, epochs=epochs, validation_split=0.2)
• return model

• # Procedure to make predictions
• Procedure getPredictonsFromModel (model, X)
• predictions = model.predict(X)
• return predictions

• epochs = 100
• neurons = 50
• predictions = empty
16
• # Fit the LSTM model
• model = LSTMAlgorithm (train, epoch, neurons)

• # Make predictions
• pred = model.predict(train)
• # Validate the model

• n = len(dataset)

• error = 0
• for i in range(n): error += (abs(real[i] - pred[i])/real[i]) * 100
• accuracy = 100 - error/n
4. Design
4.1 System Architecture

Structure Chart: A structure chart (SC) in software engineering and


organizational theory is a chart that shows the breakdown of a system to its lowest
manageable levels.
4.2 UML Diagrams
A UML diagram is a partial graphical representation (view) of a model of a system
under design, implementation, or already in existence. UML diagram contains
graphical elements (symbols) - UML nodes connected with edges (also known as
paths or flows) - that represent elements in the UML model of the designed system.
Use Case Diagram:
4.3 Data Flow Diagram:
A DFD is a type of diagram that represents a workflow or process. A DFD can also
be defined as a diagrammatic representation of an algorithm, a step-by-step
approach to solving a task. The DFD shows the steps as boxes of various kinds and
their order by connecting the boxes with arrows.
5. Experimental Analysis

5.1 Downloading the Data


1. Getting Data from Alphavantage
2. Getting Data from Kaggle

5.2 Data Exploration


Output

5.3 Data Visualization


5.4 Splitting Data into a Training set and a Test set

5.5 Normalizing the Data

5.6 Data Generator


This data generator will have a method called. Unroll_batches(...) will output a set
of num_unrollings batches of input data obtained sequentially, where a pack of
data is of size [batch_size, 1]. Then each collection of input data will have a
corresponding output batch.

5.7 Data Augmentation


Output

5.8 Defining Hyperparameters

5.9 Defining Inputs and Outputs


5.10 Defining Parameters of the LSTM and Regression layer

5.11 Calculating LSTM output and Feeding it to the regression layer to get
final prediction

5.12 Loss Calculation and Optimizer


5.13 Prediction Related Calculations

5.14 Running the LSTM


5.15 Visualizing the prediction
6. Bibliography

[1] Stock Price Prediction Using LSTM on Indian Share Market by Achyut Ghosh,

Soumik Bose1, GiridharMaji, Narayan C. Debnath, Soumya Sen

[2] S. Selvin, R. Vijayakumar, E. A. Gopalkrishnan, V. K. Menon and K. P. Soman

- Stock price prediction using LSTM, RNN and CNN-sliding window model -

2017.

[3] Murtaza Roondiwala, Harshal Patel, Shraddha Varma, “Predicting Stock Prices

Using LSTM” in Undergraduate Engineering Students, Department of Information

Technology, Mumbai University, 2015.

[4] Xiongwen Pang, Yanqiang Zhou, Pan Wang, Weiwei Lin, “An innovative

neural network approach for stock market prediction”, 2018

[5] Ishita Parmar, Navan Shu Agarwal, Sheirsh Saxena, Ridam Arora, Shikhin

Gupta, Himanshu Dhiman, Lokesh Chouhan Department of Computer Science and

Engineering National Institute of Technology, Hamirpur – 177005, INDIA - Stock

Market Prediction Using Machine Learning. [6] Pranav Bhat Electronics and

Telecommunication Department, Maharashtra Institute of Technology, Pune.

Savitribai Phule Pune University - A Machine Learning Model for Stock Market

Prediction.

[7] Anurag Sinha Department of computer science, Student, Amity University

Jharkhand Ranchi, Jharkhand (India), 834001 - Stock Market Prediction Using


Machine Learning.

[8] V Kranthi Sai Reddy Student, ECM, Sreenidhi Institute of Science and

Technology, Hyderabad, India - Stock Market Prediction Using Machine Learning.

You might also like