Granger Causality in Python : Data Science Code
Key Takeaways
This video demonstrates how to perform a Granger Causality test in Python using the statsmodels library, specifically the grangercausalitytests function, to determine if one time series causes another.
Full Transcript
hi everyone welcome back so this is going to be a very very short video just showing you how to do the granger causality test in python so i had a whole video on the theory of the grincher causality test and i'll link that in the description below just in a nutshell again what do we use granger causality for so a lot of times we'll have two or more time series we'll be interested in if one of the time series causes another time series and just to give a real world example let's say we have one neighborhood and another neighborhood that's right next to it we might have a case where house prices in one neighborhood change and then house prices in the other neighborhood change in a similar way except it takes one or two months for that change to be seen so in that case the house price in one neighborhood is causing the house price in the other neighborhood now it's actually pretty difficult to figure out this causality it takes a lot of very difficult work so we use kind of a proxy called granger causality so this just looks at the two time series and sees if we can use a shifted version of one of the time series to well predict one of the other time series so again the theory video is linked in the description below this is just a very short code video the only special library you're going to need is from statsmodels.tsa.statstools import granger causality tests so as you might have guessed we're not writing any of the code ourselves it's already been written i will just show you how to properly call this function and how to interpret the results so we'll also be using some simulated data for today just to make things easier and you don't have to download any data yourself so here i've just generated a very simple ar1 process so the coefficient is 0.5 really nothing special going on here and that's called t1 and then t2 is going to be the same time series as t1 except i add a little bit more random noise at each step just to make this more realistic looking and this cell right here is basically shifting the time series by three months or weeks or whatever the unit of time you want to consider is so that time series t2 now kind of follows the same signature as time series t1 except it's shifted three months into the future the easiest way to see that is in this graph so t1 is the blue curve and t2 is the red curve so although it's a little bit difficult to see if you look at the red curve that it is roughly a shifted version of the blue curve for example look at this peak in the blue curve here you see that three months later we see a similar peak in the red curve here look at this spike in the blue curve here you see that in the red curve we see a similar spike about three months later so of course there is some random noise added to the red curve so it's not an exact copy three months shifted but it is a rough copy three months shifted so how do we pick up on that using code so first we need to pack these two time series into a pandas data frame and here is a very very important part you're going to have two columns in your data frame so those are going to be your two time series you want to make sure to put the first column as the time series that you think is being granger caused by the second one so here what i'm doing is checking whether time series t2 is granger caused by time series t1 therefore t2 goes in the first position the first column and t1 goes in the second column constructed that data frame here just two columns one with time series t1 and the other with time series t2 and then our hard work is done all we have to do is call the granger causality tests function put in our data frame and here you can put in the number of lags that you want to check so by putting in three i'm saying go ahead and check if it grains your causes at one lag at two lags and three lags so you can do as many as you want and let's first look at what happens if we consider one lag so it runs these four tests and we can look at the p values which are kind of in the same ballpark and they're all pretty far away from point zero five so we can confidently say that t2 is not granger caused by t1 looking at just one lag alone let's look at if we look at two lags here these p values are getting lower but still kind of far away from low values like .05 so we can also say that time series t2 is not granger caused by t1 looking at two lags only now here's where it's interesting if we look at three lags we see that all the p values go to zero or virtually zero so we have very strong evidence to say that time series t2 is grained or caused by time series t1 by looking at three lags and that's exactly the way we constructed it and it's nice to see that we get the same exact result when we actually call the function and so that's about it you all just wanted to quickly show you how to do a grainger causality test in python this code will as always be available in the description below and i'll see you next time
Original Description
Coding Granger Causality in Python!
Granger Causality Theory Video: https://www.youtube.com/watch?v=b8hzDzGWyGM
Link to Code: https://github.com/ritvikmath/Time-Series-Analysis/blob/master/Granger%20Causality.ipynb
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from ritvikmath · ritvikmath · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Math Team Update
ritvikmath
Single Variable Calculus Volume of a Sphere - Proof 1
ritvikmath
Single Variable Calculus Volume of a Sphere - Proof 2
ritvikmath
Multivariable Calculus Volume of a Sphere Proof - Triple Integrals
ritvikmath
Multivariable Calculus Volume of a Sphere Proof - Double Integrals
ritvikmath
The Euclidian Algorithm
ritvikmath
Proving the Chain Rule
ritvikmath
Proving the Fundamental Theorem of Calculus Part 1
ritvikmath
Proving the Fundamental Theorem of Calculus Part 2
ritvikmath
Math Puzzle - Poison Perplexity
ritvikmath
Math Puzzle - Poison Perplexity - Solution
ritvikmath
Expected Value and Variance of Continuous Random Variables (Calculus)
ritvikmath
Expected Value and Variance of Discrete Random Variables (No Calculus)
ritvikmath
Array Method
ritvikmath
Complex Power Series and their Derivatives
ritvikmath
Distributions - Intro
ritvikmath
The Poisson Distribution
ritvikmath
The Bernoulli Distribution
ritvikmath
The Binomial Distribution
ritvikmath
The Continuous Uniform Distribution
ritvikmath
The Geometric Distribution
ritvikmath
The Triangular Distribution
ritvikmath
The Exponential Distribution
ritvikmath
The Borel Distribution + Notes on Poisson Distribution
ritvikmath
The Gamma Distribution
ritvikmath
The Normal Distribution
ritvikmath
The Laplace Distribution
ritvikmath
The Chi - Squared Distribution
ritvikmath
Overfitting
ritvikmath
Vector Norms
ritvikmath
Truths Behind the Titanic : K-Nearest Neighbor
ritvikmath
The Mathematics of Breakups
ritvikmath
Sillyfish
ritvikmath
Finding Optimal Paths - Dynamic Programming
ritvikmath
HowToDataScience : Scraping Twitter Data
ritvikmath
Decision Trees
ritvikmath
Perceptron
ritvikmath
Naive Bayes
ritvikmath
K-Nearest Neighbor
ritvikmath
Evaluating Machine Learning Models
ritvikmath
Decision Tree Pruning
ritvikmath
K-Means Clustering
ritvikmath
Gaussian Mixture Model
ritvikmath
Data Science - Fuzzy Record Matching
ritvikmath
Time Series Talk : Autocorrelation and Partial Autocorrelation
ritvikmath
Time Series Talk : Autoregressive Model
ritvikmath
Time Series Talk : Moving Average Model
ritvikmath
Time Series Talk : ARMA Model
ritvikmath
Time Series Talk : ARCH Model
ritvikmath
Time Series Talk : White Noise
ritvikmath
Time Series Talk : Stationarity
ritvikmath
Time Series Talk : ARIMA Model
ritvikmath
Time Series Talk : Lag Operator
ritvikmath
Time Series Talk : What is Seasonality ?
ritvikmath
Time Series Talk : Seasonal ARIMA Model
ritvikmath
So ... What Actually is a Matrix ? : Data Science Basics
ritvikmath
Derivative of a Matrix : Data Science Basics
ritvikmath
Basics of PCA (Principal Component Analysis) : Data Science Concepts
ritvikmath
Eigenvalues & Eigenvectors : Data Science Basics
ritvikmath
The Covariance Matrix : Data Science Basics
ritvikmath
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
After interviewing over 100 ML Candidates. Last Week Someone Walked In and Made Me Take Notes.
Medium · Machine Learning
How AI Learns with Less Labeled Data
Medium · Machine Learning
Mastering TypeScript — Understanding the TypeScript Compiler (tsc) from Scratch — Lesson 2
Medium · JavaScript
Stop Overfitting With Basically One Line of Code
Medium · AI
🎓
Tutor Explanation
DeepCamp AI