CommencerCommencer gratuitement

Investing a percentage of your income (II)

To finish up your investment simulation, you will need to loop through each time period, calculate the growth of any investments you have already made, add your new monthly deposit, and calculate your net worth at each point in time.

You can do it!

Cumulative savings (cumulative_savings_new) from the previous exercise is available, and investment_portfolio and net_worth are pre-allocated empty numpy arrays of length equal to forecast_months.

Cet exercice fait partie du cours

Introduction to Financial Concepts in Python

Afficher le cours

Instructions

  • For each period, set your previous_investment equal to the previous value of investment_portfolio, unless it is the first iteration, in which case you have no investments yet.
  • Calculate your net worth at each time period by summing your cumulative savings and investment portfolio at that same time period.
  • Run the provided code to see a plot of net worth vs. savings and investments.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

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)
Modifier et exécuter le code