自相關與偏自相關

使用 Python 視覺化時間序列資料

Thomas Vincent

Head of Data Science, Getty Images

時間序列中的自相關

  • 自相關是量測時間序列與其延遲版本之間的相關係數。
  • 例如,階數為 3 的自相關會回傳時間序列在點位(t_1t_2t_3、...)與其落後 3 個時間點的值(t_4t_5t_6、...)之間的相關性
  • 用來找出時間序列中的重複模式或週期訊號
使用 Python 視覺化時間序列資料

Statsmodels

statsmodels 是一個 Python 模組,提供多種統計模型的估計類別與函式,也可進行統計檢定與資料探索。

使用 Python 視覺化時間序列資料

繪製自相關

import matplotlib.pyplot as plt
from statsmodels.graphics import tsaplots
fig = tsaplots.plot_acf(co2_levels['co2'], lags=40)

plt.show()
使用 Python 視覺化時間序列資料

解讀自相關圖

時間序列的自相關

使用 Python 視覺化時間序列資料

時間序列中的偏自相關

  • 與自相關相反,偏自相關會移除前面時間點的影響
  • 例如,order 3 的偏自相關函式會回傳時間序列(t1t2t3、...)與其落後 3 個時間點(t4t5t6、...)的相關性,但會先移除落後 1 與 2 的所有影響
使用 Python 視覺化時間序列資料

繪製偏自相關

import matplotlib.pyplot as plt

from statsmodels.graphics import tsaplots
fig = tsaplots.plot_pacf(co2_levels['co2'], lags=40)

plt.show()
使用 Python 視覺化時間序列資料

解讀偏自相關圖

時間序列的偏自相關

使用 Python 視覺化時間序列資料

一起來練習吧!

使用 Python 視覺化時間序列資料

Preparing Video For Download...