Skip to content

ENH/BUG: add update _init_keys (still test failure) - #2259

Merged
josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:sarimax_initkeys
Feb 14, 2015
Merged

josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:sarimax_initkeys

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

see #2252 followup state space, SARIMAX

recreating a model, motivation: #1093

the current design for this is to define

  • _init_keys attribute list of keyword keys in __init__ and
  • _get_init_kwds method to return dictionary with init keys and values. generic version is attached in base, can be used to overwrite specific values (e.g. log exposure in count and glm)

The problem is that here endog is transformed in some cases.

still test failures, I don't define and use specialized _get_init_kwds method yet

@josef-pkt

Copy link
Copy Markdown
Member Author

@ChadFulton Is there an easy, direct way to get the original arguments used in SARIMAX.__init__ out of an existing sarimax model instance ?

22 out of more than 60 test cases fail
problem with transformed endog ?
I haven't looked at what's in **kwargs yet, which will also need special handling

@josef-pkt

Copy link
Copy Markdown
Member Author

This can go into 0.8, if there is no quick solution

Comment thread statsmodels/tsa/statespace/sarimax.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.

I think there should be a comma here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, that's the second comma that is missing. It took me half an hour to find the first. thanks

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, that's the first, I forgot to commit this change, still the 22 failures (using data.orig_endog)
will push now the missing commit.

@josef-pkt

Copy link
Copy Markdown
Member Author

ok, down to 6 failures

The original orig_endog is attached to the model. the usual `data.orig_endog is already a transformed endog attached by super, i.e. base.Model.

@josef-pkt

Copy link
Copy Markdown
Member Author

(bit of a mess pushing the right thing to the right place)

the remaining 6 failures are all diffuse
the unit tests for diffuse all have an additional method call
self.model.initialize_approximate_diffuse(1e9)

@ChadFulton is this initialize_approximate_diffuse call required or can it be triggered through the keyword arguments in SARIMAX.__init__?

@josef-pkt

Copy link
Copy Markdown
Member Author

@ChadFulton related question: which class and which class.__init__ is setting the initialization method? (diffuse or not)
I don't find it with skimming through the class hierarchy of SARIMAX.

@ChadFulton

Copy link
Copy Markdown
Member

You bring up two good issues:

  1. Data: Any time that simple_differencing is going on, mod.endog (and mod.exog too) will, as it currently stands, not be the same as the initial endog (exog) passed as a parameter. The reason that this approach was taken (altering the datasets themselves) is that the base classes (e.g. Representation) assume that the given self.endog refers to the observed data series in the state space representation of the model.

    One option if this doesn't feel right is to decouple the state space mechanism from the SARIMAX mechanism, so that MLEModel only inherits from TimeSeriesModel and holds a KalmanFilter object, which it can populate with whatever endog it likes. I can't right now think of any reason why there must be an inheritance relationship.

  2. Kalman filter initialization: in the base state space classes there is currently no way to set the initialization via keyword, but there's no reason that it couldn't be added.

    There are currently three initialization options: approximate diffuse, stationary, and known. If it is approximate diffuse, an initial variance needs to be specified, if it is known then an initial state vector and an initial state covariance need to be specified. Nothing additional need be specified for stationary.

    initial_variance is already a keyword parameter, and we could add initial_state and initial_state_cov as keyword parameters for the known initialization case. Then the keyword initialization would ask for a string ('approximate_diffuse', 'stationary', or 'known').

Those two things I think are the only things preventing full specification and re-implementation of the model from the keyword parameters.

@josef-pkt

Copy link
Copy Markdown
Member Author

for this PR's case of recreating a model, the location of the orig_data will be easy to handle (in _get_init_kwds), the option to choose the initialization method needs to be added, also to make it easier to understand for users.
(but it can also wait for 0.8)
(the most urgent for user interface is IMO the missing wrapper class)

to 2) one question is whether the initialization method is a __init__ or a fit option. When in doubt add it to __init__. If it's in __init__, then a user will need to create a new model to change the initialization method. If it's a fit argument. then a user might call with different initialization, and different results instances would point to the same model which might not have the right initialization.

To 1 Data refactoring in general:
My guess (after browsing this a bit) is that you should separate model, statespace representation and kalman filter to use delegation instead of inheritance to make the code easier to understand and maintain, independent of the data transformation issue.
For example, it took me a while to find where the SARIMAXResults instance is actually created, browsing through the class hierarchy and finally searching in all statespace files. (Note, I also have to do this sometimes with our other models, but they don't use triple inheritance.)
Right now the three super classes look tightly integrated and it is not clear which attribute is set and changed in which class.

Also coming back to both holding state and to which data is used: If the representation and the kalman filter are delegated instances from the main mle or sarimax model, then it would be easier to "recreate", for example, a filter to do some auxiliary calculations, instead of reusing the same inherited structure each time.

An analogy that is overall much simpler then here: In fit_constrained or fit_transformed I just created a new model instance that hold the transformed data. This makes it relatively easy to do the estimation in the auxiliary model, but present the original model in the original representation to the user, and possibly or optionally keep the transformed model attached for later reuse.
Having the parameterization of interest available is also relevant to be able take advantage of generic calculation for residual, prediction measures and similar.

Based on your explanation, it seems to me that in these cases (when endog and exog are swapped and transformed) the results statistics are in the transformed model.
Note, this is not necessarily bad. we also transform in formulas and present users with the formula created model. (And I'm not familiar with the details Skipper's ARIMA.)
For the (S)ARIMA(X) for example, do we, as users, have the stationary differenced version or the integrated version. (I haven't checked.) But in terms of results and prediction, I might be interested in both behaviors.

(Writing a lot because I'm thinking when I have to write, and might not remember later.)

@josef-pkt

Copy link
Copy Markdown
Member Author

The latest commit BUG/ENH/TST:add SARIMAX._get_init_kwds, use SkipTest for known failures
should make the test run green.
I'm raising SkipTest for the not supported diffuse initialization through keywords. (raising KnownFailure has noisy test output, IIRC)

also: getting endog and exog through _get_init_kwds is "non-standard". it was only intended for keyword arguments

@josef-pkt

Copy link
Copy Markdown
Member Author

@ChadFulton I think we can merge this (when TravisCI is green)
It provides the general setup for recreating a model, and will be easy to adjust when initialization is available through keywords in __init__.
endog, exog handling is a bit non-standard but easy to work around, and can wait for a possible refactoring.
(need to open 2 new issues following the comments here.)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.0%) to 83.91% when pulling 17bae99 on josef-pkt:sarimax_initkeys into c3313a5 on statsmodels:master.

josef-pkt added a commit that referenced this pull request Feb 14, 2015
ENH: fix/add SARIMAX _init_keys and get_init_kwds
@josef-pkt
josef-pkt merged commit 22de292 into statsmodels:master Feb 14, 2015
@josef-pkt
josef-pkt deleted the sarimax_initkeys branch April 10, 2015 14:47
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.

3 participants