Skip to content

Error when getting variance decomp from SVAR in statsmodels #6537

Open

Description

@Havlin

0

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

df1 = DF[[various data fields]]

A = np.asarray([[1, 0, 0],[0, 1, 0],['E', 'E', 1]])
B = np.asarray([['E', 0, 0], [0, 'E', 0], [0, 0, 'E']])
A_guess = np.asarray([0.5, 0.25, -0.38])
B_guess = np.asarray([0.5, 0.1, 0.05])
mymodel = SVAR(df1, svar_type='AB', A=A, B=B)
res = mymodel.fit(maxlags=1, maxiter=10000, maxfun=10000, solver='bfgs')

res.fevd()
returns this error:

TypeError: irf() got an unexpected keyword argument 'var_decomp'
When I try it this way:

res.fevd
I simply get the memory location of the instance

Activity

  1. josef-pkt commented on Feb 21, 2020

    @josef-pkt
    Member

    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.

  2. Havlin commented on Feb 21, 2020

    @Havlin
    Author

    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.

  3. josef-pkt commented on Feb 21, 2020

    @josef-pkt
    Member

    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.

  4. Shoresh92 commented on Apr 28, 2021

    @Shoresh92

    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!

  5. jansila commented on Jan 28, 2023

    @jansila

    Hi, is this going to be potentially fixed?

  6. added this to the 0.15 milestone on Jan 28, 2023
  7. josef-pkt commented on Jan 28, 2023

    @josef-pkt
    Member

    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.

  8. Gerard9199 commented on Jan 19, 2024

    @Gerard9199

    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.

        if svar:
            self.svar_irfs = model.svar_ma_rep(periods, P=P)
        else:
            self.orth_irfs = model.orth_ma_rep(periods, P=P)
    

    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:

        self.irfobj = model.irf(periods=periods) #Remove the var_comp parameter
        try:
            self.orth_irfs = self.irfobj.orth_irfs
        except:
            self.orth_irfs = self.irfobj.svar_irfs
    

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions