利用ARIMA模型来进行时间序列的预测,数据集是雅虎财经的离线股票数据
import pandas as pd import pandas_datareader import datetime import matplotlib.pyplot as plt import seaborn as sns from matplotlib.pylab import style from statsmodels.tsa.arima_model import ARIMA from statsmodels.graphics.tsaplots import plot_acf, plot_pacf style.use(‘ggplot‘) plt.rcParams[‘font.sans-serif‘] = [‘SimHei‘]#用来正常显示中文标签 plt.rcParams[‘axes.unicode_minus‘] = False#用来正常显示负号 stock = pd.read_csv(‘D:/T10yr.csv‘, index_col=0, parse_dates=[0]) # print(stock.head()) stock_week = stock[‘Close‘].resample(‘W-MON‘).mean() stock_train = stock_week[‘2000‘ : ‘2012‘] # print(stock_train) stock_train.plot(figsize=(12, 8)) plt.legend(bbox_to_anchor=(1.25, 0.5)) plt.title(‘Stock Close‘) sns.despine()# 删除上方和右方的边框 # plt.show() #数据差异过大,进行一阶差分 stock_diff = stock_train.diff() stock_diff = stock_diff.dropna() print(stock_diff) plt.figure()#新生成一块画布 plt.plot(stock_diff) plt.title(‘一阶差分‘) # plt.show() #画图ACF确定几阶 acf = plot_acf(stock_diff, lags=20) plt.title(‘ACF‘) # plt.show() #PACF pacf = plot_pacf(stock_diff, lags=20) plt.title(‘PACF‘) # plt.show() #ARIMA model model = ARIMA(stock_train, order=(1, 1, 1), freq=‘W-MON‘) result = model.fit() # print(result.summary()) pred = result.predict(‘20110103‘, ‘20160701‘, dynamic=True, typ=‘levels‘) print(pred) plt.figure(figsize=(6, 6)) plt.plot(pred) plt.plot(stock_week) plt.show()