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.

5 comments:

Michel Son said...

Congratulation.Your dream comes true.Your ides are great.Really nice.Keeping this info about mortgage rate calculator in mind I can able to built my own house also.This is great thing about mortgage rate calculator.
Keeping updates.

http://homeloansingeorgia.blogspot.com/

Naomi Cruz said...

That is a good breakdown of things. People should refinance strategically, especially when the interest rates go down. It also helps to make these calculations for mortgage payments, to keep track of how much you’ve already paid, and how much you still have to pay for – any way to make it less of a problem from this point onward. Cheers!

Naomi Cruz @ 4 Pillars

Robbin06 said...

Hey Thanks for sharing this informative blog, it seems very helpful. i was looking for same kind of content about Housing loan

Hitesh Chavan said...

Thank you for sharing such great information. It is informative, For More Details Visit: house loan interest rates

Bhushanshirke said...

Thanks For Sharing Such Informative Blog On Home Loan Interest Rates.