mortgage_calc 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest CHANGED
@@ -1,3 +1,4 @@
1
+ Manifest
1
2
  README.rdoc
2
3
  Rakefile
3
4
  Version.yml
@@ -8,4 +9,3 @@ lib/mortgage_calc.rb
8
9
  lib/mortgage_calc/mortgage_util.rb
9
10
  spec/mortgage_calc/mortgage_util_spec.rb
10
11
  spec/spec_helper.rb
11
- Manifest
data/Rakefile CHANGED
@@ -10,9 +10,9 @@ require 'spec/rake/spectask'
10
10
 
11
11
  Echoe.new("mortgage_calc", MortgageCalc::VERSION) do |p|
12
12
  p.description = "Mortgage utilities"
13
- p.url = "http://github.com/pathfinderdev/mortgage_calc"
13
+ p.url = "http://github.com/perry3819/mortgage_calc"
14
14
  p.author = "Perry Hertler"
15
- p.email = "phertler@pathf.com"
15
+ p.email = "perry@hertler.org"
16
16
  p.ignore_pattern = ["tmp/*", "script/*, .idea/*"]
17
17
  p.development_dependencies = []
18
18
  end
data/Version.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 5
4
+ :patch: 6
@@ -11,20 +11,19 @@ module MortgageCalc
11
11
  end
12
12
 
13
13
  def apr
14
- calculate_apr_with_newton_raphson(self.period, monthly_payment_with_fees, self.loan_amount, monthly_interst_rate + 1)
14
+ @apr ||= calculate_apr
15
15
  end
16
16
 
17
17
  def monthly_payment
18
- calculate_monthly_payment(self.loan_amount, monthly_interst_rate, self.period)
18
+ @monthly_payment ||= calculate_monthly_payment(self.loan_amount, monthly_interst_rate, self.period)
19
19
  end
20
20
 
21
21
  def monthly_payment_with_fees
22
- calculate_monthly_payment(self.loan_amount + total_fees, monthly_interst_rate, self.period)
22
+ @monthly_payment_with_fees ||= calculate_monthly_payment(self.loan_amount + total_fees, monthly_interst_rate, self.period)
23
23
  end
24
24
 
25
25
  def total_fees
26
- buyer_points = self.points <= 0 ? 0 : self.points
27
- self.lender_fee + (self.loan_amount * buyer_points.abs/100)
26
+ @total_fees ||= calculate_total_fees
28
27
  end
29
28
 
30
29
  private
@@ -36,23 +35,39 @@ module MortgageCalc
36
35
  amount * (monthly_rate/(1 - (1 + monthly_rate)**(-period)))
37
36
  end
38
37
 
38
+ def calculate_total_fees
39
+ buyer_points = self.points <= 0 ? 0 : self.points
40
+ self.lender_fee + (self.loan_amount * buyer_points.abs/100)
41
+ end
42
+
39
43
  # solves APR
40
44
  # where a = APR/1200, N = period, P = monthly payment, C = loan_amount
45
+ # [a (1 + a)^N] / [(1 + a)^N - 1] - P/C = 0
41
46
  # calculate APR uses the Newton-Raphson to find the root (the value for 'a' that makes f(a) = 0)
42
47
  # for best performance call this with 'start'= interest rate
43
- def calculate_apr_with_newton_raphson(periods, monthly_payment, loan_amount, start=1.1)
44
- payment_ratio = monthly_payment / loan_amount
45
- f = lambda {|k| (k**(periods + 1) - (k**periods * (payment_ratio + 1)) + payment_ratio)}
46
- f_slope = lambda { |k| ((periods + 1) * k**periods) - (periods * (payment_ratio + 1) * k**(periods - 1))}
48
+ def calculate_apr
49
+ payment_ratio = monthly_payment_with_fees / loan_amount
50
+ f = lambda {|k| (k**(self.period + 1) - (k**self.period * (payment_ratio + 1)) + payment_ratio)}
51
+ f_slope = lambda { |k| ((self.period + 1) * k**self.period) - (self.period * (payment_ratio + 1) * k**(self.period - 1))}
52
+
53
+ root = newton_raphson(f, f_slope, monthly_interst_rate + 1)
54
+ 100 * 12 * (root - 1).to_f
55
+ end
47
56
 
57
+ # if 'start' is the monthly_interest_rate, Newton Raphson will find the apr root very quickly
58
+ # k1 = k0 - f(k0)/f'(k0)
59
+ # k_plus_one = k - f(k)/f_slope(k)
60
+ # We find the k-intercept of the tangent line at point k_plus_one and compare k to k_plus_one.
61
+ # This is repeated until a sufficiently accurate value is reached, which can be specified with the 'precision' parameter
62
+ def newton_raphson(f, f_slope, start, precision = 5)
48
63
  k_plus_one = start
49
64
  k = 0.0
50
65
 
51
- while ((k - 1) * 100000).to_f.floor != ((k_plus_one - 1) * 100000).to_f.floor
66
+ while ((k - 1) * 10**precision).to_f.floor != ((k_plus_one - 1) * 10**precision).to_f.floor
52
67
  k = k_plus_one
53
68
  k_plus_one = k - f.call(k) / f_slope.call(k)
54
69
  end
55
- 100 * 12 * (k_plus_one - 1).to_f
70
+ k_plus_one
56
71
  end
57
72
  end
58
73
  end
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{mortgage_calc}
5
- s.version = "0.1.5"
5
+ s.version = "0.1.6"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Perry Hertler"]
9
- s.date = %q{2010-02-13}
9
+ s.date = %q{2010-02-15}
10
10
  s.description = %q{Mortgage utilities}
11
- s.email = %q{phertler@pathf.com}
11
+ s.email = %q{perry@hertler.org}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/mortgage_calc.rb", "lib/mortgage_calc/mortgage_util.rb"]
13
- s.files = ["README.rdoc", "Rakefile", "Version.yml", "features/apr.feature", "features/step_definitions/apr_steps.rb", "features/support/env.rb", "lib/mortgage_calc.rb", "lib/mortgage_calc/mortgage_util.rb", "spec/mortgage_calc/mortgage_util_spec.rb", "spec/spec_helper.rb", "Manifest", "mortgage_calc.gemspec"]
14
- s.homepage = %q{http://github.com/pathfinderdev/mortgage_calc}
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "Version.yml", "features/apr.feature", "features/step_definitions/apr_steps.rb", "features/support/env.rb", "lib/mortgage_calc.rb", "lib/mortgage_calc/mortgage_util.rb", "spec/mortgage_calc/mortgage_util_spec.rb", "spec/spec_helper.rb", "mortgage_calc.gemspec"]
14
+ s.homepage = %q{http://github.com/perry3819/mortgage_calc}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Mortgage_calc", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{mortgage_calc}
@@ -46,16 +46,16 @@ module MortgageCalc
46
46
  @mortgage_util = MortgageUtil.new('100000', '6.0', 360, 1200, '-1.25')
47
47
  end
48
48
  it "should convert rate to float if necessary" do
49
- @mortgage_util.interest_rate.type.should == Float
49
+ @mortgage_util.interest_rate.class.should == Float
50
50
  end
51
51
  it "should convert points to float if necessary" do
52
- @mortgage_util.points.type.should == Float
52
+ @mortgage_util.points.class.should == Float
53
53
  end
54
54
  it "should convert loan_amount to float if necessary" do
55
- @mortgage_util.loan_amount.type.should == Float
55
+ @mortgage_util.loan_amount.class.should == Float
56
56
  end
57
57
  it "should convert period to integer if necessary" do
58
- @mortgage_util.period.type.should == Fixnum
58
+ @mortgage_util.period.class.should == Fixnum
59
59
  end
60
60
  end
61
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mortgage_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Perry Hertler
@@ -9,12 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-13 00:00:00 -06:00
12
+ date: 2010-02-15 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description: Mortgage utilities
17
- email: phertler@pathf.com
17
+ email: perry@hertler.org
18
18
  executables: []
19
19
 
20
20
  extensions: []
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  - lib/mortgage_calc.rb
25
25
  - lib/mortgage_calc/mortgage_util.rb
26
26
  files:
27
+ - Manifest
27
28
  - README.rdoc
28
29
  - Rakefile
29
30
  - Version.yml
@@ -34,10 +35,9 @@ files:
34
35
  - lib/mortgage_calc/mortgage_util.rb
35
36
  - spec/mortgage_calc/mortgage_util_spec.rb
36
37
  - spec/spec_helper.rb
37
- - Manifest
38
38
  - mortgage_calc.gemspec
39
39
  has_rdoc: true
40
- homepage: http://github.com/pathfinderdev/mortgage_calc
40
+ homepage: http://github.com/perry3819/mortgage_calc
41
41
  licenses: []
42
42
 
43
43
  post_install_message: