close

[Solved] Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests? in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests Error Occurs?

Today I get the following error Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests? in python.

How To Solve Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests Error ?

  1. How To Solve Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests Error ?

    To Solve Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests Error Another thing to keep an eye out for is duplicate columns. Duplicate columns will have a correlation score of 1.0, resulting in singularity.

  2. Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests?

    To Solve Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests Error Another thing to keep an eye out for is duplicate columns. Duplicate columns will have a correlation score of 1.0, resulting in singularity.

Solution 1

The problem arises due to the perfect correlation between the two series in your data. From the traceback, you can see, that internally a wald test is used to compute the maximum likelihood estimates for the parameters of the lag-time series. To do this an estimate of the parameters covariance matrix (which is then near-zero) and its inverse is needed (as you can also see in the line invcov = np.linalg.inv(cov_p) in the traceback). This near-zero matrix is now singular for some maximum lag number (>=5) and thus the test crashes. If you add just a little noise to your data, the error disappears:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import grangercausalitytests

n = 1000
ls = np.linspace(0, 2*np.pi, n)
df1Clean = pd.DataFrame(np.sin(ls))
df2Clean = pd.DataFrame(2*np.sin(ls+1))
dfClean = pd.concat([df1Clean, df2Clean], axis=1)
dfDirty = dfClean+0.00001*np.random.rand(n, 2)

grangercausalitytests(dfClean, maxlag=20, verbose=False)    # Raises LinAlgError
grangercausalitytests(dfDirty, maxlag=20, verbose=False)    # Runs fine

Solution 2

Another thing to keep an eye out for is duplicate columns. Duplicate columns will have a correlation score of 1.0, resulting in singularity. Otherwise, it’s also possible you have 2 features that are perfectly correlated. And easy way to check this is with df.corr(), and look for pairs of columns with correlation = 1.0.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read