You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
maxlag should be < nobs always OCCUR in python3, how to fix it? #4465
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.
The problem is that you need more observations to estimate the model. For
example, with SARIMAX (0, 0, 0)x(1, 0, 0, 52), you need at least 53
observations, and with SARIMAX (0, 0, 0)x(1, 1, 0, 52) then you need at
least 105 observations. With SARIMAX (0, 0, 0)x(2, 1, 0, 52) you need at
least 157 observations.
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.
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?
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.
Reacted by Akash Lakhera, Alex Martens and James Mercer
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.
@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.
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.
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
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.
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.
Activity
ChadFulton commentedon Apr 11, 2018
xgkkk commentedon Apr 12, 2018
@ChadFulton So how do Q and q influence the number of observations? Thanks.
ChadFulton commentedon Apr 13, 2018
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
And:
ChadFulton commentedon Apr 13, 2018
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.
csxiang18 commentedon Jul 28, 2018
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?
ChadFulton commentedon Jul 30, 2018
This is just a heuristic based on things like:
C0DK commentedon Aug 12, 2019
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 7or 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.bashtage commentedon Aug 12, 2019
PRs to improve error messages are welcome. The error should be Value Error since there is a problem with the value passed.
C0DK commentedon Aug 12, 2019
@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:
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:
bashtage commentedon Aug 12, 2019
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.
bashtage commentedon Aug 12, 2019
Looking at the TB it would be best to catch this error earlier and return a more meaningful message about starting value generation.
C0DK commentedon Aug 12, 2019
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.
Make sense. Thank you very much
I would love to be able to
tgsmith61591 commentedon Aug 12, 2019
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
(parenthetical mine)
C0DK commentedon Aug 12, 2019
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.
Ewetoye commentedon Oct 14, 2019
This may be the best way to handle the error. May be, I am missing something.
ChadFulton commentedon Oct 14, 2019
This was fixed in #6102 and will be in the next release.
ChadFulton commentedon Oct 14, 2019
Closing due to #6102.
Ewetoye commentedon Oct 15, 2019
Wow, thanks @ChadFulton for making the commit.
manav3475 commentedon Aug 24, 2021
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