使用ARMA的Statsmodel
我刚来这里不久,但正在尝试让一个叫做statsmodel的ARMA预测工具正常工作。我从Yahoo导入了一些股票数据,并让ARMA给我了一些拟合参数。不过,当我使用预测代码时,得到的只是一些我搞不懂的错误信息。我不太确定自己哪里出错了:
import pandas
import statsmodels.tsa.api as tsa
from pandas.io.data import DataReader
start = pandas.datetime(2013,1,1)
end = pandas.datetime.today()
data = DataReader('GOOG','yahoo')
arma =tsa.ARMA(data['Close'], order =(2,2))
results= arma.fit()
results.predict(start=start,end=end)
错误信息是:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Windows\system32\<ipython-input-84-25a9b6bc631d> in <module>()
13 results= arma.fit()
14 results.summary()
---> 15 results.predict(start=start,end=end)
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\base\wrapp
er.pyc in wrapper(self, *args, **kwargs)
88 results = object.__getattribute__(self, '_results')
89 data = results.model.data
---> 90 return data.wrap_output(func(results, *args, **kwargs), how)
91
92 argspec = inspect.getargspec(func)
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\arima_
model.pyc in predict(self, start, end, exog, dynamic)
1265
1266 """
-> 1267 return self.model.predict(self.params, start, end, exog, dynamic
)
1268
1269 def forecast(self, steps=1, exog=None, alpha=.05):
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\arima_
model.pyc in predict(self, params, start, end, exog, dynamic)
497
498 # will return an index of a date
--> 499 start = self._get_predict_start(start, dynamic)
500 end, out_of_sample = self._get_predict_end(end, dynamic)
501 if out_of_sample and (exog is None and self.k_exog > 0):
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\arima_
model.pyc in _get_predict_start(self, start, dynamic)
404 #elif 'mle' not in method or dynamic: # should be on a date
405 start = _validate(start, k_ar, k_diff, self.data.dates,
--> 406 method)
407 start = super(ARMA, self)._get_predict_start(start)
408 _check_arima_start(start, k_ar, k_diff, method, dynamic)
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\arima_
model.pyc in _validate(start, k_ar, k_diff, dates, method)
160 if isinstance(start, (basestring, datetime)):
161 start_date = start
--> 162 start = _index_date(start, dates)
163 start -= k_diff
164 if 'mle' not in method and start < k_ar - k_diff:
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\base\d
atetools.pyc in _index_date(date, dates)
37 freq = _infer_freq(dates)
38 # we can start prediction at the end of endog
---> 39 if _idx_from_dates(dates[-1], date, freq) == 1:
40 return len(dates)
41
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\base\d
atetools.pyc in _idx_from_dates(d1, d2, freq)
70 from pandas import DatetimeIndex
71 return len(DatetimeIndex(start=d1, end=d2,
---> 72 freq = _freq_to_pandas[freq])) - 1
73 except ImportError, err:
74 from pandas import DateRange
D:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7.egg\statsmodels\tsa\base\d
atetools.pyc in __getitem__(self, key)
11 # being lazy, don't want to replace dictionary below
12 def __getitem__(self, key):
---> 13 return get_offset(key)
14 _freq_to_pandas = _freq_to_pandas_class()
15 except ImportError, err:
D:\Python27\lib\site-packages\pandas\tseries\frequencies.pyc in get_offset(name)
484 """
485 if name not in _dont_uppercase:
--> 486 name = name.upper()
487
488 if name in _rule_aliases:
AttributeError: 'NoneType' object has no attribute 'upper'
2 个回答
当我运行你的代码时,出现了以下错误:
“ValueError: 这些日期没有频率,日期2013-01-01 00:00:00不在日期索引中。请尝试使用一个在日期索引中的日期,或者使用一个整数。”
因为交易日期是不规律的(比如假期和周末),所以模型并不能智能地知道该用什么频率来进行计算。
如果你用这些日期在索引中的整数位置来替换日期,那么你就能得到预测结果。然后你可以把原来的索引再放回结果中。
prediction = results.predict(start=0, end=len(data) - 1)
prediction.index = data.index
print(prediction)
2010-01-04 689.507451
2010-01-05 627.085986
2010-01-06 624.256331
2010-01-07 608.133481
...
2017-05-09 933.700555
2017-05-10 931.290023
2017-05-11 927.781427
2017-05-12 929.661014
另外,你可能想在每日收益率上运行这样的模型,而不是直接在原始价格上。直接在原始价格上运行并不能像你想的那样捕捉到趋势和均值回归。你的模型是基于价格的绝对值,而不是价格的变化、趋势、移动平均等其他你可能想用的因素。你创建的预测看起来会不错,因为它们只预测一步,所以不会捕捉到累积误差。这让很多人感到困惑。相对于股票价格的绝对值,误差看起来很小,但模型的预测能力并不强。
我建议你先看看这个入门指南:
http://www.johnwittenauer.net/a-simple-time-series-analysis-of-the-sp-500-index/
看起来这像是个错误。我会去查一下。
https://github.com/statsmodels/statsmodels/issues/712
补充说明: 作为一个临时解决办法,你可以把数据表中的时间索引去掉,然后用一个numpy数组来代替。这样在做预测的时候处理日期会稍微复杂一点,但其实在没有频率的情况下,用日期来预测本来就挺麻烦的,所以只知道开始和结束的日期基本上没有什么意义。
import pandas
import statsmodels.tsa.api as tsa
from pandas.io.data import DataReader
import pandas
data = DataReader('GOOG','yahoo')
dates = data.index
# start at a date on the index
start = dates.get_loc(pandas.datetools.parse("1-2-2013"))
end = start + 30 # "steps"
# NOTE THE .values
arma =tsa.ARMA(data['Close'].values, order =(2,2))
results= arma.fit()
results.predict(start, end)