温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python怎么实现ADF检验

发布时间:2022-01-12 16:50:50 阅读:743 作者:iii 栏目:大数据
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容介绍了“python怎么实现ADF检验”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

ADF检验

在使用很多时间序列模型的时候,如 ARMA、ARIMA,都会要求时间序列是平稳的,所以一般在研究一段时间序列的时候,第一步都需要进行平稳性检验,除了用肉眼检测的方法,另外比较常用的严格的统计检验方法就是ADF检验,也叫做单位根检验。

ADF检验全称是 Augmented Dickey-Fuller test,顾名思义,ADF是 Dickey-Fuller检验的增广形式。DF检验只能应用于一阶情况,当序列存在高阶的滞后相关时,可以使用ADF检验,所以说ADF是对DF检验的扩展。
单位根(unit root)

在做ADF检验,也就是单位根检验时,需要先明白一个概念,也就是要检验的对象——单位根

当一个自回归过程中:y_{t} = by_{t-1} + a + \epsilon _{t} ,如果滞后项系数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函数的参数意义分别是:
1、x:一维的数据序列。
2、maxlag:最大滞后数目。
3、regression:回归中的包含项(c:只有常数项,默认;ct:常数项和趋势项;ctt:常数项,线性二次项;nc:没有常数项和趋势项)
4、autolag:自动选择滞后数目(AIC:赤池信息准则,默认;BIC:贝叶斯信息准则;t-stat:基于maxlag,从maxlag开始并删除一个滞后直到最后一个滞后长度基于 t-statistic 显著性小于5%为止;None:使用maxlag指定的滞后)
5、store:True  False,默认。
6、regresults:True 完整的回归结果将返回。False,默认。

返回值意义为:

1、adf:Test statistic,T检验,假设检验值。
2、pvalue:假设检验结果。
3、usedlag:使用的滞后阶数。
4、nobs:用于ADF回归和计算临界值用到的观测值数目。
5、icbest:如果autolag不是None的话,返回最大的信息准则值。
6、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' (defaultor '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检验,看一下结果:

python怎么实现ADF检验

result = adfuller(rb_price)print(result)(-0.451538676878085740.90113154544026491198, {'5%': -2.876250632135043'1%': -3.4638151713286316'10%': -2.574611347821651}, 1172.4579344852016)

看到 t-statistic 的值 -0.451 要大于10%的显著性水平,所以无法拒绝原假设,另外,p-value的值也很大。

将数据进行一阶差分滞后,看一下结果如何:

python怎么实现ADF检验

rb_price = np.diff(rb_price)result = adfuller(rb_price)print(result)(-15.4360342115112042.90628134201655e-280198, {'5%': -2.876250632135043'1%': -3.4638151713286316'10%': -2.574611347821651}, 1165.1556545612445)

 看到 t-statistic 的值 -15 要小于5%,所以拒绝原假设,另外,p-value的值也很小,所以说明一阶差分之后的数据是平稳的。

“python怎么实现ADF检验”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/4586457/blog/4428022

AI

开发者交流群×