讓時間序列成為定態

Python 中的 ARIMA 模型

James Fulton

Climate informatics researcher

總覽

  • 定態性的統計檢定
  • 讓資料集成為定態
Python 中的 ARIMA 模型

擴增 Dickey-Fuller 檢定

  • 趨勢型非定態的檢定
  • 虛無假設為時間序列非定態
Python 中的 ARIMA 模型

套用 adfuller 檢定

from statsmodels.tsa.stattools import adfuller

results = adfuller(df['close'])
Python 中的 ARIMA 模型

解讀檢定結果

print(results)
(-1.34, 0.60, 23, 1235, {'1%': -3.435, '5%': -2.913, '10%': -2.568}, 10782.87)
  • 第 0 個元素為檢定統計量(-1.34)
    • 越小(越負)代表越可能為定態
  • 第 1 個元素為 p 值(0.60)
    • 若 p 值很小 $\rightarrow$ 拒絕虛無假設。也就是拒絕非定態。
  • 第 4 個元素為臨界值統計量
Python 中的 ARIMA 模型

解讀檢定結果

print(results)
(-1.34, 0.60, 23, 1235, {'1%': -3.435, '5%': -2.863, '10%': -2.568}, 10782.87)
  • 第 0 個元素為檢定統計量(-1.34)
    • 越小(越負)代表越可能為定態
  • 第 1 個元素為 p 值(0.60)
    • 若 p 值很小 $\rightarrow$ 拒絕虛無假設。也就是拒絕非定態。
  • 第 4 個元素為臨界值統計量
1 https://www.statsmodels.org/dev/generated/statsmodels.tsa.stattools.adfuller.html
Python 中的 ARIMA 模型

繪圖的價值

  • 繪製時間序列能避免你做出錯誤假設
Python 中的 ARIMA 模型

繪圖的價值

Python 中的 ARIMA 模型

讓時間序列成為定態

Python 中的 ARIMA 模型

取差分

差分:$\Delta y_t = y_t - y_{t-1}$

Python 中的 ARIMA 模型

取差分

df_stationary = df.diff()
            city_population
date                       
1969-09-30              NaN
1970-03-31        -0.116156
1970-09-30         0.050850
1971-03-31        -0.153261
1971-09-30         0.108389
Python 中的 ARIMA 模型

取差分

df_stationary = df.diff().dropna()
            city_population
date                       
1970-03-31        -0.116156
1970-09-30         0.050850
1971-03-31        -0.153261
1971-09-30         0.108389
1972-03-31        -0.029569
Python 中的 ARIMA 模型

取差分

Python 中的 ARIMA 模型

其他轉換

其他轉換範例

  • 取對數
    • np.log(df)
  • 取平方根
    • np.sqrt(df)
  • 取比例變化
    • df.shift(1)/df
Python 中的 ARIMA 模型

一起來練習吧!

Python 中的 ARIMA 模型

Preparing Video For Download...