Introduction to Pandas Granger Causality
=====================================
In the realm of time series analysis, understanding causality between variables is crucial for making informed decisions and predictions. The concept of Granger causality is a statistical test used to determine whether one time series variable can be said to have caused or influenced another. In this article, we will delve into the world of Pandas Granger Causality, exploring its implementation in Python using the popular Pandas library.
Background: Understanding Granger Causality
Granger causality is a concept introduced by Clive Granger in 1969. It states that if one time series variable can be used to forecast another variable, then it has caused or influenced that variable. In other words, the presence of the first variable indicates the presence of the second.
The idea behind this test is based on the following hypothesis:
- Null Hypothesis (H0): Xt does not granger causes Yt.
- Alternate Hypothesis (H1): Xt granger causes Yt.
Overview of Pandas Granger Causality Functionality
The pandas.stats.var package, although deprecated, provides an implementation for performing Granger causality tests on time series data. However, the function has been moved to the stats.model_selection module in newer versions of Pandas. We will explore both options and their usage.
Using stats.model_selection.grangercausality()
In Pandas 1.0.0 and later versions, you can use the following function for Granger causality:
from statsmodels.tsa.stattools import grangercausalitytests
# Example data
import pandas as pd
np.random.seed(123)
n = 100
x = np.cumsum(np.random.normal(size=n))
y = x + np.random.normal(size=n)
# Create a DataFrame and apply the Granger causality test
df = pd.DataFrame({'Xt': [x], 'Yt': [y]})
granger_test = grangercausalitytests(df, maxlag=1)
print(granger_test)
Using stats.var.granger_causality()
Although deprecated in newer versions of Pandas, we can still use the original stats.var package for Granger causality:
from pandas.stats.var import VAR
# Example data
import pandas as pd
np.random.seed(123)
n = 100
x = np.cumsum(np.random.normal(size=n))
y = x + np.random.normal(size=n)
# Create a DataFrame and apply the Granger causality test
df = pd.DataFrame({'Xt': [x], 'Yt': [y]})
var_model = VAR(df, maxlag=1)
granger_test_result = var_model.granger_causality()
print(granger_test_result.pvalues)
Interpreting the Results of Granger Causality Test
When performing a Granger causality test, you will typically obtain two tables:
p-valuetable: Each cell in this table corresponds to a cell in thef-stattable. The values in these cells represent the p-values corresponding to each pair of variables.f-stattable: Each value in this table represents the f-statistic for each pair of variables.
Let’s break down an example output:
p-value:
C B A
A 0.472122 0.798261 0.412984
B 0.327602 0.783978 0.494436
C 0.071369 0.385844 0.688292
f-stat:
C B A
A 0.524075 0.065955 0.680298
B 0.975334 0.075878 0.473030
C 3.378231 0.763898 0.162619
Here’s a brief explanation of the values:
p-value: This value represents the probability of observing an f-statistic as extreme or more extreme than the one calculated, assuming that there is no underlying relationship between variables.f-stat: This value measures the strength of the linear relationship between two variables. A higher absolute value indicates a stronger relationship.
To make sense of these values, we need to consider the context of our data:
- If P-value < 0.05 for any pair, it means that variable X has caused or influenced variable Y.
- High f-statistic values indicate strong linear relationships between variables.
Interpreting Results and Drawing Conclusions
Based on your results, you can conclude the following:
- Null Hypothesis (H0) Rejection: If P-value < 0.05 for any pair, you can reject the Null hypothesis (H0), which indicates that variable X has caused or influenced variable Y.
- Alternate Hypothesis (H1) Support: If P-value > 0.05 for all pairs, it means that there is no evidence to support the Alternate hypothesis (H1). In this case, you cannot conclude that variable X causes variable Y.
It’s crucial to remember that Granger causality does not imply direction of causation or imply a causal relationship in an economic sense. The test only confirms if one time series can be used for prediction purposes with respect to another.
Example Use Case: Predicting Time Series Data
Here is an example use case where we predict the future values of a time series based on its past:
from statsmodels.tsa.stattools import grangercausalitytests
import pandas as pd
import numpy as np
# Generate random time series data
np.random.seed(123)
n = 100
x = np.cumsum(np.random.normal(size=n))
y = x + np.random.normal(size=n)
# Create a DataFrame and apply the Granger causality test
df = pd.DataFrame({'Xt': [x], 'Yt': [y]})
granger_test_result = grangercausalitytests(df, maxlag=1)
print(granger_test_result)
# If P-value < 0.05 for X causes Y, use ARIMA model to predict future values of Y
from statsmodels.tsa.arima.model import ARIMA
if granger_test_result.pvalues['Yt,Xt'] < 0.05:
arima_model = ARIMA(df['Xt'], order=(5,1,0))
fit = arima_model.fit()
y_pred = fit.forecast(steps=30)
else:
print("There is no evidence that X causes Y")
In this example, we check if the time series Yt can be predicted using values from Xt. If there is a strong linear relationship (P-value < 0.05), we use an ARIMA model to forecast future values of Yt.
This concludes our journey into Pandas Granger Causality. With this functionality, you can easily perform causality analysis between time series variables and make predictions based on the relationships found.
Last modified on 2024-06-19