The Impactof Twitter Sentimentson Stock Market Trends
The Impactof Twitter Sentimentson Stock Market Trends
The Impactof Twitter Sentimentson Stock Market Trends
net/publication/368507953
CITATIONS READS
0 2,448
4 authors, including:
Melvin Mokhtari
Isfahan University of Technology
2 PUBLICATIONS 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Melvin Mokhtari on 18 February 2023.
Authors:
Melvin Mokhtari (melvin.mokhtari@ec.iut.ac.ir)
Ali Seraj
Niloufar Saeedi
Adel Karshenas
Authors’ Declaration
We declare that the work included in the above paper is original and is an outcome of the
research carried out by the authors indicated in it, and that we are the sole authors of this
work. We would like to certify that there are no known conflicts of interest linked with this
publication and that no financial assistance has been provided for this study that might have
impacted its conclusion.
We also confirm that the work does not infringe on any copyrights or property rights of oth-
ers, including licenses, and it is free from plagiarism. For the inclusion of figures, tables,
animations, or text quotations from copyrighted works (including websites), necessary per-
mission has been obtained from the copyright holders. By doing so, we guarantee that we
have followed the regulations concerning intellectual property.
iii
Abstract
The Web is a vast virtual space where people can share their opinions, impacting all aspects
of life and having implications for marketing and communication. The most up-to-date and
comprehensive information can be found on social media because of how widespread and
straightforward it is to post a message. Proportionately, they are regarded as a valuable
resource for making precise market predictions. In particular, Twitter has developed into a
potent tool for understanding user sentiment.
This dissertation examines how well tweets can influence stock symbol trends. We analyze
the volume, sentiment, and mentions of the top five stock symbols in the S&P 500 index
on Twitter over three months. Long Short-Term Memory, Bernoulli Naïve Bayes, and Ran-
dom Forest were the three algorithms implemented in this process. Our study revealed a
significant correlation between stock prices and Twitter sentiment.
v
Table of Contents
List of Figures xi
xiii
1 Introduction 1
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Motivation and Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Document Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Related Work 5
2.1 The Analogy Between Emotions and Sentiments . . . . . . . . . . . . . . . 5
2.2 Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1 Sentiment Analysis Levels . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1.1 Document-Level Sentiment Classification . . . . . . . . 7
2.2.1.2 Sentence-Level Sentiment Classification . . . . . . . . . 7
2.2.1.3 Entity and Aspect Level Sentiment Classification . . . . . 8
2.2.2 Sentiment Classification Techniques . . . . . . . . . . . . . . . . . 8
2.2.2.1 Lexicon-Based Approaches . . . . . . . . . . . . . . . . 8
2.2.2.2 Machine Learning Approaches . . . . . . . . . . . . . . 9
vii
The Impact of Twitter Sentiments on Stock Market Trends
Supervised Learning . . . . . . . . . . . . . . . . . . . . . 10
Regression . . . . . . . . . . . . . . . . . . . . . . . . 10
Classification . . . . . . . . . . . . . . . . . . . . . . 10
Unsupervised Learning . . . . . . . . . . . . . . . . . . . . 10
Clustering . . . . . . . . . . . . . . . . . . . . . . . . 10
Association . . . . . . . . . . . . . . . . . . . . . . . 10
2.2.2.3 The Hybrid Approach . . . . . . . . . . . . . . . . . . . 10
2.2.3 Algorithmic Approaches . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.3.1 Naïve Bayes Classifier . . . . . . . . . . . . . . . . . . . 11
Advantages of Naïve Bayes Classifier . . . . . . . . . . . . 12
Disadvantages of Naïve Bayes Classifier . . . . . . . . . . . 12
2.2.3.2 Random Forest Classifier . . . . . . . . . . . . . . . . . 13
Advantages of Random Forest Classifier . . . . . . . . . . . 13
Disadvantages of Random Forest Classifier . . . . . . . . . 14
2.2.3.3 Long Short Term Memory Model (LSTM) . . . . . . . . 14
2.3 Applications of Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . 15
Stock Market . . . . . . . . . . . . . . . . . . . . . . . . . 15
E-commerce . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.1 Twitter Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . . 16
Politics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Business . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4 Data Collection and Preparation . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.1 Twitter Data Collection Methods . . . . . . . . . . . . . . . . . . . 17
2.4.1.1 Twitter API . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4.1.2 Tweepy . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4.1.3 Twint . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4.1.4 Preprocessed Datasets . . . . . . . . . . . . . . . . . . . 18
viii
Table of Contents
4 Performance Evaluation 29
4.1 A Diagrammatic Structure for Performance Evaluation . . . . . . . . . . . 29
4.2 Detailed evaluation of the results . . . . . . . . . . . . . . . . . . . . . . . 30
4.2.1 Evaluation of The Apple Stock ($AAPL) . . . . . . . . . . . . . . 30
4.2.2 Evaluation of the Facebook Stock ($FB) . . . . . . . . . . . . . . . 36
4.2.3 Evaluation of the Microsoft Stock ($MSFT) . . . . . . . . . . . . . 39
4.2.4 Evaluation of the Tesla Stock ($TSLA) . . . . . . . . . . . . . . . 42
4.2.5 Evaluation of the Amazon Stock ($AMZN) . . . . . . . . . . . . . 45
4.2.6 Overall Comparison . . . . . . . . . . . . . . . . . . . . . . . . . 47
5 Conclusion 49
5.1 Results and Discussions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.2 Prospects and Future work . . . . . . . . . . . . . . . . . . . . . . . . . . 50
References 51
ix
List of Figures
xi
The Impact of Twitter Sentiments on Stock Market Trends
xii
List of Tables
xiii
Chapter 1
Introduction
This chapter introduces the research’s motivation, describes the problem, and defines the
scope and theoretical approaches. The goals of the research are then presented. Finally, it
highlights this dissertation’s main contributions and outlines the document’s structure.
1.1 Overview
The recent technological revolution has resulted in an unprecedented data deluge that has
fundamentally altered how we view social and economic sciences. Computers and the In-
ternet are now a part of almost everyone’s daily lives. Similar increases in online activity
have been sparked by the continuously growing Internet usage, such as for business or news.
Due to this issue, social media’s emergence has provided a platform for web users to express
their opinions on various topics. Therefore, events and the creation of textual documents
have grown significantly over the past few years on these platforms, and consequently, mas-
sive datasets are being produced by the interaction of technological systems, documenting
collective behavior in previously unthinkable ways. Finally, we can discover the global
population’s interests, worries, and intentions regarding various economic, political, and
cultural phenomena in this massive database of Internet activities.
Twitter is a widely used microblogging and social networking platform where people can
read, write, publish, and update short text messages called tweets [1–4]. In 2021, Twitter’s
global user base was approximately 429.79 million. Furthermore, it is anticipated that by
2025, there will be 497.48 million users worldwide [5]. As a result, this microblogging
and social networking service piqued the interest of researchers from a variety of fields,
including politics, health care, and finance.
Sentiment analysis is the name given to the scientific study of the semantic content of
these tweets. In general, sentiment analysis is a technique for identifying and classifying
1
The Impact of Twitter Sentiments on Stock Market Trends
a text’s polarity, aiming to determine whether a specific document has a positive or neg-
ative value in accordance with a standard categorization. According to some researchers,
processing Twitter user opinions in the context of financial markets makes it possible for ev-
eryone to gather pertinent information about the stock market and use it to forecast changes
in stock prices. Indeed, since the very beginning of stock markets, investors have always
sought to gain a competitive advantage. Not illegally, such as through insider trading, but
by wisely and sparingly using information.
Here, we present a case study on financial markets, one of the numerous fields in which
data collection, analysis, and modeling are applied. According to our analysis, social net-
work metrics on social aspects are particularly helpful in understanding financial turnovers.
Indeed, collective phenomena such as herd behavior frequently precipitate financial crises
and demonstrate the financial system’s complex nature. As a result, policymakers are ea-
ger to anticipate unusual collective behavior among investors to be able to intervene sooner
when necessary.
The inherent risks and complications of investing in the financial market have led to the
development of techniques that minimize investor losses and maximize returns. These tech-
niques use machine learning algorithms to find connections between individuals’ thoughts
about the financial market and the movements of stock prices. This will make it possible to
predict how the market will change.
2
Chapter 1. Introduction
research that investor sentiments and decisions ultimately drive the market [7]. Therefore,
a model that considers feelings will provide better microeconomic predictions.
Traders take positions in the market that are held for only a short time, sometimes for
just a few moments, so many investors may quickly join and leave those positions daily.
Consequently, many investors will look for anything that will give them an advantage when
placing market bets. Several great traders have prepared bots based on artificial intelligence
to identify trends in social media without understanding the implications of the more in-
depth knowledge available there.
If we collect tweets and financial data associated with our target stock symbols, process
the data using frameworks to deduce a tweet’s sentiment score, and then compare the results
to changes in the stock price, we can create a useful momentum marker for the share market.
The frameworks will classify tweets by polarity to resolve these concerns. A tweet’s
sentiment can be classified as positive, neutral, or negative, depending on its polarity. After
tweets are categorized, a daily score is computed.
We are particularly interested in the following main research questions:
• What potential effects could tweets about a particular company have on the future
trends of its share?
Hence, understanding whether sentiment can be used to predict the direction of trends for a
share price is a crucial part of the research.
1.3 Methodology
As implied by the preceding sections, this study’s methodological approach entails gather-
ing data from tweets and then categorizing them based on their sentiment polarity. However,
because the scope of the world’s stock markets is immense, this document will rely on some
of the most frequently occurring companies in the S&P 500 index. This decision was made
for many reasons, including that the S&P 500, one of the world’s most widely used and rel-
evant indices, is recognized as the best measure of US big-cap equities, and its companies
are popular on social media platforms.
The study was performed using the Python programming language. Additionally, Jupyter
Notebook and the PyCharm IDE were used to implement the scripts created throughout the
various modules.
The official Twitter API can be used to gather tweets. However, given the vast amount of
data we seek, this has several limitations and is inappropriate for our case. The fact that there
3
The Impact of Twitter Sentiments on Stock Market Trends
are few options makes finding Twitter data one of the study’s biggest challenges. Although
there were other options, such as Twint, a comprehensive and reliable Python web scraper,
or Tweepy, a Python library for extracting tweets, we used a preprocessed dataset from the
Kaggle website [13] to get around these constraints.
We used the top 5 index companies of the S&P 500, the hashtag ”# STOCK,” the cahsh-
tag ”$ STOCK,” and a carefully curated and annotated dataset of tweets from April 9th
to July 16th, 2020, to explain the relationship between stock price returns and sentiment
reflected in tweets.
4
Chapter 2
Related Work
We will discuss sentiment analysis’s state of the art in this chapter. The background and its
underlying ideas are presented in the beginning. Then we take a look at how to use Twitter
as a valuable tool in sentiment analysis. After that, we take a quick look at various algo-
rithmic strategies, specifically machine learning methods. Finally, we will discuss various
approaches to Twitter data collection, including their advantages and disadvantages, and our
chosen approach.
5
The Impact of Twitter Sentiments on Stock Market Trends
6
Chapter 2. Related Work
• Document Level
• Sentence Level
This level identifies whether the concept of the entire document is positive or negative at
this point by treating it as a whole. This approach presumes that a document discusses a
single subject. For example, it determines whether a movie or book review is positive or
negative overall.
At this stage, it separates each sentence and classifies its opinion as positive, negative, or
neutral. Unlike the document level, each document can have different opinions about mul-
tiple entities. Each sentence is classified into an objective or subjective type [18]. Subjec-
tive sentences are based on personal perspective and opinion, like ”She has beautiful hair.”
Whereas the objective sentences do not consist of any people’s opinions and are based on
facts like the sentence ”I have black hair.”
7
The Impact of Twitter Sentiments on Stock Market Trends
This level is also known as the feature-based level and, unlike document and sentence lev-
els, approaches opinions directly. Its goal is to discover the opinions and determine their
sentiments that could be positive, negative, or neutral about a subject or an entity.
• Lexicon-based approaches
Lexicon-based techniques were among the first techniques used for sentiment analysis. Each
word is given a polarity score in lexicon-based systems, and after going through the text,
8
Chapter 2. Related Work
the system gives a sentiment score to that text. The advantage of this method is that it does
not require data automation [20].
The lexicon-based technique is divided into two sub techniques which are dictionary-
based and corpus-based. Dictionary-based is done using a dictionary of terms like WordNet
[21]. On the other hand, corpus-based does not need a predefined dictionary; instead, it uses
statistical analysis approaches such as K-nearest neighbors.
Machine learning is a branch of artificial intelligence that uses data as input to make better
judgments rather than being explicitly instructed on what to do.
Machine learning approaches in sentiment analysis [22] can derive opinions and their
sentiment towards an entity. For this purpose, various methods are employed, including
support vector machines (SVM) and Naïve Bayes.
Generally, machine learning techniques are divided into two categories:
• Supervised learning
• Unsupervised learning
9
The Impact of Twitter Sentiments on Stock Market Trends
Supervised Learning The goal of this technique is to predict outcomes in uncertain sit-
uations.
The model is first trained using data with a known output and label. Then, the system
will learn from this data to make the best decision.
There are two types of supervised learning techniques:
Regression The goal is to predict a real value based on different independent inputs.
For example, determining a budget based on sales.
Classification Placing various items in different categories. This is the approcehe that
we use for sentiment analysis.
Serval algorithms are used to classify objects. For example, a support vector machine
separates every object into two different classes, called binary classification. It tries to find
a line to separate the members of the classes. Then it updates the values of the slope and
parameters of that line based on each piece of data to find the best line for this classification.
Clustering This Technique divides unlabeled data into groups with structures based
on similarities and contrasts between the items. Clustering algorithms might be hierarchical,
overlapping, or probabilistic.
K-means clustering is an example of clustering where the data is separated into K groups,
and each set of data closest to each centroid is categorized under that category. Each group’s
size and level of detail can be altered by altering K, like in Figure 2.4.
The hybrid sentiment analysis approach uses both statistical and knowledge-based method-
ologies for polarity detection. As a result, it inherits both stability and excellent accuracy
from the lexicon-based method and machine learning [24].
10
Chapter 2. Related Work
One of the most straightforward and least time-complex supervised algorithms for datasets
containing big data with millions of records is Naïve Bayes. According to Naïve Bayes,
there is no correlation between the incidence of one character in a class and the occurrence
of another feature. Because of this, it is known as a ”Naïve” algorithm.
The Bayes theorem, shown in Figure 2.6, on which the Naïve Bayes algorithm is based,
enables us to calculate the posterior probability, for instance, 𝑃(𝐻|𝐸), using 𝑃(𝐸 |𝐻), 𝑃(𝐻),
and 𝑃(𝐸) where 𝑃(𝐻|𝐸) = 𝑃(𝑒 1 |𝐻) ∗ 𝑃(𝑒 2 |𝐻) ∗ ... ∗ 𝑃(𝑒 𝑛 |𝐻) ∗ 𝑃(𝐻).
To better understand how the Naïve Bayes algorithm works, consider the scenario of
differentiating spam and genuine emails by scanning each email and extracting words and
11
The Impact of Twitter Sentiments on Stock Market Trends
Figure 2.5 The assumption of conditional independence in the Naïve Bayes classifier
[25]
phrases. In order to determine if an email is spam or not, we create a table that contains
the likelihood of discovering a word inside the message. For example, given that the email
is spam, we compute the likelihood of detecting the term ”offer.” Additionally, we have to
consider the possibility that the email was not a spam email. Remember that we used the
training dataset to calculate these probabilities.
The followings are the advantages and disadvantages of the Naïve Bayes algorithm:
• The zero-frequency problem causes predictions to be ignored for categories with zero
occurrences in the training data and zero probability.
• This classifier tends to create assumptions that are mostly not valid in reality.
12
Chapter 2. Related Work
Random forest is another standard classification algorithm. The moniker ”Random Forest”
refers to a system that builds a lot of random decision trees and uses most of those predictions
to determine the outcome. Each decision tree’s features and data are chosen at random.
As seen in Figure 2.6, the dataset is divided into separate, entirely random decision trees,
and when attempting to forecast an item, it employs a majority vote as the final prediction.
In addition, it could be used to solve regression problems by calculating the mean or average
of each decision tree prediction.
This algorithm has several advantages and disadvantages like other algorithms:
13
The Impact of Twitter Sentiments on Stock Market Trends
Long short-term memory models are a kind of recurrent neural network that is very effective
in handling a large amount of sequential data. New recurrent neural networks differ from
traditional ones because they can take feedback from previous outputs to generate their
current output. The main feature of LSTMs is storing information for an extended period of
time, and they do that using a cell state. The cell states’ primary role is to store information
from the past efficiently [27].
In each iteration, the LSTM must decide:
• Produce the output based on the input and report it to the cell.
The LSTM model will not be affected by outliers in making its predictions and can train its
parameters. Figure 2.7 depicts a simple mechanism of this model.
14
Chapter 2. Related Work
Stock Market The stock market is crucial to every country’s economy. Predicting how
a stock will act in the upcoming future helps us make a better investment with our money.
Sentimental analysis extracts opinions about stocks and tries to find the correlation between
the opinions and the stock by gathering different datasets and processing them. With the
help of sentimental analysis, we could try to predict a stock’s trajectory after data collection
and preparation.
15
The Impact of Twitter Sentiments on Stock Market Trends
used sentiment analysis to improve the quality of the different services it provides.
So, the popularity of sentiment analysis is due to the great interest shown by these ap-
plications.
Politics With the emergence of Twitter, many studies have been conducted to replace tra-
ditional polling methods like telephone poles and internet polls with Twitter sentiment anal-
ysis. Studies showed that algorithms like SVM classifier could help us predict the polls with
high accuracy. Detecting sarcasm about a candidate is the most challenging part. In this
case, positive sentiments are viewed negatively.
Business Many companies use Tweets to extract sentiment and feelings toward their prod-
uct directly or indirectly. This helps companies to improve their product and offer better
services.
2. Removing usernames
16
Chapter 2. Related Work
4. Removing letters that contain no emotion, such as ”a”, and ”the,” which are also
known as stop characters
We could use the official Twitter API for collecting data. The Twitter API comes in 3 sub-
scriptions, standard, premium, and enterprise, where the standard is free to use. The main
limitation of the Twitter API is that you can only fetch 3200 tweets from the past seven
days, and it also needs an account to use. To overcome these limitations, we can subscribe
to premium services, but they are not free. That is why we prefer to use libraries and tools
such as Twint and Tweepy.
2.4.1.2 Tweepy
Tweepy is a Python library used for collecting Twitter data. It uses the Twitter API for
collecting and gathering tweets. Tweepy has complete documentation from beginner level
to advanced. Moreover, it allows its users to post tweets and retweets and even has the ability
to like tweets.
Despite this, it is a paid application, and its free version also has the same limitations as
the Twitter API.
2.4.1.3 Twint
Twint uses scraping to collect tweets and does not depend on the official Twitter API. With
Twint, you could collect unlimited tweets with no limitations. Nevertheless, the documen-
tation on Twint is incomplete and outdated compared to the recent updates on Twitter and
cannot be used for posting or liking tweets.
The main differences between Twint and Tweepy are as follows:
17
The Impact of Twitter Sentiments on Stock Market Trends
• Tweepy uses the Twitter API to collect tweets, while Twint uses web scraping to gather
tweets.
• Tweepy has the same limitations as the Twitter API in the number of tweets it can
collect, while on the other hand, Twint has no limitations in the number of tweets it
can gather.
• Due to its excellent documentation, Tweepy is much easier to learn and use. On the
other hand, in the case of Twint, because of its weak documentation, it can be a hassle
to learn and use its features.
• Twint is limited to only collecting tweets and gathering information, while Tweepy
can be used for posting and liking tweets.
Using preprocessed datasets is the alternative to the Twitter API and other third-party appli-
cations. Employing preprocessed datasets removes the trouble of trying to collect the tweets
but has less flexibility than tweet collecting tools.
For example, the Kaggle website offers many preprocessed datasets that can be used for
different studies.
We have chosen this method for our data and used a semi-preprocessed dataset from the
Kaggle website.
18
Chapter 3
3.1 Data
In this section, we explain the details of the datasets that we have used, the purpose of
those specific datasets, the operations that we performed to make data ready for use, and the
procedure of labeling the data.
19
The Impact of Twitter Sentiments on Stock Market Trends
Then we preprocessed the data and created a separate data frame for each stock:
AAPL_DF = stockDF [ stockDF ['full_text ']. str. contains ('aapl ')]
2 AMZN_DF = stockDF [ stockDF ['full_text ']. str. contains ('amzn ')]
FB_DF = stockDF [ stockDF ['full_text ']. str. contains ('fb ')]
4 MSFT_DF = stockDF [ stockDF ['full_text ']. str. contains ('msft ')]
TSLA_DF = stockDF [ stockDF ['full_text ']. str. contains ('tsla ')]
This data contains six attributes: Open, High, Low, Close, Adj Close, and Volume. We
only Close, to calculate the amount of 𝑅𝑒𝑡𝑢𝑟𝑛 and check the correlation with twitter data,
respectively.
20
Chapter 3. Methodological Approaches and Technical Frameworks
We removed punctuations:
1 english_punctuations = string . punctuation
punctuations_list = english_punctuations
3 def cleaning_punctuations (text):
translator = str. maketrans ('', '', punctuations_list )
5 return text. translate ( translator )
The next step is to tokenize the words in the text so that it will be easier to go through
and process. We changed the text of each tweet into a vector of words.
tokenizer = RegexpTokenizer (r'\w+')
We also stem the words. It means converting the different words with the same root to
their single root. e.g., the root of all the words payment, pay, and paying is the word pay.
Having the stem word instead of these different forms helps the procedure of processing and
learning easier while maintaining the main meaning and context.
1 import nltk
st = nltk. PorterStemmer ()
3 def stemming_on_text (data):
text = [st.stem(word) for word in data]
21
The Impact of Twitter Sentiments on Stock Market Trends
5 return data
Listing 7 Stemming
Another necessity is to lemmatize the words, meaning we omit all different shapes of one
word using the nltk built-in function WordNetLemmatizer().
1 nltk. download ('wordnet ')
lm = nltk. WordNetLemmatizer ()
3 def lemmatizer_on_text (data):
text = [lm. lemmatize (word) for word in data]
5 return data
Listing 8 Lemmatizing
We applied all the created functions using lambda expression sing the following syntax:
data = data. apply ( lambda x: function (x))
And further did a one-hot encoding on each word, converting each word to a vector to
be used for processing.
One-hot encoding (Figure 3.1) represents each word with a vector containing N values,
N being the total number of words in the database. All the values in this vector are 0, except
the one for which the index belongs to the target word. As the number of words in the
database is enormous, we omitted the least repeated words and kept the most repeated ones
to make processing workable for an extensive database.
Once preprocessing is done, we are good to go to fit our model into the data. For this
purpose, we split the data into train and test sections and built our classifier models.
22
Chapter 3. Methodological Approaches and Technical Frameworks
3.1.4 Labelling
We labelled the data extracted from our second dataset, by the classifier models built using
the first dataset. As mentioned before, the labels of our training data were 0 and 4. For the
sake of simplicity, we replaced label 4 with 1.
Labelling using the Random Forest model:
rfPred = np. where ( rfPred ==4 ,1 , rfPred )
2 stockDF = dup_top5_noLable .copy ()
stockDF ['Label_rf ']= rfPred
23
The Impact of Twitter Sentiments on Stock Market Trends
We further use a confusion matrix to observe the result. A confusion matrix is a table
containing four cells: TP (True Positive), TN (True Negative), FP (False Positive), and FN
(False Negative).
• TN shows the number of values that are predicted as negative and are actually nega-
tive.
• FN represents the number of positive values that are falsely predicted as negative.
And checked the accuracy score, representing the percentage of records the label of
which our model has predicted accurately.
24
Chapter 3. Methodological Approaches and Technical Frameworks
25
The Impact of Twitter Sentiments on Stock Market Trends
Activation functions are necessary for a deep model to learn appropriately as they fix the
neuron values to prevent them from becoming so big or small that they become useless.
Popular activation functions are Relu, sigmoid, and tangent.
ReLU function is considered to be the most reliable activation function to be used in
between the layers. It returns the input itself unless the input is negative, in which case it
returns zero. The sigmoid and tangent activation functions are used in classification prob-
lems.
In order to proceed to the explanation of the code, we need to define the loss function
and optimizer.
The loss function compares the prediction output of the model to the expected values,
which are the actual values, and returns a number, or loss, for each observation. The model
is trained best when the amount of loss is minimized.
The most popular loss functions are mean squared error (MSE), categorical cross-entropy,
and binary cross-entropy. Each is used for a different kind of problem.
MSE works best for regression problems and cross-entropy for classification ones. As
we are expecting our input to have only two values, we use binary cross-entropy.
The optimizer is the algorithm used to update the weights of the neurons using the gra-
dient of the loss function. GSD and Adam are two of the most stable optimizers. The only
26
Chapter 3. Methodological Approaches and Technical Frameworks
parameter of the optimizer that may require adjustments is the learning rate. If the learning
rate is too small, it searches for the minima of the loss function in very tiny steps, resulting
in a super-slow process of learning. Otherwise, if the learning rate is too large, it may learn
quickly initially but may not find the minima due to the large steps it takes while searching,
resulting in an unstable training process.
model . compile (loss='binary_crossentropy ',
2 optimizer ='adam ',
metrics =[ 'accuracy '])
4 model . summary ()
The batch size specifies the number of observations passed to the network at each train-
ing step. The number of epochs assesses the number of times that the full training data is
shown to the neural network.
num_epochs = 2
2 history = model .fit( train_padded , lstm_y_train ,
epochs = num_epochs , verbose =1,
4 validation_split =0.1)
Setting the 𝑣𝑒𝑟𝑏𝑜𝑠𝑒 attribute results in seeing the progress bar while running, and
𝑣𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛_𝑠𝑝𝑙𝑖𝑡 represents the fraction of data used for validation to the total amount of
training data.
Eventually, we tested the accuracy of the model along with replacing the label 4 with 1.
prediction = model . predict ( test_padded )
2 lstm_y_test = np.where ( lstm_y_test ==4 ,1 , lstm_y_test )
pred_labels = []
4 for i in prediction :
if i >= 0.5:
6 pred_labels . append (1)
else:
8 pred_labels . append (0)
print (" Accuracy of prediction on test set : ", accuracy_score (
lstm_y_test , pred_labels ))
27
Chapter 4
Performance Evaluation
And extracted a variable 𝑅𝑒𝑡𝑢𝑟𝑛 from the 𝐶𝑙𝑜𝑠𝑒 feature of financial data.
29
The Impact of Twitter Sentiments on Stock Market Trends
The Opening value of a stock is the value for which the stock is traded at the beginning
of the day, and the Closing value is the price of a stock that has been agreed upon after all
transactions of that day, or simply the price for the last transaction of the day for that stock.
To see how much the value of a stock has grown or dropped in one day relative to the
day before, we calculate a variable Return for each day 𝑡 [34].
Finally, we analyzed the correlation between the financial feature ”Return” and the Twit-
ter data feature ”Bullishness”.
30
Chapter 4. Performance Evaluation
As we can see in Figure 4.1, Naïve Bayes and LSTM models have pretty similar results,
but far from the predicted bullishness of Random Forest model. If we look more precisely,
we can recognize a similar pattern for all classifiers for most of the time intervals.
Here is the diagram of calculated return on the data extracted from Yahoo Finance statis-
tics (graphical results are available in Figure ??):
1 AAPL_R = calc_Return ( apple_data )
plt.plot( AAPL_R .Date , AAPL_R . Values )
3 plt.show
Then we merged the financial and Twitter results into one data frame using 𝐹𝑖𝑛𝑅𝑒𝑠:
1 import time
import datetime
3 def finalRes ( return_data , bullishness_data ):
fin = pd. DataFrame ( columns =[ 'Date ','lstm ','rf','nb ','Values '])
5 l =[]
for i in range (1, len( return_data . index )):
7 for j in range (0, len( bullishness_data . index )):
if datetime . datetime . strptime ( bullishness_data .Date.iloc[j],"%Y -%m -%d"
) >= return_data .Date.iloc[i]:
9 l. append ([ bullishness_data .Date.iloc[j -1] , bullishness_data .lstm.iloc[j
-1], bullishness_data .rf.iloc[j -1] , bullishness_data .nb.iloc[j -1] ,
return_data . Values .iloc[i]])
31
The Impact of Twitter Sentiments on Stock Market Trends
break
11 fin = pd. DataFrame (l, columns =[ 'Date ','lstm ','rf ','nb ','Values '])
return fin
13 res = finalRes (AAPL_R , AAPL_B )
Listing 25 Merging the prediction and Return value into one dataset
We further used the merged data to compare each classifier output to the real market
events by visualization.
1 def drawAllForStock (data ,str):
bull = bullishness (data)
3 start = bull['Date ']. iloc [0]
end = bull['Date ']. iloc [ -1]
5 stock_data = yf. download (str , start =start , end=end)
stock_data .drop( apple_data . columns . difference ([ 'Close ']) , 1, inplace =
True)
7 ret = calc_Return ( stock_data )
res = finalRes (ret ,bull)
9 drawPlot (res.Date ,res.lstm ,res.Values ,"lstm model ")
drawPlot (res.Date ,res.rf ,res.Values ," Random forest model ")
11 drawPlot (res.Date ,res.nb ,res.Values ," Naïve Bayes model ")
32
Chapter 4. Performance Evaluation
Considering Figure 4.2, for most days, the positivity or negativity of the Return approves
of the predicted bullishness of the market, and there are close predictions on some days, such
as June 25th to July 2nd. The gap between the prediction and the actual return after this date
is quite understandable. As the stock has been growing rapidly, people would expect it to
continue likewise, resulting in many positive comments. Nevertheless, the amount of the
Return has dropped unexpectedly. The predictions are also pretty close after this gap to July
15th.
33
The Impact of Twitter Sentiments on Stock Market Trends
Figure 4.3 The Graph of the Naïve Bayes Classifier for AAPL
Figure 4.3 depicts that the Naïve Bayes classifier has probably predicted the worst.
Though the predictions of this model are pretty similar to the Naïve Bayes’, the concur-
rent gaps are larger for this classifier. One interesting point to take into consideration is that
on some days, the amount of Return is pretty close to the prediction for tomorrow, meaning
that there has been a closer relationship between the positivity of people and the amount of
Return on the same day than the day after.
34
Chapter 4. Performance Evaluation
Figure 4.4 The Graph of the Random Forest Classifier for AAPL
Random Forest has predicted the most satisfactory, especially from June 18th on. (Fig-
ure 4.4)
35
The Impact of Twitter Sentiments on Stock Market Trends
As shown in Figure 4.5, like the Apple stock, Naïve Bayes and LSTM outputs are more
similar compared to Random Forest. We can observe some similarities between the patterns.
36
Chapter 4. Performance Evaluation
In Figure 4.6, we can see that there are some lucky days that predictions are close, but
in most cases, the predictions are quite unreliable. The closest shot time interval is similar
to that of Apple’s, from June 23th to 29th.
37
The Impact of Twitter Sentiments on Stock Market Trends
Naïve Bayes’ predictions (Figure 4.7) are slightly closer than those of LSTM. The results
are the most satisfactory from May 1st to 5th and June 24th to July 7th.
As it is visible in Figure 4.8, many of the predictions of Random Forest are far away
from the actual stock Returns, and a few are pretty close, making it difficult to evaluate the
job. The closest predictions belong to the first days of July, in which the predictions of the
other classifiers, even the ones belonging to the other stocks, have also been close.
38
Chapter 4. Performance Evaluation
Naïve Bayes and LSTM outputs are more similar compared with Random Forest (Fig-
ure 4.9). Many similarities between the patterns can be observed in all three. All classifiers
agree on the peak points.
39
The Impact of Twitter Sentiments on Stock Market Trends
Figure 4.10 depicts that there are some patterns that are predicted closely in LSTM
Model. However, some predictions are far away from the actual values.
Figure 4.11 The Graph of the Naïve Bayes Classifier for MSFT
As described through Figure 4.11, most predictions of the Naïve Bayes Classifier are
40
Chapter 4. Performance Evaluation
Figure 4.12 The Graph of the Random Forest Classifier for MSFT
The predictions of the Random Forest classifier are not that satisfactory Figure 4.12, but
closer than the other two.
41
The Impact of Twitter Sentiments on Stock Market Trends
Considering Figure 4.13, Naïve Bayes and LSTM outputs are more similar compared with
Random Forest. Many similarities between the patterns can be observed in all three. All
classifiers agree on the peak points.
42
Chapter 4. Performance Evaluation
There are a few patterns that are predicted closely in LSTM model (Figure 4.14). How-
ever, most predictions are far from the actual values.
Figure 4.15 The Graph of the Naïve Bayes Classifier for TSLA
By analyzing (Figure 4.15), we can understand that the Naïve Bayes has done better than
43
The Impact of Twitter Sentiments on Stock Market Trends
LSTM in this case, most of the peaks and minimums are predicted accurately.
Figure 4.16 The Graph of the Random Forest Classifier for TSLA
Random Forest predictions are the closest (Figure 4.16), and this diagram has the closest
predictions of all, with only a few errors.
44
Chapter 4. Performance Evaluation
The three models agree on most of the rising or falling patterns as shown in Figure 4.17.
Random Forest has predicted the most optimistic and Naïve Bayes the most pessimistic.
LSTM is somewhere in between.
45
The Impact of Twitter Sentiments on Stock Market Trends
There are some patterns that are predicted very closely in LSTM model (Figure 4.18).
However, some predictions are far away from the actual values.
Figure 4.19 The Graph of the Naïve Bayes Classifier for AMZN
The predictions of Naïve Bayes are not that satisfactory as it seems in Figure 4.19, yet
46
Chapter 4. Performance Evaluation
Figure 4.20 The Graph of the Random Forest Classifier for AMZN
Figure 4.20 depicts that the Random Forest’s performance is similar to Naïve Bayes, on
some days Naïve Bayes’ results are closer, and on some Random Forest’s.
47
Chapter 5
Conclusion
The key results of this dissertation, along with potential future research directions, are out-
lined in this chapter.
49
The Impact of Twitter Sentiments on Stock Market Trends
• Although Twitter has many international users, we solely concentrate on English sen-
tences. Our method should work to categorize sentiment in other languages as well.
• Current algorithms categorize the general tone of a tweet. The polarity of a tweet
can vary depending on your interpretation of the tweet. For example, the sentiment
in the tweet ”England beats Germany” is favorable for England and unfavorable for
Belgium. Semantics could be useful here. The classification would be done in accor-
dance with which noun is primarily connected with the verb based on the results of
a semantic role labeler. This may allow ”Germany beats England” to be categorized
differently as ”England beats Germany.”
• It is important to extend the study period to capture a wider variety of tweets and
events, overcoming the limitations of specific time events like the COVID-19 pan-
demic. The productivity of the models may also be better understood by examining
them in the context of various economic conditions, such as booms and recessions.
• An artificial neural network can be used to analyze sentimental tweets more accu-
rately.
50
References
[1] M. Graham, S. A. Hale, D. Gaffney, Where in the world are you? geolocation and language
identification in twitter, The Professional Geographer 66 (2014) 568–578.
[2] V. D. Nguyen, B. Varghese, A. Barker, The royal birth of 2013: Analysing and visualising
public sentiment in the uk using twitter, in: 2013 IEEE International Conference on Big Data,
IEEE, 2013, pp. 46–54.
[3] A. Java, X. Song, T. Finin, B. Tseng, Why we twitter: understanding microblogging usage and
communities, in: Proceedings of the 9th WebKDD and 1st SNA-KDD 2007 workshop on Web
mining and social network analysis, 2007, pp. 56–65.
[4] G. Ranco, D. Aleksovski, G. Caldarelli, M. Grčar, I. Mozetič, The effects of twitter sentiment
on stock price returns, PloS one 10 (2015) e0138441.
[5] J. Degenhard, Forecast of the number of twitter users in the world from 2017 to 2025, 2021.
URL: https://www.statista.com/forecasts/1146722/twitterusersintheworld.
[6] H. Miao, S. Ramchander, J. K. Zumwalt, Information driven price jumps and trading strategy:
Evidence from stock index futures, SSRN eLibrary (2011).
[7] B. Qian, K. Rasheed, Stock market prediction with multiple classifiers, Applied Intelligence
26 (2007) 25–33.
[8] B. G. Malkiel, The efficient market hypothesis and its critics, Journal of economic perspectives
17 (2003) 59–82.
[9] H. Mao, S. Counts, J. Bollen, Predicting financial markets: Comparing survey, news, twitter
and search engine data, arXiv preprint arXiv:1112.1051 (2011).
[10] T. O. Sprenger, A. Tumasjan, P. G. Sandner, I. M. Welpe, Tweets and trades: The information
content of stock microblogs, European Financial Management 20 (2014) 926–957.
[11] E. Gilbert, K. Karahalios, Widespread worry and the stock market, in: Fourth International
AAAI Conference on Weblogs and Social Media, 2010.
[12] J. Bollen, H. Mao, X. Zeng, Twitter mood predicts the stock market, Journal of computational
science 2 (2011) 1–8.
[13] B. Taborda, A. De Almeida, J. C. Dias, F. Batista, R. Ribeiro, Stock market
tweets data, 2021. URL: https://www.kaggle.com/datasets/utkarshxy/
stock-markettweets-lexicon-data.
[14] D. Watson, A. Tellegen, Toward a consensual structure of mood., Psychological bulletin 98
(1985) 219.
[15] P. Ekman, Are there basic emotions? (1992).
51
The Impact of Twitter Sentiments on Stock Market Trends
[16] L. Yue, W. Chen, X. Li, W. Zuo, M. Yin, A survey of sentiment analysis in social media,
Knowledge and Information Systems 60 (2019) 617–663.
[17] J. M. G. F. F. Sacramento, Sentiment Analysis in the Stock Market Based on Twitter Data,
Ph.D. thesis, ISCTE-Instituto Universitario de Lisboa (Portugal), 2021.
[18] A. Yadav, D. K. Vishwakarma, Sentiment analysis using deep learning architectures: a review,
Artificial Intelligence Review 53 (2020) 4335–4385.
[19] W. Medhat, A. Hassan, H. Korashy, Sentiment analysis algorithms and applications: A survey,
Ain Shams engineering journal 5 (2014) 1093–1113.
[20] Z. Drus, H. Khalid, Sentiment analysis in social media and its application: Systematic literature
review, Procedia Computer Science 161 (2019) 707–714.
[21] A. Agarwal, B. Xie, I. Vovsha, O. Rambow, R. J. Passonneau, Sentiment analysis of twitter
data, in: Proceedings of the workshop on language in social media (LSM 2011), 2011, pp.
30–38.
[22] N. C. Dang, M. N. Moreno-García, F. De la Prieta, Sentiment analysis based on deep learning:
A comparative study, Electronics 9 (2020) 483.
[23] A. Teixeira-Pinto, J. Harezlak, 2021. URL: https://bookdown.org/tpinto_home/
Unsupervised-learning/k-means-clustering.html.
[24] I. El Alaoui, Y. Gahi, R. Messoussi, Y. Chaabi, A. Todoskoff, A. Kobi, A novel adaptable
approach for sentiment analysis on big social data, Journal of Big Data 5 (2018) 1–18.
[25] B. Gamal, Naïve bayes algorithm, 2021. URL: https://medium.com/analytics-vidhya/
na%C3%AFve-bayes-algorithm-5bf31e9032a2.
[26] O. Mbaabu, 2020. URL: https://www.section.io/engineering-education/
introduction-to-random-forest-in-machine-learning/.
[27] K. Gopalakrishnan, F. M. Salem, Sentiment analysis using simplified long short-term memory
recurrent neural networks, arXiv preprint arXiv:2005.03993 (2020).
[28] MATLAB, YouTube, 2018. URL: https://www.youtube.com/watch?v=5dMXyiWddYs.
[29] A. Go, R. Bhayani, L. Huang, Twitter sentiment classification using distant supervision,
CS224N project report, Stanford 1 (2009) 2009.
[30] S. V. Kolasani, R. Assaf, Predicting stock movement using sentiment analysis of twitter feed
with neural networks, Journal of Data Analysis and Information Processing 8 (2020) 309–319.
[31] G. A. Ruz, P. A. Henríquez, A. Mascareño, Sentiment analysis of twitter data during critical
events through bayesian networks classifiers, Future Generation Computer Systems 106 (2020)
92–104.
[32] G. Novack, Building a one hot encoding layer with ten-
sorflow, 2020. URL: https://towardsdatascience.com/
building-a-one-hot-encoding-layer-with-tensorflow-f907d686bf39.
[33] S. Sharma, Activation functions in neural networks, 2021. URL: https://
towardsdatascience.com/activation-functions-neural-networks-1cbd9f8d91d6.
[34] T. Rao, S. Srivastava, Twitter sentiment analysis: How to hedge your bets in the stock markets,
in: State of the art applications of social network analysis, Springer, 2014, pp. 227–247.
52