0% found this document useful (0 votes)
9 views22 pages

Rishi

Uploaded by

Rishi Chourasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

Rishi

Uploaded by

Rishi Chourasia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1

Indian Institute of Technology Roorkee

Stock price prediction using Time series and NLP

Training Seminar Report

Submitted by:

Rishi Chourasia (21112089)

1
2

Student Declaration

I, Rishi Chourasia, enrolment number 21112089, of the Department of Chemical Engineering, IIT
Roorkee, hereby certify that I have carried out my summer internship at Oasis Info byte from May 1,
2024 to July 5, 2024. This report is being submitted for the evaluation of the CHN 499 Training
Seminar and is an authentic account of the work I carried out during my project.

23/08/2024

Date of Submission Signature

2
3

3
4

Abstract

The accurate prediction of stock prices has long been a critical challenge in financial markets,
offering significant potential benefits for investors and traders. Despite extensive research, achieving
reliable and precise stock price forecasts remains an unresolved issue due to the inherent volatility
and complexity of financial time series data. This study addresses the gap by employing advanced
time series forecasting techniques, including ARIMA and LSTM, to predict stock prices. The
research methodology involves data preprocessing, model training, and evaluation using historical
stock price data, followed by a comparative analysis of the model’s performance based on the Mean
Absolute Error (MAE). Our findings indicate that the LSTM model outperforms traditional methods,
providing higher accuracy in stock price predictions. This research demonstrates the potential of deep
learning approaches in financial forecasting, contributing valuable insights into model selection and
application for improved predictive accuracy in stock markets.

4
5

INDEX

1. Introduction 9-10

2. Literature Review 11-12

3. Research Methods 13-20

21-23

4. Results and Discussions

24

5. Conclusions

5
6

List of Figures Page no.

12
Fig 1 Different Models used for stock price prediction Research

Fig 2 Initial part of dataset 13

14
Fig 3 Price history of SPY Jan 1993-sept 2020

14
Fig 4 Train Valid Test split of Data

15
Fig. 5 Naïve Forecast Model

Fig 6 SMA-20 Vs Actual 15

16
Fig 7 SMA-5 Forecast Vs Actual

16
Fig 8 Season Trend Decomposition for ARIMA

16
Fig 9 ARIMA Forecast Vs actual

17
Fig 10 RNN Forecast Vs Actual

17
Fig 11 LSTM 20 Forecast VS Actual

18
Fig. 12 LSTM 30 Forecast VS Actual

6
7

18
Fig. 13 Preprocess with CNN forecast 30

18
Fig. 14 Full CNN model 20

21
Fig 15 Mean Average Error of all models

7
8

I. INTRODUCTION

THE stock market is characterized by its dynamic, unpredictable, and non-linear nature. Predicting
stock prices is a challenging task due to the influence of various factors including political conditions,
global economic trends, and a company’s financial performance. To maximize profit and minimize
losses, developing techniques to predict stock values in advance by analyzing historical trends can be
highly beneficial for making informed decisions about stock market movements.

Traditionally, stock price prediction methods fall into two main categories: technical analysis and
qualitative analysis. Technical analysis relies on historical stock prices and trading volumes to forecast
future prices, while qualitative analysis considers external factors such as company profiles, market
conditions, political and economic factors, and textual information from financial news articles and
social media. Recently, advanced intelligent techniques, particularly those based on machine learning,
have been increasingly applied to stock market prediction. These techniques can handle the vast and
non-linear nature of stock market data, identifying hidden patterns and complex relationships within
large datasets. Machine learning approaches have shown significant improvements in prediction
efficiency compared to traditional methods.

In this research, we explore various models and methods for predicting stock prices using the
SPY ETF dataset, employing Mean Absolute Error (MAE) as the performance metric to compare the
efficiency of different models. Our analysis begins with the Naive Forecast Model, a simple baseline
that assumes the stock price tomorrow will be the same as today.
Despite its simplicity, this model provides a useful benchmark against which to measure more
complex approaches.

Moving Average Models are another straightforward technique we investigate. By averaging the past
prices over a fixed window (5 days for SMA 5 and 20 days for SMA 20), these models smooth out
short-term fluctuations and highlight longer-term trends. However, their performance is often limited
by their inability to capture more complex patterns in the data.

We then apply the ARIMA Model, a classical time series forecasting technique that combines
autoregression, differencing, and moving averages. This model is adept at handling data with trends

8
9

and seasonality, making it more robust than simpler methods like the Naive or Moving Average
Models.

Our research also includes neural network approaches implemented using Keras/TensorFlow. The
Linear Model serves as a basic neural network with no hidden layers, while the Dense Model
incorporates multiple layers to capture non-linear relationships. These models demonstrate the power
of neural networks in handling complex datasets, although their performance can vary depending on
the network architecture and training process.

Recurrent Neural Networks (RNNs) are particularly suited for time series prediction due to their
ability to maintain a memory of previous inputs. We leverage this capability to model the temporal
dependencies in stock prices. Long Short-Term Memory (LSTM) networks, a specific type of RNN,
further enhance this by learning long-term dependencies, making them especially effective for our
dataset. The LSTM model in particular, exhibits the lowest MAE in our study, highlighting its
superior performance in predicting stock prices.

Finally, we explore a hybrid approach using CNN pre- processing for RNN and a full CNN with a
Wavenet-like architecture. The CNN preprocessing step extracts hierarchical features from the data,
which are then fed into the RNN for sequential learning. The full CNN model aims to capture both
local and global patterns in the data. However, these sophisti- cated models, while promising in
theory, do not outperform the LSTM model in our experiments, possibly due to the specific
characteristics of the SPY ETF dataset.

The results of our comparative analysis, as depicted in the attached image, provide valuable insights
into the performance of these models. The LSTM 30-day model stands out with the lowest MAE,
indicating its superior capability in stock price prediction. Conversely, model such as SMA 20 exhibit
higher MAEs, reflecting their lower prediction accuracy. By evaluating the performance of these
diverse models, this study aims to identify the most effective approaches for stock price prediction,
contributing to the development of robust predictive models in financial markets.

9
10

II. LITERATURE REVIEW

Stock price prediction has been a subject of extensive research due to its significant implications in
finance and eco- nomics. The challenge of accurately forecasting stock prices stems from the
inherent volatility and complexity of financial markets. This section reviews various approaches and
models used in stock price prediction, emphasizing the evolution from traditional statistical methods
to advanced machine learning techniques.

Early approaches to stock price prediction heavily relied on technical analysis, which involves the use
of historical price and volume data to predict future stock movements. Tech- niques such as Moving
Average (MA), Exponential Moving Average (EMA), and Moving Average Convergence Diver-
gence (MACD) have been widely used. These methods are based on the assumption that historical
price patterns tend to repeat over time.

The AutoRegressive Integrated Moving Average (ARIMA) model, proposed by Box and Jenkins
in the 1970s, is a classical statistical approach to time series forecasting. ARIMA models combine
autoregression, differencing, and moving averages to capture various aspects of the time series data.
Studies have shown ARIMA’s effectiveness in capturing linear depen- dencies in stock price data,
making it a robust baseline for comparison .

Artificial Neural Networks (ANNs) have become a popu- lar choice for stock price prediction due
to their ability to model complex non-linear relationships. ANNs, inspired by the human brain,
consist of interconnected neurons that learn to map inputs to outputs through a training process.
Studies have shown that ANNs can effectively predict stock prices by learning from historical data
and identifying hidden patterns that are not apparent with traditional methods .

Recurrent Neural Networks (RNNs) are specifically de- signed for sequential data, making them
suitable for time series forecasting. RNNs maintain a memory of previous inputs, allowing them to
capture temporal dependencies in stock price data. Long Short-Term Memory (LSTM) networks, an
extension of RNNs, address the issue of long-term depen- dency learning by incorporating memory
cells that can retain information over longer periods. LSTMs have shown superior performance in
stock price prediction, as they can capture both short-term fluctuations and longterm trends .

10
11

Convolutional Neural Networks (CNNs) have primarily been used in image processing but have
found applications in time series forecasting as well. CNNs can capture local patterns in data through
convolutional layers, which can be beneficial for identifying short-term trends in stock prices. Recent
advancements involve combining CNNs with RNNs to leverage the strengths of both architectures.
For instance, CNN preprocessing for RNN models can enhance feature extraction before sequential
learning, improving prediction accuracy .

Several comparative studies have been conducted to evaluate the performance of different models
in stock price prediction.
These studies typically use metrics such as Mean Absolute Error (MAE), Mean Squared Error
(MSE), and Root Mean Squared Error (RMSE) to assess prediction accuracy. Results generally
indicate that advanced machine learning models, particularly LSTMs and hybrid models,
outperform traditional statistical methods and simpler machine learning algorithms .

The evolution of stock price prediction methods reflects the increasing complexity and capability
of the models employed. While traditional methods like ARIMA and technical analysis provide a
solid foundation, advanced machine learning tech- niques such as SVMs, ANNs, RNNs, LSTMs,
and hybrid mod- els offer significant improvements in accuracy and robustness. This literature
review highlights the ongoing advancements in this field and underscores the potential of integrating
multiple approaches to enhance stock price prediction.

Fig. 1. Different models used for Stock price prediction research

11
12

III. RESEARCH METHODS

The research methodology encompasses several steps, in- cluding data collection, preprocessing,
model selection, train- ing, evaluation, and validation.

A. Data Collection

For this study, we utilized the SPY ETF dataset, which tracks the performance of the S&P 500
Index. The dataset includes historical stock prices, including opening, closing, high, and low prices,
as well as trading volumes. The data spans several years, providing a rich source for time series
analysis.

B. Data Preprocessing

Data preprocessing is a critical step to ensure the quality and relevance of the data used for
modeling. The preprocessing steps include:

Fig. 2. Initial part of dataset

1) Data Cleaning:

• Handling Missing Values: Missing values in the dataset were handled using forward fill or
interpolation methods to ensure continuity in the time series data.
• Outlier Detection and Removal Statistical methods and visualization techniques were employed
to detect and remove outliers that could skew the results.
2) Feature Engineering:

• Creating New Features New features such as moving averages (5-day, 20-day), volatility indices,
and lagged returns were created to capture additional patterns and trends in the stock prices.
• Normalization: All feature values were normalized using min-max scaling to ensure that they
are on a comparable scale, which is particularly important for neural network models.

12
13

3) Train-Test Split:

• The dataset was split into training and testing sets. Typically, 70-80% of the data was used for
training, and the remaining 20-30% was reserved for testing. The split was done chronologically
to preserve the time series nature of the data.

Fig. 3. Price history of SPY Jan-1993 to Sept-2020

C. Model Selection

A variety of models were employed to predict stock prices, each leveraging different aspects of the
time series data. The models used include:

Fig. 4. Train Valid Test Split of Data

1) Naive Forecast Model: This model uses the most recent observation as the forecast for the next
period. It serves as a baseline for comparing the performance of more complex models.

13
14

Fig. 5. Naive Forecast model vs Actual zoomed 2)


Moving Average Models:
• Simple Moving Average (SMA): Computes the average of stock prices over a fixed number of
past days.
• Exponential Moving Average (EMA: Gives more weight to recent prices, making it more
responsive to recent changes.
3) ARIMA Model:

• The AutoRegressive Integrated Moving Average (ARIMA) model combines autoregression,


differencing, and moving averages to capture the linear dependencies in the time series data.
The parameters (p, d, q) were selected using grid search and AIC/BIC criteria.
4) Linear and Dense Models Using Keras/TensorFlow:

• Linear Model: A simple linear regression model predict- ing future stock prices based on
historical data.
• Dense Model: A feed-forward neural network with mul- tiple layers and neurons designed to
capture non-linear relationships in the data.

Fig. 6. SMA20 vs Actual

14
15

Fig. 7. SMA5 Forecast vs Actual

Fig. 8. Season trend Decomposition for ARIMA

Fig. 9. ARIMA forecast vs Actual

5) Recurrent Neural Networks (RNN):

15
16

• RNNs are designed to handle sequential data and main- tain information about previous inputs
through hidden states. They are well-suited for capturing temporal de- pendencies in stock
prices.

Fig. 10. RNN forecast vs Actual

6) LSTM Model:

• Long Short-Term Memory (LSTM) networks are a type of RNN capable of learning longterm
dependencies. They include memory cells that can retain information over longer periods,
making them effective for time series forecasting.

7) CNN Preprocessing for RNN and Full CNN with Wavenet-like Architecture:

Fig. 11. LSTM 20 forecast vs Actual

16
17

Fig. 12. LSTM 30 forecast vs Actual

Fig. 13. Preprocess with CNN forecast 30

Fig. 14. Full CNN Model 20

17
18

• CNN Preprocessing: Convolutional Neural Networks (CNNs) were used to extract spatial
features from the time series data before feeding them into an RNN.
• Full CNN with Wavenet Architecture: This model lever- ages CNN layers to capture hierarchical
features and a Wavenet-like architecture to model the sequential nature of the data.

D. Model Training

Each model was trained on the training dataset using appropriate algorithms and loss functions:
• Naive Forecast and Moving Average Models: These mod- els were implemented using
straightforward calculations.
• ARIMA Model: Trained using maximum likelihood esti- mation.
• Linear and Dense Models: Implemented using Keras/TensorFlow, with the Adam optimizer and
mean squared error (MSE) as the loss function.
• RNN and LSTM Models: Trained using backpropagation through time (BPTT) with gradient
descent optimization.
• CNN and Hybrid Models: Trained using standard back- propagation with appropriate loss
functions and optimizers.

E. Model Evaluation

The performance of each model was evaluated using the Mean Absolute Error (MAE) metric,
which measures the average magnitude of the errors between predicted and actual values:

where yˆi is the predicted


value, yi is the actual value, and n is the number of observations.
• To ensure robustness, models were validated using a separate validation set and crossvalidation
techniques. The comparison of models was based on their MAE scores, providing insights into
their relative performance and suitability for stock price prediction.
• This research methodology outlines a systematic ap- proach to predicting stock prices using
time series data. By leveraging a diverse set of models and rigorously evaluating their
performance, this study aims to identify the most effective techniques for stock price

18
19

prediction. The use of advanced machine learning models, such as LSTMs and CNN-RNN
hybrids, highlights the potential for improved accuracy in forecasting stock market trends.

IV. THE RESULTS AND DISCUSSION

Fig. 15. Mean average error of all Models

The results of my study on stock price prediction using time series analysis are summarized in
the bar chart, which illus- trates the Mean Absolute Error (MAE) of all models tested. The MAE
serves as a key performance metric, highlighting the average magnitude of errors in predictions
without considering their direction.Below is a detailed discussion of the results and the performance
of each model.

A. Naive Forecast Model

The Naive Forecast Model, which assumes the next day’s price will be the same as today’s,
achieved an MAE of approximately 2.5. While simplistic, this model provides a baseline for
comparing more sophisticated methods. Despite its simplicity, it performed reasonably well,
underscoring the challenges of stock price prediction.

19
20

B. Moving Average Models

The Simple Moving Average (SMA) models with window sizes of 5 and 20 days demonstrated
varied performance. The 5-day SMA model had an MAE close to 7, while the 20-day SMA model
performed significantly worse, with an MAE of around 17. This indicates that while shortterm
moving averages can capture recent trends, longer windows may dilute responsiveness to recent
changes, leading to poorer predictions.

C. ARIMA Model

The ARIMA model, designed to handle both trend and seasonality in time series data, achieved an
MAE of about 2.8. This performance highlights the ARIMA model’s strength in modeling linear
relationships and its effectiveness in capturing the temporal structure of stock prices. The ability to
fine-tune parameters (p, d, q) contributed to its relatively low error rate.

D. Linear and Dense Models Using Keras/TensorFlow

The linear model and the dense neural network model showed comparable performance, with
MAEs around 3.8 and 5.7, respectively. The dense model, with its ability to capture non-linear
relationships through multiple layers, slightly out- performed the linear model. However, the
improvement was not substantial, suggesting that linear relationships still play a significant role in
stock price movements.

E. Recurrent Neural Networks (RNN)

The RNN model, leveraging its capability to maintain sequential dependencies, achieved an MAE
of approximately
5.8. Although RNNs are theoretically suited for time series data, their performance in this case was
moderate, potentially due to the challenges in capturing long-term dependencies without
enhancements like LSTM units.

F. LSTM Model

The LSTM model, which extends the RNN by incorporating memory cells to capture long-term
dependencies, was the best- performing model with an MAE of about 1.2. This significant reduction
in error demonstrates the LSTM’s ability to effec- tively learn and predict complex temporal

20
21

patterns in stock prices. The memory cell mechanism helps in retaining relevant information over
longer periods, making LSTMs particularly powerful for time series forecasting.

G. CNN Preprocessing for RNN and Full CNN with Wavenet- like Architecture

The CNN preprocessing model for RNNs and the full CNN with Wavenet-like architecture yielded
higher MAEs of around
12 and 13, respectively. These models, while effective in capturing local spatial patterns, might have
struggled with the inherently sequential nature of stock prices, leading to higher prediction errors.
The complexity of these models also introduces more parameters to tune, which can be a
disadvantage without sufficient data.
The results indicate that traditional statistical models like ARIMA and simple machine learning
models like lin- ear regression perform reasonably well in predicting stock prices. However,
advanced neural network models, particularly LSTM, show significant promise due to their ability
to capture complex temporal dependencies. The superior performance of the LSTM model suggests
that integrating memory mecha- nisms is crucial for effective stock price prediction

V. CONCLUSION

In this research on stock price prediction using time series, we assessed the performance of
various models on the SPY ETF dataset. The models ranged from traditional statisti- cal methods
to advanced deep learning algorithms, and our findings highlight significant differences in their
predictive capabilities.
The Naive Forecast Model and Moving Average Models (with MAE around 2.5 and 7 for SMA
5, respectively) provided baseline performance levels. The ARIMA model showed improved
accuracy with an MAE of approximately 2.8, confirming its efficacy in capturing linear temporal
de- pendencies. Machine learning models, including linear and dense mod- els, achieved MAEs of

21
22

around 3.8 and 5.7, respectively. These models, while effective in identifying linear relationships,
were outperformed by more advanced neural networks.
Recurrent Neural Networks (RNNs) achieved an MAE of about 5.8, reflecting their ability to
model sequential data. However, the Long Short-Term Memory (LSTM) model out- performed all
other models, with an impressive MAE of 1.2. This underscores the LSTM’s superior capability to
capture long-term dependencies in stock price movements.
Our study demonstrates that while simpler models provide useful benchmarks, advanced models
like LSTM significantly enhance prediction accuracy. The LSTM model’s superior performance
suggests its potential for real-world financial forecasting and investment strategies.

Future work could explore hybrid models that combine the strengths of different approaches, such
as integrating CNNs for feature extraction with LSTMs for sequence modeling, to further improve
prediction accuracy.

22

You might also like