収入の一定割合を投資する(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)