ความสัมพันธ์ระหว่างอนุกรมเวลา: สหสัมพันธ์

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

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

สหสัมพันธ์และความสัมพันธ์ระหว่างอนุกรม

  • ที่ผ่านมา: มุ่งเน้นที่คุณสมบัติของตัวแปรแต่ละตัว
  • ต่อไป: ความสัมพันธ์ระหว่างตัวแปร
  • สหสัมพันธ์: วัดความสัมพันธ์เชิงเส้น
  • ตลาดการเงิน: สำคัญสำหรับการพยากรณ์และการบริหารความเสี่ยง
  • pandas & seaborn มีเครื่องมือคำนวณและแสดงผล
การจัดการข้อมูล Time Series ใน Python

สหสัมพันธ์และความสัมพันธ์เชิงเส้น

  • ค่าสัมประสิทธิ์สหสัมพันธ์: การเคลื่อนไหวคู่ของสองตัวแปรรอบค่าเฉลี่ยมีความคล้ายคลึงกันมากเพียงใด?
  • มีค่าระหว่าง -1 ถึง +1 $\ \ \ \ \ r = \frac{\sum_{i=1}^{N} (x_i - \bar{x})(y_i - \bar{y})}{s_xs_y}$

 

ch3_4_v2 - Correlation & Heatmaps.011.png

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

นำเข้าอนุกรมเวลาราคา 5 รายการ

data = pd.read_csv('assets.csv', parse_dates=['date'], 
                   index_col='date')

data = data.dropna().info()
DatetimeIndex: 2469 entries, 2007-05-25 to 2017-05-22
Data columns (total 5 columns):
sp500     2469 non-null float64
nasdaq    2469 non-null float64
bonds     2469 non-null float64
gold      2469 non-null float64
oil       2469 non-null float64
การจัดการข้อมูล Time Series ใน Python

แสดงภาพความสัมพันธ์เชิงเส้นแบบคู่

daily_returns = data.pct_change()

sns.jointplot(x='sp500', y='nasdaq', data=data_returns);

ch3_4_v2 - Correlation & Heatmaps.015.png

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

คำนวณสหสัมพันธ์ทั้งหมด

correlations = returns.corr()

correlations
bonds       oil      gold     sp500    nasdaq
bonds   1.000000 -0.183755  0.003167 -0.300877 -0.306437
oil    -0.183755  1.000000  0.105930  0.335578  0.289590
gold    0.003167  0.105930  1.000000 -0.007786 -0.002544
sp500  -0.300877  0.335578 -0.007786  1.000000  0.959990
nasdaq -0.306437  0.289590 -0.002544  0.959990  1.000000
การจัดการข้อมูล Time Series ใน Python

แสดงภาพสหสัมพันธ์ทั้งหมด

sns.heatmap(correlations, annot=True)

ch3_4_v2 - Correlation & Heatmaps.019.png

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

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

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

Preparing Video For Download...