在 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