statsmodels / statsmodels

Statsmodels: statistical modeling and econometrics in Python
http://www.statsmodels.org/devel/
BSD 3-Clause "New" or "Revised" License
10.14k stars 2.88k forks source link

UnboundLocalError: local variable 'fittedvalues' referenced before assignment #1399

Closed mihneagiurgea closed 10 years ago

mihneagiurgea commented 10 years ago

When I try to run an ARIMA prediction I get an UnboundLocalError.

Code to reproduce:

import numpy as np
from statsmodels.tsa.arima_model import ARIMA

SAMPLE = [7.16, 5.04, 5.57, 5.10, 4.93, 5.04, 5.06, 5.06, 4.85, 4.96]

arima = ARIMA(np.array(SAMPLE), (0, 1, 0))
result = arima.fit(method='css', disp=-1)
print result.predict(1, 1)

Error traceback:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print result.predict(1, 1)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels/base/wrapper.py", line 90, in wrapper
    return data.wrap_output(func(results, *args, **kwargs), how)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 1567, in predict
    return self.model.predict(self.params, start, end, exog, typ, dynamic)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 1021, in predict
    dynamic)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 642, in predict
    k_ar, method)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels/tsa/arima_model.py", line 279, in _arma_predict_in_sample
    fv_end = min(len(fittedvalues), end + 1)
UnboundLocalError: local variable 'fittedvalues' referenced before assignment

Paste from github code causing the error:

def _arma_predict_in_sample(start, end, endog, resid, k_ar, method):
    """
    Pre- and in-sample fitting for ARMA.
    """
    if 'mle' in method:
        fittedvalues = endog - resid  # get them all then trim
    else:
        fittedvalues = endog[k_ar:] - resid

    fv_start = start
    if 'mle' not in method:
        fv_start -= k_ar  # start is in terms of endog index
    fv_end = min(len(fittedvalues), end + 1)
    return fittedvalues[fv_start:fv_end]

(taken from here: https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/arima_model.py#L287)

josef-pkt commented 10 years ago

In that code from master, fittedvalues is always defined.

The else was recently fixed https://github.com/statsmodels/statsmodels/commit/1c15e82b4152ea53f096225255737cd1e582306a#diff-c46cce4ad3d34e501b159ad412567dc1L288

I get 'fittedvalues' referenced before assignment with 0.5.0

needs a check whether we have a fix for 0.5.1

josef-pkt commented 10 years ago

Forget about 0.5.1:

ARMA(0,0) was not supported in 0.5 and earlier, only in master do we have the recent extension by Kevin. fixed in #1262 and #1370 AFAICS

I 'm not sure if 1370 also included ARIMA(0,1,0)

josef-pkt commented 10 years ago

@mihneagiurgea can you try your examples with current master.

mihneagiurgea commented 10 years ago

Yes, the current master fixes the error.

However, I wasn't able to install the master branch doe due to this error:

error: can't copy 'statsmodels/nonparametric/_smoothers_lowess.c': doesn't exist or not a regular file

Both python setup.py install and pip install yielded the same error.

josef-pkt commented 10 years ago

I guess you don't have cython installed. Cython is required to build from source (and a c compiler).

In case you are on Windows, there are also nightly Windows binaries of master at http://statsmodels.sourceforge.net/binaries/

mihneagiurgea commented 10 years ago

Thanks, I'll try installing with Cython.