コード例 #1
0
 def arma_forecast(self, ts,  p, q,):
     arma = ARMA(ts, order=(p, q)).fit(disp=-1)
     ts_predict = arma.predict()
     next_ret = arma.forecast(1)[0]
     #print("Forecast stock extra return of next day: ", next_ret)
     # plt.clf()
     # plt.plot(ts_predict, label="Predicted")
     # plt.plot(ts, label="Original")
     # plt.legend(loc="best")
     # plt.title("AR Test {},{}".format(p, q))
     # #plt.show()
     return next_ret, arma.summary2()
コード例 #2
0
D_data.plot()  # 时序图
plt.show()
plot_acf(D_data).show()  # 自相关图
plt.show()
plot_pacf(D_data).show()  # 偏自相关图
plt.show()
print(u'1阶差分序列的ADF检验结果为:', ADF(D_data[u'dst差分']))
print(u'差分序列的白噪声检验结果为:', acorr_ljungbox(D_data, lags=1))

data[u'dst'] = data[u'dst'].astype(float)
pmax = int(len(data) / 10)
qmax = int(len(data) / 10)
bic_matrix = []
for p in range(pmax + 1):
    tmp = []
    for q in range(qmax + 1):
        try:
            tmp.append(ARMA(data, (p, q)).fit().bic)
        except:
            tmp.append(None)
    bic_matrix.append(tmp)
bic_matrix = pd.DataFrame(bic_matrix)  # 从中可以找出最小值
#  print(bic_matrix)
p, q = bic_matrix.stack().idxmin()
print(u'bic最小的P值和q值为:%s、%s' % (p, q))

model = ARMA(data, (p, q)).fit()
model.summary2()  # 给出一份模型报告
forecast = model.forecast(5)  # 作为期5天的预测,返回预测结果、标准误差、置信区间
print(forecast)
コード例 #3
0
        tmp_data.dropna(inplace=True)
    return tmp_data


diffed_ts = diff_ts(dta_log,d=[1,1])
test_stationarity.testStationarity(diffed_ts)
test_stationarity.draw_acf_pacf(diffed_ts,l=31) 
model = arima_model(diffed_ts)
pdb.set_trace()
model.get_proper_model()
print 'bic:',model.bic,'p:',model.p,'q:',model.q
print model.properModel.forecast()[0]
# print model.forecast_next_day_value(type='day')

model2=ARMA(diffed_ts,(model.p,1,model.q)).fit()
model2.summary2()
predict_sunspots = model2.predict('2090','2100',dynamic=True)
a = model2.forecast(5)[0]
a_ts = predict_diff_recover(a,d=[1,1])
log_a = np.exp(a_ts)


print log_a
pdb.set_trace()



model.certain_model(6,0)

predict_ts = model.properModel.predict()
diff_recover_ts = predict_diff_recover(predict_ts,d=[1,1])
コード例 #4
0
 def arma_forecast(self, ts, p, q):
     arma = ARMA(ts, order=(p, q)).fit(disp=-1)
     # ts_predict = arma.predict()
     next_ret = arma.forecast(1)[0]
     return next_ret, arma.summary2()
コード例 #5
0
    print('predicted=%f, expected=%f' ,predictions[0,i], test[0,i])

from sklearn.metrics import mean_squared_error
error = mean_squared_error(test, predictions)
print('Test MSE: %.3f' % error)

test=np.array(test)
predictions=np.array(predictions)
plt.plot(test[0,:],'r-')
plt.plot(predictions[0,:],'g-')
plt.show()

############接着,使用AR(2)模型
from statsmodels.tsa.arima_model import ARIMA,ARMA
ar_2= ARMA(rishouyi[1:500], order=(2,0)).fit(disp=-1)
print(ar_2.summary2()) #给出一份模型报告

######对AR(2)模型的预测结果进行评估
test=rishouyi[500:505]
predictions_ar2 = ar_2.predict(start=len(mydata), end=len(mydata)+len(test)-1,
                               dynamic=False)
predictions_ar2=np.matrix(predictions_ar2)
test=np.matrix(test)
# print(predictions[0,1])
# print(test[0,1])
#print(test)
for i in range(5):
    #print(test[i])
    print('predicted_ar2=%f, expected=%f' ,predictions_ar2[0,i], test[0,i])

from sklearn.metrics import mean_squared_error
print(table)

#自动取阶p和q 的最大值,即函数里面的max_ar,和max_ma。
#ic 参数表示选用的选取标准,这里设置的为aic,当然也可以用bic。然后函数会算出每个 p和q 组合(这里是(0,0)~(3,3)的AIC的值,取其中最小的。

(p, q) = (sm.tsa.arma_order_select_ic(time_series,
                                      max_ar=3,
                                      max_ma=3,
                                      ic='aic')['aic_min_order'])
print((p, q))

# 6) ARIMA(0,1,1)
# p=0,q=2,一阶差分
p, d, q = (0, 1, 1)
arma_mod = ARMA(time_series, (p, d, q)).fit(disp=-1, method='mle')
summary = (arma_mod.summary2(alpha=.05, float_format="%.8f"))
print(summary)

# 7)白噪声检验
arma_mod = ARMA(time_series, (0, 1, 2)).fit(disp=-1, method='mle')
resid = arma_mod.resid
t = sm.tsa.stattools.adfuller(resid)
output = pd.DataFrame(index=[
    'Test Statistic Value', "p-value", "Lags Used",
    "Number of Observations Used", "Critical Value(1%)", "Critical Value(5%)",
    "Critical Value(10%)"
],
                      columns=['value'])
output['value']['Test Statistic Value'] = t[0]
output['value']['p-value'] = t[1]
output['value']['Lags Used'] = t[2]