繪製連續兩年投資組合報酬
「回歸到平均」在投資中也很重要。這裡你要查看標普 500 指數(S&P 500)成分公司的年報酬,分別為 2018 年與 2019 年。
sp500_yearly_returns 資料集包含三個欄位:
| variable | meaning |
|---|---|
| symbol | 可唯一識別公司的股票代號。 |
| return_2018 | 2018 年的投資績效衡量。 |
| return_2019 | 2019 年的投資績效衡量。 |
報酬為正表示投資價值上升;為負則表示價值下跌。
就像全壘打數的例子一樣,一個直觀但天真的預測是投資績效每年都維持不變,會落在 y 等於 x 的直線上。
sp500_yearly_returns 以 pandas 的 DataFrame 形式提供。
本練習屬於課程
使用 Python 中的 statsmodels 進行回歸入門
練習說明
- 建立一個新的圖形物件
fig,以便進行圖層疊加。 - 繪出 y 等於 x 的直線。這一步已為你完成。
- 使用
sp500_yearly_returns,以return_2019對return_2018繪製散佈圖,並加上線性迴歸趨勢線,不要顯示標準誤差帶。 - 設定座標軸,讓 x 與 y 軸上的距離比例相同。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a new figure, fig
fig = plt.____
# Plot the first layer: y = x
plt.axline(xy1=(0,0), slope=1, linewidth=2, color="green")
# Add scatter plot with linear regression trend line
sns.____(____)
# Set the axes so that the distances along the x and y axes look the same
plt.____(____)
# Show the plot
plt.show()