CommencerCommencer gratuitement

Simuler des paiements périodiques (II)

Vous avez décidé d’étendre votre programme de l’exercice précédent pour stocker les paiements de capital et d’intérêts effectués à chaque période, et d’afficher les résultats sous forme de graphique au lieu de simplement les imprimer.

Dans cet exemple, le code de visualisation est déjà prêt ; vous devez simplement terminer la logique à l’intérieur de la boucle for et l’initialisation des variables qui seront mises à jour à chaque itération.

Cet exercice fait partie du cours

Introduction aux concepts financiers en Python

Afficher le cours

Instructions

  • Stockez simplement interest_paid et principal_paid pour chaque période.
  • Calculez le principal_remaining pour chaque période à partir du remboursement de capital et du capital restant dû.
  • Exécutez le code fourni pour tracer les paiements mensuels d’intérêts par rapport au capital.

Exercice interactif pratique

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

# Loop through each mortgage payment period
for i in range(0, mortgage_payment_periods):
    
    # Handle the case for the first iteration
    if i == 0:
        previous_principal_remaining = mortgage_loan
    else:
        previous_principal_remaining = principal_remaining[i-1]
        
    # Calculate the interest based on the previous principal
    interest_payment = round(previous_principal_remaining*mortgage_rate_periodic, 2)
    principal_payment = round(periodic_mortgage_payment - interest_payment, 2)
    
    # Catch the case where all principal is paid off in the final period
    if previous_principal_remaining - principal_payment < 0:
        principal_payment = previous_principal_remaining
        
    # Collect the historical values
    interest_paid[i] = ____
    principal_paid[i] = ____
    principal_remaining[i] = ____
    
# Plot the interest vs principal
plt.plot(interest_paid, color="red")
plt.plot(principal_paid, color="blue")
plt.legend(handles=[interest_plot, principal_plot], loc=2)
plt.show()
Modifier et exécuter le code