Skip to content

ENH: Add new version of ARIMA-type estimators (AR, ARIMA, SARIMAX) - #5827

Merged
ChadFulton merged 1 commit into
statsmodels:masterfrom
ChadFulton:tsa2
Sep 11, 2019
Merged

ChadFulton merged 1 commit into
statsmodels:masterfrom
ChadFulton:tsa2

Conversation

@ChadFulton

@ChadFulton ChadFulton commented Jun 5, 2019 •

Copy link
Copy Markdown
Member

Update: TODOs:

  • decide where to put the new ARIMA model class / current ARIMA deprecation strategy.
  • decide where to put the Brockwell and Davis datasets.

This is very much a WIP, and basically none of the design / organizational things have been thought through (e.g. where to put it, what to call things, etc.). But is concrete enough already that hopefully there may be some useful discussion at this point about the general goals of such a project.

A couple of preliminaries:

  • It is not done, and at this stage I'm most interested in soliciting very general design feedback.
  • It is totally okay if this is not what we want / how we want it. Still, I think it is a good idea.

My starting point for this project was:

  1. We should have a wider range of estimator options for ARIMA-type models (e.g. Hannan-Rissanen, etc.), and these should be, as much as possible, stand-alone functions and be standardized.

    • In fact, we already have a lot of estimators in Statsmodels, particularly since @bashtage has been adding more, but they are scattered around and not as easy to use as they could be.
  2. We should have a single model class that is the point of entry for all ARIMA-type models.

    • I think that this should ultimately be based on the state space setup, since it handles the most general case of all estimators.
    • Given a set of estimated parameters, I think that all postestimation stuff (forecasting, hypothesis tests, etc.) should be done through the state space framework.
  3. It is not worth it to salvage the old AR / ARIMA models.

So this PR adds tsa2.sarimax.estimators, which has the following estimators for ARIMA-type models:

  • yule_walker for AR(p) models (wrapper around linear_model.yule_walker)
  • burg for AR(p) models (wrapper around linear_model.burg)
  • durbin_levinson for fast computation of Yule-Walker across consecutive AR(p) orders (not sure why I didn't wrap tsatools.levinson_durbin - maybe it'll come to me if I look back at it later).
  • innovations for MA(q) models (wrapper around stattools.innovations_algo)
  • hannan_rissanen for ARMA models with a sum-of-squares approach (implementation based on Brockwell and Davis)
  • innovations_mle for SARIMA models with numerical likelihood maximization (implementation based on Brockwell and Davis, ultimately wraps the arma_innovations code)
  • statespace for SARIMAX model (wrapper around statespace.SARIMAX)
  • gls for regression with ARIMA-type errors, where the ARIMA parameters for the error process can be estimated using any of the above (implementation based on Brockwell and Davis, uses innovation algorithm for fast transformation of endog / exog to induce homoskedasticity).

Input is standardized using the SARIMAXSpecification class, which handles a lot of things that ARIMA-type models need to worry about (handling different ways of specifying model orders, like non-consecutive lags, handling trends, getting parameter names, describing which of the above estimators are valid for a given set of model orders, etc.).

Output is standardized using the SARIMAParams class, which handles a lot of things that ARIMA-type parameters need to worry about (getting and settings parameters based on the usual econometric form vs lag polynomial form, checking for stationarity and invertibilty, etc.).

Finally, there is a new model class ARIMA, which is meant to (eventually) be a single point of entry for all ARIMA-type models (including SARIMAX models). It is a subclass of statespace.SARIMAX but allows fitting using any of the estimators above. It returns results that (will be) a subclass of statespace.SARIMAXResults, so that all postestimation can be done through the state space framework.

Anyway, I hope that this can be a step forward on tsa.

@pep8speaks

pep8speaks commented Jun 5, 2019 •

Copy link
Copy Markdown

Hello @ChadFulton! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-09-11 22:40:15 UTC

Comment thread statsmodels/tsa2/sarimax/params.py Resolved
Comment thread statsmodels/tsa2/sarimax/model.py Resolved
@ChadFulton

Copy link
Copy Markdown
Member Author

A couple of additional implementation details that might be useful:

  • Implementing this was based pretty much on Brockwell and Davis, and the basic line of testing is against their book or their software (ITSM or the R copycat package ITSMR).

  • The convention here is that all intercept / regression effects (including trend effects) are estimated using the regression with SARIMA errors approach and so must either go through the gls estimator or the statespace estimator.

    • We do this in Statsmodels already, except that outside of state space models we just use the OLS estimator rather than the GLS estimator.
    • In the state space SARIMAX, the trend argument is estimated in intercept form. I now think for consistency, it is better for the basic ARIMA model to have all regression-type effects enter in the same way.
    • All of the estimators do include a demean argument that defaults to True (except gls and statespace which have include_constant). This is (a) since theoretically the SARIMA estimators should be applied to mean-zero processes, and (b) for consistency with other Statsmodels functions and other packages.
  • In the estimator functions, integrated models are handled in one of three ways:

    1. The simpler estimators - yule_walker, durbin_levinson, burg, innovations, and hannan_rissanen - just don't allow specification of an integrated model (the have arguments like ar_order, so there just isn't a place to put an order of integration).
    2. The more complete estimators - innovations_mle and gls (since gls can use innovations_mle as part of the GLS estimation) - allows specification of an integrated model, but always directly difference the data before estimating the parameters.
    3. The state space estimator continues to incorporate integration directly into the state space form.

@jbrockmendel

Copy link
Copy Markdown
Contributor

Thanks for expanding on that. Should I think of this PR as a Proof Of Concept, or something you would like to merge in this form? After the release, are you hoping to get this done soonish (i.e. does this block other goals?) or can the review be more leisurely?

@ChadFulton

Copy link
Copy Markdown
Member Author

Should I think of this PR as a Proof Of Concept, or something you would like to merge in this form? After the release, are you hoping to get this done soonish (i.e. does this block other goals?) or can the review be more leisurely?

Basically, I think that the setup here is good, and potentially mergeable after I clean up everything and add more tests. Maybe it should be merged soon after the next release so that other people besides me can start using it.

My main goal, actually, is that we can start some real documentation on how to use tsa, and I think that to do that we need to make the model interfaces more consistent. The biggest problem there is the signatures of the AR and ARIMA model methods, but also the state space models have some weird things too (e.g. the way trend is estimated), and so the goal of this PR is to figure out a consistent interface for all ARIMA-type models that we can confidently recommend to users as "the way".

But I think that as we write "good" documentation for this entry point to ARIMA-type models, we'll probably figure out that I didn't do some things quite right in this PR (like I set up some things in a way that turns out to be counter-intuitive, or hard to explain), and we'll need to change the implementation. I think that's okay for a new set of models that haven't been in a release yet, but it's something to keep in mind. And of course the more of those things that we can figure out in this PR already, the better.

@codecov

codecov Bot commented Aug 6, 2019

Copy link
Copy Markdown

Codecov Report

Merging #5827 into master will increase coverage by 0.29%.
The diff coverage is 96.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5827      +/-   ##
==========================================
+ Coverage    84.1%   84.39%   +0.29%     
==========================================
  Files         611      638      +27     
  Lines       97065    99279    +2214     
  Branches    10549    10815     +266     
==========================================
+ Hits        81636    83786    +2150     
- Misses      13052    13104      +52     
- Partials     2377     2389      +12
Impacted Files Coverage Δ
...2/sarimax/estimators/tests/test_durbin_levinson.py 100% <100%> (ø)
...smodels/tsa2/sarimax/estimators/tests/test_burg.py 100% <100%> (ø)
statsmodels/tsa2/sarimax/tests/test_tools.py 100% <100%> (ø)
statsmodels/tsa2/sarimax/estimators/yule_walker.py 100% <100%> (ø)
statsmodels/tsa2/sarimax/params.py 100% <100%> (ø)
.../tsa2/sarimax/estimators/tests/test_yule_walker.py 100% <100%> (ø)
...smodels/tsa2/sarimax/estimators/hannan_rissanen.py 100% <100%> (ø)
...tsa2/datasets/brockwell_davis_2002/data/oshorts.py 100% <100%> (ø)
.../tsa2/sarimax/estimators/tests/test_innovations.py 100% <100%> (ø)
statsmodels/tsa2/sarimax/estimators/innovations.py 100% <100%> (ø)
... and 55 more

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 ed222e8...598a9cb. Read the comment docs.

@codecov

codecov Bot commented Aug 6, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5827 into master will increase coverage by 0.25%.
The diff coverage is 97.07%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5827      +/-   ##
==========================================
+ Coverage    84.1%   84.36%   +0.25%     
==========================================
  Files         613      640      +27     
  Lines       97768    99981    +2213     
  Branches    10661    10928     +267     
==========================================
+ Hits        82232    84346    +2114     
- Misses      13121    13201      +80     
- Partials     2415     2434      +19
Impacted Files Coverage Δ
statsmodels/tsa/statespace/mlemodel.py 87.9% <ø> (ø) ⬆️
statsmodels/tsa/api.py 100% <100%> (ø) ⬆️
...tatsmodels/tsa/arima/estimators/tests/test_burg.py 100% <100%> (ø)
statsmodels/tsa/arima/estimators/yule_walker.py 100% <100%> (ø)
...rima/datasets/brockwell_davis_2002/data/oshorts.py 100% <100%> (ø)
...tsa/arima/estimators/tests/test_durbin_levinson.py 100% <100%> (ø)
statsmodels/tsa/arima/params.py 100% <100%> (ø)
...sa/arima/datasets/brockwell_davis_2002/__init__.py 100% <100%> (ø)
...dels/tsa/arima/estimators/tests/test_statespace.py 100% <100%> (ø)
...tatsmodels/tsa/arima/estimators/hannan_rissanen.py 100% <100%> (ø)
... and 54 more

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 fad69bc...c0ddd87. Read the comment docs.

@coveralls

coveralls commented Aug 6, 2019 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 86.791% when pulling c0ddd87 on ChadFulton:tsa2 into fad69bc on statsmodels:master.

@ChadFulton
ChadFulton force-pushed the tsa2 branch 4 times, most recently from 1856c1a to 2e9072f Compare August 8, 2019 04:00
@ChadFulton ChadFulton changed the title WIP/DESIGN/ENH: Add new version of ARIMA-type estimators (AR, ARIMA, SARIMAX) ENH: Add new version of ARIMA-type estimators (AR, ARIMA, SARIMAX) Aug 8, 2019
@ChadFulton
ChadFulton force-pushed the tsa2 branch 2 times, most recently from fae01ae to 240923b Compare September 10, 2019 21:38
@ChadFulton

Copy link
Copy Markdown
Member Author

Unless there are any concerns, I will merge this once I get Windows to stop complaining about precision issues in tests (so hopefully tonight).

@jbrockmendel

Copy link
Copy Markdown
Contributor

I'll take another look this evening, if you're OK with "tonight" being Pacific Time

@ChadFulton

Copy link
Copy Markdown
Member Author

I'll take another look this evening, if you're OK with "tonight" being Pacific Time

That would be much appreciated, thanks! (I know it's a huge PR so whatever you have time for would be a great check).

Comment thread statsmodels/tsa/arima/estimators/gls.py Resolved
Comment thread statsmodels/tsa/arima/estimators/gls.py Resolved
@jbrockmendel

Copy link
Copy Markdown
Contributor

Ran out of gas before getting through all of it. A few comments about the pytest.mark.todo marker possibly being redundant, otherwise looks really good, particularly the documentation on where the test results are coming from.

Big picture I'd be more comfortable putting these in place if there was an agreed-upon gameplan for getting rid of the old versions (and potential overlap with #6087). But you've been pretty patient about this so I can't in good conscience ask you to wait any more on my account.

@ChadFulton

Copy link
Copy Markdown
Member Author

Ran out of gas before getting through all of it. A few comments about the pytest.mark.todo marker possibly being redundant, otherwise looks really good, particularly the documentation on where the test results are coming from.

Thanks

Big picture I'd be more comfortable putting these in place if there was an agreed-upon gameplan for getting rid of the old versions (and potential overlap with #6087).

But I think there is a game plan for getting rid of older versions - once we are comfortable that this code works, we will follow this deprecation strategy:

In any case, presumably the deprecation will look like:

  • v0.x - add new class for optional use and FutureWarning noting that the API endpoint sm.tsa.ARIMA will switch from sm.tsa.arima_model.ARIMA to sm.tsa.arima.ARIMA in v0.x+1.
  • v0.x+1 - actually make the switch, change the FutureWarning to note that old classes (sm.tsa.arima_model.ARIMA) will be removed in v0.x+2
  • v0.x+2 - remove warnings and old classes.

The only thing that's unclear to me is whether we can set x=11, since if we want to release in October that may not be enough time to make sure the new classes are robust. However, that is what I hope for.

But you've been pretty patient about this so I can't in good conscience ask you to wait any more on my account.

:) this is going in tonight.

@ChadFulton

Copy link
Copy Markdown
Member Author

Merging when green.

@ChadFulton

Copy link
Copy Markdown
Member Author

Thanks @jbrockmendel and @bashtage!

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.

5 participants