關(guān)于adfuller函數(shù)返回值的參數(shù)說明與記錄
adfuller函數(shù)返回值的參數(shù)說明
from statsmodels.tsa.stattools import adfuller
t = adfuller(train['total_redeem_amt'])
返回值為(-5.2350403606036302, 7.4536580061930903e-06, 0, 60, {'1%': -3.5443688564814813, '5%': -2.9110731481481484, '10%': -2.5931902777777776}, 1935.4779504450603)
最近在學(xué)習(xí)用ARIMA模型建模處理預(yù)測數(shù)據(jù)的時候遇到的一個用來評測穩(wěn)定性的函數(shù),該函數(shù)可以返回一個數(shù)組,包含五個數(shù)據(jù)。
- 第一個是adt檢驗(yàn)的結(jié)果,也就是t統(tǒng)計量的值。
- 第二個是t統(tǒng)計量的P值。
- 第三個是計算過程中用到的延遲階數(shù)。
- 第四個是用于ADF回歸和計算的觀測值的個數(shù)。
- 第五個是配合第一個一起看的,是在99%,95%,90%置信區(qū)間下的臨界的ADF檢驗(yàn)的值。如果第一個值比第五個值小證明平穩(wěn),反正證明不平穩(wěn)。根據(jù)結(jié)果看出來,你的數(shù)據(jù)不平穩(wěn)。
- 至于第六個數(shù)值就不太明白了,網(wǎng)上也沒有查找到相應(yīng)的資料
查看adfuller()函數(shù)的模型擬合系數(shù)
adfuller()函數(shù)的輸入?yún)?shù)中有regresults一項(xiàng),網(wǎng)上教程中大多數(shù)默認(rèn)設(shè)為False。這個參數(shù)到底有什么用,這里我們研究一下。
adfuller()函數(shù)原型
adfuller()是ADF檢驗(yàn)常用的函數(shù)(還有一個常用函數(shù)為arch.unitroot包中的ADF()函數(shù)),需導(dǎo)入的包為:
import statsmodels.tsa.stattools as ts
其函數(shù)原型為:
t=adfuller(x, maxlag=None, regression='c', autolag='AIC', store=False, regresults=False)
輸入?yún)?shù):
x
:array_like,1d,要測試的數(shù)據(jù)系列。maxlag
: 測試中包含的最大延遲,默認(rèn)為12 *(nobs / 100)^ {1/4}。regression
:{‘c’,‘ct’,‘ctt’,‘nc’}, 包含在回歸中的常量和趨勢順序。‘c’:僅限常量(默認(rèn)值)。 ‘ct’:恒定和趨勢。 ‘ctt’:常數(shù),線性和二次趨勢。 ‘nc’:沒有恒定,沒有趨勢。autolag
: {‘AIC’,‘BIC’,‘t-stat’,None}自動確定滯后時使用的方法。如果為None,則使用maxlag滯后。如果是’AIC’(默認(rèn)值)或’BIC’,則選擇滯后數(shù)以最小化相應(yīng)的信息標(biāo)準(zhǔn)。基于’t-stat’的maxlag選擇。從maxlag開始并使用5%大小的測試來降低延遲,直到最后一個滯后長度的t統(tǒng)計量顯著為止。store
:bool,如果為True,則另外返回adf統(tǒng)計信息的結(jié)果實(shí)例。默認(rèn)值為False。regresults
:bool,optional,如果為True,則返回完整的回歸結(jié)果。默認(rèn)值為False。
返回參數(shù):
ADF
:float,測試統(tǒng)計。pvalue
:float,probability value:MacKinnon基于MacKinnon的近似p值(1994年,2010年)。usedlag
:int,使用的滯后數(shù)量。NOBS
:int,用于ADF回歸和計算臨界值的觀察數(shù)。critical values
:dict,測試統(tǒng)計數(shù)據(jù)的臨界值為1%,5%和10%。基于MacKinnon(2010)。icbest
:float,如果autolag不是None,則最大化信息標(biāo)準(zhǔn)。resstore
:ResultStore, optional,一個虛擬類,其結(jié)果作為屬性附加。
regresults參數(shù)
adfuller()函數(shù)的其他參數(shù),網(wǎng)上的各種教程已經(jīng)將的很清楚了。但是對regresults,卻一直諱莫如深,從函數(shù)原型也看的一頭霧水,搞不清楚這個參數(shù)怎么用的。首先通過兩段代碼看看regresults參數(shù)對輸出結(jié)果的影響。
regresults=False:
r=ts.adfuller(data,12,'ctt',regresults=False) print(r)
輸出結(jié)果:
(-1.6596695973336932, 0.9169218489129718, 0, 230, {'1%': -4.422218041176954, '5%': -3.8583127840881075, '10%': -3.569276584942878}, 1640.0264270221523)
可以看到,依次為t-statistic, p-value, usedlag, nobs, critical-value, AIC這幾個參數(shù)。
regresults=True:
r=ts.adfuller(data,12,'ctt',regresults=True) print(r)
輸出結(jié)果:
(-1.6596695973336932, 0.9169218489129718, {'1%': -4.422218041176954, '5%': -3.8583127840881075, '10%': -3.569276584942878}, <statsmodels.tsa.stattools.ResultsStore object at 0x000000000F3B2198>)
前面幾項(xiàng)依次為t-statistic, p-value,critical-value,沒有了usedlag, nobs,多出來一個注釋“statsmodels.tsa.stattools.ResultsStore object at 0x000000000F3B2198”,這個注釋貌似是resstore的注釋,但怎么調(diào)用這個參數(shù)呢?
adfuller()函數(shù)原代碼
為了弄清楚這個問題,我們研究一下adfuller()函數(shù)的原代碼。這里進(jìn)行部分截?。?/p>
if regresults: ? ? store = True ... if store: ? ? resstore.resols = resols ? ? resstore.maxlag = maxlag ? ? resstore.usedlag = usedlag ? ? resstore.adfstat = adfstat ? ? resstore.critvalues = critvalues ? ? resstore.nobs = nobs ? ? resstore.H0 = ("The coefficient on the lagged level equals 1 - " ? ? ? ? ? ? ? ? ? ?"unit root") ? ? resstore.HA = "The coefficient on the lagged level < 1 - stationary" ? ? resstore.icbest = icbest ? ? resstore._str = 'Augmented Dickey-Fuller Test Results' ? ? return adfstat, pvalue, critvalues, resstore else: ? ? if not autolag: ? ? ? ? return adfstat, pvalue, usedlag, nobs, critvalues ? ? else: ? ? ? ? return adfstat, pvalue, usedlag, nobs, critvalues, icbest
可以看出,不同的輸入?yún)?shù)有不同的返回值,當(dāng)regresults=True時,確實(shí)將詳細(xì)的結(jié)果賦給resstore參數(shù),并作為最后一個參數(shù)返回。這個參數(shù)的子項(xiàng)包括:resols, maxlag, usedlag, adfstat, critvalues, nobs, H0(原假設(shè)描述),HA (備擇假設(shè)描述),icbest ,_str 等。
因此可以得到兩個結(jié)論:
(1)當(dāng)regresults=True時,雖然沒有返回usedlag, nobs參數(shù),但這些參數(shù)都是存在的,雖然沒有返回,但仍然可以通過resstore進(jìn)行顯示或調(diào)用。
(2)最后一項(xiàng)"statsmodels.tsa.stattools.ResultsStore object at 0x000000000F3B2198"表示計算過程中resstore參數(shù)的暫存地址(當(dāng)參數(shù)被遺棄時顯示)。
測試:
[t,p,c,r]=ts.adfuller(data,12,'ctt',regresults=True) print(r.usedlag) print(r.nobs)
結(jié)果:
0
230
adfuller()的回歸模型系數(shù)
resstore參數(shù)中還有一項(xiàng)resols,這一項(xiàng)是默認(rèn)不返回的。我們繼續(xù)在原代碼中尋找這一項(xiàng)的計算過程:
? ? ... ? ? if regression != 'nc': ? ? ? ? resols = OLS(xdshort, add_trend(xdall[:, :usedlag + 1], ? ? ? ? ? ? ? ? ? ? ?regression)).fit() ? ? else: ? ? ? ? resols = OLS(xdshort, xdall[:, :usedlag + 1]).fit() ? ? ...
看到了,resols就是最小二乘擬合函數(shù)OLS()的返回結(jié)果。因此,resols所包含的子項(xiàng)可以通過查閱OLS()函數(shù)原型得到,其中必然也包括回歸模型的擬合系數(shù)。
測試:
[t,p,c,r]=ts.adfuller(data,12,'ctt',regresults=True) print(r.resols.summary()) print(r.resols.params)
結(jié)果:
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.019
Model: OLS Adj. R-squared: 0.006
Method: Least Squares F-statistic: 1.430
Date: Thu, 26 Dec 2019 Prob (F-statistic): 0.235
Time: 23:15:35 Log-Likelihood: -858.43
No. Observations: 230 AIC: 1725.
Df Residuals: 226 BIC: 1739.
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
x1 -0.0287 0.017 -1.660 0.098 -0.063 0.005
const 55.6700 32.455 1.715 0.088 -8.283 119.623
x2 0.0209 0.053 0.395 0.693 -0.083 0.125
x3 -0.0002 0.000 -0.785 0.433 -0.001 0.000
==============================================================================
Omnibus: 8.509 Durbin-Watson: 1.870
Prob(Omnibus): 0.014 Jarque-Bera (JB): 11.078
Skew: 0.274 Prob(JB): 0.00393
Kurtosis: 3.925 Cond. No. 1.15e+06
==============================================================================Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.15e+06. This might indicate that there are
strong multicollinearity or other numerical problems.
[-2.86531492e-02 5.56699632e+01 2.08909695e-02 -1.95482480e-04]
可以看出,結(jié)果顯示了OLS擬合的詳細(xì)結(jié)果,可以對ADF檢驗(yàn)中的擬合模型和擬合效果進(jìn)行進(jìn)一步詳細(xì)研究。
結(jié)論
通過對adfuller()函數(shù)源碼的研究,明確了輸入?yún)?shù)regresults的作用和返回參數(shù)resstore的結(jié)構(gòu)組成。通過返回的resstore參數(shù),可以進(jìn)一步得到ADF檢驗(yàn)中的擬合模型和對應(yīng)參數(shù),有助于對檢驗(yàn)結(jié)果進(jìn)行更加深入的分析。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)批量將MP3音頻轉(zhuǎn)為WAV格式詳解
這篇文章主要介紹了通過Python實(shí)現(xiàn)將MP3音頻轉(zhuǎn)為WAV格式的方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,感興趣的可以了解一下2021-12-12python3中@dataclass的實(shí)現(xiàn)示例
@dataclass?是 Python 3.7 引入的一個裝飾器,用于方便地定義符合數(shù)據(jù)類協(xié)議的類,本文主要介紹了python3中@dataclass的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-02-02python實(shí)現(xiàn)微信自動回復(fù)功能
這篇文章主要為大家詳細(xì)介紹了使用python實(shí)現(xiàn)微信自動回復(fù)功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Python中的list.sort()方法和函數(shù)sorted(list)
這篇文章主要介紹了Python中的list.sort()方法和函數(shù)sorted(list),sort()是列表的方法,修改原列表使得它按照大小排序,沒有返回值,返回None2022-08-08Python unittest如何生成HTMLTestRunner模塊
這篇文章主要介紹了Python unittest如何生成HTMLTestRunner模塊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09