# Using Granger Causality to Determine Whether Twitter Sentiment Predicts Bitcoin Price Movement

February 22, 2022

In this article, we are again going to combine my current favorite subjects: natural language processing, time series analysis, and financial analysis.

Recently, I created a couple lectures covering Granger causality, so this topic is fresh on my mind.

In short, Granger causality is used to determine whether one time series can be used to forecast another (i.e. predict the future).

In these lectures, I demonstrated that some economics variables are Granger causal (in particular, GDP and term spread).

Of course, another easy application is to determine whether or not Twitter sentiment can predict cryptocurrency movements.

This post is based on this short publication: “Does Twitter Predict Bitcoin?” by Shen, D., Urquhart, A. and Wang, P. (2019) and can be found at https://centaur.reading.ac.uk/80420/1/Twitter.Bitcoin.pdf

The premise is quite simple and you really have to just understand these 3 components in order to implement this yourself:

1) How to get a Twitter sentiment time series

2) How to get Bitcoin price time series

3) How to implement the Granger causality test

If you can do 1-3, you can predict Bitcoin! (at least, partially)

So let’s go over each of these 3 topics in order.

### How to get a Twitter sentiment time series

This is going to probably be the most difficult part for most students. Most students are used to downloading a CSV dataset that I typically make very nice and simple for my courses.

Unfortunately, real life is not like this.

This becomes a data engineering problem.

Which tweets by which authors do you choose?

Where do you store the tweets?

Once you’ve figured that out, you need to convert the tweets into a number (sentiment) such that the numbers collectively form a time series.

That part is not so hard.

I’ve demonstrated several methods of doing this, such as:

a) training your own model on sentiment data (you could even create your own dataset)

b) using a pretrained Transformer model

### How to get Bitcoin price time series

In contrast to the first task, this is probably the easiest.

In the past, I’ve demonstrated how you can easily get minute, daily, monthly, etc. data for essentially any ticker using the yfinance Python package.

### How to implement the Granger causality test

For those of you who haven’t learned Time Series Analysis with me in the past, you perhaps have never heard of Granger causality.

In short, we build a multivariate autoregressive time series model called a VAR model.

It takes the form of:

$$y(t) = \sum_{\tau=1}^L A_\tau y(t-\tau) + \varepsilon(t)$$

Essentially, if you find any component $$A_\tau(j,i)$$ is “big enough” (in magnitude), then you can conclude that $$y_i(t)$$ Granger causes $$y_j(t)$$.

As in regression analysis, one decides whether these model coefficients are statistically significant by using hypothesis testing.

It’s important to note that Granger causality is not “true” causality as one usually thinks of it (e.g. eating food causes me to be satiated). Granger causal simply means that one time series is useful in forecasting another (hence the cross-coefficients being non-zero).

Luckily, the Granger causality test is very easy to use in Python with the statsmodels package.

Suppose you have your 2 time series (BTC returns and Twitter sentiment) in a 2-column dataframe (sidenote: your time series should be stationary so you should use returns and not prices).

Then you simply call the statsmodels function:

This will output p-values for every lag so you can see whether or not the sentiment at that particular lag affects the BTC return.

Final note: unfortunately, the paper only shows that Twitter sentiment Granger causes some function of the squared return. This means we lose information about whether the return is actually going up or down!

# [NEW COURSE] Machine Learning: Natural Language Processing in Python (V2)

December 20, 2021

# Machine Learning: Natural Language Processing in Python (V2)

===The Complete Natural Language Processing Course Has Arrived===

Hello friends!

Welcome to my latest course, on Natural Language Processing (NLP).

Don’t want to read my little spiel? Just click here to get the VIP discount (expires in 30 days – Jan 20, 2022!):

https://www.udemy.com/course/natural-language-processing-in-python/?couponCode=NLPVIP

UPDATE: The opportunity to get the VIP version on Udemy has expired. However, the main part of the course (without the VIP parts) is now available at a new low price. Click here to automatically get the current lowest price: https://bit.ly/3nT5fTX

June 10, 2019

For the next week, all my Deep Learning and AI courses are available for just $9.99! (In addition to other courses on the site for the next few days) For those of you who have been around for some time, you know that this sale doesn’t come around very often – just a few times per year. If you’ve been on the fence about getting a course, NOW is the time to do so. Get it now – save it for later. For my courses, please use the coupons below (included in the links), or if you want, enter the coupon code: JUN2019. As usual, if you want to know what order to take my courses in, check out the lecture “What order should I take your courses in?” in the Appendix of any of my courses (including the free Numpy course). For prerequisite courses (math, stats, Python programming) and all other courses, follow the links at the bottom for sales of up to 90% off! Since ALL courses on Udemy on sale, if you want any course not listed here, just click the general (site-wide) link, and search for courses from that page. https://www.udemy.com/cutting-edge-artificial-intelligence/?couponCode=JUN2019 ### PREREQUISITE COURSE COUPONS And just as important,$9.99 coupons for some helpful prerequisite courses. You NEED to know this stuff to understand machine learning in-depth:

General (site-wide): http://bit.ly/2oCY14Z
Python http://bit.ly/2pbXxXz
Calc 1 http://bit.ly/2okPUib
Calc 2 http://bit.ly/2oXnhpX
Calc 3 http://bit.ly/2pVU0gQ
Linalg 1 http://bit.ly/2oBBir1
Linalg 2 http://bit.ly/2q5SGEE
Probability (option 1) http://bit.ly/2p8kcC0
Probability (option 2) http://bit.ly/2oXa2pb
Probability (option 3) http://bit.ly/2oXbZSK

### OTHER UDEMY COURSE COUPONS

As you know, I’m the “Lazy Programmer”, not just the “Lazy Data Scientist” – I love all kinds of programming!

iOS courses:
https://lazyprogrammer.me/ios

Android courses:
https://lazyprogrammer.me/android

Ruby on Rails courses:
https://lazyprogrammer.me/ruby-on-rails

Python courses:
https://lazyprogrammer.me/python

Big Data (Spark + Hadoop) courses:

Javascript, ReactJS, AngularJS courses:
https://lazyprogrammer.me/js

### EVEN MORE COOL STUFF

Into Yoga in your spare time? Photography? Painting? There are courses, and I’ve got coupons! If you find a course on Udemy that you’d like a coupon for, just let me know and I’ll hook you up!

# Udemy St. Patrick’s Day Sale 🍀

March 13, 2019

### Do beer and AI go together?

For the next week, all my Deep Learning and AI courses are available for just $11.99! ($1.00 less than the current sale, woohoo!)

For my courses, please use the coupons below (included in the links), or if you want, enter the coupon code: MAR2019.

For prerequisite courses (math, stats, Python programming) and all other courses, follow the links at the bottom for sales of up to 90% off!

Since ALL courses on Udemy on sale, if you want any course not listed here, just click the general (site-wide) link, and search for courses from that page.

https://www.udemy.com/support-vector-machines-in-python/?couponCode=MAR2019

https://www.udemy.com/recommender-systems/?couponCode=MAR2019

And just as important, $11.99 coupons for some helpful prerequisite courses. You NEED to know this stuff to understand machine learning in-depth: General (site-wide): http://bit.ly/2oCY14Z Python http://bit.ly/2pbXxXz Calc 1 http://bit.ly/2okPUib Calc 2 http://bit.ly/2oXnhpX Calc 3 http://bit.ly/2pVU0gQ Linalg 1 http://bit.ly/2oBBir1 Linalg 2 http://bit.ly/2q5SGEE Probability (option 1) http://bit.ly/2p8kcC0 Probability (option 2) http://bit.ly/2oXa2pb Probability (option 3) http://bit.ly/2oXbZSK ### OTHER UDEMY COURSE COUPONS As you know, I’m the “Lazy Programmer”, not just the “Lazy Data Scientist” – I love all kinds of programming! iOS courses: https://lazyprogrammer.me/ios Android courses: https://lazyprogrammer.me/android Ruby on Rails courses: https://lazyprogrammer.me/ruby-on-rails Python courses: https://lazyprogrammer.me/python Big Data (Spark + Hadoop) courses: https://lazyprogrammer.me/big-data-hadoop-spark-sql Javascript, ReactJS, AngularJS courses: https://lazyprogrammer.me/js ### EVEN MORE COOL STUFF Into Yoga in your spare time? Photography? Painting? There are courses, and I’ve got coupons! If you find a course on Udemy that you’d like a coupon for, just let me know and I’ll hook you up! Go to comments # How to Meet Your New Years Resolutions in 2019 (Udemy Coupons$9.99)

January 1, 2019

#### Deep Learning and AI Courses for just $9.99 # New Years 2019 ### How to meet your New Years resolutions in 2019 Firstly, I’d like to wish everyone on this list a happy new year, we are off to a great start. The new year is a time to set goals, turn things around, and be better than we were before. What better way than to learn from thousands of experts around the world who are the best at what they do? Luckily, I’ve got something that will make it just a little easier. I know a lot of you have been waiting for this – well here it is – the LOWEST price possible on ALL Udemy courses (yes, the whole site!) For the next 10 days, ALL courses on Udemy (not just mine) are available for just$9.99!

For my courses, please use the Udemy coupons below (included in the links below), or if you want, enter the coupon code: JAN2019.

For prerequisite courses (math, stats, Python programming) and all other courses (Bitcoin, meditation, yoga, guitar, photography, whatever else you want to learn), follow the links at the bottom (or go to my website).

Since ALL courses on Udemy are on sale, if you want any course not listed here, just click the general (site-wide) link, and search for courses from that page.

https://www.udemy.com/recommender-systems/?couponCode=JAN2019

# Boxing Day 2018

### Celebrate the Holidays with New AI & Deep Learning Courses!

I’ve been busy making free content and updates for my existing courses, so guess what that means? Everything on sale!

For the next week, all my Deep Learning and AI courses are available for just $11.99! For my courses, please use the coupons below (included in the links), or if you want, enter the coupon code: DEC2018. For prerequisite courses (math, stats, Python programming) and all other courses, follow the links at the bottom for sales of up to 90% off! Since ALL courses on Udemy on sale, if you want any course not listed here, just click the general (site-wide) link, and search for courses from that page. https://www.udemy.com/recommender-systems/?couponCode=DEC2018 ### PREREQUISITE COURSE COUPONS And just as important,$11.99 coupons for some helpful prerequisite courses. You NEED to know this stuff to understand machine learning in-depth:

General (site-wide): http://bit.ly/2oCY14Z
Python http://bit.ly/2pbXxXz
Calc 1 http://bit.ly/2okPUib
Calc 2 http://bit.ly/2oXnhpX
Calc 3 http://bit.ly/2pVU0gQ
Linalg 1 http://bit.ly/2oBBir1
Linalg 2 http://bit.ly/2q5SGEE
Probability (option 1) http://bit.ly/2p8kcC0
Probability (option 2) http://bit.ly/2oXa2pb
Probability (option 3) http://bit.ly/2oXbZSK

### OTHER UDEMY COURSE COUPONS

As you know, I’m the “Lazy Programmer”, not just the “Lazy Data Scientist” – I love all kinds of programming!

iOS courses:
https://lazyprogrammer.me/ios

Android courses:
https://lazyprogrammer.me/android

Ruby on Rails courses:
https://lazyprogrammer.me/ruby-on-rails

Python courses:
https://lazyprogrammer.me/python

Big Data (Spark + Hadoop) courses:

Javascript, ReactJS, AngularJS courses:
https://lazyprogrammer.me/js

### EVEN MORE COOL STUFF

Into Yoga in your spare time? Photography? Painting? There are courses, and I’ve got coupons! If you find a course on Udemy that you’d like a coupon for, just let me know and I’ll hook you up!