statsmodels ARMA预测样本外

时间:2015-08-03 08:51:14

标签: python statsmodels

我想预测一个时间序列的回归,我首先拟合了数据集,但是当我预测明天的回归时它并没有起作用。我的代码是

    date = datetime.datetime(2014,12,31)
    todayDate = (date).strftime('%Y-%m-%d')
    startdate = (date - timedelta(days = 1)).strftime('%Y-%m-%d') 
    enddate = (date + timedelta(days = 2)).strftime('%Y-%m-%d')         
    data = get_pricing([symbol],start_date= date1, end_date = todayDate, frequency='daily')
    df =  pd.DataFrame({"value": data.price.values.ravel()},index = data.major_axis.ravel())
    result = df.pct_change().dropna() 

    degree = {}
    for x in range(0,5):
        for y in range(0,5):
            try:
                arma = ARMA(result, (x,y)).fit()
                degree[str(x) +str(y)] = arma.aic

            except:
                continue

    dic= sorted(degree.iteritems(), key = lambda d:d[1])

    p = int(dic[0][0][0])
    q = int(dic[0][0][1])
    arma = ARMA(result, (p,q)).fit()
    predicts = arma.predict()
    exogx = np.array(range(1,4))
    predictofs = arma.predict(startdate,enddate, exogx)

最后一行无效并产生错误

  

ValueError:如果没有提供数据,则必须提供freq参数

我不明白。有人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,因为你的索引缺少Freq参数。如果你打印data.index,你会看到像

这样的东西
  

DatetimeIndex(['2015-06-27','2015-06-29','2015-06-30','2015-07-01',   '2015-07-02','2015-07-03','2015-07-04','2015-07-06','2015-07-07',   '2015-07-08','2015-07-09','2015-07-10','2015-07-11','2015-07-13',   '2015-07-14','2015-07-15','2015-07-16','2015-07-17','2015-07-18',   '2015-07-20','2015-07-21','2015-07-22','2015-07-23','2015-07-24',   '2015-07-25','2015-07-27','2015-07-28','2015-07-29','2015-07-30',   '2015-07-31'],dtype ='datetime64 [ns]',name = u'Date',freq = None)]

注意'Freq = None'

你可以这样做:

data = Series(data.values, data.index)
data = data.asfreq('D')

你也可以通过

来硬指定频率
data.index.freq = 'D'

如果这有点帮助,请告诉我。

如果这不起作用,您只需使用整数进行预测,然后手动填写索引