Class: MortgageBuddy::AprCalculator
- Inherits:
-
Object
- Object
- MortgageBuddy::AprCalculator
- Defined in:
- lib/mortgage_buddy/apr_calculator.rb
Instance Attribute Summary collapse
-
#loan_amount ⇒ Object
Returns the value of attribute loan_amount.
-
#monthly_interest_rate ⇒ Object
Returns the value of attribute monthly_interest_rate.
-
#monthly_payment_with_fees ⇒ Object
Returns the value of attribute monthly_payment_with_fees.
-
#period ⇒ Object
Returns the value of attribute period.
Instance Method Summary collapse
-
#apr ⇒ Object
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).
-
#initialize(attributes = {}) ⇒ AprCalculator
constructor
A new instance of AprCalculator.
-
#newton_raphson(f, f_deriv, start, precision = 5) ⇒ Object
if ‘start’ is the monthly_interest_rate, Newton Raphson will find the apr root very quickly k1 = k0 - f(k0)/f’(k0) k_plus_one = k - f(k)/f_deriv(k) We find the k-intercept of the tangent line at point k_plus_one and compare k to k_plus_one.
Constructor Details
#initialize(attributes = {}) ⇒ AprCalculator
5 6 7 8 9 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 5 def initialize(attributes={}) attributes.each do |name, value| send("#{name}=", value) end end |
Instance Attribute Details
#loan_amount ⇒ Object
Returns the value of attribute loan_amount.
3 4 5 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 3 def loan_amount @loan_amount end |
#monthly_interest_rate ⇒ Object
Returns the value of attribute monthly_interest_rate.
3 4 5 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 3 def monthly_interest_rate @monthly_interest_rate end |
#monthly_payment_with_fees ⇒ Object
Returns the value of attribute monthly_payment_with_fees.
3 4 5 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 3 def monthly_payment_with_fees @monthly_payment_with_fees end |
#period ⇒ Object
Returns the value of attribute period.
3 4 5 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 3 def period @period end |
Instance Method Details
#apr ⇒ Object
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)
15 16 17 18 19 20 21 22 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 15 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 |
#newton_raphson(f, f_deriv, start, precision = 5) ⇒ Object
if ‘start’ is the monthly_interest_rate, Newton Raphson will find the apr root very quickly k1 = k0 - f(k0)/f’(k0) k_plus_one = k - f(k)/f_deriv(k) We find the k-intercept of the tangent line at point k_plus_one and compare k to k_plus_one. This is repeated until a sufficiently accurate value is reached, which can be specified with the ‘precision’ parameter
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mortgage_buddy/apr_calculator.rb', line 29 def newton_raphson(f, f_deriv, start, precision = 5) k_plus_one = start k = 0.0 while ((k - 1) * 10**precision).to_f.floor != ((k_plus_one - 1) * 10**precision).to_f.floor k = k_plus_one k_plus_one = k - f.call(k) / f_deriv.call(k) end k_plus_one end |