시작하기무료로 시작하기

수입의 일정 비율 투자하기 (II)

이제 투자 시뮬레이션을 마무리해 볼까요? 각 기간을 반복(loop)하면서, 이미 보유한 투자금의 성장분을 계산하고, 새로 납입하는 월 적립금을 더한 뒤, 각 시점의 순자산(net worth)을 계산해야 합니다.

할 수 있어요!

이전 연습 문제에서 만든 누적 저축(cumulative_savings_new)이 제공되며, investment_portfolionet_worthforecast_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)
코드 편집 및 실행