Build My First Mortgage Calculator by Numpy in Colab

Figure 1 Mortgage Calculator. Source: BNZ https://www.bnz.co.nz/personal-banking/home-loans/calculators/home-loan-calculator
!pip install numpy-financialimport numpy as np
import numpy_financial as npf

Slider Inputs

#@title MORTGAGE CALCULATOR - INPUT DATA
mort_rate = 2.55 #@param {type:"slider", min:0.00, max:10.00, step:0.05}
loan_amt = 700000 #@param {type:"slider", min:100000, max:1500000, step:10000}
repay_yr = 30 #@param {type:"slider", min:1, max:40, step:1}
Figure 2 Sliders for Data Input

Calculate Monthly Repayment Amount

#Mortgage Calculator - monthly repayment amount
rate_x = mort_rate/1200 #monthly mortgage rate in 2 decimal places, 2% -> 0.02
nper_x = repay_yr*12 #duration of loan in number of months
pv_x = -1*loan_amt #loan principle amount in NZD as negative cash flow
pmt_x1 = npf.pmt(rate_x, nper_x, pv_x) #monthly mortgage repayment amount
print("Monthly Repayment Amount: NZD", np.round(pmt_x1,2))
Monthly Repayment Amount: NZD 2784.08

Amortization Schedule:

#Amortization Table - mortgage interest and principle repayment schedule
per_x = np.arange(nper_x) + 1
ipmt_x = npf.ipmt(rate_x, per_x, nper_x, pv_x) #monthly mortgage interest amount
ppmt_x = npf.ppmt(rate_x, per_x, nper_x, pv_x) #monthly mortgage principle amount
fmt = '{0:2d} {1:8.2f} {2:8.2f} {3:8.2f} {4:8.2f}'#format
for paymentno in per_x:
index = paymentno - 1
pmt_x = ipmt_x + ppmt_x
pv_x = pv_x + ppmt_x[index]
print(fmt.format(paymentno, ppmt_x[index], ipmt_x[index], pmt_x[index], pv_x))
Figure 3 Amortization Schedule
#Total Amount of Interest 
totint_x = np.sum(ipmt_x)
print("Total Amount of Interest Paid: NZD", np.round(totint_x, 2))
Total Amount of Interest Paid: NZD 302268.02

What-if Question

whatif_pmt_x = 4000
whatif_nper = npf.nper(rate_x, whatif_pmt_x, pv_x)
print("New Repayment Period - Number of Months:", np.round(whatif_nper,0))
New Repayment Period - Number of Months: 219.0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store