gpt4 book ai didi

python - Python 中的增强 Dickey-Fuller 测试存在少量观察的问题

转载 作者:行者123 更新时间:2023-12-03 17:32:32 25 4
gpt4 key购买 nike

我想在时间序列(nobs = 23)上测试平稳性,并从 statsmodels.tsa.stattools 实现 adfuller 测试。

以下是原始数据:

1995-01-01      3126.0
1996-01-01 3321.0
1997-01-01 3514.0
1998-01-01 3690.0
1999-01-01 3906.0
2000-01-01 4065.0
2001-01-01 4287.0
2002-01-01 4409.0
2003-01-01 4641.0
2004-01-01 4812.0
2005-01-01 4901.0
2006-01-01 5028.0
2007-01-01 5035.0
2008-01-01 5083.0
2009-01-01 5183.0
2010-01-01 5377.0
2011-01-01 5428.0
2012-01-01 5601.0
2013-01-01 5705.0
2014-01-01 5895.0
2015-01-01 6234.0
2016-01-01 6542.0
2017-01-01 6839.0

这是我正在使用的自定义 ADF 函数(归功于此 blog):
def test_stationarity(timeseries):
print('Results of Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC', maxlag = None)
dfoutput = pd.Series(dftest[0:4], index=['ADF Statistic', 'p-value', '#Lags Used', 'Number of Obs Used'])
for key, value in dftest[4].items():
dfoutput['Critical Value (%s)' % key] = value
print(dfoutput)

以下是对原始数据进行 ADF 测试的结果:
ADF Statistic           -0.126550
p-value 0.946729
#Lags Used 8.000000
Number of Obs Used 14.000000
Critical Value (1%) -4.012034
Critical Value (5%) -3.104184
Critical Value (10%) -2.690987

ADF 统计量大于所有临界值,并且 p 值 > alpha 0.05 表明该系列不是平稳的,因此我对数据进行了第一次差分。这是差分函数和 ADF 测试的结果:
def difference(dataset):
diff = list()
for i in range(1, len(dataset)):
value = dataset[i] - dataset[i - 1]
#print(value)
diff.append(value)
return pd.Series(diff)


ADF Statistic -1.169799
p-value 0.686451
#Lags Used 9.000000
Number of Obs Used 12.000000
Critical Value (1%) -4.137829
Critical Value (5%) -3.154972
Critical Value (10%) -2.714477

ADF 统计量和 p 值都提高了,但序列仍然不是平稳的,所以我执行了第二次差分,这里是结果:
ADF Statistic           -0.000000
p-value 0.958532
#Lags Used 9.000000
Number of Obs Used 11.000000
Critical Value (1%) -4.223238
Critical Value (5%) -3.189369
Critical Value (10%) -2.729839

对数据进行第二次差分后,ADF 测试统计量变为 -0.0000(考虑到未舍入值的 print() 返回 -0.0,这令人费解,但无论哪种方式都意味着某处存在除零以外的一些有效数字)和 p 值现在比开始时更糟。我也收到此警告:
RuntimeWarning: divide by zero encountered in double_scalars
return np.dot(wresid, wresid) / self.df_resid.

p, d, q 值的网格搜索返回一个 ARIMA(1, 1, 0) 模型,但我认为第二次差分仍然是必要的,因为第一次差分没有实现。

我怀疑奇怪的测试统计量和 p 值是由于 ADF 测试的默认设置 (maxlag = None) 使用的样本量小和滞后数高。我知道当 maxlag 设置为 None 时,它​​使用公式 int(np.ceil(12. * np.power(nobs/100., 1/4.)))。

这合适吗?如果没有,对于具有少量观察的数据集是否有任何解决方法,或者在 ADF 函数中手动设置 maxlag 值以避免出现错误的测试统计量的经验法则。我搜索了 here , here , 和 here但找不到解决办法。

我正在使用 statsmodels 版本 0.8.0。

最佳答案

您看到的问题是最大滞后长度太高。首先,您的数据具有很强的趋势,因此您最初应该包括 trend="ct" .这改善了检验统计量,但这还不够。当您不同时,差异数据具有非零均值,因此趋势应为“c”。这仍然不拒绝,因此需要双重差异。可能需要双重差异,因为该系列是持久的,但也因为 ADF 测试的功效较低。
您应该将最大滞后设置为小于样本大小的平方根。这里发生的事情是使用了太多的滞后,这减少了有效样本量,因此模型拟合接近完美。这会产生大量被选择的滞后。

from arch.unitroot import ADF
import pandas as pd
import numpy as np

y = [3126.0, 3321.0, 3514.0, 3690.0, 3906.0, 4065.0, 4287.0,
4409.0, 4641.0, 4812.0, 4901.0, 5028.0, 5035.0, 5083.0,
5183.0, 5377.0, 5428.0, 5601.0, 5705.0, 5895.0, 6234.0,
6542.0, 6839.0]
y = pd.Series(y)

max_lags = int(np.sqrt(y.shape[0]))
print(f"max_lags: {max_lags}")
ADF(y, trend="ct", max_lags=max_lags).summary()
输出
max_lags: 4

Augmented Dickey-Fuller Results
=====================================
Test Statistic -2.009
P-value 0.596
Lags 2
-------------------------------------

Trend: Constant and Linear Time Trend
Critical Values: -4.50 (1%), -3.66 (5%), -3.27 (10%)
Null Hypothesis: The process contains a unit root.
Alternative Hypothesis: The process is weakly stationary.
接下来,区别,
ADF(y.diff().dropna(), trend="c", max_lags=max_lags).summary()
返回
   Augmented Dickey-Fuller Results
=====================================
Test Statistic -2.224
P-value 0.198
Lags 0
-------------------------------------

Trend: Constant
Critical Values: -3.79 (1%), -3.01 (5%), -2.65 (10%)
Null Hypothesis: The process contains a unit root.
Alternative Hypothesis: The process is weakly stationary.
不拒绝空值。再区别一次,这次是 trend="n" ,最终产生一个非常平稳的系列。
ADF(y.diff().diff().dropna(), trend="n", max_lags=max_lags).summary()
   Augmented Dickey-Fuller Results
=====================================
Test Statistic -7.346
P-value 0.000
Lags 0
-------------------------------------

Trend: No Trend
Critical Values: -2.69 (1%), -1.96 (5%), -1.61 (10%)
Null Hypothesis: The process contains a unit root.
Alternative Hypothesis: The process is weakly stationary.
挑战在于,当时间序列很短时,不能完全依赖 ADF 测试。例如,差异看起来并不是特别不稳定。
enter image description here

关于python - Python 中的增强 Dickey-Fuller 测试存在少量观察的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51293378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com