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

var_model test_causality vs test_inst_causality redundant code? #4463

Closed
jbrockmendel opened this issue Apr 10, 2018 · 6 comments
Closed

var_model test_causality vs test_inst_causality redundant code? #4463

jbrockmendel opened this issue Apr 10, 2018 · 6 comments

Comments

@jbrockmendel
Copy link
Contributor

The construction of the restriction matrix is done differently in these two methods, but I suspect they end up equivalnet

@josef-pkt
Copy link
Member

@jbrockmendel Add the name of the module, so we don't have to guess.

I never looked at test_inst_causality.
I think those two hypothesis tests are very different.
Test_causality is a Wald test for zero restriction on lag parameters, the instantaneous is some (?) test on the structure of the contemporaneous correlation matrix (I guess).

@jbrockmendel jbrockmendel changed the title test_causality vs test_inst_causality redundant code? var_model test_causality vs test_inst_causality redundant code? Apr 14, 2018
@jbrockmendel
Copy link
Contributor Author

I'll have to take a closer look then to be sure. Strong prior for code being duplicated based on a) its in var_model and b) test_inst_causality came in with vecm.

@josef-pkt
Copy link
Member

check some references, it's not easy to see from the code what it is supposed to do.

I'm pretty sure that it is a test for the covariance/correlation structure (like a off-diagonal block of zeros)
(maybe similar to #4143 or LR based tests for correlation structures)

@jbrockmendel
Copy link
Contributor Author

it's not easy to see from the code what it is supposed to do.

You're right, that does sound like a problem in and of itself.

Lines 1760-1770 look analogous to 1633-1653. Then:

       k, p = self.neqs, self.k_ar

        # number of restrictions
        num_restr = len(causing) * len(caused) * p
        num_det_terms = self.k_trend

        # Make restriction matrix
        C = np.zeros((num_restr, k * num_det_terms + k**2 * p), dtype=float)
        cols_det = k * num_det_terms
        row = 0
        for j in range(p):
            for ing_ind in causing_ind:
                for ed_ind in caused_ind:
                    C[row, cols_det + ed_ind + k * ing_ind + k**2 * j] = 1
                    row += 1

vs

        k, t, p = self.neqs, self.nobs, self.k_ar

        num_restr = len(causing) * len(caused)  # called N in Lutkepohl

        sigma_u = self.sigma_u
        vech_sigma_u = util.vech(sigma_u)
        sig_mask = np.zeros(sigma_u.shape)
        # set =1 twice to ensure, that all the ones needed are below the main
        # diagonal:
        sig_mask[causing_ind, caused_ind] = 1
        sig_mask[caused_ind, causing_ind] = 1
        vech_sig_mask = util.vech(sig_mask)
        inds = np.nonzero(vech_sig_mask)[0]

        # Make restriction matrix
        C = np.zeros((num_restr, len(vech_sigma_u)), dtype=float)
        for row in range(num_restr):
            C[row, inds[row]] = 1

... aren't quite doing the same thing, but look related. I'll see if I can sort this out.

@jbrockmendel
Copy link
Contributor Author

Uh also it looks like VARResults.cov_params may having a wrapping problem

data = sm.datasets.macrodata.load_pandas().data
endog = data[['realgdp', 'realcons', 'realinv']].diff().iloc[1:]
endog.index = pd.date_range('2016-01-01', periods=len(endog))
model = sm.tsa.VAR(endog)
res = model.fit(3)

>>> res.cov_params
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/statsmodels/base/wrapper.py", line 41, in __getattribute__
    obj = data.wrap_output(obj, how=how)
  File "/usr/local/lib/python2.7/site-packages/statsmodels/base/data.py", line 411, in wrap_output
    return self.attach_cov(obj)
  File "/usr/local/lib/python2.7/site-packages/statsmodels/base/data.py", line 531, in attach_cov
    columns=self.param_names)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 361, in __init__
    copy=copy)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 533, in _init_ndarray
    return create_block_manager_from_blocks([values], [columns, index])
  File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 4631, in create_block_manager_from_blocks
    construction_error(tot_items, blocks[0].shape[1:], axes, e)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 4608, in construction_error
    passed, implied))
ValueError: Shape of passed values is (30, 30), indices imply (10, 10)

@josef-pkt
Copy link
Member

open a separate bug issue for VARResults.cov_params
workaround use res._results.cov_params to access the underlying ndarray

I'll let you figure out the two causality tests, but they look very different to me, the first quote is making a restriction matrix for params, the second is making a restriction matrix for vech(sigma_u) (naming cov_resid = sigma_u, IIRC)

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

2 participants