Monday, February 9, 2015

Our dream home, and a monthly mortgage rate calculator in ruby!


We just finished the construction of our dream home:



Now that construction is complete, we are converting from a construction loan into a normal mortgage, refinancing to get the best rate.   We are doing all the normal trade-offs, and being the geek that I am, I wanted to have a deeper look to find out if paying points was worth it, when the break-evens would be, etc.

So, I decided to write a short little ruby program that I could play around with.

It turns out that the math behind the monthly payment is actually kind of neat.  Have a look at the derivation for a fixed rate mortgage:
http://en.wikipedia.org/wiki/Fixed-rate_mortgage

With the math already done, it was a simple problem to code.  Here is my work:
def money (x)
  sprintf("%05.2f", x)
end

balance = 500000
annual_rate = 3.25.to_f * 0.01
monthly_rate = annual_rate / 12.to_f
n = 15*12

puts "Loan term (Number of payments) [#{n}]"
puts "Annual interest rate [#{annual_rate*100}]"
puts "Monthly interest rate [#{monthly_rate}]"
term = (1 + monthly_rate)**n
puts "Term = [#{term}]"

monthly_payment = balance * (monthly_rate * term / (term - 1))
puts "Monthly Payment = [#{money(monthly_payment)}]"

while (balance > 0)
  interest_payment = balance * monthly_rate
  principal_payment = monthly_payment - interest_payment
  balance = balance - principal_payment
  puts("Interest [$#{money(interest_payment)}], Principal [$#{money(principal_payment)}], Balance = [$#{money(balance)}]")
end

Up top of the ruby are all the parameters.  For an example, I used $500K @ 3.25% for 15 years. (n = 15*12, where n is the number of months for the loan)

Then, you will see the loop where it outputs the principle payment, the interest payment, and the remaining balance.  For each month, your interest payment is always the interest rate multiplied by the remaining balance.  Then, whatever the difference is between that and the monthly payment is what gets applied to the principal.

It's kind of neat, and you can run multiple scenarios based on different initial outlays and percentage rates.

Hopefully someone finds this useful.  If not, no worries, it was my mental yoga for the day.

No comments: