Skip to content

sirf_errband_mc / error message #svar #5280

Closed

Description

@mk1564

Hi,

I get error message when I try to find error band of my SVAR model. "shape mismatch: value array of shape (0,) could not be broadcast to indexing result of shape (0,4,4)" Can you guys help me out?

ValueError Traceback (most recent call last)
in ()
----> 1 result.sirf_errband_mc(orth=False, repl=1000, T=10,signif=0.05, seed=None, burn=100, cum=False)
~\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in sirf_errband_mc(self, orth, repl, T, signif, seed, burn, cum)
721 if i < 10:
722 sol = SVAR(sim, svar_type=s_type, A=A_pass,
--> 723 B=B_pass).fit(maxlags=k_ar)
724 g_list.append(np.append(sol.A[A_mask].tolist(),
725 sol.B[B_mask].tolist()))
~\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in fit(self, A_guess, B_guess, maxlags, method, ic, trend, verbose, s_method, solver, override, maxiter, maxfun)
183 return self._estimate_svar(start_params, lags, trend=trend,
184 solver=solver, override=override,
--> 185 maxiter=maxiter, maxfun=maxfun)
186
187
~\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in _estimate_svar(self, start_params, lags, maxiter, maxfun, trend, solver, override)
253 solver=solver,
254 maxiter=maxiter,
--> 255 maxfun=maxfun)
256 A_mask = self.A_mask
257 B_mask = self.B_mask
~\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in _solve_AB(self, start_params, maxiter, maxfun, override, solver)
357 A_len = len(A[A_mask])
358
--> 359 A[A_mask] = start_params[:A_len]
360 B[B_mask] = start_params[A_len:]
361
ValueError: shape mismatch: value array of shape (0,) could not be broadcast to indexing result of shape (0,4,4)

Activity

  1. changed the title [-]sirf_errband_mc / error message[/-] [+]sirf_errband_mc / error message #svar[/+] on Sep 30, 2018
  2. mk1564 commented on Sep 30, 2018

    @mk1564
    Author

    Here is my model.

    A = np.asarray([[1, 0, 0,0],['E', 1, 0,0],['E', 'E', 1,0],['E', 'E', 'E',1]])
    B = np.asarray([['E', 0, 0,0], [0, 'E',0, 0], [0, 0, 'E',0],[0,0,0,'E']])
    model = SVAR(df[['r','gm','gp','gy']], svar_type='AB', A=A, B=B)
    result = model.fit(maxlags=1,maxiter=500, maxfun=500)
    result.sirf_errband_mc(orth=False, repl=1000, T=10,signif=0.05, seed=None, burn=100, cum=False)

    so A_mask is going to be

    array([[False, False, False, False], [ True, False, False, False], [ True, True, False, False], [ True, True, True, False]])

  3. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    @mk1564
    I tried a simple example with your setting with random numbers, but for me it fails already earlier.
    I'm using python 3.4 and have problems with bytes versus strings. my numpy is 1.11.2

    (Pdb) A_pass
    array([[b'1', b'0', b'0', b'0'],
           [b'E', b'1', b'0', b'0'],
           [b'E', b'E', b'1', b'0'],
           [b'E', b'E', b'E', b'1']], 
          dtype='|S1')
    (Pdb) A_mask
    array([[False, False, False, False],
           [ True, False, False, False],
           [ True,  True, False, False],
           [ True,  True,  True, False]], dtype=bool)
    (Pdb) A_pass == 'E'
    False
    (Pdb) A_pass == b'E'
    array([[False, False, False, False],
           [ True, False, False, False],
           [ True,  True, False, False],
           [ True,  True,  True, False]], dtype=bool)
    

    What are your python, numpy and statsmodels version?

    Given that there are no unit tests for sirf_errband_mc, it is likely that there are still some basic python 3 incompatibilities.

  4. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    A changes dtype

    the A array created by the user code is U(nicode)

    array([['1', '0', '0', '0'],
           ['E', '1', '0', '0'],
           ['E', 'E', '1', '0'],
           ['E', 'E', 'E', '1']], 
          dtype='<U11')
    

    SVARResult.sirf_errband_mc recreates it as S1 bytes array

  5. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    If I recreate the model in SVARResult.sirf_errband_mc with dtype '<U11', then the
    sirf_eb = result.sirf_errband_mc(orth=False, repl=100, T=10, signif=0.05, seed=None, burn=100, cum=False)
    finishes without exception
    sirf_eb is a 2-tuple with

    (Pdb) sirf_eb[0].shape
    (11, 4, 4)
    (Pdb) sirf_eb[1].shape
    (11, 4, 4)
    
  6. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    irf plot seems to work also

    >>> irf = result.irf()
    >>> irf.plot()
    <matplotlib.figure.Figure object at 0x00000000060BE828>
    >>> irf.plot(stderr_type='mc')
    <matplotlib.figure.Figure object at 0x000000000C8A3BE0>
    

    But I have no idea whether the numbers are correct.

    So quickfix seems to be to recreate A and B with the same dtype S or U in sirf_errband_mc.
    The better fix would be to store the original user provided A and B and use it directly instead of recreating it.

    (Based on quick bughunting in those lines that cause problems without trying to understand larger parts of SVAR. I haven't looked at it in quite some time, and never at IRF for SVAR.)

  7. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    AFAICS, there is no caching of the MonteCarlo results. repeated calls to irf.plot perform a new simulation each time.
    Because there are options for irf and irf.plot, we cannot simply store the results, i.e. they would only be valid for the last set of options, until we memoize for given arguments.

  8. mk1564 commented on Oct 1, 2018

    @mk1564
    Author

    Thank you for your comment! So do you mean I need to create A like

    A1 = np.array([[1, 0, 0,0],['E', 1, 0,0],['E', 'E', 1,0],['E', 'E', 'E',1]],dtype='|S1')? It seems like it does not work even at the model fitting step.

    How did you change the model with dtype U?

  9. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    Because we will be dropping Python 2.7 within the next one or two years, it is better to stick with unicode

    The change that I made locally is in sirf_errband_mc in svar_model.py lines 677 and 678

    @@ -672,12 +672,12 @@ class SVARResults(SVARProcess, VARResults):
    ...
    -        A_pass = np.zeros(A.shape, dtype='|S1')
    -        B_pass = np.zeros(B.shape, dtype='|S1')
    +        A_pass = np.zeros(A.shape, dtype='<U11') #'|S1')
    +        B_pass = np.zeros(B.shape, dtype='<U11') #'|S1')
    
  10. mk1564 commented on Oct 1, 2018

    @mk1564
    Author

    Thanks for your explanation, but after I changed the code as you said, I run the model again

    A = np.array([[1, 0, 0,0],['E', 1, 0,0],['E', 'E', 1,0],['E', 'E', 'E',1]])
    B = np.array([['E', 0, 0,0], [0, 'E',0, 0], [0, 0, 'E',0],[0,0,0,'E']])
    model = SVAR(df[['r','gm','gp','gy']], svar_type='AB', A=A, B=B)
    result = model.fit()

    the last line causes an error like the following.. Do you know how to fix this?

    TypeError Traceback (most recent call last)
    in ()
    4 #Bguess=np.array([0.59,8.75,0.15,0.54])
    5 model = SVAR(df[['r','gm','gp','gy']], svar_type='AB', A=A, B=B)
    ----> 6 result = model.fit()
    7 #A_guess=Aguess, B_guess=Bguess,

    in fit(self, A_guess, B_guess, maxlags, method, ic, trend, verbose, s_method, solver, override, maxiter, maxfun)
    182 return self._estimate_svar(start_params, lags, trend=trend,
    183 solver=solver, override=override,
    --> 184 maxiter=maxiter, maxfun=maxfun)
    185
    186

    in _estimate_svar(self, start_params, lags, maxiter, maxfun, trend, solver, override)
    253 solver=solver,
    254 maxiter=maxiter,
    --> 255 maxfun=maxfun)
    256 A_mask = self.A_mask
    257 B_mask = self.B_mask

    in _solve_AB(self, start_params, maxiter, maxfun, override, solver)
    362
    363 if override == False:
    --> 364 J = self._compute_J(A, B)
    365 self.check_order(J)
    366 self.check_rank(J)

    in _compute_J(self, A_solve, B_solve)
    394 #first generate duplication matrix, see MN (1980) for notation
    395
    --> 396 D_nT=np.zeros([(1.0/2)(neqs)(neqs+1),neqs**2])
    397
    398 for j in range(neqs):

    TypeError: 'float' object cannot be interpreted as an integer

  11. josef-pkt commented on Oct 1, 2018

    @josef-pkt
    Member

    that's strange because there was no change in fit.

    I opened #5285 to see how the fix works across python and numpy versions.

  12. added this to the 0.10 milestone on Dec 11, 2018
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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions