Simulating periodic payments (I)
You have all the tools you'll need to simulate the mortgage payments over time.
Every time a mortgage payment is made, the following payment will have a slightly lower percentage, which is used to pay off interest. This means that more of the remainder will go towards the portion of the home that you own instead of the bank. This is important to determine how much you will gain from selling the home before paying off your mortgage, or to determine when your mortgage is underwater. But more on that later.
You will now write a simple program to calculate the interest and mortgage portions of each payment over time.
The mortgage_loan
, mortgage_rate_periodic
, and periodic_mortgage_payment
variables from the third exercise are available for use.
The principal_remaining
variable is initialized as an array of 0's with length equal to the number of payment periods.
This exercise is part of the course
Introduction to Financial Concepts in Python
Exercise instructions
- Set the
previous_principal_remaining
for the first period, i.e. when(i == 0)
, equal to the mortgage loan value. - Set the
previous_principal_remaining
for all other periods equal to the remaining principal from the previous period ([i - 1]
). - Calculate the interest payment (the product of previous principal remaining and the periodic mortgage rate) and principal payment (the difference between periodic mortgage payment and the interest paid) for each period.
Hands-on interactive exercise
Have a go at this exercise by completing this sample 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 = ____
else:
previous_principal_remaining = ____
# Calculate the interest and principal payments
interest_payment = round(____*____, 2)
principal_payment = round(____-____, 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 principal remaining values in an array
principal_remaining[i] = previous_principal_remaining - principal_payment
# Print the payments for the first few periods
print_payments(i, interest_payment, principal_payment, principal_remaining)