1 数据说明
“Shibor.xlsx”数据中包含了2020/1/2到2020/12/31的上海银行间同业拆放利率(shibor),其中第2列是隔夜利率,我们采用ARMA模型来构建shibor隔夜利率的预测模型。
2 导入模块
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.graphics.tsaplots import plot_predict
3 导入数据
data = pd.read_excel('D:\\Desktop\\Shibor.xlsx', sheet_name='Sheet1', na_values='n/a')
data_balance = data.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
print(data_balance)
shibor=pd.Series(data_balance['O/N'].values, index=data_balance['日期']) # 由于隔夜利率不是连续的日度数据,所以不指定shibor的Index的频率。
4 单位根检验
shibor.plot()
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决中文显示乱码问题!
plt.rc('axes', unicode_minus=False) # 解决坐标轴负号显示乱码问题!
plt.show()
shibor隔夜利率的走势图呈现平稳的态势(围绕一个固定值上下波动)。
print(sm.tsa.adfuller(shibor, regression='nc'))
print(sm.tsa.adfuller(shibor, regression='c'))
print(sm.tsa.adfuller(shibor, regression='ct'))
单位根检验中,“有截距无趋势项形式:”统计量的P值为0.00946小于0.05,因而表明原始序列平稳。可以直接构建ARMA模型。
- 无截距无趋势项形式:
(-0.9849737939938005, 0.2941462468205783, 3, 245, {'1%': -2.5749261391087046, '5%': -1.9421502633766543, '10%': -1.6157794081377657}, 21.725334688402256)
- 有截距无趋势项形式:
(-3.4466119219297466, 0.009468863200876127, 3, 245, {'1%': -3.4573260719088132, '5%': -2.873410402808354, '10%': -2.573095980841316}, 13.177838508681646)
- 有截距有趋势项形式:
(-3.404279820472794, 0.05081873567750385, 3, 245, {'1%': -3.996204153626465, '5%': -3.428563622657226, '10%': -3.1376703806237196}, 14.444287012418101)
5 ARMA模型形式选择
fig = plt.figure(figsize=(8,4))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(shibor.values.squeeze(), lags=12, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(shibor.values.squeeze(), lags=12, ax=ax2)
plt.show()
根据shibor隔夜利率的自相关和偏自相关图可以大致判断出,ARMA可能的形式是ARMA(2,0)或ARMA(1,1),具体哪个模型更优则需要通过AIC和BIC准则来判断。
arma_mod200 = ARIMA(shibor, order=(2, 0, 0)).fit()
print(arma_mod200.summary())
print(arma_mod200.aic, arma_mod200.bic)
估计结果显示:ARMA(2,0)的AIC和BIC分别为37.062和51.132;
arma_mod101 = ARIMA(shibor, order=(1, 0, 1)).fit()
print(arma_mod101.summary())
print(arma_mod101.aic, arma_mod101.bic)
估计结果显示:ARMA(1,1)的AIC和BIC分别为34.211和48.281;均小于ARMA(2,0)的值,因为认为ARMA(1,1)的模型更优!
6 shibor拟合值的图
fig, ax = plt.subplots(figsize=(10, 8))
fig = plot_predict(arma_mod101, ax=ax)
legend = ax.legend(loc="upper left")
plt.show()
最后,可以绘制出拟合值的走势图,如下所示:
7 shibor预测值的图
real = shibor.reset_index(drop=True)
fct = arma_mod101.forecast(10) # 预测未来10期的值
real.plot()
fct.plot()
plt.show()