Lag, การเปลี่ยนแปลง และผลตอบแทนของราคาหุ้น

การจัดการข้อมูล Time Series ใน Python

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

การคำนวณ Time Series เบื้องต้น

  • การดำเนินการทั่วไปกับ Time Series:

    • เลื่อนค่าไปข้างหน้าหรือข้างหลังตามเวลา

    • คำนวณผลต่างของค่าในช่วงเวลาที่กำหนด

    • คำนวณการเปลี่ยนแปลงเป็นเปอร์เซ็นต์ในจำนวนคาบเวลาใดก็ได้

  • เมธอดใน pandas ต้องใช้ pd.DateTimeIndex

การจัดการข้อมูล Time Series ใน Python

ดึงราคาหุ้น GOOG

  • ให้ pd.read_csv() แปลงข้อมูลให้อัตโนมัติ!
google = pd.read_csv('google.csv', parse_dates=['date'], index_col='date')
google.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 504 entries, 2015-01-02 to 2016-12-30
Data columns (total 1 columns):
price    504 non-null float64
dtypes: float64(1)
การจัดการข้อมูล Time Series ใน Python

ดึงราคาหุ้น GOOG

google.head()
             price
date
2015-01-02  524.81
2015-01-05  513.87
2015-01-06  501.96
2015-01-07  501.10
2015-01-08  502.68
การจัดการข้อมูล Time Series ใน Python

.shift(): เลื่อนข้อมูลระหว่างอดีตและอนาคต

  • .shift():
    • ค่าเริ่มต้น periods=1
    • เลื่อนข้อมูล 1 คาบไปข้างหน้า
google['shifted'] = google.price.shift() # default: periods=1

google.head(3)
             price  shifted
date
2015-01-02  542.81      NaN
2015-01-05  513.87   542.81
2015-01-06  501.96   513.87
การจัดการข้อมูล Time Series ใน Python

.shift(): เลื่อนข้อมูลระหว่างอดีตและอนาคต

  • .shift(periods=-1):
    • ข้อมูลแบบ lag
    • เลื่อนข้อมูล 1 คาบย้อนหลัง
google['lagged'] = google.price.shift(periods=-1)

google[['price', 'lagged', 'shifted']].tail(3)
             price  lagged  shifted
date
2016-12-28  785.05  782.79   791.55
2016-12-29  782.79  771.82   785.05
2016-12-30  771.82     NaN   782.79
การจัดการข้อมูล Time Series ใน Python

คำนวณการเปลี่ยนแปลงเป็นเปอร์เซ็นต์รายคาบ

  • $x_t$ / $x_{t-1}$
google['change'] = google.price.div(google.shifted)

google[['price', 'shifted', 'change']].head(3)
             price  shifted    change
Date
2017-01-03  786.14      NaN       NaN
2017-01-04  786.90   786.14  1.000967
2017-01-05  794.02   786.90  1.009048
การจัดการข้อมูล Time Series ใน Python

คำนวณการเปลี่ยนแปลงเป็นเปอร์เซ็นต์รายคาบ

google['return'] = google.change.sub(1).mul(100)

google[['price', 'shifted', 'change', 'return']].head(3)
             price  shifted  change  return
date
2015-01-02  524.81      NaN     NaN     NaN
2015-01-05  513.87   524.81    0.98   -2.08
2015-01-06  501.96   513.87    0.98   -2.32
การจัดการข้อมูล Time Series ใน Python

.diff(): คำนวณการเปลี่ยนแปลงใน Time Series

  • ผลต่างของค่าระหว่างสองคาบที่ติดกัน

  • $x_t - x_{t-1}$

google['diff'] = google.price.diff()

google[['price', 'diff']].head(3)
             price        diff
date
2015-01-02  524.81         NaN
2015-01-05  513.87      -10.94
2015-01-06  501.96      -11.91
การจัดการข้อมูล Time Series ใน Python

.pct_change(): คำนวณ % การเปลี่ยนแปลงใน Time Series

  • การเปลี่ยนแปลงเป็นเปอร์เซ็นต์ระหว่างสองคาบที่ติดกัน

  • $\frac{x_t}{x_{t-1}}$

google['pct_change'] = google.price.pct_change().mul(100)

google[['price', 'return', 'pct_change']].head(3)
             price    return  pct_change
date
2015-01-02  524.81       NaN         NaN
2015-01-05  513.87     -2.08       -2.08
2015-01-06  501.96     -2.32       -2.32
การจัดการข้อมูล Time Series ใน Python

ก้าวต่อไป: คำนวณผลตอบแทนหลายคาบ

google['return_3d'] = google.price.pct_change(periods=3).mul(100)

google[['price', 'return_3d']].head()
             price  return_3d
date
2015-01-02  524.81        NaN
2015-01-05  513.87        NaN
2015-01-06  501.96        NaN
2015-01-07  501.10  -4.517825
2015-01-08  502.68  -2.177594
  • การเปลี่ยนแปลงเป็นเปอร์เซ็นต์ระหว่างสองคาบที่ห่างกัน 3 วันทำการ
การจัดการข้อมูล Time Series ใน Python

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

การจัดการข้อมูล Time Series ใน Python

Preparing Video For Download...