Skip to content

ENH: Zivot-Andrews structural-break unit-root test #4746

Closed

Description

@JimVaranelli

We have a nice c++ implementation which I quickly ported to python for experimentation. Repo is JimVaranelli/ZAUnitRoot. Not all params/features are implemented yet but it should give a good idea of execution. I tried to optimize the design matrix assembly process but please suggest improvements. The c++ implementation follows the ZA paper as written (T-trim autolag regressions). It's an expensive test, so for the python I used Baum's (2004/2015) approximation which runs a single autolag regression on the base model and uses that lag value for all of the T-trim break-period regressions. The Baum method tends to be more pessimistic than the original but I haven't quantified the size/power tradeoff. I haven't added it to the repo code yet, but I also have MC-derived CVs (nobs=2000, repl=1MM) for each of the three models in order to generate pvals. Take a look and let me know if there is any interest in adding it to SM in which case I will finalize the implementation in the repo and submit a PR. I also included 4 test cases, three of which were used in the original ZA paper.

Activity

  1. josef-pkt commented on Jun 16, 2018

    @josef-pkt
    Member

    In general I find it useful to have more unit root tests under different assumptions on the underlying process. (I put some on my reading list.)
    I don't know the Zivot-Andrews test but purely based on citation it looks popular.

    Based on skimming https://github.com/JimVaranelli/ZAUnitRoot/blob/master/ZAUnitRoot/ZAUnitRoot.py
    I don't see any obvious changes. Maybe there is room for some performance improvements, but I don't see anything obvious.
    I think in general we should switch to returning some testresults instance by default, but that still hasn't been settled yet.

    We have a nice c++ implementation which I quickly ported to python for experimentation.

    Do you have permission to port the C++ code or does it have a compatible license?
    But given that the code is using straightforward numpy and statsmodels, that might not be an issue.

    I will not be able to look at this seriously, and read the articles, until I get my head out of my current outlier-influence in GLM PR which will take a few more days.

    @bashtage @ChadFulton Any comments?

  2. ChadFulton commented on Jun 21, 2018

    @ChadFulton
    Member

    The ZA test would be a great one to get in Statsmodels, thanks for the suggestion and code! I also haven't had a chance to get totally into the details, but the code looks like it's pretty good. Two small comments:

    • Is there a package we could unit test against? e.g. the R package urca? (I gave it a quick try to see if things matched up with the output of ur.za and couldn't immediately make it work, but that's just my lack of understanding, I suspect).
    • The docstring says that it expects a 1d series, but I think right now it requires a 2d series (for e.g. x[:,0])
  3. JimVaranelli commented on Jun 27, 2018

    @JimVaranelli
    ContributorAuthor

    @josef-pkt @ChadFulton -

    Sorry for the delay I just got back from a much-needed vacation.

    Do you have permission to port the C++ code or does it have a compatible license?

    Unfortunately our C++ library is given proprietary treatment so I can't share it directly but I can port certain things. An underlying C/C++ implementation would be great for this particular test. As an alternative, I could always multiprocess the python version if there are no backwards compatibility issues. I think python 2.6 or later is required for the multiprocessing package.

    Is there a package we could unit test against? e.g. the R package urca?

    SAS does not have the test (the primary motivation for implementing it...we're a SAS/C++ shop but slowly moving away from SAS due to the horrible scripting language...that's why I've been testing with SM) so I have not compared it to other packages. I think R, Stata, and Eviews are the only implementations out there, but I have verified the original Nelson-Plosser data sets against the results in the ZA paper. I don't have access to Stata or Eviews but I should be able to compare it to the R/ur.za package before submitting a PR.

    The docstring says that it expects a 1d series, but I think right now it requires a 2d series

    That's correct I just cut/paste the params section from adfuller and forgot to erase the 1d. Although if 1d is preferred I can certainly do it that way as well.

    I'll finalize the ZAUnitRoot.py implementation (add support for all required params as well as pvals) and check against ur.za. I'll also add an MP version for you to see if interested.

  4. JimVaranelli commented on Jul 2, 2018

    @JimVaranelli
    ContributorAuthor

    @josef-pkt @ChadFulton

    I updated the ZAUnitRoot repo (https://github.com/JimVaranelli/ZAUnitRoot/tree/master/ZAUnitRoot) with the pvalues. I followed @bashtage method in arch. I also verified the results against R (3.2) package urca.ur.za (1.13-0), e.g. see example summary output below.

    In terms of performance, I added a multiprocessing version to compare. The series needs to be ~10k samples to show improvement. For the included rand10000 test case, serial version runs ~185s with MP version running ~155s. As a point of comparison, it takes the R ur.za package ~1400s for this test case. There is also a ur.za.fast in the R/TED package with an underlying C++ implementation that runs the rand10000 in ~30s but produces a different stat value than the python or ur.za versions. It also doesn't have the same functionality for lag and autolag method.

    Please take a look and let me know if there's anything you'd like me to update before submitting a PR. Thanks.

    ur.za(df, model = "intercept", lag = 8)

    ################################################

    Zivot-Andrews Unit Root / Cointegration Test

    ################################################

    The value of the test statistic is: -5.5762

  5. bashtage commented on Aug 21, 2018

    @bashtage
    Member

    Although if 1d is preferred I can certainly do it that way as well.

    I would think 1d or 2d (nobs,1) should be supported. You can always immediately make it what you want, e.g. np.reshape(np.asarray(x),(-1,1)), although you should check that if 2d it really is nobs by 1.

  6. bashtage commented on Aug 21, 2018

    @bashtage
    Member

    @JimVaranelli I pushed an optimized version that drops the run time from 160s to about 4s on my machine when you have 10,000 observation. I think the module is in good shape and should be included in sm. I'd also be happy to put it with some other unit root tests I maintain in https://github.com/bashtage/arch/

  7. JimVaranelli commented on Aug 21, 2018

    @JimVaranelli
    ContributorAuthor
  8. JimVaranelli commented on Jan 6, 2019

    @JimVaranelli
    ContributorAuthor

    @bashtage @josef-pkt I'm just about ready to PR this test after incorporating Kevin's optimizations which dropped my runtime to 8.5s. Fantastic! Should the _quick_ols function be included in the PR or is there a preferred SM function to use? If including the _quick_ols should I wrap into a class or keep as standalone? Also, I have 5 tests I've verified against R package urca.ur.za (1.13-0) and I know Kevin has some tests as well so let me know which ones you'd like me to include. I can generate the pull request later today or Monday depending upon when we decide on these issues. Thanks again Kevin for the awesome code opt! Jim

  9. josef-pkt commented on Jan 6, 2019

    @josef-pkt
    Member

    Kevin added a WLS estimator in regression._tools which uses a simplified API and computations and is only for internal use.

    However, the _quick_ols in your branch just returns the tvalues, Given that this is very specialized, I think it could just remain as helper function next to the main functions.

    not clear to me: The quick ols might need a method argument. The version in your repo just solves the moment equations which is fast for long exog, but numerically not very stable, i.e. results become unreliable at multicollinearity sooner than a QR or SVD based estimator.

  10. JimVaranelli commented on Jan 6, 2019

    @JimVaranelli
    ContributorAuthor

    A possible solution to the multicollinearity issue would be to use the full OLS method on the first iteration to check the condition/rank? We know the dummy cols are not collinear by construction so a single check should suffice. My only question here: if MC is detected, do you continue the full run with OLS or does it make sense to exit with warning?

  11. JimVaranelli commented on Jan 7, 2019

    @JimVaranelli
    ContributorAuthor

    I added a new version to my repo which wraps everything into a class and does a rank check as described above. It exits with warning if less than full rank.

  12. JimVaranelli commented on Jan 8, 2019

    @JimVaranelli
    ContributorAuthor

    @josef-pkt @bashtage Just about to issue the PR for this. One final issue: the optimized version is producing slightly different stat for one of the test cases from the previous version (which matches R). The difference is not very significant (SM: -3.03974,0.63241; R/prev: -3.02761,0.63993). It is unrelated to OLS vs _quick_ols as they produce the same stat so it appears to be related to exog construction for the trend-only model. Once I figure out the discrepancy I will issue the PR.

  13. JimVaranelli commented on Jan 8, 2019

    @JimVaranelli
    ContributorAuthor

    Fixed the issue which was an indexing error in the exog creation code for the trend-only model. I just pushed the corrected version to my repo. I'll generate the PR later today.

  14. JimVaranelli commented on Jan 9, 2019

    @JimVaranelli
    ContributorAuthor

    Just issued PR 5455 for this.

  15. bashtage commented on Aug 2, 2019

    @bashtage
    Member

    Closes by #6014. Thanks @JimVaranelli .

  16. JimVaranelli commented on Aug 2, 2019

    @JimVaranelli
    ContributorAuthor

    @bashtage You are very welcome!

  17. added this to the 0.11 milestone on Dec 18, 2019
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