Skip to content

ARIMA missing predict exog causes type error #3737

Closed

Description

@tgsmith61591

I am getting a TypeError when forecasting on an ARIMA when exog is not None. Here is the setup for a reproducible example:

from statsmodels.tsa.arima_model import ARIMA
import numpy as np
from numpy.random import RandomState
rs = RandomState(42)

hr = np.array([84.2697, 84.2697, 84.0619, 85.6542, 87.2093, 87.1246,
               86.8726, 86.7052, 87.5899, 89.1475, 89.8204, 89.8204,
               90.4375, 91.7605, 93.1081, 94.3291, 95.8003, 97.5119,
               98.7457, 98.904, 98.3437, 98.3075, 98.8313, 99.0789,
               98.8157, 98.2998, 97.7311, 97.6471, 97.7922, 97.2974,
               96.2042, 95.2318, 94.9367, 95.0867, 95.389, 95.5414,
               95.2439, 94.9415, 95.3557, 96.3423, 97.1563, 97.4026,
               96.7028, 96.5516, 97.9837, 98.9879, 97.6312, 95.4064,
               93.8603, 93.0552, 94.6012, 95.8476, 95.7692, 95.9236,
               95.7692, 95.9211, 95.8501, 94.6703, 93.0993, 91.972,
               91.7821, 91.7911, 90.807, 89.3196, 88.1511, 88.7762,
               90.2265, 90.8066, 91.2284, 92.4238, 93.243, 92.8472,
               92.5926, 91.7778, 91.2974, 91.6364, 91.2952, 91.771,
               93.2285, 93.3199, 91.8799, 91.2239, 92.4055, 93.8716,
               94.5825, 94.5594, 94.9453, 96.2412, 96.6879, 95.8295,
               94.7819, 93.4731, 92.7997, 92.963, 92.6996, 91.9648,
               91.2417, 91.9312, 93.9548, 95.3044, 95.2511, 94.5358,
               93.8093, 93.2287, 92.2065, 92.1588, 93.6376, 94.899,
               95.1592, 95.2415, 95.5414, 95.0971, 94.528, 95.5887,
               96.4715, 96.6158, 97.0769, 96.8531, 96.3947, 97.4291,
               98.1767, 97.0148, 96.044, 95.9581, 96.4814, 96.5211,
               95.3629, 93.5741, 92.077, 90.4094, 90.1751, 91.3312,
               91.2883, 89.0592, 87.052, 86.6226, 85.7889, 85.6348,
               85.3911, 83.8064, 82.8729, 82.6266, 82.645, 82.645,
               82.645, 82.645, 82.645, 82.645, 82.645, 82.645])

# define model fit
fit = ARIMA(hr, order=(2, 1, 2), exog=rs.rand(hr.shape[0], 4))
res = fit.fit(disp=0)

And then when forecasting, here's what I get:

>>> res.forecast(steps=5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 1869, in forecast
    exog, method=self.model.method)
  File "//anaconda/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 326, in _arma_predict_out_of_sample
    exog)
  File "//anaconda/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 292, in _get_predict_out_of_sample
    X = lagmat(np.dot(exog, exparams), p, original='in', trim='both')
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

Is this a bug or something I'm doing wrong?

Activity

  1. josef-pkt commented on Jun 2, 2017

    @josef-pkt
    Member

    I don't know if there is a good place to raise an easier to understand error.

    If you forecast and you have used an exog, then you also need to provide a exog for the forecast periods. Given that forecast is out of sample, there is not enough information to make a forecast.
    I think the specific exception is because, exog=None, by default.

    (My guess would be that the estimation exog is available and used for in-sample prediction.)

  2. tgsmith61591 commented on Jun 2, 2017

    @tgsmith61591
    Author

    That makes sense. Would it be worth storing a class attribute, something like self.fit_with_exog_, in __init__ and then test if self.fit_with_exog_ and exog is None: raise? I know it's clunky to save that extra attribute, but it might cause less confusion...

  3. josef-pkt commented on Jun 2, 2017

    @josef-pkt
    Member

    We wouldn't need an extra attribute to check this. We can just check whether self.model.exog is not None

  4. josef-pkt commented on Oct 7, 2017

    @josef-pkt
    Member

    It is possible to add this check in forecast, where we always need new exog, but not in predict where it depends on the predict dates whether we need out-of-sample exog or have them in-sample.

  5. changed the title [-]ARIMA TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'[/-] [+]ARIMA missing predict exog causes type error[/+] on Oct 8, 2017
  6. josef-pkt commented on Oct 9, 2017

    @josef-pkt
    Member

    We also have k-exog as attribute.

    Design: we can raise an exception if exog is None and k_exog >0 either

    • in the forecast function, or
    • in the final function that does the computation, which is _get_predict_out_of_sample

    The first has the advantage that input errors are caught at the source and it is obvious what we check for. The disadvantage is that we need to add it to all forecast methods.
    The second is hiding away the check but doesn't require code duplication (example for problems because of hiding input checks #3503 (comment) )

    Note: _get_predict_out_of_sample has two places where exog dot params is computed, and initially I was reading the wrong one

  7. josef-pkt commented on Oct 9, 2017

    @josef-pkt
    Member

    Related check: IIRC, we don't impose that exog for forecast has the correct length, i.e. it can be longer than needed. (based on what I remember from previous bug fixes)

    so we don't check shape, exog.shape[1] != k_exog should raise automatically an informative exception, but I don't know if exog.ndim = 2 is required.

  8. added this to the 0.9 milestone on Oct 9, 2017
  9. added a commit that references this issue on Aug 15, 2018
    b2507d9
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