29.1 平稳性
- 平稳性就是要求经由样本时间序列所得到的拟合曲线在未来的一段期间内仍能
顺着现有的形态“惯性”地延续下去 - 平稳性要求序列的均值和方差不发生明显变化
29.2 严平稳与弱平稳
- 严平稳:严平稳表示的分布不随时间的改变而改变。
- 如:白噪声(正态),无论怎么取,都是期望为0,方差为1
- 弱平稳:期望与相关系数(依赖性)不变
- 未来某时刻的t的值Xt就要依赖于它的过去信息,所以需要依赖性
29.3 差分法:时间序列在t与t-1时刻的差值
29.4 自回归模型(AR)
- 描述当前值与历史值之间的关系,用变量自身的历史时间数据对自身进行预测
- 自回归模型必须满足平稳性的要求
- p阶自回归过程的公式定义:
29.4.1 自回归模型的限制
- 自回归模型是用自身的数据来进行预测
- 必须具有平稳性
- 必须具有自相关性,如果自相关系数(φi)小于0.5,则不宜采用
- 自回归只适用于预测与自身前期相关的现象
29.5 移动平均模型(MA)
-
移动平均模型关注的是自回归模型中的误差项的累加
-
q阶自回归过程的公式定义:
-
移动平均法能有效地消除预测中的随机波动
29.5.1 自回归移动平均模型(ARMA)
- 自回归与移动平均的结合
- 公式定义:
- ARIMA(p,d,q)模型全称为差分自回归移动平均模型 (Autoregressive Integrated Moving Average Model,简记ARIMA)
- AR是自回归, p为自回归项; MA为移动平均
- q为移动平均项数,d为时间序列成为平稳时所做的差分次数
- 原理:将非平稳时间序列转化为平稳时间序列然后将因变量
- 仅对它的滞后值以及随机误差项的现值和滞后值进行回归所建立的模型
- AR是自回归, p为自回归项; MA为移动平均
29.6 自相关函数ACF(autocorrelation function)
-
有序的随机变量序列与其自身相比较,自相关函数反映了同一序列在不同时序的取值之间的相关性
-
公式:
-
Pk的取值范围为[-1,1]
29.7 偏自相关函数(PACF)(partial autocorrelation function)
- 对于一个平稳AR§模型,求出滞后k自相关系数p(k)时
- 实际上得到并不是x(t)与x(t-k)之间单纯的相关关系
- x(t)同时还会受到中间k-1个随机变量x(t-1)、x(t-2)、……、x(t-k+1)的影响
- 而这k-1个随机变量又都和x(t-k)具有相关关系
- 所以自相关系数p(k)里实际掺杂了其他变量对x(t)与x(t-k)的影响
- 剔除了中间k-1个随机变量x(t-1)、x(t-2)、……、x(t-k+1)的干扰之后 x(t-k)对x(t)影响的相关程度。
- ACF还包含了其他变量的影响
- 而偏自相关系数PACF是严格这两个变量之间的相关性
29.7.1 ARIMA(p,d,q)阶数确定
截尾:落在置信区间内(95%的点都符合该规则)
– AR§ 看PACF
– MA(q) 看ACF
29.7.2 ARIMA建模流程
- 将序列平稳(差分法确定d)
- p和q阶数确定:ACF与PACF
- ARIMA(p,d,q)
- 模型选择AIC与BIC: 选择更简单的模型
- AIC:赤池信息准则(Akaike Information Criterion,AIC)
- 𝐴𝐼𝐶 = 2𝑘 − 2ln(𝐿)
- BIC:贝叶斯信息准则(Bayesian Information Criterion,BIC)
- 𝐵𝐼𝐶 = 𝑘𝑙𝑛 𝑛 − 2ln(𝐿)
- k为模型参数个数,n为样本数量,L为似然函数
- 模型残差检验:
- ARIMA模型的残差是否是平均值为0且方差为常数的正态分布
- QQ图:线性即正态分布
29.8 实验
读取数据并画图分析平稳性
In:
import pandas as pd
In:
milk = pd.read_csv("../../data/milkproduction.csv",index_col=0)
In:
#1975年后的数据做测试
milk_train = milk['1962-01':'1974-12']
milk_test = milk['1975-01':]
In:
milk_train.plot(kind='line')
out:
<matplotlib.axes._subplots.AxesSubplot at 0x2328f871448>
In:
#差分处理,d=1
milk_train_1d = milk_train.diff().dropna()
milk_train_1d.plot()
out:
<matplotlib.axes._subplots.AxesSubplot at 0x2328fb40208>
In:
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
#自相关图, q=5
plot_acf(milk_train_1d)
In:
plot_pacf(milk_train_1d) #p=3
In:
#参数调优
p,d,q = 1,1,1
bic_list =[]
from statsmodels.tsa.arima_model import ARIMA
for p in range(1,10):
for q in range(1,10):
dic1 = {}
mk_model = ARIMA(milk_train_1d,order=(p,d,q)).fit()
dic1['p'] = p
dic1['q'] = q
dic1['bic'] = mk_model.bic #越小越好
bic_list.append(dic1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-d3709f5cd9af> in <module>
6 for q in range(1,10):
7 dic1 = {}
----> 8 mk_model = ARIMA(milk_train_1d,order=(p,d,q)).fit()
9 dic1['p'] = p
10 dic1['q'] = q
D:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in fit(self, start_params, trend, method, transparams, solver, maxiter, full_output, disp, callback, start_ar_lags, **kwargs)
1147 method, transparams, solver,
1148 maxiter, full_output, disp,
-> 1149 callback, start_ar_lags, **kwargs)
1150 normalized_cov_params = None # TODO: fix this?
1151 arima_fit = ARIMAResults(self, mlefit._results.params,
D:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in fit(self, start_params, trend, method, transparams, solver, maxiter, full_output, disp, callback, start_ar_lags, **kwargs)
936 else: # estimate starting parameters
937 start_params = self._fit_start_params((k_ar, k_ma, k), method,
--> 938 start_ar_lags)
939
940 if transparams: # transform initial parameters to ensure invertibility
D:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in _fit_start_params(self, order, method, start_ar_lags)
552 func = lambda params: -self.loglike_css(params)
553 #start_params = [.1]*(k_ar+k_ma+k_exog) # different one for k?
--> 554 start_params = self._fit_start_params_hr(order, start_ar_lags)
555 if self.transparams:
556 start_params = self._invtransparams(start_params)
D:\ProgramData\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in _fit_start_params_hr(self, order, start_ar_lags)
538 elif q and not np.all(np.abs(np.roots(np.r_[1, start_params[k + p:]]
539 )) < 1):
--> 540 raise ValueError("The computed initial MA coefficients are not "
541 "invertible\nYou should induce invertibility, "
542 "choose a different model order, or you can\n"
ValueError: The computed initial MA coefficients are not invertible
You should induce invertibility, choose a different model order, or you can
pass your own start_params.
In:
bic_list
out:
[{'p': 1, 'q': 1, 'bic': 1657.0906744033432},
{'p': 1, 'q': 2, 'bic': 1637.7192682193438},
{'p': 1, 'q': 3, 'bic': 1619.5972451163823},
{'p': 1, 'q': 4, 'bic': 1624.441481867638},
{'p': 1, 'q': 5, 'bic': 1593.2070136352102},
{'p': 1, 'q': 6, 'bic': 1582.0278211660034},
{'p': 1, 'q': 7, 'bic': 1571.3063299722721},
{'p': 1, 'q': 8, 'bic': 1559.4712033030812},
{'p': 1, 'q': 9, 'bic': 1541.5832303313707},
{'p': 2, 'q': 1, 'bic': 1650.3731599473097},
{'p': 2, 'q': 2, 'bic': 1633.851836306331},
{'p': 2, 'q': 3, 'bic': 1612.1590672147997},
{'p': 2, 'q': 4, 'bic': 1616.26148503598},
{'p': 2, 'q': 5, 'bic': 1592.1094735165811},
{'p': 2, 'q': 6, 'bic': 1575.1861228888317},
{'p': 2, 'q': 7, 'bic': 1568.9905763065128},
{'p': 2, 'q': 8, 'bic': 1560.3067296507938},
{'p': 2, 'q': 9, 'bic': 1538.3145038066534},
{'p': 3, 'q': 1, 'bic': 1650.5452994424288},
{'p': 3, 'q': 2, 'bic': 1633.1923944259408},
{'p': 3, 'q': 3, 'bic': 1588.8745458361975},
{'p': 3, 'q': 4, 'bic': 1617.86368898235},
{'p': 3, 'q': 5, 'bic': 1622.6955273532042},
{'p': 3, 'q': 6, 'bic': 1568.632101808397},
{'p': 3, 'q': 7, 'bic': 1570.857051179361}]