Tuesday, February 16, 2010

mort_calc gem

I recently published the mort_calc gem at gemcutter.org. The code can be found at http://github.com/perry3819/mort_calc/.

The gem calculates the APR and monthly payment for a mortgage in the United States.

Calculating the monthly payment is straight forward.


C = Loan amount

E = Extra costs

r = monthly interest rate = interest rate / 1200

N = amortization term in months

An iterative approach is needed to find the APR. The equation for the APR follows.


a = APR/1200

N = amortization term in months

P = Monthly payment (including all fees paid by borrower)

C = Loan amount

The following graph shows the APR calculation plotted for N = 360, P = 2500, and C = 400,000. The APR is where the line crosses the A axis.


I chose the Newton-Raphson method to quickly find a precise solution.

Screen shot 2010-02-16 at 1.51.57 PM

The MortgageUtil.calculate_apr method provides a starting value of the interest rate, which makes the Newton-Raphson converge on a accurate solution between 1 and 3 passes.


% sudo gem install gemcutter

% gem tumble

% sudo gem install mortgage_calc


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]