使用ARMA对沪指进行拟合(python)

历年沪指下载地址
在这里插入图片描述
初步查看趋势,季节等影响因素:

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

在这里插入图片描述

代码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝翔厨师长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值