时间序列分析之协整检验

协整关系

协整(Cointegration)理论是恩格尔(Engle)和格兰杰(Granger)在1978年提出的。平稳性是进行时间序列分析的一个很重要的前提,很多模型都是基于平稳下进行的,而现实中,很多时间序列都是非平稳的,所以协整是从分析时间序列的非平稳性入手的

协整的内容是:

设序列X_{t}是 d 阶单整的,记为X_{t}\sim I(d),如果存在一个非零向量 \beta 使得Y_{t} = \beta X_{t} \sim I(d-b),则称X_{t}具有 d, b 阶协整关系,记为X_{t}\sim CI(d, b),则 \beta 称为协整向量。

特别当 X_{t} 和 Y_{t} 都是一阶单整时,一般而言,X_{t} 和 Y_{t} 的线性组合 Y_{t} - \beta X_{t} 仍然是一阶单整的,但是对于某些非零向量 \beta ,会使得 Y_{t} - \beta X_{t}\sim I(0),此时非零向量 \beta 称作协整向量,其中每一项  \beta_{t} 为 t 时刻的协整系数。通俗点说,如果两组序列都是非平稳的,但是经过一阶差分后是平稳的,且这两组序列经过某种线性组合也是平稳的,则它们之间就存在协整关系。

协整理论的意义在于:

  • 首先,因为或许单个序列是非平稳的,但是通过协整我们可以建立起两个或者多个序列之间的平稳关系,进而充分应用平稳性的性质。
  • 其次,可以避免伪回归。如果一组非平稳的时间序列不存在协整关系,那么根据它们构造的回归模型就可能是伪回归。
  • 区别变量之间长期均衡关系和短期波动关系。

非平稳序列很容易出现伪回归,而协整的意义就是检验它们的回归方程所描述的因果关系是否是伪回归的,所以常用的协整检验有两种:Engel-Granger 两步协整检验法和 Johansen 协整检验法,它们二者的区别在于 Engler-Granger 采用的是一元方程技术,而 Johansen 则是多元方程技术,所以Johansen 协整检验法受限更小。

Engel-Granger 两步协整检验法

EG检验的方法实际上就是对回归方程的残差进行单位根检验

因为从协整的角度来看,因变量能被自变量的线性组合所解释,说明二者之间具有稳定的均衡关系;因变量不能被自变量解释的部分就构成了一个残差序列,这个残差序列不应该是序列相关的,也就是说残差应该是平稳的。所以EG检验一组变量是否具有协整关系也就是检验残差序列是否是平稳的。

Engle-Granger提出的两步法的步骤如下:

1、用 OLS 估计协整回归方程,从而得到协整系数:

\large : \begin{center} Y_{t} = \beta X_{t} + \epsilon_{t}\end{center}

2、检验 \large \epsilon_{t} 的平稳性,如果 \large \epsilon_{t} 平稳,则 X_{t} , Y_{t} 是协整的,否则不成立。对于\large \epsilon_{t} 平稳性的检验通常用 ADF 检验。

Johansen Test 协整检验法

当协整检验的VAR模型中如果含有多个滞后项时,如下:

\large : \begin{center} Y_{t} = \beta_{1} X_{t} + \beta_{2} X_{t-1} + \beta_{3} X_{t-2} + ...+ \epsilon_{t}\end{center}

采用EG检验就不能找出两个以上的协整向量了,此时可以用 Johansen Test 来进行协整检验,它的思想是采用极大似然估计来检验多变量之间的协整关系。

具体步骤以后填

-------------------------------------------------

用 python 代码进行协整检验

我们从 rb 期货中选择两个品种进行分析,具体的品种根据相关性选择,后期会另外补充。


 
 
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. a_price = pd.read_csv( './CloseA.csv')[: 200]
  5. b_price = pd.read_csv( './CloseB.csv')[: 200]
  6. fig = plt.figure()
  7. ax = fig.add_subplot( 111)
  8. ax.plot(range(len(a_price)), a_price)
  9. ax.plot(range(len(b_price)), b_price)
  10. ax.legend([ 'a', 'b'])
  11. plt.show()

 从图中看,两个品种具有很强的相关性,并且都是不稳定的。

下面,我们通过ADF检验来看一下,两个序列是否是一阶单整的:


 
 
  1. from statsmodels.tsa.stattools import adfuller
  2. a_price = np.reshape(a_price.values, -1)
  3. a_price_diff = np.diff(a_price)
  4. b_price = np.reshape(b_price.values, -1)
  5. b_price_diff = np.diff(b_price)
  6. print(adfuller(a_price_diff))
  7. print(adfuller(b_price_diff))
  8. ( -15.436034211511204, 2.90628134201655e-28, 0, 198, { '1%': -3.4638151713286316, '5%': -2.876250632135043, '10%': -2.574611347821651}, 1165.1556545612445)
  9. ( -14.259156751414892, 1.4365811614283181e-26, 0, 198, { '1%': -3.4638151713286316, '5%': -2.876250632135043, '10%': -2.574611347821651}, 1152.4222884399824)

从结果来看,两个序列都满足一阶单整,下面来判断两者是否存在协整关系。statsmodels 模块中有 coint 函数可以用来检测协整关系,它的内部实现就是基于 EG 协整检验的。

coint 函数如下:


 
 
  1. def coint(y0, y1, trend='c', method='aeg', maxlag=None, autolag='aic',
  2. return_results=None):
  3. """Test for no-cointegration of a univariate equation
  4. The null hypothesis is no cointegration. Variables in y0 and y1 are
  5. assumed to be integrated of order 1, I(1).
  6. This uses the augmented Engle-Granger two-step cointegration test.
  7. Constant or trend is included in 1st stage regression, i.e. in
  8. cointegrating equation.
  9. **Warning:** The autolag default has changed compared to statsmodels 0.8.
  10. In 0.8 autolag was always None, no the keyword is used and defaults to
  11. 'aic'. Use `autolag=None` to avoid the lag search.
  12. Parameters
  13. ----------
  14. y1 : array_like, 1d
  15. first element in cointegrating vector
  16. y2 : array_like
  17. remaining elements in cointegrating vector
  18. trend : str {'c', 'ct'}
  19. trend term included in regression for cointegrating equation
  20. * 'c' : constant
  21. * 'ct' : constant and linear trend
  22. * also available quadratic trend 'ctt', and no constant 'nc'
  23. method : string
  24. currently only 'aeg' for augmented Engle-Granger test is available.
  25. default might change.
  26. maxlag : None or int
  27. keyword for `adfuller`, largest or given number of lags
  28. autolag : string
  29. keyword for `adfuller`, lag selection criterion.
  30. * if None, then maxlag lags are used without lag search
  31. * if 'AIC' (default) or 'BIC', then the number of lags is chosen
  32. to minimize the corresponding information criterion
  33. * 't-stat' based choice of maxlag. Starts with maxlag and drops a
  34. lag until the t-statistic on the last lag length is significant
  35. using a 5%-sized test
  36. return_results : bool
  37. for future compatibility, currently only tuple available.
  38. If True, then a results instance is returned. Otherwise, a tuple
  39. with the test outcome is returned.
  40. Set `return_results=False` to avoid future changes in return.
  41. Returns
  42. -------
  43. coint_t : float
  44. t-statistic of unit-root test on residuals
  45. pvalue : float
  46. MacKinnon's approximate, asymptotic p-value based on MacKinnon (1994)
  47. crit_value : dict
  48. Critical values for the test statistic at the 1 %, 5 %, and 10 %
  49. levels based on regression curve. This depends on the number of
  50. observations.
  51. Notes
  52. -----

 
 
  1. from statsmodels.tsa.stattools import coint
  2. print(coint(a_price, b_price))
  3. ( -3.9532731584015215, 0.008362293067615467, array([ -3.95232129, -3.36700631, -3.06583125]))

 从返回结果可以看出 t-statistic 值要小于1%的置信度,所以有99%的把握拒绝原假设,而且p-value的值也比较小,所以说存在协整关系。

 

Ref :

《统计套利:理论与实战》金志宏著

  • 2
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值