历年沪指下载地址
初步查看趋势,季节等影响因素:
time_price_data = time_price_data.set_index('日期')
#对趋势、季节性、残差进行画图分析,默认加法模型
res = sm.tsa.seasonal_decompose(time_price_data['收盘价'],freq = 289)
res.plot()
plt.show()
按照月,季度,年来统计,并画图
data_month = time_price_data.resample('M').mean()
data_Q = time_price_data.resample('Q-DEC').mean()
data_year = time_price_data.resample('A-DEC').mean()
#按照月,季度,年来画图
fig = plt.figure(figsize = [20,10])
plt.suptitle('沪指',fontsize = 30)
plt.subplot(221)
plt.plot(time_price_data['收盘价'],label = '按天')
plt.subplot(222)
plt.plot(data_month['收盘价'],label = '按月')
plt.subplot(223)
plt.plot(data_Q['收盘价'],label = '按季度')
plt.subplot(224)
plt.plot(data_year['收盘价'],label = '按年')
plt.show()
ARMA设置参数范围,并寻找aic最小的参数
#设置参数范围
from itertools import product
ps = [i for i in range(5)]
qs = [i for i in range(5)]
parameters = list(product(ps,qs))
from statsmodels.tsa.arima_model import ARMA, ARIMA
#暴露寻最优arma,aic最小
best_aic = float('inf')
for parameter in parameters:
try:
model = ARMA(time_price_data['收盘价'],order = (parameter[0],parameter[1])).fit()
except ValueError:
print('参数错误',parameter)
continue
aic = model.aic
if aic < best_aic:
best_model =model
best_aic = aic
best_parameter = parameter
print(best_parameter,best_aic)
最后进行预测
import numpy as np
data_days2['predict'] = best_model.predict(start = 0,end =data_days2.shape[0] )
data_days2.loc[0,'predict']=np.NaN
data_days2.set_index('日期',inplace = True)
data_days2