2010 年投资 $1,000 于 Google 与 Apple 的累计收益 I
为了把您新学到的累计收益计算用于实际问题,我们来比较一下:如果在 2010 年把 $1,000 投资到 Google("GOOG")或 Apple("AAPL"),现在各自会值多少钱。
本练习是课程的一部分
Python 中的时间序列数据处理
练习说明
我们已经将 pandas 导入为 pd,并将 matplotlib.pyplot 导入为 plt。同时把 Google 和 Apple 的股价加载到了变量 data 中。
- 定义变量
investment,其值为1000。 - 对
data应用.pct_change()计算returns。 - 将
returns加 1 并赋给returns_plus_one,然后对returns_plus_one调用.cumprod(),结果赋给cumulative_return。 - 用
investment乘以cumulative_return,并绘制结果图。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define your investment
investment = ____
# Calculate the daily returns here
returns = ____
# Calculate the cumulative returns here
returns_plus_one = ____
cumulative_return = ____
# Calculate and plot the investment return here