开始使用免费开始使用

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 

编辑并运行代码