绘制连续两年的投资组合收益
均值回归在投资中同样重要。本练习将查看标准普尔 500 指数(S&P 500)成分公司的年度收益,分别来自 2018 年和 2019 年。
数据集 sp500_yearly_returns 包含 3 列:
| variable | meaning |
|---|---|
| symbol | 唯一标识公司的股票代码(ticker)。 |
| 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()