使时间序列平稳

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 值小 → 拒绝原假设,即拒绝"非平稳"
  • 第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 值小 → 拒绝原假设,即拒绝"非平稳"
  • 第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 模型

Vamos praticar!

Python 中的 ARIMA 模型

Preparing Video For Download...