411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Mortgages and Loans
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Mortgages and Loans Traveling Salesman Pension Simulator
Mathematical Treatment
At time t=0, a mortgage of amount is granted. This sum is repaid in constant installments of amount R, starting at time t=1. Therefore, if we consider the year as the unit of measurement, then R is the annual installment (knowing the monthly installment, simply multiply it by 12).
If r represents the constant interest rate used to calculate the interest on the residual debt , we have:
Calculating , i.e., the mortgage after 1 year, we have:
Calculating , i.e., the mortgage after 2 years, we have:
Calculating that is, the mortgage after 3 years is:
Therefore, the mortgage at time t will be:
Set:
Let's rewrite as:
Multiplying both sides of the equation by we have:
Adding the two equations side by side we get:
From which we obtain:
Then the mortgage at time t is equal to:
Considering the continuous sequence , its derivative can be calculated to see when it is increasing or decreasing. Therefore, we have:
so the derivative is positive and the function is increasing for , and therefore in this case the loan would never be repaid, while for the derivative is negative, the function is decreasing, so after a certain time the loan is repaid.
To calculate how long it takes for the mortgage to be repaid, we must impose the following condition:
from which we obtain the exponential equation:
Therefore, by switching to logarithms, we obtain that the time at which the mortgage is repaid is:
while the The annual payment to repay the mortgage in a yearly period at an interest rate r is:
Example calculation with Julia
Calculate the monthly payment for a €200,000 mortgage at 3.5% over 25 years. This results in a monthly payment of €1,001.25:
function mortgage_payment(principal, annual_rate, years)
r = annual_rate / 12
n = years * 12
payment = principal * r * (1 + r)^n / ((1 + r)^n - 1)
return round(payment, digits=2)
end
payment = mortgage_payment(200,000, 0.035, 25)
println("Monthly payment: €$payment")
Monthly payment: €1,001.25
Calculate the number of years required to repay a €136,000 mortgage at 3.5% with a monthly payment of €616. We get a time of 30 years:
function mortgage_or_loan_repayment_time()
M=136000;
R=616*12;
r=0.035;
print("A mortgage or loan of $M € with an interest rate of 3.5% and a monthly payment of €616 is repaid in years:")
println(log(R/(R-M*r))/log(1+r))
end
mortgage_or_loan_payback_time()
30.017775988227683
Finally, if we want to calculate the annual interest rate for a mortgage of €136,000 with a monthly payment of €616 for 30 years, using the Roots library, we obtain a rate of r=0.035 = 3.5%:
using Roots
M=136000;
R=616*12;
t=30;
y(r)=-R +(M*r*(1+r)^t) /((1+r)^t-1);
find_zero(y,0.1)
0.034973001159437175
Amortization Schedule
The amortization schedule describes how each mortgage payment is divided between interest and principal, and how the remaining debt decreases over time.
The key concept is that at the beginning, you pay mostly interest (the debt is high), while towards the end, you repay almost only the principal (French amortization).
In Julia:
function amortization_plan(principal, annual_rate, years)
r = annual_rate / 12
n = years * 12
installment = mortgage_payment(principal, annual_rate, years)
balance = principal
for month in 1:n
interest = round(balance * r, digits=2)
principal_amount = round(installment - interest, digits=2)
balance = round(balance - principal_amount, digits=2)
println("Month $month | Installment: €$installment | Interest: €$interest | Principal: €$principal_amount | Balance: €$balance")
end
end
amortization_plan(200000, 0.035, 25)
Month 1 | Installment: €1001.25 | Interest: €583.33 | Principal: €417.92 | Balance: €199,582.08
Month 2 | Installment: €1001.25 | Interest: €582.11 | Principal: €419.14 | Balance: €199,162.94
Month 3 | Installment: €1001.25 | Interest: €580.89 | Principal: €420.36 | Balance: €198,742.58
Month 4 | Installment: €1001.25 | Interest: €579.67 | Principal: €421.58 | Balance: €198,321.0
Month 5 | Installment: €1001.25 | Interest: €578.44 | Principal: €422.81 | Balance: €197,898.19
Month 6 | Installment: €1,001.25 | Interest: €577.2 | Principal: €424.05 | Balance: €197,474.14
Month 7 | Installment: €1,001.25 | Interest: €575.97 | Principal: €425.28 | Balance: €197,048.86
Month 8 | Installment: €1,001.25 | Interest: €574.73 | Principal: €426.52 | Balance: €196,622.34
Month 9 | Installment: €1,001.25 | Interest: €573.48 | Principal: €427.77 | Balance: €196,194.57
Month 10 | Installment: €1,001.25 | Interest: €572.23 | Principal: €429.02 | Balance: €195,765.55
Month 11 | Installment: €1,001.25 | Interest: €570.98 | Principal: €430.27 | Balance: €195,335.28
Month 12 | Installment: €1,001.25 | Interest: €569.73 | Principal: €431.52 | Balance: €194,903.76
Month 13 | Installment: €1,001.25 | Interest: €568.47 | Principal: €432.78 | Balance: €194,470.98
Month 14 | Installment: €1,001.25 | Interest: €567.21 | Principal: €434.04 | Balance: €194,036.94
Month 15 | Installment: €1001.25 | Interest: €565.94 | Principal: €435.31 | Balance: €193,601.63
Month 16 | Installment: €1001.25 | Interest: €564.67 | Principal: €436.58 | Balance: €193,165.05
Month 17 | Installment: €1001.25 | Interest: €563.4 | Principal: €437.85 | Balance: €192,727.2
Month 18 | Installment: €1001.25 | Interest: €562.12 | Principal: €439.13 | Balance: €192,288.07
Month 19 | Installment: €1001.25 | Interest: €560.84 | Principal: €440.41 | Balance: €191,847.66
Month 20 | Installment: €1,001.25 | Interest: €559.56 | Principal: €441.69 | Balance: €191,405.97
-------------------------------------------------------------
Month 280 | Installment: €1,001.25 | Interest: €59.4 | Principal: €941.85 | Balance: €19,423.31
Month 281 | Installment: €1,001.25 | Interest: €56.65 | Principal: €944.6 | Balance: €18,478.71
Month 282 | Installment: €1,001.25 | Interest: €53.9 | Principal: €947.35 | Balance: €17,531.36
Month 283 | Installment: €1,001.25 | Interest: €51.13 | Principal: €950.12 | Balance: €16,581.24
Month 284 | Installment: €1,001.25 | Interest: €48.36 | Principal: €952.89 | Balance: €15,628.35
Month 285 | Installment: €1,001.25 | Interest: €45.58 | Principal: €955.67 | Balance: €14,672.68
Month 286 | Installment: €1,001.25 | Interest: €42.8 | Principal: €958.45 | Balance: €13,714.23
Month 287 | Installment: €1,001.25 | Interest: €40.00 | Principal: €961.25 | Balance: €12,752.98
Month 288 | Installment: €1,001.25 | Interest: €37.20 | Principal: €964.05 | Balance: €11,788.93
Month 289 | Installment: €1,001.25 | Interest: €34.308 | Principal: €966.87 | Balance: €10,822.06
Month 290 | Installment: €1,001.25 | Interest: €31.56 | Principal: €969.69 | Balance: €9,852.37
Month 291 | Installment: €1,001.25 | Interest: €28.74 | Principal: €972.51 | Balance: €8,879.86
Month 292 | Installment: €1,001.25 | Interest: €25.9 | Principal: €975.35 | Balance: €7,904.51
Month 293 | Installment: €1,001.25 | Interest: €23.05 | Principal: €978.2 | Balance: €6,926.31
Month 294 | Installment: €1001.25 | Interest: €20.2 | Principal: €981.05 | Balance: €5945.26
Month 295 | Installment: €1001.25 | Interest: €17.34 | Principal: €983.91 | Balance: €4961.35
Month 296 | Installment: €1001.25 | Interest: €14.47 | Principal: €986.78 | Balance: €3974.57
Month 297 | Installment: €1001.25 | Interest: €11.59 | Principal: €989.66 | Balance: €2984.91
Month 298 | Installment: €1001.25 | Interest: €8.71 | Principal: €992.54 | Balance: €1992.37
Month 299 | Installment: €1001.25 | Interest: €5.81 | Principal: €995.44 | Balance: €996.93
Month 300 | Installment: €1001.25 | Interest: €2.91 | Principal: €998.34 | Balance: €-1.41