Skip to content

Design: Coupling of statespace classes with downstream models #2265

Closed

Description

@ChadFulton

Currently the design suggests that estimated state space models be descendants of the Representation and KalmanFilter classes, where the inheritance is through MLEModel and Model.

For example, the SARIMAX class has the following inheritance structure:

Representation - KalmanFilter
                               \
                                Model - MLEModel - SARIMAX
                               /
              TimeSeriesModel

The entire purpose of the Model class is to join the inheritance trees of statespace and statsmodels. However, there is no particular reason for the relatively strict coupling via inheritance, particularly since most users will not care about the state space representation (e.g. the design matrix with the MLE parameters, etc.).

There are several issues with the current approach:

  • The inheritance structure enforces that the self.endog for Representation is the same array as the self.endog for SARIMAX. This is problematic when simple_differencing=True, which means that the self.endog used by Representation has to be the differenced time series, whereas the self.endog for SARIMAX should probably be the endog array that was passed in (and similar for `exog).
  • The inheritance structure requires the SARIMAX model to hold state.
  • There are many attributes in the SARIMAX class that are somewhat unrelated to the model per se (e.g. state space matrices, filtering options, etc.).

One way to refactor the model is via delegation. In that case, the Model class would likely be eliminated, and the inheritance structure for SARIMAX could be:

TimeSeriesModel - MLEModel - SARIMAX

Then MLEModel could delegate all filtering and likelihood computation to a KalmanFilter instance (e.g. self.ssm or self.statespace or similar). MLEResults could delegate the base predict method to FilterResults.

Any fix of the problem via delegation still needs to take into account copying versus references of endog and exog arrays.

Activity

  1. josef-pkt commented on Feb 22, 2015

    @josef-pkt
    Member

    another possible issue circular references (just found them in #2288)
    If delegation requires circular references between Model, Representation and KalmanFilter, then it might also not be the right choice. (Based on previous cases of circular references, it can create problems with garbage collection and blow up the memory requirements when we create models in a loop for example for bootstrapping. It needs explicit gc.collect)

    Main question is which class needs to hold on and change which attributes, and whether it's possible to work at least partially in a functional way instead of working mainly through side-effects (changing attributes).

  2. added this to the 0.8 milestone on Feb 22, 2015
  3. josef-pkt commented on Feb 22, 2015

    @josef-pkt
    Member

    One possibility is to start with increasing separation within the current class structure (triple inheritance), to have more incremental refactoring (e.g. to make it easier to add the wrapper class) instead of one big refactor.

  4. ChadFulton commented on Feb 12, 2016

    @ChadFulton
    MemberAuthor

    The decoupling was completed in #2473.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions