Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multivariate Granger causality #8710

Open
MatteoSerafino opened this issue Mar 2, 2023 · 0 comments
Open

Multivariate Granger causality #8710

MatteoSerafino opened this issue Mar 2, 2023 · 0 comments

Comments

@MatteoSerafino
Copy link

According to the documentation:

The data for testing whether the time series in the second column Granger causes the time series in the first column.

So it seems to visit possible to compute the GC using more time series. However: the following code:

import numpy as np
import statsmodels.api as sm

# Generate example data
nobs = 100
X1 = np.random.randn(nobs)
X2 = np.random.randn(nobs)
X3 = np.random.randn(nobs)
X4 = np.random.randn(nobs)
X5 = np.random.randn(nobs)

# Create numpy array with shape (nobs, 5)
data = np.column_stack((X1, X2, X3, X4, X5))

# Compute Granger causality tests for all pairs of variables and lags up to 2
maxlag = 2
results = sm.tsa.stattools.grangercausalitytests(data, maxlag, verbose=False)

# Print the p-values for the Granger causality tests
for lag in range(1, maxlag+1):
    print(f"Lag {lag}:")
    for i in range(data.shape[1]):
        for j in range(data.shape[1]):
            if i != j:
                print(f"X{i+1} -> X{j+1}: p-value = {results[lag][i+1][j]['ssr_ftest'][1]}")

raise the following error:

ValueError: wrong shape for coefs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant