You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: Zivot-Andrews structural-break unit-root test #4746
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.
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.
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.
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])
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.
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.
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.
@bashtage that speed up is outstanding! I will give it a try on my machine to get apples/apples timings. My runs are on a low-spec Lenovo t440 laptop with 8gb ram and i5-4300 cpu (1.9gh with two hyper threaded cores or 4 threads total) although I can get workstation timings as well with 40 threads. Either way that certainly embarrasses the R version.
@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
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.
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?
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.
@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.
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.
Activity
josef-pkt commentedon Jun 16, 2018
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.
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?
ChadFulton commentedon Jun 21, 2018
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:
urca? (I gave it a quick try to see if things matched up with the output ofur.zaand couldn't immediately make it work, but that's just my lack of understanding, I suspect).x[:,0])JimVaranelli commentedon Jun 27, 2018
@josef-pkt @ChadFulton -
Sorry for the delay I just got back from a much-needed vacation.
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.
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.
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.
JimVaranelli commentedon Jul 2, 2018
@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.
################################################
Zivot-Andrews Unit Root / Cointegration Test
################################################
The value of the test statistic is: -5.5762
bashtage commentedon Aug 21, 2018
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.
bashtage commentedon Aug 21, 2018
@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/
JimVaranelli commentedon Aug 21, 2018
JimVaranelli commentedon Jan 6, 2019
@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
josef-pkt commentedon Jan 6, 2019
Kevin added a WLS estimator in regression._tools which uses a simplified API and computations and is only for internal use.
However, the
_quick_olsin 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.
JimVaranelli commentedon Jan 6, 2019
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?
JimVaranelli commentedon Jan 7, 2019
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.
JimVaranelli commentedon Jan 8, 2019
@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.
JimVaranelli commentedon Jan 8, 2019
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.
JimVaranelli commentedon Jan 9, 2019
Just issued PR 5455 for this.
bashtage commentedon Aug 2, 2019
Closes by #6014. Thanks @JimVaranelli .
JimVaranelli commentedon Aug 2, 2019
@bashtage You are very welcome!