網站首頁 編程語言 正文
adfuller函數返回值的參數說明
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)
最近在學習用ARIMA模型建模處理預測數據的時候遇到的一個用來評測穩定性的函數,該函數可以返回一個數組,包含五個數據。
- 第一個是adt檢驗的結果,也就是t統計量的值。
- 第二個是t統計量的P值。
- 第三個是計算過程中用到的延遲階數。
- 第四個是用于ADF回歸和計算的觀測值的個數。
- 第五個是配合第一個一起看的,是在99%,95%,90%置信區間下的臨界的ADF檢驗的值。如果第一個值比第五個值小證明平穩,反正證明不平穩。根據結果看出來,你的數據不平穩。
- 至于第六個數值就不太明白了,網上也沒有查找到相應的資料
查看adfuller()函數的模型擬合系數
adfuller()函數的輸入參數中有regresults一項,網上教程中大多數默認設為False。這個參數到底有什么用,這里我們研究一下。
adfuller()函數原型
adfuller()是ADF檢驗常用的函數(還有一個常用函數為arch.unitroot包中的ADF()函數),需導入的包為:
import statsmodels.tsa.stattools as ts
其函數原型為:
t=adfuller(x, maxlag=None, regression='c', autolag='AIC', store=False, regresults=False)
輸入參數:
-
x
:array_like,1d,要測試的數據系列。 -
maxlag
: 測試中包含的最大延遲,默認為12 *(nobs / 100)^ {1/4}。 -
regression
:{‘c’,‘ct’,‘ctt’,‘nc’}, 包含在回歸中的常量和趨勢順序?!甤’:僅限常量(默認值)。 ‘ct’:恒定和趨勢。 ‘ctt’:常數,線性和二次趨勢。 ‘nc’:沒有恒定,沒有趨勢。 -
autolag
: {‘AIC’,‘BIC’,‘t-stat’,None}自動確定滯后時使用的方法。如果為None,則使用maxlag滯后。如果是’AIC’(默認值)或’BIC’,則選擇滯后數以最小化相應的信息標準。基于’t-stat’的maxlag選擇。從maxlag開始并使用5%大小的測試來降低延遲,直到最后一個滯后長度的t統計量顯著為止。 -
store
:bool,如果為True,則另外返回adf統計信息的結果實例。默認值為False。 -
regresults
:bool,optional,如果為True,則返回完整的回歸結果。默認值為False。
返回參數:
-
ADF
:float,測試統計。 -
pvalue
:float,probability value:MacKinnon基于MacKinnon的近似p值(1994年,2010年)。 -
usedlag
:int,使用的滯后數量。 -
NOBS
:int,用于ADF回歸和計算臨界值的觀察數。 -
critical values
:dict,測試統計數據的臨界值為1%,5%和10%?;贛acKinnon(2010)。 -
icbest
:float,如果autolag不是None,則最大化信息標準。 -
resstore
:ResultStore, optional,一個虛擬類,其結果作為屬性附加。
regresults參數
adfuller()函數的其他參數,網上的各種教程已經將的很清楚了。但是對regresults,卻一直諱莫如深,從函數原型也看的一頭霧水,搞不清楚這個參數怎么用的。首先通過兩段代碼看看regresults參數對輸出結果的影響。
regresults=False:
r=ts.adfuller(data,12,'ctt',regresults=False) print(r)
輸出結果:
(-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這幾個參數。
regresults=True:
r=ts.adfuller(data,12,'ctt',regresults=True) print(r)
輸出結果:
(-1.6596695973336932, 0.9169218489129718, {'1%': -4.422218041176954, '5%': -3.8583127840881075, '10%': -3.569276584942878}, <statsmodels.tsa.stattools.ResultsStore object at 0x000000000F3B2198>)
前面幾項依次為t-statistic, p-value,critical-value,沒有了usedlag, nobs,多出來一個注釋“statsmodels.tsa.stattools.ResultsStore object at 0x000000000F3B2198”,這個注釋貌似是resstore的注釋,但怎么調用這個參數呢?
adfuller()函數原代碼
為了弄清楚這個問題,我們研究一下adfuller()函數的原代碼。這里進行部分截?。?/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
可以看出,不同的輸入參數有不同的返回值,當regresults=True時,確實將詳細的結果賦給resstore參數,并作為最后一個參數返回。這個參數的子項包括:resols, maxlag, usedlag, adfstat, critvalues, nobs, H0(原假設描述),HA (備擇假設描述),icbest ,_str 等。
因此可以得到兩個結論:
(1)當regresults=True時,雖然沒有返回usedlag, nobs參數,但這些參數都是存在的,雖然沒有返回,但仍然可以通過resstore進行顯示或調用。
(2)最后一項"statsmodels.tsa.stattools.ResultsStore object at 0x000000000F3B2198"表示計算過程中resstore參數的暫存地址(當參數被遺棄時顯示)。
測試:
[t,p,c,r]=ts.adfuller(data,12,'ctt',regresults=True) print(r.usedlag) print(r.nobs)
結果:
0
230
adfuller()的回歸模型系數
resstore參數中還有一項resols,這一項是默認不返回的。我們繼續在原代碼中尋找這一項的計算過程:
? ? ... ? ? if regression != 'nc': ? ? ? ? resols = OLS(xdshort, add_trend(xdall[:, :usedlag + 1], ? ? ? ? ? ? ? ? ? ? ?regression)).fit() ? ? else: ? ? ? ? resols = OLS(xdshort, xdall[:, :usedlag + 1]).fit() ? ? ...
看到了,resols就是最小二乘擬合函數OLS()的返回結果。因此,resols所包含的子項可以通過查閱OLS()函數原型得到,其中必然也包括回歸模型的擬合系數。
測試:
[t,p,c,r]=ts.adfuller(data,12,'ctt',regresults=True) print(r.resols.summary()) print(r.resols.params)
結果:
? ? ? ? ? ? ? ? ? ? ? ? ? ? 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]
可以看出,結果顯示了OLS擬合的詳細結果,可以對ADF檢驗中的擬合模型和擬合效果進行進一步詳細研究。
結論
通過對adfuller()函數源碼的研究,明確了輸入參數regresults的作用和返回參數resstore的結構組成。通過返回的resstore參數,可以進一步得到ADF檢驗中的擬合模型和對應參數,有助于對檢驗結果進行更加深入的分析。
原文鏈接:https://blog.csdn.net/qq_36707798/article/details/88640684
相關推薦
- 2022-07-29 C++超詳細講解字符串類_C 語言
- 2023-05-14 Python中數字(Number)數據類型常用操作_python
- 2022-07-06 Flutter?DateTime日期轉換的詳細使用_Android
- 2022-07-15 C#使用BitConverter與BitArray類進行預定義基礎類型轉換_C#教程
- 2022-04-11 python文件讀寫操作小結_python
- 2024-07-14 Guava自加載緩存LoadingCache
- 2022-09-03 解決Python報錯問題[SSL:?SSLV3_ALERT_HANDSHAKE_FAILURE]_p
- 2022-10-04 解決Pandas生成Excel時的sheet問題的方法總結_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支