Skip to content

Added kpss test - #2749

Closed
N-Wouda wants to merge 44 commits into
statsmodels:masterfrom
N-Wouda:N-Wouda-kpss
Closed

N-Wouda wants to merge 44 commits into
statsmodels:masterfrom
N-Wouda:N-Wouda-kpss

Conversation

@N-Wouda

@N-Wouda N-Wouda commented Jan 5, 2016

Copy link
Copy Markdown
Contributor

Added an implementation of the Kwiatkowski–Phillips–Schmidt–Shin (KPSS) test to tsa.stattools.

Added a KPSS test for level/trend stationarity. Largely based on the R implementation in tseries.
@N-Wouda N-Wouda changed the title N wouda kpss Added kpss test Jan 5, 2016
@ChadFulton

Copy link
Copy Markdown
Member

I think KPSS would be a nice addition to Statsmodels.

Unless I'm missing something, though, I think this couldn't be included because the tseries library is GPL licensed (see https://cran.r-project.org/web/packages/tseries/index.html) which is incompatible with the Statsmodels license.

@N-Wouda

N-Wouda commented Jan 15, 2016

Copy link
Copy Markdown
Contributor Author

@ChadFulton I could do the dirty work of going back to the original paper by Kwiatkowski et al. and refactor everything. There's not much to a KPSS test: the final result of such a refactoring will not deviate substantially from this format (although it will be rewritten completely independently of the R code, so it can be under any license we like).

If you want me to, I'm not opposed to just doing so. It will not be that hard, at most a couple of hours worth of work :).

@ChadFulton

Copy link
Copy Markdown
Member

Unfortunately, I think that rewriting would be necessary - I appreciate the work you've already put into this.

@josef-pkt is rewriting sufficient to satisfy licensing problems?

@N-Wouda

N-Wouda commented Jan 15, 2016

Copy link
Copy Markdown
Contributor Author

@ChadFulton already pulled the paper via my university. I'll get to it this weekend, depending on @josef-pkt's reply!

@N-Wouda

N-Wouda commented Jan 16, 2016

Copy link
Copy Markdown
Contributor Author

@ChadFulton @josef-pkt I have completely reworked the test, so this should be fine. I am still somewhat unhappy with two things: (1) the flag parameter, indicating whether the p-value is within the range of critical values (as the kpss statistic is interpolated to obtain a p-value), and (2) the lag selection.

About the flag parameter: should I raise a warning if the statistic is more extreme than the critical values? Or just leave it at this?

About the lag selection: I could probably hijack _autolag() for this purpose. I'd need to look into it, but that may cut it. The lag selection as outlined in Schwert (1989) has poor small-sample power, but as it stands, users can still provide their own lag parameter. Not too sure about this.

@ChadFulton

Copy link
Copy Markdown
Member

Thanks, I'll take a closer look at the code soon. In the meantime, it would be good to add some unit tests against known results. To get the known results, it is acceptable to use the output of the R routines (even though we couldn't port their code).

Been a while since I lost wrote a few of these. May not work; will rewrite if not.
Made an incredibly stupid typo when pushing to brach. Fixed now.
@N-Wouda

N-Wouda commented Jan 16, 2016

Copy link
Copy Markdown
Contributor Author

@ChadFulton done! Results should be coming in after the Travis build. Tests run fine on my machine, but the first few builds are going to fail: I accidentally pushed a typo into my local branch..

Latest commit should address these issues and build/test just fine.

Comment thread statsmodels/tsa/stattools.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as if nobs != x.size:?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely correct. No idea why I took this approach. Will push a revision!

Comment thread statsmodels/tsa/stattools.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a note in the current version that pvalues are truncated, i.e. report boundary points if pvalue < 0.01 or > 0.1, and interpolates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@N-Wouda

N-Wouda commented Jan 18, 2016

Copy link
Copy Markdown
Contributor Author

@josef-pkt That just leaves us with the argument-consistency. Would you rather have me call it trend? I am not sure if that makes it any clearer in this particular case, as the test is quite specific about trend or level stationarity. Naming the argument itself trend may be confusing to some (trend = "level" may be a bit.. awkward).

Other than that, thank you for checking my work! I already updated it, and the Travis results should be coming in in a short while.

@josef-pkt

Copy link
Copy Markdown
Member

about null_hypo: I don't like it much because we never used something like this, and we are removing the trend (or trend of order 0), the hypothesis is stationarity.

I will be offline for some time today, the main thing is to work towards consistent argument names, instead of introducing another name. I remembered "trend" from ARMA and similar models.

@N-Wouda

N-Wouda commented Jan 18, 2016

Copy link
Copy Markdown
Contributor Author

@josef-pkt would you be ok with using the regression-argument from the adfuller test? With keywords c and ct, as that is pretty much what kpss is using. This would be consistent within tsa.stattools, and not too different for users of the ADF test (whom, I assume, are most likely to also benefit from the KPSS test).

@josef-pkt

Copy link
Copy Markdown
Member

consistency with adfuller looks good
(adfuller also has varying returns depending on the options, I think we will eventually get mostly rid of that, with deprecation periods)

If autolag will be used, then lags should also be made consistent with adfuller. I haven't looked at any recommendations for KPSS.

Can you squash the commits?
I think in this case there are too many commits given that it is all on the new function (and not refactoring that where we would want to keep track of the changes).

Added a KPSS test for level/trend stationarity. Largely based on the R implementation in tseries.

Added kpss test to docs

Small update to __all__

Update stattools.py

Update stattools.py

Added authors

Update stattools.py

Update stattools.py

Update stattools.py

Update stattools.py

Update stattools.py

Turned the critical values to a dict, like in `adfuller()`

Minor update to kpss()

Renamed parameter `l` to `lags`

Just `l` was an unclear variable name.

Reworked the entire test as discussed

See comment in #2749.

Update stattools.py
Been a while since I lost wrote a few of these. May not work; will rewrite if not.

Update test_stattools.py

Updated unit test

Made an incredibly stupid typo when pushing to brach. Fixed now.

Update test_stattools.py

Update test_stattools.py

Added R-code to docstring

x.size rather than involved approach

Update tsa.rst

Raise warning for out of bounds interpolation of test statistic

Removed `flag` from tests

Test for warning

Update test_stattools.py

Update test_stattools.py

Rewrote some of the docstring

Updated based on Josef's comments

Updated to take into account resstore

Update test_stattools.py

Changed null_hypo argument to regression

Updated regression argument in tests

Statsmodels style **

Found another `**` padded by spaces.

Update test_stattools.py

Update test_stattools.py

Update stattools.py
…' into N-Wouda-kpss"

This reverts commit df80f5c, reversing
changes made to 7249147.
@N-Wouda

N-Wouda commented Jan 19, 2016

Copy link
Copy Markdown
Contributor Author

@josef-pkt I absolutely suck at git; it is really not my cup of tea (and I do think I screwed up a bit just yet). I could just open a new PR with the final result of this one, reference this and keep it simple? Sorry for making such a mess of things in this PR, I do feel bad about that!

@josef-pkt

Copy link
Copy Markdown
Member

I could just open a new PR with the final result of this one, reference this and keep it simple?

Yes, you could do that, at least it's standard.
The git way for cleaning this up would be an interactive rebase (git rebase -i) which pops up a text editor and commits can be marked for squashing or ignoring. But it's always good to try on a copy in a temporary branch, and then check the new history.

Sorry for making such a mess of things in this PR, I do feel bad about that!

That's no reason to feel bad. We all have been there, and it takes quite a bit of time to go beyond simple commits in git.
Initially, I only agreed to move to git because we had a rescue squad, developers from related packages, that helped out a lot when git usage was unclear or we screwed up.

(I'm using git gui for most of my regular work, I know a few git commands on the command line, for the rest I have to reread the manual each time.)

@N-Wouda

N-Wouda commented Jan 19, 2016

Copy link
Copy Markdown
Contributor Author

@josef-pkt I will just open a new PR then.

The git way for cleaning this up would be an interactive rebase (git rebase -i)

I actually tried this, but I probably did something wrong. Not sure what though, I will read up on the manual a bit more.

@N-Wouda N-Wouda mentioned this pull request Jan 19, 2016
@N-Wouda N-Wouda closed this Jan 19, 2016
@N-Wouda
N-Wouda deleted the N-Wouda-kpss branch January 20, 2016 20:17
@josef-pkt josef-pkt mentioned this pull request Feb 17, 2016
@bashtage bashtage added the rejected Used for PRs with changes that are not acceptable label Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rejected Used for PRs with changes that are not acceptable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants