開始使用免費開始

在 Google 與 Apple 的投資中,$1,000 的累積報酬 I

把你剛學會計算累積報酬的能力用在實務上吧。我們來比較一下:如果在 2010 年把 $1,000 投資在 Google("GOOG")或 Apple("AAPL"),最後各自會變成多少。

本練習屬於課程

Manipulating Time Series Data in 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
  • cumulative_return 乘上 investment,並將結果繪圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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 

編輯並執行程式碼