将收入的一定比例用于投资(II)
为了完成这次投资模拟,您需要遍历每个时间段,计算已有投资的增长,加入新的每月存入金额,并在每个时间点计算您的净资产。
您可以做到!
上一个练习中的累计储蓄(cumulative_savings_new)已可用,investment_portfolio 和 net_worth 是预先分配的空 numpy 数组,长度等于 forecast_months。
本练习是课程的一部分
Python 金融概念入门
练习说明
- 对于每一期,如果不是第一次迭代,请将
previous_investment设为investment_portfolio的上一个值;如果是第一次迭代,则您尚无投资。 - 在每个时间段,通过将同一时间段的累计储蓄与投资组合求和来计算您的净资产。
- 运行提供的代码,查看净资产与储蓄和投资的对比图。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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)