300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 自回归移动平均模型(ARMA)-平稳序列

自回归移动平均模型(ARMA)-平稳序列

时间:2023-03-11 10:21:04

相关推荐

自回归移动平均模型(ARMA)-平稳序列

自回归滑动平均模型(ARMA模型,Auto-Regression and Moving Average Model)是研究时间序列的重要方法,由自回归模型(AR模型)与滑动平均模型(MA模型)为基础“混合”而成,具有适用范围广、预测误差小的特点。ARMA原理分析,见此篇博客。

1. 导入python中的相关模块

import tushare as tsimport pandas as pdimport matplotlib.pyplot as pltimport statsmodels.tsa.api as smtsafrom statsmodels.tsa.stattools import adfuller as ADFfrom statsmodels.tsa.arima_model import ARMAfrom statsmodels.stats.diagnostic import acorr_ljungbox as acorrfrom statsmodels.graphics.tsaplots import plot_acf, plot_pacfimport warningswarnings.filterwarnings('ignore')

2. 载入数据集

本模型使用的为某股票公开数据集

ts.set_token('46af5ae83a803c592eeff2620f87402fea47911650b115cf1f671f64')pro = ts.pro_api()data = pro.daily(ts_code='000001.SZ', start_date='0101', end_date='0101')data.sort_values(by='trade_date', inplace=True)data.reset_index(drop='True', inplace=True)

data# 查看data数据

2361 rows × 11 columns

填补data中的缺失值

data = data.iloc[:, 1:]data = data.fillna(method='ffill')# 用前一个非缺失值去填充该缺失值data.head()

data = data[['trade_date', 'open', 'close', 'high', 'low']]data.plot(subplots=True, figsize=(10, 12))plt.title(' zhangshang stock attributes from -01-01 to -01-01')plt.show()

3. 平稳性检验

#平稳性检验adf = ADF(data['close'])if adf[1] > 0.05:# adf[i]表示对data['close']数据进行1阶差分print(u'原始序列经检验不平稳,p值为:%s'%(adf[1]))else:print(u'原始序列经检验平稳,p值为:%s'%(adf[1]))

原始序列经检验平稳,p值为:0.017668059342580877

4. 白噪声检验

p值越大表示数据是随机的可能性越大,随机性很大的数据没有研究意义。

#采用LB统计量的方法进行白噪声检验p = acorr(data['close'], lags=1)if p[1] < 0.05:print(u'原始序列非白噪声序列,p值为:%s'%p[1])else:print(u'原始序列为白噪声序列,p值为:%s'%p[1])

原始序列非白噪声序列,p值为:[0.]

5. 模型识别

# 定义绘图函数plotdsdef plotds (xt, nlag=30, fig_size=(12,8)):if not isinstance(xt, pd.Series): #判断xt是否是pd.Series类型数据,不是则转化为该类型数据xt = pd.Series(xt)plt.figure(figsize=fig_size)plt.plot(xt)# 原始数据时序图plt.title("Time Series")plt.show()plt.figure(figsize=fig_size)layout = (2, 2)ax_acf = plt.subplot2grid(layout, (1, 0))ax_pacf = plt.subplot2grid(layout, (1, 1))plot_acf(xt, lags=nlag, ax=ax_acf)# 自相关图plot_pacf(xt, lags=nlag, ax=ax_pacf)# 偏自相关图plt.show()return None

plotds(data['close'].dropna(), nlag=50)

#定阶data_df = data.copy()aicVal = []for ari in range(1, 3):for maj in range(0,5):try:arma_obj = smtsa.ARMA(data_df.close.tolist(), order=(ari, maj))\.fit(maxlag=30, method='mle', trend='nc')aicVal.append([ari, maj, arma_obj.aic])except Exception as e:print(e)

aicVal

[[1, 0, 1958.0143376009719],[1, 1, 1958.7504776249589],[1, 2, 1960.3388645021796],[1, 3, 1962.0299202559945],[1, 4, 1963.6365504220475],[2, 0, 1958.7847615798437],[2, 1, 1960.5080165518111],[2, 2, 1957.0524190412552],[2, 3, 1959.0187650693679],[2, 4, 1961.0165491508224]]

6. 训练模型

aicVal的结果可以看到模型阶数为(2,2)时,AIC最小为1957.0524190412552,故选择(2,2)阶数的模型。

arma_obj_fin = smtsa.ARMA(data_df['close'].tolist(), order=(2, 2)).fit(maxlag=30, method='mle', trend='nc', disp=False)arma_obj_fin.summary()

7.模型拟合效果

#plot the curvesdata_df["ARMA"] = arma_obj_fin.predict()plt.figure(figsize=(10,8))plt.plot(data_df['close'].iloc[-100:], color='b', label='Actual')plt.plot(data_df["ARMA"].iloc[-100:], color='r', linestyle='--', label='ARMA(2,2)_pre')plt.xlabel('index')plt.ylabel('close price')plt.legend(loc='best')plt.show()

8. 模型预测

fig = arma_obj_fin.plot_predict(len(data_df)-50, len(data_df)+10)

predict = arma_obj_fin.predict(start=1, end=len(data_df)+10)predict[-10:]

array([16.44779647, 16.45696253, 16.4499, 16.4504122 , 16.43622479,16.44388842, 16.43040644, 16.43738965, 16.42456844, 16.43091446])

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。