Skip to content

Arima exog predict 2128 rebased - #2470

Merged
josef-pkt merged 7 commits into
statsmodels:masterfrom
josef-pkt:arima_exog_predict_2128_rebased
Jun 29, 2015
Merged

josef-pkt merged 7 commits into
statsmodels:masterfrom
josef-pkt:arima_exog_predict_2128_rebased

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

rebased version of #2128, fixes #2127
with additional regression test

(code comments in tests about things I didn't understand.)

@josef-pkt josef-pkt added this to the 0.7 milestone Jun 24, 2015
@josef-pkt

Copy link
Copy Markdown
Member Author

I'll merge when TravisCI is green

@ChadFulton

Copy link
Copy Markdown
Member

Before you do, there's another bug in predict that should be fixed here. Take a look at:

http://nbviewer.ipython.org/gist/ChadFulton/6d917df65bfbcd4e0c1c/

The problem is that the _get_predict_out_of_sample function doesn't subset the mu vector correctly in the case of k_trend == 1 and k_exog > 0.

I believe that the fix requires changing line 296 from:

mu = mu + (np.r_[1, -arparams[::-1]] * X).sum(1)[:, None]

to

mu = mu + (np.r_[1, -arparams[::-1]] * X).sum(1)[len(mu)-steps:, None]

Edit: No, that's wrong, sorry; it should be adding a line just after 296 that does:

mu = mu[len(mu)-steps:]

This seems to fix it for me, but I'm not sure what tests to run to make sure it doesn't have negative consequences elsewhere.

@josef-pkt

Copy link
Copy Markdown
Member Author

Thanks for looking into this, i will look at it again (in a few hours).

I couldn't figure out what values of exog are used if it doesn't have the exact length. It doesn't raise an exception if exog in predict is too long, but I couldn't replicate the result of a too long exog with an exact length exog.

@ChadFulton

Copy link
Copy Markdown
Member

I couldn't figure out what values of exog are used if it doesn't have the exact length. It doesn't raise an exception if exog in predict is too long, but I couldn't replicate the result of a too long exog with an exact length exog.

Here's the source of this bug:

exog is never subset to the required length, but what happens is that in _arma_predict_out_of_sample it iterates from the start of exog to steps-1 (the two for loops). But then for the final forecast, instead of continuing on the iteration, it selects the last element from exog. So if exog is too long, the prediction should be correct except for the last element (at least this is what I found).

I think that this can be fixed by changing:

forecast[-1] = mu[-1] + np.dot(arparams, endog[steps - 1:])

to

forecast[steps-1] = mu[steps-1] + np.dot(arparams, endog[steps - 1:])

@josef-pkt

Copy link
Copy Markdown
Member Author

@ChadFulton Thanks for the notebook, and the comparison ARIMA SARIMAX. We should add something like this to the documentation notebooks.

I'm not really an expert on our ARIMA implementation.
After playing a bit with the prediction in your notebook, I think we shouldn't try to choose a slice of a too long exog, unless we can get the correct slice via the pandas index.
If the exog is too long, then, mostly based on what you found, it uses the beginning of the exog. but for forecasting this doesn't make sense, if the user accidentially or because of misunderstanding used the entire exog instead of the exog of the prediction time. (as in the initial stackoverflow example in #2127

my suggestion: Either raise a ValueError for length mismatch, or enhance by using pandas index of the exog.

But this is a separate bug from the one in this PR (and the original issue and PR)

@josef-pkt

Copy link
Copy Markdown
Member Author

new issue for this #2472

@josef-pkt

Copy link
Copy Markdown
Member Author

something looks fishy with the ARIMA(3, 1, 1) prediction for the exog timing.

The level prediction is shifted down compared to ARMA and OLS, with the same exog.
However this could be a feature and not a bug. (last observation and their forecast errors are "initial conditions" for forecast that don't go away as prediction horizon increases.)

edit maybe being a feature in incorrect. We should be using the actual observation as starting point for the forecast, but a integrated process also accumulates innovations forever. ???

@josef-pkt

Copy link
Copy Markdown
Member Author

to the previous: timing of exog has to be correct, in the seasonal series, I get the predicted peaks and troughs at exactly the same time as OLS and ARMA. If I shift exog, then not only the height shifts up or down but the extremes shift horizontally (in plot) which is wrong.

@josef-pkt

Copy link
Copy Markdown
Member Author

reverse engineering: the shift of the prediction in ARIMA is a feature

In the seasonal data, the last year had low values, lower than average. ARIMA propagates the lower level to future predictions.
If I don't use the last year for estimating the model, and start at the year before which has slightly higher than average values, then the ARIMA prediction is a little bit above the prediction of ARMA and OLS.

@josef-pkt

Copy link
Copy Markdown
Member Author

add latest changes to description

looks good, but I haven't tried to figure out start index yet: What happens if start and end is a datetime or similar index? Is that allowed?

@josef-pkt

Copy link
Copy Markdown
Member Author

correction: I don't know why I didn't see that start is already converted to index
start = self._get_predict_start(start, dynamic)

@josef-pkt

Copy link
Copy Markdown
Member Author

need to lower precision, all python versions of TravisCI fail, but not all at the same assert.
All tests passed for me with MKL. But I'm not testing the parameter estimates.

@josef-pkt
josef-pkt force-pushed the arima_exog_predict_2128_rebased branch from 0609866 to 4d58b3b Compare June 28, 2015 23:24
@josef-pkt

Copy link
Copy Markdown
Member Author

2 python versions still failed, lowered precision some more, and amended comit, plus small cleanup

@josef-pkt

Copy link
Copy Markdown
Member Author

all green, ready to merge

@ChadFulton @jseabold any comments?

the only thing that's left of the related issues is whether we raise or warn if prediction exog is longer than needed #2472 (I haven't figured out where to check the length.)

josef-pkt added a commit that referenced this pull request Jun 29, 2015
@josef-pkt
josef-pkt merged commit 4bf1c23 into statsmodels:master Jun 29, 2015
@josef-pkt

Copy link
Copy Markdown
Member Author

merged

@josef-pkt
josef-pkt deleted the arima_exog_predict_2128_rebased branch July 27, 2015 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ARIMA.predict does not pass exog in super.

3 participants