数据获取,预处理,定阶什么的参考前面几篇文章:
金融时间序列分析:3. First Demo By Python
金融时间序列分析:5. AR模型实例(Python)
MA模型的讲解参考前文:
金融时间序列分析:7. MA滑动平均模型
这个和以前那个AR模型基本一样,我也不多说了。
print '---------------------------MA TEST------------------------------'
adf_res = adf_test(ts_diff['2014-01-01':'2015-12-31'])
print 'ADF test results (ema diff):\n', adf_res
ma_predict = draw_ma(ts_diff['2014-01-01':'2015-12-31'], int(adf_res['Lags Used']))
rtn_test(ma_predict, "ma predict")
def draw_ma(ts, w):
ma = ARMA(ts, order=(0, w)).fit(disp = -1)
ts_predict_ma = ma.predict()
ar = ARMA(ts, order=(w,0)).fit(disp=-1)
ts_predict_ar = ar.predict()
plt.clf()
plt.plot(ts_predict_ar, label="AR")
plt.plot(ts_predict_ma, label="MA")
#plt.plot(ts, label = "ORG")
plt.legend(loc="best")
plt.title("MA Test %s" % w)
plt.savefig("./PDF/test_ma_"+ str(w) +".pdf", format='pdf')
return ts_predict_ma
结果如下:
原始数据
abs mean : 0.0118623137237
std var : 0.0172625800497
Minimum value is: -0.0925615434824
Maximum value is: 0.0903425141497
Arithmetic mean is: 0.000195754536494
Unbiased variance is: 0.000297996669973
Biased skewness is: -0.479992664412
Biased kurtosis is: 3.92636403052
AR预测数据
abs mean : 0.00363097137215
std var : 0.00522169036148
Minimum value is: -0.0273872869419
Maximum value is: 0.0262852580925
Arithmetic mean is: 0.0010682950804
Unbiased variance is: 2.72660502312e-05
Biased skewness is: -0.391757274581
Biased kurtosis is: 4.91810357904
MA预测数据
abs mean : 0.00314592965807
std var : 0.00438791465633
Minimum value is: -0.0223893454803
Maximum value is: 0.0186398706818
Arithmetic mean is: 0.00106361911149
Unbiased variance is: 1.92537950312e-05
Biased skewness is: -0.59323473066
Biased kurtosis is: 3.99711788068