You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error when getting variance decomp from SVAR in statsmodels #6537
I have set up an SVAR model with statsmodels.SVARResults. When I attempt to get the variance decomp it throws error or it just returns the memory location. I have tried two ways.
import numpy as np
import numpy as np
import statsmodels.api as sm
from statsmodels.tsa.api import VAR, SVAR
import matplotlib.pyplot as plt
it's a bug, the signatures differ and we cannot set P or varcomp for irf.
I'm not familiar enough with SVAR to tell whether setting user provided P makes sense.
AFAICS, SVAR doesn't have unit test for irf, and not for fevd (obviously, given that that doesn't work)
There is a stata .do file in the test folder, but I think the unit tests only cover the estimation of the model in SVAR, and not the post-estimation parts.
P determines the decomposition of contemporary shocks
It is cholesky(resid var) by default in VAR.
SVAR needs to combine the structural matrices, so it differs for standard SVAR.
It's very spread out in the code. I was tracing it to BaseIRAnalysis.__init__ and it uses self.svar_irfs = model.svar_ma_rep(periods, P=P)
I never knew or don't remember much about SVAR irf and even less for fevd. I was just browsing the current code
I guess it wouldn't be very difficult to connect the P to get FEVD in SVAR.
However, the main work will be in writing unit tests, and checking that it actually works correctly for the SVAR case.
The last time I fixed something similar for VAR, I had a full test case. So fixing it wasn't so bad even without understanding all the code.
I am experiencing the same issue. VAR models and its variations become increasingly popular and it would be great if we can get this issue fixed. Thanks!
just some thoughts (without checking code or references)
I have not looked at VAR, VECM, SVAR in a long time and never really at the theory behind SVAR post-estimation, predict, irf, ...
given that SVAR is a simultaneous equation model:
I guess one step ahead and similar prediction would need to use the reduced form (y just as function of predetermined variables) as in simultaneous equation models.
I guess IRF, FEVD would have a similar problem.
Stata docs for var svar post-estimation does not have a technical section, it does not specify how irf, fevd and predict are computed for svar.
That is,
Can we get the forecasting results and IRF, FEVD from a reduced form VAR created from the estimated parameters of the SVAR model?
A bit similar to VECM where we use the implied VAR with parameters from the estimated VECM.
If we can do this, then it will be worth a look for the 0.15 release.
When I fixed the var_comp problem I encountered a new problem with SVAR models in statsmodels, specifically an issue related to the orth_irfs and svar_irfs attributes. Here's how I resolved both errors:
First, var_comp:
I changed two functions inside VARResults class for the var_comp error:
def irf(self, periods=10, P=None, var_order=None):
"""Analyze impulse responses to shocks in system
Parameters
----------
periods : int
var_decomp : ndarray (k x k), lower triangular
Must satisfy Omega = P P', where P is the passed matrix. Defaults
to Cholesky decomposition of Omega
var_order : sequence
Alternate variable order for Cholesky decomposition
Returns
-------
irf : IRAnalysis
"""
if var_order is not None:
raise NotImplementedError(
"alternate variable order not implemented" " (yet)"
)
return IRAnalysis(self, P=P, periods=periods)
def fevd(self, periods=10, P=None):
"""
Compute forecast error variance decomposition ("fevd")
Returns
-------
fevd : FEVD instance
"""
return FEVD(self, P=P, periods=periods)
Second, orth_irfs
The BaseIRAnalysis constructor receives svar as False by default. Based on this setting, there should be an orth_irfs attribute. However, in SVAR models, this attribute is absent, and instead, we have svar_irfs.
I modified the FEVD class in var_model.py to handle both types of attributes. I used a try-except block to assign the correct attribute based on the model type:
After applying this solution, I was able to obtain plots and results without any errors. If anyone has an alternative solution or suggestions for improving this fix, I would be delighted to hear them.
Activity
josef-pkt commentedon Feb 21, 2020
it's a bug, the signatures differ and we cannot set P or varcomp for irf.
I'm not familiar enough with SVAR to tell whether setting user provided P makes sense.
AFAICS, SVAR doesn't have unit test for irf, and not for fevd (obviously, given that that doesn't work)
There is a stata
.dofile in the test folder, but I think the unit tests only cover the estimation of the model in SVAR, and not the post-estimation parts.Havlin commentedon Feb 21, 2020
Damn. FEVDs are really important in SVARs. Do you know what 'P' stands for?
It's weird cause fevd words fine with statsmodels regular VAR.
josef-pkt commentedon Feb 21, 2020
P determines the decomposition of contemporary shocks
It is cholesky(resid var) by default in VAR.
SVAR needs to combine the structural matrices, so it differs for standard SVAR.
It's very spread out in the code. I was tracing it to
BaseIRAnalysis.__init__and it usesself.svar_irfs = model.svar_ma_rep(periods, P=P)I never knew or don't remember much about SVAR irf and even less for fevd. I was just browsing the current code
I guess it wouldn't be very difficult to connect the P to get FEVD in SVAR.
However, the main work will be in writing unit tests, and checking that it actually works correctly for the SVAR case.
The last time I fixed something similar for VAR, I had a full test case. So fixing it wasn't so bad even without understanding all the code.
josef-pkt commentedon Feb 21, 2020
the similar case for
Pin VAR and generalized irfhttps://groups.google.com/forum/#!msg/pystatsmodels/BqMqOIghN78/21NkPAEPJgIJ
Shoresh92 commentedon Apr 28, 2021
I am experiencing the same issue. VAR models and its variations become increasingly popular and it would be great if we can get this issue fixed. Thanks!
jansila commentedon Jan 28, 2023
Hi, is this going to be potentially fixed?
josef-pkt commentedon Jan 28, 2023
just some thoughts (without checking code or references)
I have not looked at VAR, VECM, SVAR in a long time and never really at the theory behind SVAR post-estimation, predict, irf, ...
given that SVAR is a simultaneous equation model:
I guess one step ahead and similar prediction would need to use the reduced form (y just as function of predetermined variables) as in simultaneous equation models.
I guess IRF, FEVD would have a similar problem.
Stata docs for var svar post-estimation does not have a technical section, it does not specify how irf, fevd and predict are computed for svar.
That is,
Can we get the forecasting results and IRF, FEVD from a reduced form VAR created from the estimated parameters of the SVAR model?
A bit similar to VECM where we use the implied VAR with parameters from the estimated VECM.
If we can do this, then it will be worth a look for the 0.15 release.
Gerard9199 commentedon Jan 19, 2024
When I fixed the var_comp problem I encountered a new problem with SVAR models in statsmodels, specifically an issue related to the orth_irfs and svar_irfs attributes. Here's how I resolved both errors:
First, var_comp:
I changed two functions inside VARResults class for the var_comp error:
Second, orth_irfs
The BaseIRAnalysis constructor receives svar as False by default. Based on this setting, there should be an orth_irfs attribute. However, in SVAR models, this attribute is absent, and instead, we have svar_irfs.
I modified the FEVD class in var_model.py to handle both types of attributes. I used a try-except block to assign the correct attribute based on the model type:
After applying this solution, I was able to obtain plots and results without any errors. If anyone has an alternative solution or suggestions for improving this fix, I would be delighted to hear them.