lib/mortgage_buddy/apr_calculator.rb
def apr
      payment_ratio = self.monthly_payment_with_fees / self.loan_amount
      f             = lambda { |k| (k**(self.period + 1) - (k**self.period * (payment_ratio + 1)) + payment_ratio) }
      f_deriv       = lambda { |k| ((self.period + 1) * k**self.period) - (self.period * (payment_ratio + 1) * k**(self.period - 1)) }

      root = newton_raphson(f, f_deriv, self.monthly_interest_rate + 1)
      100 * 12 * (root - 1).to_f
    end
def apr
      payment_ratio = self.monthly_payment_with_fees / self.loan_amount
      f             = lambda { |k| (k**(self.period + 1) - (k**self.period * (payment_ratio + 1)) + payment_ratio) }
      f_deriv       = lambda { |k| ((self.period + 1) * k**self.period) - (self.period * (payment_ratio + 1) * k**(self.period - 1)) }

      root = newton_raphson(f, f_deriv, self.monthly_interest_rate + 1)
      100 * 12 * (root - 1).to_f
    end
solves APR
 [a (1 + a)^N] / [(1 + a)^N - 1] - P/C = 0
 where a = APR/1200, N = period, P = monthly payment, C = loan_amount
 calculate APR uses the Newton-Raphson to find the root (the value for 'a' that makes f(a) = 0)