Periodische Zahlungen simulieren (II)
Du möchtest dein Programm aus der vorherigen Übung erweitern, um die Tilgungs- und Zinszahlungen in jeder Periode zu speichern und die Ergebnisse zu plotten, anstatt sie nur auszugeben.
Für dieses Beispiel ist der Plot-Code bereits fertig. Du musst nur noch die Logik innerhalb der for-Schleife und die Initialisierung der Variablen ergänzen, die in jeder Iteration aktualisiert werden.
Diese Übung ist Teil des Kurses
Einführung in finanzielle Konzepte mit Python
Anleitung zur Übung
- Speichere einfach
interest_paidundprincipal_paidfür jede Periode. - Berechne die
principal_remainingfür jede Periode basierend auf der Tilgungszahlung und der verbleibenden Restschuld. - Führe den bereitgestellten Code aus, um die monatlichen Zins- vs. Tilgungszahlungen zu plotten.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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()