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

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

Closed
mihneagiurgea opened this issue Feb 19, 2014 · 6 comments

Comments

Projects
None yet
2 participants
@mihneagiurgea
Copy link

commented Feb 19, 2014

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

This comment has been minimized.

Copy link
Member

commented Feb 20, 2014

In that code from master, fittedvalues is always defined.

The else was recently fixed
1c15e82#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

This comment has been minimized.

Copy link
Member

commented Feb 20, 2014

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

This comment has been minimized.

Copy link
Member

commented Feb 20, 2014

@mihneagiurgea can you try your examples with current master.

@mihneagiurgea

This comment has been minimized.

Copy link
Author

commented Feb 21, 2014

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

This comment has been minimized.

Copy link
Member

commented Feb 21, 2014

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

This comment has been minimized.

Copy link
Author

commented Feb 21, 2014

Thanks, I'll try installing with Cython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.