Skip to content

ARMA.predict() #3882

Closed
Closed

Description

@antvig

Hi,
There is an issue calling the predict() method of ARMA object.
Indeed, ARMA object has no k_trend property which is needed in the geterrors() method...

k = self.k_exog + self.k_trend

Activity

  1. josef-pkt commented on Aug 23, 2017

    @josef-pkt
    Member

    AFAICS, this is a consequence that trend is specified in fit, e.g. #1049
    This has never been refactored to avoid initialization code in fit.
    So attributes that are initialized or updated in fit like exog and k_trend will not be correct or available.

    as workaround you could try to call fit(maxiter=0) before calling geterrors.

    I guess we can initiallize k_trend=0 in __init__ and it will be updated in fit
    My guess is that we don't do the full refactoring of ARMA/ARIMA anymore, because we most likely will deprecate it in favor of SARIMAX

  2. antvig commented on Aug 23, 2017

    @antvig
    Author

    This issue prevents from applying a fitted model on a new dataset :

    model_1 = sm.tsa.ARMA(dataset_1, (p, q))
    model_1_fitted = model_1.fit()
    
    model_2 = sm.tsa.ARMA(dataset_2, (p, q))
    res = model_2.predict(model_1.params)
    

    k_trend, nobs and transparams attributes are initialized in fit.
    As a workaround, I set the attributes manually which is not very clean but it works fine...

    As for SARIMAX, the predict function seems to be not implemented ( even after calling fit() ).

    In definitive, my main concern is that I can't find a way to apply a model fitted for one dataset to an other dataset, but I may have missed something...

  3. josef-pkt commented on Aug 23, 2017

    @josef-pkt
    Member

    What is your specification for constant and trend, given that it is added in fit?

    The usecase itself is interesting, and we could try to specifically support it so it works this way.
    Initializing k_trend in __init__ looks fine.

    I'm not sure a trend should be predicted this way unless it is clear that all time series have the same mean or trend. But it would make sense to use it for the demeaned or detrended series.

    (related issue: There was a limitation that exog couldn't contain a constant because it was added through trend, but I don't remember the status)

    This would be a good candidate for a cookbook/example notebook.

  4. josef-pkt commented on Aug 23, 2017

    @josef-pkt
    Member

    I thought SARIMAX has an equivalent predict function

  5. ChadFulton commented on Aug 23, 2017

    @ChadFulton
    Member
  6. antvig commented on Aug 24, 2017

    @antvig
    Author

    What is your specification for constant and trend, given that it is added in fit ?

    I only specify k_trend. For ARMA model, the attribute k_constant is initialized in the __init__() (and can only be 0)

    For ARMA, the trend can only be 'nc' -> no constant or 'c' -> with constant, It seems that a trend (linear for exemple) can't be specified.
    --> k_constant seems useless for ARMA since the constant is specified with trend.

    model_1 = sm.tsa.ARMA(dataset_1, (p, q))
    model_1_fitted = model_1.fit(trend='nc')     # constant is 0 the dataset is made stationary in 
                                                    preprocessing
    
    model_2 = sm.tsa.ARMA(dataset_2, (p, q))
    
    model_2.k_trend = model_1.k_trend            # = 0 
    model_2.nobs = len(dataset_2)                # not sure it is petinent...
    model_2.transparams = False
    
    res = model_2.predict(model_1.params)
    

    For ARIMA, I think there is a predict at the model level.

    Yes indeed but same issue than with ARMA if using the predict() without fitting first.

  7. changed the title [-]ARMA.geterrors()[/-] [+]ARMA.predict()[/+] on Aug 24, 2017
  8. josef-pkt commented on Aug 24, 2017

    @josef-pkt
    Member

    @antvig Can you write some unit tests for this? I would like to know and then have it verified by unit tests which use case works. And a test failure would prevent that it might break during refactoring or changes

    e.g. if dataset_2 = data_set_1, then the prediction would be the same.
    if k_trend is initialized to zero, then the constant should/might be wrong if it is included in params.
    eg. for case dataset_2 = data_set_1 + 1

    model_2 .k_trend = model_1 .k_trend

    this will be more difficult to support because model_2 has no information about model 1.

    Another possibility to check and compare to is model2.fit(maxiter=0):
    How much extra time does it use? Does it even work without changing
    It recomputes the hessian but it's not an MLE, so inference should be wrong. (?) (valid asymptotic inference might need at least one Markov/Hessian step, if the analogy to constrained estimation works.)
    (i.e. inference, prediction confidence intervals would be more work, except prediction confidence intervals ignore parameter uncertainty, AFAIR.)

  9. ChadFulton commented on Aug 24, 2017

    @ChadFulton
    Member

    As long as the model specifications are identical (i.e. identical p, d, q and trend), you can always do:

    model_1 = sm.tsa.SARIMAX(dataset_1, order=(p, 0, q), trend=trend)
    model_1_fitted = model_1.fit()
    
    model_2 = sm.tsa.SARIMAX(dataset_2, order=(p, 0, q), trend=trend)
    res = model_2.smooth(model_1_fitted.params)
    res.predict()

    Note that trend uses n instead of nc, since it allows a more general trend than a constant.

  10. antvig commented on Sep 12, 2017

    @antvig
    Author

    Hi,
    I'm not really familiar with unit test but here is two tests i wrote:

    1. Test if ARMA.predict and ARMAResults.predict produce the same prediction if they have the same attributes

    2. Test if ARMA.predict can be applied on a dataset with params from a model fitted on an other dataset

    unit_test.zip

  11. josef-pkt commented on Oct 7, 2017

    @josef-pkt
    Member

    check this for 0.9.
    It sounds like a more serious. We won't do a fit keyword refactoring for 0.9 anymore, but maybe there is a quickfix.
    (issue didn't get a label and got lost)

    prio-high to check at least how serious this is

  12. added this to the 0.9 milestone on Oct 7, 2017
  13. 6 remaining items

  14. jbrockmendel commented on Oct 17, 2017

    @jbrockmendel
    Contributor

    My guess is that we don't do the full refactoring of ARMA/ARIMA anymore, because we most likely will deprecate it in favor of SARIMAX

    Or you could "assign" it to a maintainer who has volunteered to clean it up #3992.

    new general design principle

    This is a problem that comes up a ton in tsa models because k_trend/k_ar... don't get set in FooModel.__init__. I think the solution is to have a model-parent class for these models; something like:

    class ARMAs:
        def __init__(self, endog, exog):
        
        def spawn(self, k_ar, k_ma, k_trend):
             return ARMA(self.endog, self.exog, k_ar, k_ma, k_trend)
    

    and have ARMA set the expected attributes in __init__ so as to conform to the patterns in the broader code-base. model-selection type methods would belong in this type of parent class.

  15. josef-pkt commented on Oct 17, 2017

    @josef-pkt
    Member

    Adding a new super class or intermediate class into the class hierarchy is a different issue.
    The simple fix is to finish moving all data and specification arguments to __init__ and deprecate those in fit.
    (ARIMA delegates to ARMA, and that created or creates other sources of bugs.)

    In general spawn, class factory or class dispatch, didn't catch on in our design, e.g. #842
    In my experience it is too confusing (as a user).

  16. jbrockmendel commented on Oct 17, 2017

    @jbrockmendel
    Contributor

    Adding a new super class or intermediate class into the class hierarchy is a different issue.

    Fair enough. I don't expect any movement on this front anytime soon.

    BTW I forgot to mention from a different thread: here's to a speedy recovery for your kid.

    The simple fix is to finish moving all data and specification arguments to __init__ and deprecate those in fit.
    (ARIMA delegates to ARMA, and that created or creates other sources of bugs.)

    Looks like ARMA.__init__ sig is (self, endog, order, exog=None,... So flip the exog/order, add some validation+warnings for the change, move trend kwarg from fit to __init__... roughly the same for ARIMA and AR... updating a bunch of tests. (conceivably extend to VAR though I'd be reticent to do this in one go). The amount of labor sounds roughly Topic Week sized yah?

  17. self-assigned this
    on Jul 23, 2019
  18. added 8 commits that reference this issue on Jul 23, 2019
    def8803
    8b401b1
    0cfaddc
    a600542
    ea4dfd2
    a4830b7
    0feb0fa
    ab0f955
  19. modified the milestones: 0.10, 0.11 on Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions