Skip to content

maxlag should be < nobs always OCCUR in python3, how to fix it? #4465

Closed

Description

@xgkkk

I have more than two years data but less than three years which consists of every weekly data. And I want to make a weekly prediction. Assume each year has 52 weeks. That means I have more than 104 items.
I want to use the first 104 items (two years) to train my sarima model and use other data after these 104 weekly data to test my model.
So data_train should be data[:52*2].
When I run the code below:

    mod = sm.tsa.statespace.SARIMAX([m for m in data_train],
                                    order=(0, 0, 0),
                                    seasonal_order=(1, 1, 0, 52),
                                    enforce_stationarity=False,
                                    enforce_invertibility=False)
    results = mod.fit(disp=False)

It always turns out

  File "D:\Anaconda3\envs\tensorflow\lib\site-packages\statsmodels\tsa\statespace\sarimax.py", line 1009, in start_params
    self.k_seasonal_ma, self.polynomial_seasonal_ma
  File "D:\Anaconda3\envs\tensorflow\lib\site-packages\statsmodels\tsa\statespace\sarimax.py", line 901, in _conditional_sum_squares
    X = np.c_[X, lagmat(endog, k_ar)[r:, cols]]
  File "D:\Anaconda3\envs\tensorflow\lib\site-packages\statsmodels\tsa\tsatools.py", line 379, in lagmat
    raise ValueError("maxlag should be < nobs")
ValueError: maxlag should be < nobs

sm.version.full_version = 0.8.0
Especially when I use grid search to find the best parameter of pdq and PDQ with the lowest AIC, it will turn out more and more "maxlag should be < nobs".
I want to know if my idea is false or it is just a bug or any other thing is wrong. I am not so familiar with arima.
So is there any approach to fix it? Or any idea to finish it with other lib in python.
Thanks.

Activity

  1. ChadFulton commented on Apr 11, 2018

    @ChadFulton
    Member
  2. xgkkk commented on Apr 12, 2018

    @xgkkk
    Author

    @ChadFulton So how do Q and q influence the number of observations? Thanks.

  3. ChadFulton commented on Apr 13, 2018

    @ChadFulton
    Member

    If you have an SARIMAX (p,d,q)x(P,D,Q,s) model, a quick back-of-the-envelope calculation suggests that you need at least:

    d + D*s + max(3*q + 1, 3*Q*s + 1, p, P*s) + 1 observations

    • (0, 0, 0)x(1, 0, 0, 52) => 0 + 0 + 52 + 1 = 53
    • (0, 0, 0)x(1, 1, 0, 52) => 0 + 52 + 52 + 1 = 105
    • (0, 0, 0)x(2, 1, 0, 52) => 0 + 52 + 104 + 1 = 157

    And:

    • (0, 0, 0)x(0, 0, 1, 52) => 0 + 0 + 3*52+1 + 1 = 158
    • (0, 0, 0)x(0, 1, 1, 52) => 0 + 52 + 3*52+1 + 1 = 210
    • (0, 0, 0)x(1, 1, 1, 52) => 0 + 52 + 3*51+1 + 1 = 210
  4. ChadFulton commented on Apr 13, 2018

    @ChadFulton
    Member

    Note though that this is only required for the estimation of starting parameters. If you pass your own starting parameters (e.g. start_params = [...] and then res = mod.fit(start_params)`) then there is no lower limit in terms of an exception being raised.

    However, I would still have at the very least the number of observations suggested by the rule of thumb above, so that the estimation procedure has a hope of coming up with something reasonable.

  5. csxiang18 commented on Jul 28, 2018

    @csxiang18

    Hello, @ChadFulton , I'm trying to figure out why I need d + Ds + max(3q + 1, 3Qs + 1, p, P*s) + 1 observations in an SARIMAX model, could you explain it in more details?

  6. ChadFulton commented on Jul 30, 2018

    @ChadFulton
    Member

    This is just a heuristic based on things like:

    • To estimate seasonal effects, you should have at least one complete cycle of data (e.g. if you want to estimate a quarterly seasonal effect, you should have at least one year (four quarters) of data).
    • When the model specifies differencing, you essentially lose the first d + D*s datapoints (because they don't have a prior set of obs that can be used to compute the difference)
    • Estimating moving average parameters can be difficult since the error terms are unobserved, and so require more data.
  7. C0DK commented on Aug 12, 2019

    @C0DK

    Wouldn't it be ideal to rephrase the exception, and also possibly throw a custom exception instead of simply a ValueError? It is rather obscure, and a ValueError is difficult to catch later on compared to a custom exception.

    If the "error" is that i should supply more observations, then that would be a better exception (maybe thrown at a higher level) so i, as a user/developer, know how to correct the error rather than having to find this issue on github. Maybe even say which value should be minimum what value:
    something like
    maxlag is 10, which should be bigger than nobs, which is 7 or what ever. I'm currently using a different library which calls your library, and as i never supply a variable called maxlag or nobs, then it is pretty difficult to understand how to fix this error on my end.

  8. bashtage commented on Aug 12, 2019

    @bashtage
    Member

    PRs to improve error messages are welcome. The error should be Value Error since there is a problem with the value passed.

  9. C0DK commented on Aug 12, 2019

    @C0DK

    @bashtage I disagree that a value error is useful here.
    Maybe InvalidMaxLag(ValueError)or something? :) Making it at least possible to catch it?

    ValueError would be useful if there was a direct relation between the values i pass and the function which throws the exception (and those values). I disagree that that is the case.

    I have never supplied an argument called maxlag nor nobs, meaning there isn't a direct relation between the input a user provides and the thrown exception. So it's an internal error in your system, based on an external input.

    After reading through the sourcecode, I realize that exception essentially means:

    k_ma <np.asarray(Y - np.dot(X,  params_ar) if k_ma > 0 else None).shape[0]
    
    where:
    Y = endog[k:]
    X = lagmat(endog, k, trim='both')
    params_ar = np.linalg.pinv(X).dot(Y)
    endog = ....
    

    Which is not even fully relative to the arguments i supply to your initializer, so i have to go even deeper to figure out what that shape actually represents. I essentially have to read through most of your sourcecode to understand that exception.

    I am no mathematician, and maybe the exception is totally obvious if you have a masters in something else than CompSci, but, it would help tremendously if the input validation happened at the initializer at the highest possible level, and/or if the exception was more informative.

    I would gladly help, but problem is that I still don't fully understand the relation between these arguments:

                cov_type='opg', cov_kwds=None, method='lbfgs', maxiter=50,
                full_output=1, disp=5, callback=None, return_params=False,
                optim_score=None, optim_complex_step=None, optim_hessian=None,
                flags=None, **kwargs):```
    
    and the exception.
    
    I am not trying to complain, and sorry if it sounds like that. I know this is open source and you owe me nothing. 
    
  10. bashtage commented on Aug 12, 2019

    @bashtage
    Member

    Singleton custom error types aren't worth the effort since there are a continuum of exception cades. All that is needed is a better exception explanation, i.e., ValueErrror(A BETTER EXPLANATION THAT THE USER CAN UNDERSTAND). Imo one shouldn't be catching this error so much as checking their code before asking for a model to be estimated that cannot be estimated. It isn't possible or practical for statsmodels to protect users from themselves, and a minimal level of understanding of the model estimated is expected.

  11. bashtage commented on Aug 12, 2019

    @bashtage
    Member

    Looking at the TB it would be best to catch this error earlier and return a more meaningful message about starting value generation.

  12. C0DK commented on Aug 12, 2019

    @C0DK

    Imo one shouldn't be catching this error so much as checking their code before asking for a model to be estimated that cannot be estimated

    Our service lets the user upload a dataset and input values (for using the autoarima function https://github.com/tgsmith61591/pmdarima ), with the intent of making different types of statistical models more approachable for people. It is really difficult to make things approachable if the best response is "N000b, why haven't you read the white paper?". Maybe i need to read more up on the ARIMA model though, so we can display a message regarding how the data-input combination that doesn't work. But expecting a user "just to know" seems weird, IMO.

    Looking at the TB it would be best to catch this error earlier and return a more meaningful message about starting value generation.

    Make sense. Thank you very much
    I would love to be able to

  13. tgsmith61591 commented on Aug 12, 2019

    @tgsmith61591

    Since I have a vested interest in this conversation, given my library is the one in question, maybe I'll make a suggestion... the issue you raised on my side (alkaline-ml/pmdarima#179) didn't mention that you're building a service that allows users to upload their own datasets for modeling. Perhaps given your target market seems to be those who don't understand ARIMA modeling, you should handle capturing their input and providing useful error messages? I'm going to echo @bashtage on

    It isn't possible or practical for statsmodels (or pmdarima) to protect users from themselves, and a minimal level of understanding of the model estimated is expected.

    (parenthetical mine)

  14. C0DK commented on Aug 12, 2019

    @C0DK

    I understand your stand, and accept it all and well. I just generally speaking think that a given piece of software should, at entry points, validate input and throw helpful exceptions to make it modular and maintainable. But i understand your stand. Maybe, when i have more time, (if i end up understanding ARIMA fully) i'll make a PR.

    Thank you, never the less.

  15. Ewetoye commented on Oct 14, 2019

    @Ewetoye

    ValueErrror(A BETTER EXPLANATION THAT THE USER CAN UNDERSTAND)

    This may be the best way to handle the error. May be, I am missing something.

  16. ChadFulton commented on Oct 14, 2019

    @ChadFulton
    Member

    This was fixed in #6102 and will be in the next release.

  17. ChadFulton commented on Oct 14, 2019

    @ChadFulton
    Member

    Closing due to #6102.

  18. Ewetoye commented on Oct 15, 2019

    @Ewetoye

    Wow, thanks @ChadFulton for making the commit.

  19. added this to the 0.11 milestone on Dec 18, 2019
  20. manav3475 commented on Aug 24, 2021

    @manav3475

    maxlag should be < nobs

    my AR is not fit it will be given me this error can anyone explain me what is this error actually said

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