Skip to content

BUG/REF: SVAR sirf_errband_mc use unicode, use seed - #5285

Closed
josef-pkt wants to merge 3 commits into
statsmodels:masterfrom
josef-pkt:svar_irf_errband_5280
Closed

josef-pkt wants to merge 3 commits into
statsmodels:masterfrom
josef-pkt:svar_irf_errband_5280

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Oct 1, 2018 •

Copy link
Copy Markdown
Member

closes #5280 SVAR sirf_errband_mc raises exception, especially on py 3

this avoids recreating the original A and B, and choosing string/bytes versus unicode, py2 and py3 compatible

adds unit test that is mostly smoke test (but found a missing seed bug)
errband/confint values in plot look reasonable

@josef-pkt

Copy link
Copy Markdown
Member Author

I get a large number of warning about convergence failures when I run the svar unit tests locally.
I didn't investigate, but this might need better start_params especially in the Monte Carlo.

@josef-pkt

Copy link
Copy Markdown
Member Author

SVAR does not keep any convergence information, i.e. no mle_retvals and also no mle_settings.
I don't see any attribute that would indicate convergence or non-convergence

The actual optimization with super(...).fit call is in _solve_AB which only returns A and B and no information about the optimization.

@codecov

codecov Bot commented Oct 1, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5285 into master will increase coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5285      +/-   ##
==========================================
+ Coverage   81.71%   81.77%   +0.06%     
==========================================
  Files         569      569              
  Lines       89710    89704       -6     
  Branches    10028    10026       -2     
==========================================
+ Hits        73306    73356      +50     
+ Misses      14132    14078      -54     
+ Partials     2272     2270       -2
Impacted Files Coverage Δ
statsmodels/tsa/vector_ar/svar_model.py 80.85% <100%> (+16.41%) ⬆️
statsmodels/tsa/vector_ar/tests/test_svar.py 100% <100%> (ø) ⬆️
statsmodels/genmod/tests/test_gee.py 98.92% <0%> (-0.02%) ⬇️
...smodels/genmod/generalized_estimating_equations.py 83.1% <0%> (ø) ⬆️
statsmodels/genmod/generalized_linear_model.py 90.7% <0%> (ø) ⬆️
statsmodels/tsa/vector_ar/irf.py 59.86% <0%> (+3.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1883f8a...fa9104f. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

I changed the sirf_errband_mc optimization to start initially with the sample estimates of A and B

@coveralls

coveralls commented Oct 1, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 84.27% when pulling fa9104f on josef-pkt:svar_irf_errband_5280 into 1883f8a on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

another bug/limitation, I guess:
SVAR simulation in sirf_errband_mc does not use trend, it uses the default intercept.
This might be correct if the simulation results/distribution are invariant to trend, i.e. to a deterministic component.
That would not be the case in a VECM, but might be correct in a trend stationary system.

@josef-pkt

Copy link
Copy Markdown
Member Author

first run on travis shows that this fails on python 2.7, which I expected.
i.e. I think we need S on python 2.7 and U on python 3.

However, if we just store the original A and B, then we don't need to rebuild them and choose a dtype.

@josef-pkt

Copy link
Copy Markdown
Member Author

fails on python 2.7 with, I guess float round instead of int
i.e. need to cast to int

        index = round(signif/2*repl)-1,round((1-signif/2)*repl)-1
>       lower = ma_sort[index[0],:, :, :]
E       IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

@josef-pkt
josef-pkt force-pushed the svar_irf_errband_5280 branch from 61e971d to b0d0a3f Compare October 1, 2018 17:41
@josef-pkt

Copy link
Copy Markdown
Member Author

numerical reproducibility fails in 32bit windows
https://ci.appveyor.com/project/josef-pkt/statsmodels/build/1.0.4917/job/0agbywcn27hy6k4i

        # on my Windows environment those two are equal
>       assert_allclose(errband1, errband2, rtol=1e-10)
E       AssertionError: 
E       Not equal to tolerance rtol=1e-10, atol=0
E       
E       (mismatch 81.8181818182%)
E        x: array([-7.385032e-03,  0.000000e+00,  0.000000e+00, -3.584766e-03,
E              -5.062701e-03,  0.000000e+00, -2.963227e-02,  1.717453e-02,
E              -1.878329e-02, -9.533639e-04, -3.196770e-03, -3.543380e-04,...
E        y: array([-7.385032e-03,  0.000000e+00,  0.000000e+00, -3.584766e-03,
E              -5.062701e-03,  0.000000e+00, -2.963227e-02,  1.717454e-02,
E              -1.878329e-02, -9.533637e-04, -3.196770e-03, -3.543380e-04,...
statsmodels\tsa\vector_ar\tests\test_svar.py:72: AssertionError
=========================== short test summary info ===========================

@josef-pkt
josef-pkt force-pushed the svar_irf_errband_5280 branch 2 times, most recently from b95f843 to 530bd79 Compare October 2, 2018 15:05
@josef-pkt josef-pkt added this to the 0.10 milestone Oct 2, 2018
@josef-pkt

Copy link
Copy Markdown
Member Author

The last amend and force push cleaned up the last commit (which I kept dirty during debugging)

This is finished as BUG fix and minor refactoring. There is still work to do to clean up SVAR, but not in this PR.

@mk1564 Can you try this out with your python environment and data?
If not, then I just merge it as is when it is green.

@josef-pkt

Copy link
Copy Markdown
Member Author

one appveyor failed but in unrelated code, it looks like TestGEE.test_formulas is missing a random.seed
https://ci.appveyor.com/project/josef-pkt/statsmodels/build/1.0.4919/job/w3pwx0vh0vy9rcxw

otherwise all green and ready to merge

@mk1564

mk1564 commented Oct 3, 2018 •

Copy link
Copy Markdown

I am using Numpy 1.14.3 and python 3.6.5. I just run the code

A1 = np.array([[1, 0, 0,0],['E', 1, 0,0],['E', 'E', 1,0],['E', 'E', 'E',1]])
model2 = ts.api.SVAR(df[['r','gm','gp','gy']], svar_type='A', A=A1)
result2 = model2.fit( trend='nc',maxlags=1,maxiter=500, maxfun=500)

which works fine and I could plot irf as well. But error band still causes an error.

sirf_eb = result2.sirf_errband_mc(orth=False, repl=100, T=10, signif=0.05, seed=None, burn=100, cum=False)

gives

ValueError Traceback (most recent call last)
in ()
----> 1 sirf_eb = result2.sirf_errband_mc(orth=False, repl=100, 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)

@josef-pkt

Copy link
Copy Markdown
Member Author

@mk1564 thanks for checking and reporting this
that's still a bug if either A or B is None, i.e. not specified by user.

I guess the simplest solution would be to keep A_original and B_original as None if not specified.

Can your try your cases after moving the self.A_original self.B_original up in the method so it is after
svar_ckerr(svar_type, A, B) and before if A is None:

The example passes then for me, and A_original and B_original is whatever the user specified

        svar_ckerr(svar_type, A, B)
        
        self.A_original = A
        self.B_original = B

        #initialize A, B as I if not given
        #Initialize SVAR masks
        if A is None:

there are currently no test cases for A is None or B is None, so it's not as easy to just add an new unit test method

@mk1564

mk1564 commented Oct 3, 2018

Copy link
Copy Markdown

Okay now it works! Thank you so much for your help 👍

@josef-pkt

Copy link
Copy Markdown
Member Author

@mk1564 Thanks for the bug report.
If you see other suspicious things or bugs, then please report it also.
SVAR was a GSOC project, but the student disappeared before adding unit tests. I added some against Stata and looked at several examples which looked good. But given the small test suite for SVAR, there might still be some problems remaining.

I will push the latest version of the fix and some basic unit tests tonight or tomorrow, then merge.

@mk1564

mk1564 commented Oct 3, 2018

Copy link
Copy Markdown

@josef-pkt Sure will do! Thanks again.

@josef-pkt
josef-pkt force-pushed the svar_irf_errband_5280 branch from 530bd79 to fa9104f Compare October 3, 2018 16:47
@josef-pkt

josef-pkt commented Oct 3, 2018 •

Copy link
Copy Markdown
Member Author

I also forgot to set the seed for the MC for errband.
correction: no I didn't, without the seed the two MC wouldn't be the same. So, I guess tol is to tight for replicability.

one appveyor fails
it also has a failure in Test_Factor.test_corr_nearest_factor_sparse where converged is False instead of True

@mk1564

mk1564 commented Oct 6, 2018

Copy link
Copy Markdown

Hi Josef,
Is the latest version bug fixed? Not yet right?

@josef-pkt

Copy link
Copy Markdown
Member Author

No, not yet.
I need to increase the test tolerance, but I'm currently on a different branch with many half finished changes, WIP for another PR.

@mk1564

mk1564 commented Oct 6, 2018

Copy link
Copy Markdown

Got it! thanks for letting me know.

@josef-pkt

Copy link
Copy Markdown
Member Author

temporarily prio-high so I don't forget about it.
I didn't remember this after I switched to working on GAM.

rebase, check, merge for 0.10

@bashtage

bashtage commented May 8, 2019

Copy link
Copy Markdown
Member

closed by #5641

@bashtage bashtage closed this May 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sirf_errband_mc / error message #svar

4 participants