ADF檢驗
在使用很多時間序列模型的時候,如 ARMA、ARIMA,都會要求時間序列是平穩的,所以一般在研究一段時間序列的時候,第一步都需要進行平穩性檢驗,除了用肉眼檢測的方法,另外比較常用的嚴格的統計檢驗方法就是ADF檢驗,也叫做單位根檢驗。
ADF檢驗全稱是 Augmented Dickey-Fuller test,顧名思義,ADF是 Dickey-Fuller檢驗的增廣形式。DF檢驗只能應用於一階情況,當序列存在高階的滯後相關時,可以使用ADF檢驗,所以說ADF是對DF檢驗的擴展。
單位根(unit root)在做ADF檢驗,也就是單位根檢驗時,需要先明白一個概念,也就是要檢驗的對象——單位根。
當一個自回歸過程中: ,如果滯後項係數b為1,就稱為單位根。當單位根存在時,自變量和因變量之間的關係具有欺騙性,因為殘差序列的任何誤差都不會隨著樣本量(即時期數)增大而衰減,也就是說模型中的殘差的影響是永久的。這種回歸又稱作偽回歸。如果單位根存在,這個過程就是一個隨機漫步(random walk)。
ADF檢驗的原理ADF檢驗就是判斷序列是否存在單位根:如果序列平穩,就不存在單位根;否則,就會存在單位根。
所以,ADF檢驗的 H0 假設就是存在單位根,如果得到的顯著性檢驗統計量小於三個置信度(10%,5%,1%),則對應有(90%,95,99%)的把握來拒絕原假設。
ADF檢驗的python實現ADF檢驗可以通過python中的 statsmodels 模塊,這個模塊提供了很多統計模型。
使用方法如下:
導入adfuller函數
from statsmodels.tsa.stattools import adfuller
adfuller函數的參數意義分別是:
x:一維的數據序列。 maxlag:最大滯後數目。 regression:回歸中的包含項(c:只有常數項,默認;ct:常數項和趨勢項;ctt:常數項,線性二次項;nc:沒有常數項和趨勢項) autolag:自動選擇滯後數目(AIC:赤池信息準則,默認;BIC:貝葉斯信息準則;t-stat:基於maxlag,從maxlag開始並刪除一個滯後直到最後一個滯後長度基於 t-statistic 顯著性小於5%為止;None:使用maxlag指定的滯後) store:True False,默認。 regresults:True 完整的回歸結果將返回。False,默認。返回值意義為:
adf:Test statistic,T檢驗,假設檢驗值。 pvalue:假設檢驗結果。 usedlag:使用的滯後階數。 nobs:用於ADF回歸和計算臨界值用到的觀測值數目。 icbest:如果autolag不是None的話,返回最大的信息準則值。 resstore:將結果合併為一個dummy。def adfuller(x, maxlag=None, regression="c", autolag='AIC', store=False, regresults=False): """ Augmented Dickey-Fuller unit root test The Augmented Dickey-Fuller test can be used to test for a unit root in a univariate process in the presence of serial correlation. Parameters x : array_like, 1d data series maxlag : int Maximum lag which is included in test, default 12*(nobs/100)^{1/4} regression : {'c','ct','ctt','nc'} Constant and trend order to include in regression * 'c' : constant only (default) * 'ct' : constant and trend * 'ctt' : constant, and linear and quadratic trend * 'nc' : no constant, no trend autolag : {'AIC', 'BIC', 't-stat', None} * if None, then maxlag lags are used * if 'AIC' (default) or 'BIC', then the number of lags is chosen to minimize the corresponding information criterion * 't-stat' based choice of maxlag. Starts with maxlag and drops a lag until the t-statistic on the last lag length is significant using a 5%-sized test store : bool If True, then a result instance is returned additionally to the adf statistic. Default is False regresults : bool, optional If True, the full regression results are returned. Default is False Returns -- adf : float Test statistic pvalue : float MacKinnon's approximate p-value based on MacKinnon (1994, 2010) usedlag : int Number of lags used nobs : int Number of observations used for the ADF regression and calculation of the critical values critical values : dict Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010) icbest : float The maximized information criterion if autolag is not None. resstore : ResultStore, optional A dummy class with results attached as attributes """
現在我們用一個RB1309的收盤數據來進行ADF檢驗,看一下結果:
result = adfuller(rb_price)print(result)(-0.45153867687808574, 0.9011315454402649, 1, 198, {'5%': -2.876250632135043, '1%': -3.4638151713286316, '10%': -2.574611347821651}, 1172.4579344852016)
看到 t-statistic 的值 -0.451 要大於10%,所以無法拒絕原假設,另外,p-value的值也很大。
將數據進行一階差分滯後,看一下結果如何:
rb_price = np.diff(rb_price)result = adfuller(rb_price)print(result)(-15.436034211511204, 2.90628134201655e-28, 0, 198, {'5%': -2.876250632135043, '1%': -3.4638151713286316, '10%': -2.574611347821651}, 1165.1556545612445)
看到 t-statistic 的值 -15 要小於5%,所以拒絕原假設,另外,p-value的值也很小。