Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i index overwritten in _arma_predict_out_of_sample #3797

Open
jbrockmendel opened this issue Jun 28, 2017 · 8 comments
Open

i index overwritten in _arma_predict_out_of_sample #3797

jbrockmendel opened this issue Jun 28, 2017 · 8 comments

Comments

@jbrockmendel
Copy link
Contributor

https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/arima_model.py#L338

    if q:
        i = 0  # if q == 1
    else:
        i = -1

    for i in range(min(q, steps - 1)):

i is set and then promptly ignored. Any idea what it is supposed to do? If it helps, q here is an alias for k_ma.

@josef-pkt
Copy link
Member

No idea, maybe an ancient left-over
blame shows the line was moved/changed 5 years ago but that part didn't change.

It might have been used as a shift in the start index for the multistep forecast loop in an older version.

I don't really understand the code, and whenever I need to look into it, it takes me hours to figure out what is or should be going on, especially with this kind of updating loops over parts of arrays.
(i.e. I'm spending enough time on these things to spot a bug candidate during bugfixing, but not enough to think about refactoring)

the part looks like it can be removed

@jbrockmendel
Copy link
Contributor Author

OK, I'll add this to my list of things to figure out. Tentatively it may be that the loop a few lines below the quoted section may be intended to use i:

    if q:
        i = 0  # if q == 1
    else:
        i = -1

    for i in range(min(q, steps - 1)):
        fcast = (mu[i] + np.dot(arparams, endog[i:i + p]) +
                 np.dot(maparams[:q - i], resid[i:i + q]))
        forecast[i] = fcast
        endog[i+p] = fcast

    for i in range(i + 1, steps - 1):
        fcast = mu[i] + np.dot(arparams, endog[i:i+p])
        forecast[i] = fcast
        endog[i+p] = fcast

Best guess is that the two loops should each have non-i indexes and that i is intended to be used in the range(i+1, steps-1). I'll follow up on this.

@josef-pkt
Copy link
Member

check test coverage and an example,
It's often faster to verify that the code "works" even if it is very difficult and time consuming to figure out why it does what it does.
e.g. in this case the indexing depends a lot on whether and where the local endog and resid have already been trimmed. (IIRC from related code then endog and resid start with a past of max(p, q) or max(k_ar, k_ma) observations )

@jseabold
Copy link
Member

jseabold commented Jul 1, 2017 via email

@jbrockmendel
Copy link
Contributor Author

Just trying it out, the first loop definitely does run.

Moreover, changing the indexes in both loops from i to idx (so as to not overwrite i) causes a handful of test failures.

Is the following plausible: the initialization is there specifically in case the first loop doesn't run. The second loop runs over range(i+1, steps-1), so needs i to be defined. If this is correct, then the whole thing could be cleaned up by starting the second loop from i0 = min(k_ma, steps-1)-1.

@jbrockmendel
Copy link
Contributor Author

Should I bother trying to track this down?

@jseabold
Copy link
Member

Hey, sorry my comment wasn't clear. Your plausible is correct.

@jbrockmendel
Copy link
Contributor Author

@jseabold if I put together a PR for this will you take point on getting it merged? There's a backlog and I don't want to add to jpkt's queue.

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

No branches or pull requests

3 participants