CommencezCommencez gratuitement

Simuler les paiements périodiques (II)

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

Dans cet exemple, le code de traçage est déjà prêt. Vous n'avez donc qu'à compléter la logique à l'intérieur de la boucle for et l'initialisation des variables qui seront mises à jour à chaque itération.

Cette activité fait partie du cours

Introduction aux concepts financiers en Python

Voir le cours

Instructions de l’exercice

  • Enregistrez 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.
  • 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 ce code d’exemple.

# 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