ADF検定

  • ランダムウォークおよびランダムウォークの階差系列に対してADF検定を実施
In [1]:
import numpy as np
import pandas as pd
from statsmodels.tsa import stattools

ランダムウォークに対してADF検定

In [3]:
# サンプルデータ生成
y = pd.Series(np.random.randn(1000), index=pd.date_range('2000-1-1', periods=1000))
y = y.cumsum()
y.plot()
Out[3]:
<AxesSubplot:>
In [4]:
# 上記データに、ADF検定実施

# ADF検定
# トレンド項あり(2次まで)、定数項あり
ctt = stattools.adfuller(y, regression="ctt")
# トレンド項あり(1次)、定数項あり
ct = stattools.adfuller(y, regression="ct")
# トレンド項なし、定数項あり
c = stattools.adfuller(y, regression="c")
# トレンド項なし、定数項なし
nc = stattools.adfuller(y, regression="nc")

print("ctt:")
print(ctt)
print("ct:")
print(ct)
print("c:")
print(c)
print("nc:")
print(nc)
ctt:
(-3.733565755247948, 0.06465344078364631, 0, 999, {'1%': -4.382766025596549, '5%': -3.8383142452686947, '10%': -3.5569286306152224}, 2786.403330360814)
ct:
(-2.247160673646937, 0.46336773298113265, 0, 999, {'1%': -3.967860781661831, '5%': -3.4148938944043334, '10%': -3.1296421434972537}, 2792.7655427976106)
c:
(-2.519473379529322, 0.11080784745084737, 0, 999, {'1%': -3.4369127451400474, '5%': -2.864437475834273, '10%': -2.568312754566378}, 2792.629456632878)
nc:
(0.9190131297287226, 0.9042328904549256, 0, 999, {'1%': -2.5679816723029334, '5%': -1.9412722092921209, '10%': -1.6165568281300424}, 2799.3852595171084)
/home/yoshi-1/.local/lib/python3.8/site-packages/statsmodels/tsa/tsatools.py:821: FutureWarning: trend 'nc' has been renamed to 'n' after 0.14 is released. Use 'n' now to avoid this warning.
  warnings.warn(
  • 出力されるタプルの中身:
    (検定統計量(t統計量)、p値 、使用されたラグの数(autolag引数がNoneでなければ選択された情報量基準をもとに計算される)、 計算に使用されたデータ数 、検定統計量に対する各棄却域 、情報量基準の最大値(デフォルトではAICの値))
  • 上記結果では、すべてのパターンにおいてp値が高く、単位根過程であるという帰無仮説を棄却できない。よって、ランダムウォークは単位根過程であることがわかる。

ランダムウォークの差分系列に対してADF検定

In [6]:
# サンプルデータ生成
y_diff = y.diff().dropna()
y_diff.plot()
Out[6]:
<AxesSubplot:>
In [7]:
# 上記データに、ADF検定実施

# ADF検定
# トレンド項あり(2次まで)、定数項あり
ctt = stattools.adfuller(y_diff, regression="ctt")
# トレンド項あり(1次)、定数項あり
ct = stattools.adfuller(y_diff, regression="ct")
# トレンド項なし、定数項あり
c = stattools.adfuller(y_diff, regression="c")
# トレンド項なし、定数項なし
nc = stattools.adfuller(y_diff, regression="nc")

print("ctt:")
print(ctt)
print("ct:")
print(ct)
print("c:")
print(c)
print("nc:")
print(nc)
ctt:
(-32.54574841448798, 0.0, 0, 998, {'1%': -4.382777721611994, '5%': -3.8383201941769736, '10%': -3.556932312045306}, 2796.1763453876083)
ct:
(-32.56011110979031, 0.0, 0, 998, {'1%': -3.9678699195021783, '5%': -3.414898316296852, '10%': -3.129644744825171}, 2794.229396113533)
c:
(-32.48400391593743, 0.0, 0, 998, {'1%': -3.4369193380671, '5%': -2.864440383452517, '10%': -2.56831430323573}, 2794.8263981663813)
nc:
(-32.37415526719091, 0.0, 0, 998, {'1%': -2.5679839221127625, '5%': -1.9412724853659513, '10%': -1.6165565671073312}, 2796.3533021002086)
/home/yoshi-1/.local/lib/python3.8/site-packages/statsmodels/tsa/tsatools.py:821: FutureWarning: trend 'nc' has been renamed to 'n' after 0.14 is released. Use 'n' now to avoid this warning.
  warnings.warn(
  • すべてのパターンにおいてp値が0.0であり、単位根過程でない。つまり定常過程であることがわかる。
In [ ]: