การประมาณค่าและพยากรณ์ด้วยโมเดล AR

การวิเคราะห์อนุกรมเวลาด้วย Python

Rob Reider

Adjunct Professor, NYU-Courant Consultant, Quantopian

การประมาณค่าโมเดล AR

  • การประมาณค่าพารามิเตอร์จากข้อมูล (จำลอง)

    from statsmodels.tsa.arima_model import ARMA
    mod = ARMA(data, order=(1,0))
    result = mod.fit()
    
  • ARMA ถูกยกเลิกและแทนที่ด้วย ARIMA

    from statsmodels.tsa.arima.model import ARIMA
    mod = ARIMA(data, order=(1,0,0))
    result = mod.fit()
    
  • สำหรับ ARMA ใช้ order=(p,q)

  • สำหรับ ARIMA ใช้ order=(p,d,q)
การวิเคราะห์อนุกรมเวลาด้วย Python

การประมาณค่าโมเดล AR

  • ผลลัพธ์แบบเต็ม (ค่าจริง $\large \mu=0$ และ $\large \phi=0.9$)
    print(result.summary())
    

การวิเคราะห์อนุกรมเวลาด้วย Python

การประมาณค่าโมเดล AR

  • เฉพาะค่าประมาณของ $\large \mu$ และ $\large \phi$ (ค่าจริง $\large \mu=0$ และ $\large \phi=0.9$)
    print(result.params)
    
array([-0.03605989,  0.90535667])
การวิเคราะห์อนุกรมเวลาด้วย Python

การพยากรณ์ด้วยโมเดล AR

from statsmodels.graphics.tsaplots import plot_predict
fig, ax = plt.subplots()
data.plot(ax=ax)
plot_predict(result, start='2012-09-27', end='2012-10-06', alpha=0.05, ax=ax)
plt.show()
  • อาร์กิวเมนต์ของฟังก์ชัน plot_predict()
    • อาร์กิวเมนต์แรกคือโมเดลที่ฝึกแล้ว
    • กำหนด alpha=None เพื่อไม่แสดงช่วงความเชื่อมั่น
    • กำหนด ax=ax เพื่อพล็อตข้อมูลและการพยากรณ์บนแกนเดียวกัน

การวิเคราะห์อนุกรมเวลาด้วย Python

มาฝึกกันเถอะ!

การวิเคราะห์อนุกรมเวลาด้วย Python

Preparing Video For Download...