視覺化你的指數成分之間的相關性
為了更了解你的指數成分特性,你可以計算報酬之間的相關性。
使用這些公司的每日股價,繪製每日報酬相關性的熱度圖!
本練習屬於課程
Manipulating Time Series Data in Python
練習說明
我們已將 pandas 以 pd 匯入、將 matplotlib.pyplot 以 plt 匯入,並將 seaborn 以 sns 匯入。也已把你的指數成分的歷史價格序列載入變數 stock_prices。
- 使用
.info()檢視stock_prices。 - 計算
stock_prices的每日報酬,並將結果指派給returns。 - 計算
returns的成對相關係數,將結果指派給correlations並印出。 - 使用
seaborn繪製帶註記的每日報酬相關性熱度圖,標題為 'Daily Return Correlations'。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Inspect stock_prices here
print(____)
# Calculate the daily returns
returns = ____
# Calculate and print the pairwise correlations
correlations = ____
print(____)
# Plot a heatmap of daily return correlations