Skip to content

ENH: State space: Exact diffuse initialization - #4418

Merged
ChadFulton merged 27 commits into
statsmodels:masterfrom
ChadFulton:ss-diffuse-ctnd
May 31, 2018
Merged

ChadFulton merged 27 commits into
statsmodels:masterfrom
ChadFulton:ss-diffuse-ctnd

Conversation

@ChadFulton

Copy link
Copy Markdown
Member

This adds exact diffuse initialization for filtering and smoothing for state space models.

This PR follows on top of #4042 (I don't know if it will be possible to merge #4042 first, but hopefully we can).

I haven't actually set SARIMAX or UnobservedComponents to use the exact diffuse method yet, but that would be pretty trivial given #4042, although I guess we might want to have the approximate diffuse method deprecated for a release cycle. The main work will be going through the tests, since some of them will likely fail with the exact method.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 3 alerts and fixes 2 when merging 2112708 into 5a01211 - view on lgtm.com

new alerts:

  • 3 for Potentially uninitialized local variable

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@coveralls

coveralls commented Apr 3, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 83.282% when pulling ca0b973 on ChadFulton:ss-diffuse-ctnd into 6611e87 on statsmodels:master.

@ChadFulton

Copy link
Copy Markdown
Member Author

Just for general information, my current plan for this (and my other outstanding) PRs:

I think that this will put us in a good place for moving ahead on a number of fronts, including especially figuring out what to do with #3884. I think that with the new features from the above PRs, we will be able to move forward on a middle ground between full deprecation of ARIMA and the current situation where those models aren't maintained.

Of course we'll see how things go.

@ChadFulton ChadFulton added this to the 0.10 milestone Apr 21, 2018
@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased after merging the updated frequency-domain seasonals in structural.py

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 2 alerts when merging 7887bf7 into 7cbaa2e - view on lgtm.com

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@jyoder6

jyoder6 commented Apr 27, 2018

Copy link
Copy Markdown
Contributor

@ChadFulton do you want/need any help with your plan? I am interested in this feature and can pitch in if useful. Maybe starting in on

I haven't actually set SARIMAX or UnobservedComponents to use the exact diffuse method yet, but that would be pretty trivial given #4042, although I guess we might want to have the approximate diffuse method deprecated for a release cycle. The main work will be going through the tests, since some of them will likely fail with the exact method.

?

@ChadFulton

Copy link
Copy Markdown
Member Author

do you want/need any help with your plan? I am interested in this feature and can pitch in if useful. Maybe starting in on

I haven't actually set SARIMAX or UnobservedComponents to use the exact diffuse method yet, but that would be pretty trivial given #4042, although I guess we might want to have the approximate diffuse method deprecated for a release cycle. The main work will be going through the tests, since some of them will likely fail with the exact method.

Thanks! That would be great. The main issue is going to be that it's a little tricky figuring out what we tested against, what the other software supports, and how they set things up.

One issue is that there are not many packages that have full support for this functionality, so it can be hard to find packages to test against. I think most of our tests are against Stata and KFAS, which both fortunately support exact diffuse initialization, but unfortunately both have caveats (see below) so we need to be a little bit careful.

Here's my understanding of the status of other packages:

  • The KFAS package in R supports exact diffuse initialization, and is the best package to test against in general I think. Unfortunately, it also has a number of bugs (e.g. in the loglikelihood computation for diffuse periods, in the predicted / filtered state for diffuse periods, ...)
  • RATS supports it and would be great to test against (I think its creator put a lot of time into this), but this software is not used very much anymore, I find its syntax very unintuitive, and I don't know if it actually provides all of the output we need, so I have never put the work in to try and create tests.
  • Stata uses the DeJong approach for diffuse filtering. Generally this matches up well enough for the loglikelihood part of the tests, but not for the predicted / filtered values.
  • Matlab supports it in its econometrics package, but IIRC does not actually provide output for the diffuse periods
  • Koopman's SSFPack only supports it in the paid version.
  • There was a Matlab SSM toolbox that supported it, but it is no longer actively maintained and apparently can't be compiled with newer versions of Matlab (newer than something like 2011 versions, I think)
  • Eviews apparently does not support it (they use the approximate method).
  • Gretl does not support it (they use the approximate method)
  • I don't know of other R packages that support it.
  • I have no idea about Gauss.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 2 alerts when merging ef29cf9 into 7cbaa2e - view on lgtm.com

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@ChadFulton

Copy link
Copy Markdown
Member Author

My current plan is to merge this (i.e. not #4042 first since there's really no reason to do that) as soon as possible, since it is green on all scipy >= 0.18.

However, I guess before we do that we should drop the CI lines for scipy < 0.18 so that master isn't permanently broken.

@ChadFulton

Copy link
Copy Markdown
Member Author

Rebased. Assuming this comes back green, I will merge it (unless there are concerns). Although this PR adds the functionality, its scope is still pretty limited since no model actually uses the exact diffuse method yet.

A couple of notes:

  • In 9d6995e I have an expected failure test where I show numerical problems that can arise if exact diffuse initialization is to an "irrelevant" state in a model (see REF: Irrelevant state in UnobservedComponents freq. seasonal when s even #4619). These numerical problems also arise in e.g. the R package KFAS. This comment is just a note that these exist and a potential future test case, but this is not really a problem since we just shouldn't be specifying a state space model with an irrelevant diffuse state.

  • After this is merged, my next PR will be to strip out all of the conditional compilation stuff and remove the old state space code. I'm looking forward to this :)

  • I think the next step after that is to start using exact diffuse initialization in the RecursiveLS model, and I anticipate that in constructing that PR I will find additional improvements / refactors / fixes to the exact diffuse initialization code introduced in this PR.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 2 alerts when merging 37267cc into d465ebc - view on lgtm.com

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member

sounds good to me

I would like to plan for a release in (close to) half a year. Changes that will be merged soon, then still get some time in master to figure out and fix some problems that show up after the merges.

I will open an issue about conditional compilation for a possible 0.9.1

Question for 0.10 after removing compilation conditional on scipy:
Do we need scipy for compilation, e.g. some header files or is scipy currently needed only for the version check?

@josef-pkt

Copy link
Copy Markdown
Member

travis failure is unrelated doc build issue
CondaHTTPError: HTTP 502 BAD GATEWAY for url <https://conda.anaconda.org/r/linux-64/r-r6-2.2.2-mro343_0.tar.bz2>

appveyor errors look serious, each exits in a different test

statsmodels\tsa\statespace\tests\test_structural.py ...........Command exited with code -1073741819

statsmodels\tsa\statespace\tests\test_sarimax.py ....................... [ 72%]
........................................................................ [ 73%]
.................................................Command exited with code -1073741819
statsmodels\tsa\statespace\tests\test_dynamic_factor.py ................ [ 64%]
...................................s..Command exited with code -1073741819

@ChadFulton

ChadFulton commented May 18, 2018 via email

Copy link
Copy Markdown
Member Author

@josef-pkt

Copy link
Copy Markdown
Member

I restarted the doc build which is now green, but it looks like pandas_datareader.fred is incompatible with pandas which causes errors in the notebook builds

~/miniconda/envs/statsmodels-test/lib/python3.6/site-packages/pandas_datareader/data.py in <module>()
     12     ImmediateDeprecationError
     13 from pandas_datareader.famafrench import FamaFrenchReader
---> 14 from pandas_datareader.fred import FredReader
     15 from pandas_datareader.google.daily import GoogleDailyReader
     16 from pandas_datareader.google.options import Options as GoogleOptions
~/miniconda/envs/statsmodels-test/lib/python3.6/site-packages/pandas_datareader/fred.py in <module>()
----> 1 from pandas.core.common import is_list_like
...
ImportError: cannot import name 'is_list_like'

@ChadFulton

Copy link
Copy Markdown
Member Author

I restarted the doc build which is now green, but it looks like pandas_datareader.fred is incompatible with pandas which causes errors in the notebook builds

This is a bummer. I see that the prescient @bashtage already fixed this in pandas-datareader master (pydata/pandas-datareader/pull/520).

What is the best option for handling this? @bashtage are you planning to release a 0.6.1 for this error, or just release a 0.7.0? And is there a timeline?

@josef-pkt

Copy link
Copy Markdown
Member

We don't need to use the latest pandas for the doc build, we can just stick with a previous version of pandas that works

@ChadFulton

Copy link
Copy Markdown
Member Author

We don't need to use the latest pandas for the doc build, we can just stick with a previous version of pandas that works

Oh, good point. I'll make a PR now to change the docbuild pandas to 0.22

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 2 alerts when merging 00e6891 into d465ebc - view on lgtm.com

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@ChadFulton

Copy link
Copy Markdown
Member Author

Uh oh

But the good news is I can replicate it on Windows, so will be able to track it down and fix it.

@ChadFulton

Copy link
Copy Markdown
Member Author

Question for 0.10 after removing compilation conditional on scipy:
Do we need scipy for compilation, e.g. some header files or is scipy currently needed only for the version check?

Yes, I think so, since we have in the Cython files e.g.

cimport scipy.linalg.cython_blas as blas
cimport scipy.linalg.cython_lapack as lapack

@josef-pkt

Copy link
Copy Markdown
Member

If scipy is a build requirement, then it still needs to be added to the setup.py. If the analogy to numpy is correct, then we need to add
setup_requires.append('scipy')

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 2 alerts when merging 7a42e22 into 6611e87 - view on lgtm.com

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@ChadFulton

Copy link
Copy Markdown
Member Author

Pretty sure that fixed the Appveyor problems. I've also added scipy to setup_requires.

If green, will merge...

@josef-pkt

Copy link
Copy Markdown
Member

This pull request fixes 2 alerts when merging ca0b973 into 6611e87 - view on lgtm.com

fixed alerts:

  • 2 for Variable defined multiple times

Comment posted by lgtm.com

@ChadFulton

Copy link
Copy Markdown
Member Author

Is green, am merging.

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.

4 participants