將收入的一定比例用於投資(II)
為了完成你的投資模擬,你需要迴圈走訪每個時間期,計算既有投資的成長、加入本月的新存入金額,並在每個時間點計算你的淨資產。
你一定可以做到!
上一個練習中的累積儲蓄(cumulative_savings_new)已可使用,另外 investment_portfolio 與 net_worth 已預先配置為長度等於 forecast_months 的空 numpy 陣列。
本練習屬於課程
Python 金融概念入門
練習說明
- 對於每一期,將
previous_investment設為investment_portfolio的前一期值;但若為第一次迭代,代表你尚未有任何投資,請設為 0。 - 於每個時間期,將同一期的累積儲蓄與投資組合相加,計算你的淨資產。
- 執行提供的程式碼,以查看淨資產、儲蓄與投資的走勢圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
import numpy as np
# Loop through each forecast period
for i in range(forecast_months):
# Find the previous investment deposit amount
if i == 0:
previous_investment = ____
else:
previous_investment = ____
# Calculate the value of your previous investments, which have grown
previous_investment_growth = previous_investment*(1 + investment_rate_monthly)
# Add your new deposit to your investment portfolio
investment_portfolio[i] = previous_investment_growth + investment_deposit_forecast[i]
# Calculate your net worth at each point in time
net_worth[i] = ____
# Plot your forecasted cumulative savings vs investments and net worth
plot_investments(investment_portfolio, cumulative_savings_new, net_worth)