python怎么實現(xiàn)ADF檢驗

本篇內(nèi)容介紹了“python怎么實現(xiàn)ADF檢驗”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

在宜豐等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需設(shè)計網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站制作,宜豐網(wǎng)站建設(shè)費用合理。

ADF檢驗

在使用很多時間序列模型的時候,如 ARMA、ARIMA,都會要求時間序列是平穩(wěn)的,所以一般在研究一段時間序列的時候,第一步都需要進行平穩(wěn)性檢驗,除了用肉眼檢測的方法,另外比較常用的嚴格的統(tǒng)計檢驗方法就是ADF檢驗,也叫做單位根檢驗。

ADF檢驗全稱是 Augmented Dickey-Fuller test,顧名思義,ADF是 Dickey-Fuller檢驗的增廣形式。DF檢驗只能應(yīng)用于一階情況,當(dāng)序列存在高階的滯后相關(guān)時,可以使用ADF檢驗,所以說ADF是對DF檢驗的擴展。
單位根(unit root)

在做ADF檢驗,也就是單位根檢驗時,需要先明白一個概念,也就是要檢驗的對象——單位根。

當(dāng)一個自回歸過程中:y_{t} = by_{t-1} + a + \epsilon _{t} ,如果滯后項系數(shù)b為1,就稱為單位根。當(dāng)單位根存在時,自變量和因變量之間的關(guān)系具有欺騙性,因為殘差序列的任何誤差都不會隨著樣本量(即時期數(shù))增大而衰減,也就是說模型中的殘差的影響是永久的。這種回歸又稱作偽回歸。如果單位根存在,這個過程就是一個隨機漫步(random walk)。

ADF檢驗的原理

ADF檢驗就是判斷序列是否存在單位根:如果序列平穩(wěn),就不存在單位根;否則,就會存在單位根。

所以,ADF檢驗的 H0 假設(shè)就是存在單位根,如果得到的顯著性檢驗統(tǒng)計量小于三個置信度(10%,5%,1%),則對應(yīng)有(90%,95,99%)的把握來拒絕原假設(shè)。

ADF檢驗的python實現(xiàn)

ADF檢驗可以通過python中的 statsmodels 模塊,這個模塊提供了很多統(tǒng)計模型。

使用方法如下:

導(dǎo)入adfuller函數(shù)

from statsmodels.tsa.stattools import adfuller

adfuller函數(shù)的參數(shù)意義分別是:
1、x:一維的數(shù)據(jù)序列。
2、maxlag:最大滯后數(shù)目。
3、regression:回歸中的包含項(c:只有常數(shù)項,默認;ct:常數(shù)項和趨勢項;ctt:常數(shù)項,線性二次項;nc:沒有常數(shù)項和趨勢項)
4、autolag:自動選擇滯后數(shù)目(AIC:赤池信息準則,默認;BIC:貝葉斯信息準則;t-stat:基于maxlag,從maxlag開始并刪除一個滯后直到最后一個滯后長度基于 t-statistic 顯著性小于5%為止;None:使用maxlag指定的滯后)
5、store:True  False,默認。
6、regresults:True 完整的回歸結(jié)果將返回。False,默認。

返回值意義為:

1、adf:Test statistic,T檢驗,假設(shè)檢驗值。
2、pvalue:假設(shè)檢驗結(jié)果。
3、usedlag:使用的滯后階數(shù)。
4、nobs:用于ADF回歸和計算臨界值用到的觀測值數(shù)目。
5、icbest:如果autolag不是None的話,返回最大的信息準則值。
6、resstore:將結(jié)果合并為一個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
    """

 現(xiàn)在我們用一個RB1309的收盤數(shù)據(jù)來進行ADF檢驗,看一下結(jié)果:

python怎么實現(xiàn)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%的顯著性水平,所以無法拒絕原假設(shè),另外,p-value的值也很大。

將數(shù)據(jù)進行一階差分滯后,看一下結(jié)果如何:

python怎么實現(xiàn)ADF檢驗

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%,所以拒絕原假設(shè),另外,p-value的值也很小,所以說明一階差分之后的數(shù)據(jù)是平穩(wěn)的。

“python怎么實現(xiàn)ADF檢驗”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

新聞名稱:python怎么實現(xiàn)ADF檢驗
當(dāng)前路徑:http://bm7419.com/article38/goeopp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、Google、響應(yīng)式網(wǎng)站、手機網(wǎng)站建設(shè)、網(wǎng)站設(shè)計小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管