平穩性與去趨勢 (ADF/KPSS)¶
平穩性表示時間序列的統計特性,即平均數、變異數和共變異數,不會隨時間改變。許多統計模型需要序列是平穩的,才能做出有效且精確的預測。
將使用兩個統計檢定來檢查時間序列的平穩性 – 擴增迪基-福勒 (Augmented Dickey Fuller, “ADF”) 檢定和夸特科夫斯基-菲利浦斯-施密特-辛 (Kwiatkowski-Phillips-Schmidt-Shin, “KPSS”) 檢定。也將使用一種將非平穩時間序列轉換為平穩序列的方法。
第一個儲存格匯入標準套件並設定繪圖顯示在內嵌。
[1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import statsmodels.api as sm
使用太陽黑子數據集。它包含來自國家地球物理數據中心的太陽黑子年度 (1700-2008) 數據。
[2]:
sunspots = sm.datasets.sunspots.load_pandas().data
對數據進行一些預處理。“YEAR” 欄用於建立索引。
[3]:
sunspots.index = pd.Index(sm.tsa.datetools.dates_from_range("1700", "2008"))
del sunspots["YEAR"]
現在繪製數據。
[4]:
sunspots.plot(figsize=(12, 8))
[4]:
<Axes: >

ADF 檢定¶
ADF 檢定用於確定序列中是否存在單位根,因此有助於了解序列是否平穩。此檢定的虛無假設和對立假設如下
虛無假設:序列具有單位根。
對立假設:序列沒有單位根。
如果無法拒絕虛無假設,此檢定可能會提供證據表明該序列是非平穩的。
建立一個函數,以對時間序列執行 ADF 檢定。
[5]:
from statsmodels.tsa.stattools import adfuller
def adf_test(timeseries):
print("Results of Dickey-Fuller Test:")
dftest = adfuller(timeseries, autolag="AIC")
dfoutput = pd.Series(
dftest[0:4],
index=[
"Test Statistic",
"p-value",
"#Lags Used",
"Number of Observations Used",
],
)
for key, value in dftest[4].items():
dfoutput["Critical Value (%s)" % key] = value
print(dfoutput)
KPSS 檢定¶
KPSS 是另一個用於檢查時間序列平穩性的檢定。KPSS 檢定的虛無假設和對立假設與 ADF 檢定相反。
虛無假設:過程是趨勢平穩的。
對立假設:序列具有單位根(序列不平穩)。
建立一個函數,以對時間序列執行 KPSS 檢定。
[6]:
from statsmodels.tsa.stattools import kpss
def kpss_test(timeseries):
print("Results of KPSS Test:")
kpsstest = kpss(timeseries, regression="c", nlags="auto")
kpss_output = pd.Series(
kpsstest[0:3], index=["Test Statistic", "p-value", "Lags Used"]
)
for key, value in kpsstest[3].items():
kpss_output["Critical Value (%s)" % key] = value
print(kpss_output)
ADF 檢定給出以下結果 – 檢定統計量、p 值以及在 1%、5% 和 10% 信賴區間的臨界值。
現在將 ADF 檢定應用於數據。
[7]:
adf_test(sunspots["SUNACTIVITY"])
Results of Dickey-Fuller Test:
Test Statistic -2.837781
p-value 0.053076
#Lags Used 8.000000
Number of Observations Used 300.000000
Critical Value (1%) -3.452337
Critical Value (5%) -2.871223
Critical Value (10%) -2.571929
dtype: float64
基於 0.05 的顯著性水平和 ADF 檢定的 p 值,無法拒絕虛無假設。因此,該序列是非平穩的。
KPSS 檢定給出以下結果 – 檢定統計量、p 值以及在 1%、5% 和 10% 信賴區間的臨界值。
現在將 KPSS 檢定應用於數據。
[8]:
kpss_test(sunspots["SUNACTIVITY"])
Results of KPSS Test:
Test Statistic 0.669866
p-value 0.016285
Lags Used 7.000000
Critical Value (10%) 0.347000
Critical Value (5%) 0.463000
Critical Value (2.5%) 0.574000
Critical Value (1%) 0.739000
dtype: float64
基於 0.05 的顯著性水平和 KPSS 檢定的 p 值,有證據支持拒絕虛無假設而支持對立假設。因此,根據 KPSS 檢定,該序列是非平穩的。
最好總是同時應用這兩個檢定,以便可以確保序列是真正平穩的。應用這些平穩性檢定的可能結果如下
在這裡,由於 ADF 檢定和 KPSS 檢定的結果存在差異,可以推斷出該序列是趨勢平穩的,而不是嚴格平穩的。可以透過差分法或模型擬合來去除序列的趨勢。
差分法去趨勢¶
這是去除時間序列趨勢的最簡單方法之一。建構一個新序列,其中當前時間步的值計算為原始觀察值與前一個時間步的觀察值之間的差值。
對數據應用差分法並繪製結果。
[9]:
sunspots["SUNACTIVITY_diff"] = sunspots["SUNACTIVITY"] - sunspots["SUNACTIVITY"].shift(
1
)
sunspots["SUNACTIVITY_diff"].dropna().plot(figsize=(12, 8))
[9]:
<Axes: >

現在將 ADF 檢定應用於這些去趨勢值,並檢查平穩性。
[10]:
adf_test(sunspots["SUNACTIVITY_diff"].dropna())
Results of Dickey-Fuller Test:
Test Statistic -1.486166e+01
p-value 1.715552e-27
#Lags Used 7.000000e+00
Number of Observations Used 3.000000e+02
Critical Value (1%) -3.452337e+00
Critical Value (5%) -2.871223e+00
Critical Value (10%) -2.571929e+00
dtype: float64
基於 ADF 檢定的 p 值,有證據支持拒絕虛無假設而支持對立假設。因此,該序列現在是嚴格平穩的。
現在將 KPSS 檢定應用於這些去趨勢值,並檢查平穩性。
[11]:
kpss_test(sunspots["SUNACTIVITY_diff"].dropna())
Results of KPSS Test:
Test Statistic 0.021193
p-value 0.100000
Lags Used 0.000000
Critical Value (10%) 0.347000
Critical Value (5%) 0.463000
Critical Value (2.5%) 0.574000
Critical Value (1%) 0.739000
dtype: float64
/tmp/ipykernel_4050/1512460390.py:6: InterpolationWarning: The test statistic is outside of the range of p-values available in the
look-up table. The actual p-value is greater than the p-value returned.
kpsstest = kpss(timeseries, regression="c", nlags="auto")
基於 KPSS 檢定的 p 值,無法拒絕虛無假設。因此,該序列是平穩的。
結論¶
使用兩個檢定來檢查時間序列的平穩性,即 ADF 檢定和 KPSS 檢定。使用差分法進行去趨勢。趨勢平穩的時間序列被轉換為嚴格平穩的時間序列。現在可以將必要的預測模型應用於平穩時間序列數據。