Hi, i'm trying to use an ARMA model to predict out of sample. In the
predict method, i've used datetime objects to specify the start and end
dates, but it throws an error. Does anyone know what i'm doing wrong?
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from datetime import datetime
from statsmodels.tsa.arima_process import arma_generate_sample
%matplotlib inline
df = pd.DataFrame()
nobs = 1000
arparams = np.r_[1, -np.array([0.6])]
maparams = np.r_[1, np.array([-0.5])]
df["ARMA_11"] = arma_generate_sample(arparams, maparams, nobs)
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
arma_mod = sm.tsa.ARMA(df["ARMA_11"].values, order=(1,1))
arma_res = arma_mod.fit(trend='nc', disp=-1)
arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26),
dynamic=True)
Error:
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-33-bf138e6ab0e6> in <module>()----> 1 arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26), dynamic=True)
/usr/local/lib/python3.4/dist-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs) 93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:]) 94 elif how:---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how) 96 return obj 97
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, dynamic) 1457 1458 def predict(self, start=None, end=None, exog=None, dynamic=False):-> 1459 return self.model.predict(self.params, start, end, exog, dynamic) 1460 predict.__doc__ = _arma_results_predict 1461
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, params, start, end, exog, dynamic) 717 718 # will return an index of a date--> 719 start = self._get_predict_start(start, dynamic) 720 end, out_of_sample = self._get_predict_end(end, dynamic) 721 if out_of_sample and (exog is None and self.k_exog > 0):
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _get_predict_start(self, start, dynamic) 652 #elif 'mle' not in method or dynamic: # should be on a date 653 start = _validate(start, k_ar, k_diff, self.data.dates,--> 654 method) 655 start = super(ARMA, self)._get_predict_start(start) 656 _check_arima_start(start, k_ar, k_diff, method, dynamic)
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _validate(start, k_ar, k_diff, dates, method) 374 def _validate(start, k_ar, k_diff, dates, method): 375 if isinstance(start, (string_types, datetime)):--> 376 start = _index_date(start, dates) 377 start -= k_diff 378 if 'mle' not in method and start < k_ar - k_diff:
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/base/datetools.py in _index_date(date, dates) 40 return dates.indexMap[date] 41 else:---> 42 date = dates.get_loc(date) 43 try: # pandas 0.8.0 returns a boolean array 44 len(date)
AttributeError: 'NoneType' object has no attribute 'get_loc'
Sorry there was a mistake in my copy/paste (the error still remains).
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
should be
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'),
inplace=True)
Post by Anjum Sayed
Hi, i'm trying to use an ARMA model to predict out of sample. In the
predict method, i've used datetime objects to specify the start and end
dates, but it throws an error. Does anyone know what i'm doing wrong?
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from datetime import datetime
from statsmodels.tsa.arima_process import arma_generate_sample
%matplotlib inline
df = pd.DataFrame()
nobs = 1000
arparams = np.r_[1, -np.array([0.6])]
maparams = np.r_[1, np.array([-0.5])]
df["ARMA_11"] = arma_generate_sample(arparams, maparams, nobs)
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
arma_mod = sm.tsa.ARMA(df["ARMA_11"].values, order=(1,1))
arma_res = arma_mod.fit(trend='nc', disp=-1)
arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26),
dynamic=True)
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-33-bf138e6ab0e6> in <module>()----> 1 arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26), dynamic=True)
/usr/local/lib/python3.4/dist-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs) 93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:]) 94 elif how:---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how) 96 return obj 97
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, dynamic) 1457 1458 def predict(self, start=None, end=None, exog=None, dynamic=False):-> 1459 return self.model.predict(self.params, start, end, exog, dynamic) 1460 predict.__doc__ = _arma_results_predict 1461
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _get_predict_start(self, start, dynamic) 652 #elif 'mle' not in method or dynamic: # should be on a date 653 start = _validate(start, k_ar, k_diff, self.data.dates,--> 654 method) 655 start = super(ARMA, self)._get_predict_start(start) 656 _check_arima_start(start, k_ar, k_diff, method, dynamic)
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/base/datetools.py in _index_date(date, dates) 40 return dates.indexMap[date] 41 else:---> 42 date = dates.get_loc(date) 43 try: # pandas 0.8.0 returns a boolean array 44 len(date)
AttributeError: 'NoneType' object has no attribute 'get_loc'
Post by Anjum Sayed
Hi, i'm trying to use an ARMA model to predict out of sample. In the
predict method, i've used datetime objects to specify the start and end
dates, but it throws an error. Does anyone know what i'm doing wrong?
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from datetime import datetime
from statsmodels.tsa.arima_process import arma_generate_sample
%matplotlib inline
df = pd.DataFrame()
nobs = 1000
arparams = np.r_[1, -np.array([0.6])]
maparams = np.r_[1, np.array([-0.5])]
df["ARMA_11"] = arma_generate_sample(arparams, maparams, nobs)
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
arma_mod = sm.tsa.ARMA(df["ARMA_11"].values, order=(1,1))
arma_res = arma_mod.fit(trend='nc', disp=-1)
arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26),
dynamic=True)
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-33-bf138e6ab0e6> in <module>()----> 1 arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26), dynamic=True)
/usr/local/lib/python3.4/dist-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs) 93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:]) 94 elif how:---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how) 96 return obj 97
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, dynamic) 1457 1458 def predict(self, start=None, end=None, exog=None, dynamic=False):-> 1459 return self.model.predict(self.params, start, end, exog, dynamic) 1460 predict.__doc__ = _arma_results_predict 1461
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _get_predict_start(self, start, dynamic) 652 #elif 'mle' not in method or dynamic: # should be on a date 653 start = _validate(start, k_ar, k_diff, self.data.dates,--> 654 method) 655 start = super(ARMA, self)._get_predict_start(start) 656 _check_arima_start(start, k_ar, k_diff, method, dynamic)
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/base/datetools.py in _index_date(date, dates) 40 return dates.indexMap[date] 41 else:---> 42 date = dates.get_loc(date) 43 try: # pandas 0.8.0 returns a boolean array 44 len(date)
AttributeError: 'NoneType' object has no attribute 'get_loc'
Are you using statsmodels 0.6.1?
Based on a quick look at the traceback, I guess I fixed this a short time
ago.
The end date couldn't be a datetime index, but string and others were
accepted., IIRC.
Josef
I was using 0.6.1, but I upgraded to '0.8.0.dev0+20f219a' from GitHub
earlier this evening to see if that fixed it.
I'm also using Pandas '0.17.0'.
Using strings in the form below still gives the same error:
arma_res.predict(start="2013-9-26", end="2013-10-26", dynamic=True)
Post by j***@gmail.com
Post by Anjum Sayed
Hi, i'm trying to use an ARMA model to predict out of sample. In the
predict method, i've used datetime objects to specify the start and end
dates, but it throws an error. Does anyone know what i'm doing wrong?
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from datetime import datetime
from statsmodels.tsa.arima_process import arma_generate_sample
%matplotlib inline
df = pd.DataFrame()
nobs = 1000
arparams = np.r_[1, -np.array([0.6])]
maparams = np.r_[1, np.array([-0.5])]
df["ARMA_11"] = arma_generate_sample(arparams, maparams, nobs)
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
arma_mod = sm.tsa.ARMA(df["ARMA_11"].values, order=(1,1))
arma_res = arma_mod.fit(trend='nc', disp=-1)
arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26),
dynamic=True)
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-33-bf138e6ab0e6> in <module>()----> 1 arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26), dynamic=True)
/usr/local/lib/python3.4/dist-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs) 93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:]) 94 elif how:---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how) 96 return obj 97
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, dynamic) 1457 1458 def predict(self, start=None, end=None, exog=None, dynamic=False):-> 1459 return self.model.predict(self.params, start, end, exog, dynamic) 1460 predict.__doc__ = _arma_results_predict 1461
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _get_predict_start(self, start, dynamic) 652 #elif 'mle' not in method or dynamic: # should be on a date 653 start = _validate(start, k_ar, k_diff, self.data.dates,--> 654 method) 655 start = super(ARMA, self)._get_predict_start(start) 656 _check_arima_start(start, k_ar, k_diff, method, dynamic)
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/base/datetools.py in _index_date(date, dates) 40 return dates.indexMap[date] 41 else:---> 42 date = dates.get_loc(date) 43 try: # pandas 0.8.0 returns a boolean array 44 len(date)
AttributeError: 'NoneType' object has no attribute 'get_loc'
Are you using statsmodels 0.6.1?
Based on a quick look at the traceback, I guess I fixed this a short time
ago.
The end date couldn't be a datetime index, but string and others were
accepted., IIRC.
Josef
Post by Anjum Sayed
I was using 0.6.1, but I upgraded to '0.8.0.dev0+20f219a' from GitHub
earlier this evening to see if that fixed it.
I'm also using Pandas '0.17.0'.
arma_res.predict(start="2013-9-26", end="2013-10-26", dynamic=True)
Post by j***@gmail.com
Post by Anjum Sayed
Hi, i'm trying to use an ARMA model to predict out of sample. In the
predict method, i've used datetime objects to specify the start and end
dates, but it throws an error. Does anyone know what i'm doing wrong?
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from datetime import datetime
from statsmodels.tsa.arima_process import arma_generate_sample
%matplotlib inline
df = pd.DataFrame()
nobs = 1000
arparams = np.r_[1, -np.array([0.6])]
maparams = np.r_[1, np.array([-0.5])]
df["ARMA_11"] = arma_generate_sample(arparams, maparams, nobs)
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
arma_mod = sm.tsa.ARMA(df["ARMA_11"].values, order=(1,1))
arma_res = arma_mod.fit(trend='nc', disp=-1)
arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26),
dynamic=True)
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-33-bf138e6ab0e6> in <module>()----> 1 arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26), dynamic=True)
/usr/local/lib/python3.4/dist-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs) 93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:]) 94 elif how:---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how) 96 return obj 97
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, dynamic) 1457 1458 def predict(self, start=None, end=None, exog=None, dynamic=False):-> 1459 return self.model.predict(self.params, start, end, exog, dynamic) 1460 predict.__doc__ = _arma_results_predict 1461
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _get_predict_start(self, start, dynamic) 652 #elif 'mle' not in method or dynamic: # should be on a date 653 start = _validate(start, k_ar, k_diff, self.data.dates,--> 654 method) 655 start = super(ARMA, self)._get_predict_start(start) 656 _check_arima_start(start, k_ar, k_diff, method, dynamic)
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/base/datetools.py in _index_date(date, dates) 40 return dates.indexMap[date] 41 else:---> 42 date = dates.get_loc(date) 43 try: # pandas 0.8.0 returns a boolean array 44 len(date)
AttributeError: 'NoneType' object has no attribute 'get_loc'
Are you using statsmodels 0.6.1?
Based on a quick look at the traceback, I guess I fixed this a short time
ago.
The end date couldn't be a datetime index, but string and others were
accepted., IIRC.
Josef
Thanks for providing a working example
right now I use
'0.15.2'
in my current python
if you use `df.values`, then you drop the time index, and ARMA doesn't have
the dates information (so dates is None)
Using
arma_mod = ARMA(df, order=(1,1))
with your df that has the date index, works with your string end date, but
it fails on the datetime.datetime forecast dates. So that is still not
supported yet.
using pandas DatetimeIndex Timestamp for start and end works with master
(IIRC that's the one I fixed
there is also pandas datetime which doesn't work either
Post by Anjum Sayed
Post by j***@gmail.com
Post by Anjum Sayed
arma_res.predict(start="2013-9-26", end="2013-10-26", dynamic=True)
2013-09-26 7.545560e-02
2013-09-27 3.213861e-02
...
2013-10-25 1.344071e-12
2013-10-26 5.724766e-13
Freq: D, dtype: float64
Post by Anjum Sayed
Post by j***@gmail.com
Post by Anjum Sayed
arma_res.predict(start="2013-9-26", end=pd.datetime(2013,10,26),
dynamic=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\base\wrapper.py",
line 95, in wrapper
obj = data.wrap_output(func(results, *args, **kwargs), how)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\arima_model.py",
line 1459, in predict
return self.model.predict(self.params, start, end, exog, dynamic)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\arima_model.py",
line 720, in predict
end, out_of_sample = self._get_predict_end(end, dynamic)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\arima_model.py",
line 661, in _get_predict_end
return super(ARMA, self)._get_predict_end(end)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\base\tsa_model.py",
line 214, in _get_predict_end
raise ValueError("no rule for interpreting end")
ValueError: no rule for interpreting end
Timestamp('2013-10-26 00:00:00')
end=pd.DatetimeIndex([pd.datetime(2013,10,26)])[0], dynamic=True)
2013-09-26 7.545560e-02
2013-09-27 3.213861e-02
2013-09-28 1.368871e-02
2013-09-29 5.830397e-03
2013-09-30 2.483326e-03
I'm just doing minimal debugging and bugfixing, I never tried to understand
all the different parts of Dates and pandas's datetime handling.
We need a github issue for whatever still needs to be added to support the
different versions of how to specify dates.
Josef
Thanks for your help Josef, it's working now. I think the df.values came
from when I was trying to emulate an example from somewhere. Using just the
df makes more sense to maintain the time stamps. Thanks again!
Post by j***@gmail.com
Post by Anjum Sayed
I was using 0.6.1, but I upgraded to '0.8.0.dev0+20f219a' from GitHub
earlier this evening to see if that fixed it.
I'm also using Pandas '0.17.0'.
arma_res.predict(start="2013-9-26", end="2013-10-26", dynamic=True)
Post by j***@gmail.com
Post by Anjum Sayed
Hi, i'm trying to use an ARMA model to predict out of sample. In the
predict method, i've used datetime objects to specify the start and end
dates, but it throws an error. Does anyone know what i'm doing wrong?
import pandas as pd
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt
from datetime import datetime
from statsmodels.tsa.arima_process import arma_generate_sample
%matplotlib inline
df = pd.DataFrame()
nobs = 1000
arparams = np.r_[1, -np.array([0.6])]
maparams = np.r_[1, np.array([-0.5])]
df["ARMA_11"] = arma_generate_sample(arparams, maparams, nobs)
df.set_index(pd.date_range('1/1/2011', periods=nobs, freq='D'))
arma_mod = sm.tsa.ARMA(df["ARMA_11"].values, order=(1,1))
arma_res = arma_mod.fit(trend='nc', disp=-1)
arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26),
dynamic=True)
---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-33-bf138e6ab0e6> in <module>()----> 1 arma_res.predict(start=datetime(2013,9,26), end=datetime(2013,10,26), dynamic=True)
/usr/local/lib/python3.4/dist-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs) 93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:]) 94 elif how:---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how) 96 return obj 97
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in predict(self, start, end, exog, dynamic) 1457 1458 def predict(self, start=None, end=None, exog=None, dynamic=False):-> 1459 return self.model.predict(self.params, start, end, exog, dynamic) 1460 predict.__doc__ = _arma_results_predict 1461
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/arima_model.py in _get_predict_start(self, start, dynamic) 652 #elif 'mle' not in method or dynamic: # should be on a date 653 start = _validate(start, k_ar, k_diff, self.data.dates,--> 654 method) 655 start = super(ARMA, self)._get_predict_start(start) 656 _check_arima_start(start, k_ar, k_diff, method, dynamic)
/usr/local/lib/python3.4/dist-packages/statsmodels/tsa/base/datetools.py in _index_date(date, dates) 40 return dates.indexMap[date] 41 else:---> 42 date = dates.get_loc(date) 43 try: # pandas 0.8.0 returns a boolean array 44 len(date)
AttributeError: 'NoneType' object has no attribute 'get_loc'
Are you using statsmodels 0.6.1?
Based on a quick look at the traceback, I guess I fixed this a short
time ago.
The end date couldn't be a datetime index, but string and others were
accepted., IIRC.
Josef
Thanks for providing a working example
right now I use
'0.15.2'
in my current python
if you use `df.values`, then you drop the time index, and ARMA doesn't
have the dates information (so dates is None)
Using
arma_mod = ARMA(df, order=(1,1))
with your df that has the date index, works with your string end date, but
it fails on the datetime.datetime forecast dates. So that is still not
supported yet.
using pandas DatetimeIndex Timestamp for start and end works with master
(IIRC that's the one I fixed
there is also pandas datetime which doesn't work either
Post by Anjum Sayed
Post by j***@gmail.com
Post by Anjum Sayed
arma_res.predict(start="2013-9-26", end="2013-10-26", dynamic=True)
2013-09-26 7.545560e-02
2013-09-27 3.213861e-02
...
2013-10-25 1.344071e-12
2013-10-26 5.724766e-13
Freq: D, dtype: float64
Post by Anjum Sayed
Post by j***@gmail.com
Post by Anjum Sayed
arma_res.predict(start="2013-9-26", end=pd.datetime(2013,10,26),
dynamic=True)
File "<stdin>", line 1, in <module>
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\base\wrapper.py",
line 95, in wrapper
obj = data.wrap_output(func(results, *args, **kwargs), how)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\arima_model.py",
line 1459, in predict
return self.model.predict(self.params, start, end, exog, dynamic)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\arima_model.py",
line 720, in predict
end, out_of_sample = self._get_predict_end(end, dynamic)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\arima_model.py",
line 661, in _get_predict_end
return super(ARMA, self)._get_predict_end(end)
File
"m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\base\tsa_model.py",
line 214, in _get_predict_end
raise ValueError("no rule for interpreting end")
ValueError: no rule for interpreting end
Timestamp('2013-10-26 00:00:00')
end=pd.DatetimeIndex([pd.datetime(2013,10,26)])[0], dynamic=True)
2013-09-26 7.545560e-02
2013-09-27 3.213861e-02
2013-09-28 1.368871e-02
2013-09-29 5.830397e-03
2013-09-30 2.483326e-03
I'm just doing minimal debugging and bugfixing, I never tried to
understand all the different parts of Dates and pandas's datetime handling.
We need a github issue for whatever still needs to be added to support the
different versions of how to specify dates.
Josef