finance 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,3 +1,9 @@
1
+ = Version 1.1.2
2
+ 16 Jun 2012
3
+
4
+ * Bugfix: Array#irr and Array#xirr check for a valid sequence of cash flows.
5
+ * Bugfix: Integer#months and Integer#years no longer collide with Rails methods.
6
+
1
7
  = Version 1.1.0
2
8
  11 Sep 2011
3
9
 
@@ -44,6 +44,12 @@ module Finance
44
44
  # @see http://en.wikipedia.org/wiki/Internal_rate_of_return
45
45
  # @api public
46
46
  def irr
47
+ # Make sure we have a valid sequence of cash flows.
48
+ positives, negatives = self.partition{ |i| i >= 0 }
49
+ if positives.empty? || negatives.empty?
50
+ raise ArgumentError, "Calculation does not converge."
51
+ end
52
+
47
53
  func = Function.new(self, :npv)
48
54
  rate = [ func.one ]
49
55
  n = nlsolve( func, rate )
@@ -83,6 +89,12 @@ module Finance
83
89
  # @transactions.xirr(0.6) #=> Rate("0.024851", :apr, :compounds => :annually)
84
90
  # @api public
85
91
  def xirr(iterations=100)
92
+ # Make sure we have a valid sequence of cash flows.
93
+ positives, negatives = self.partition{ |t| t.amount >= 0 }
94
+ if positives.empty? || negatives.empty?
95
+ raise ArgumentError, "Calculation does not converge."
96
+ end
97
+
86
98
  func = Function.new(self, :xnpv)
87
99
  rate = [ func.one ]
88
100
  n = nlsolve( func, rate )
@@ -1,19 +1,13 @@
1
1
  class Integer
2
- # convert an integer value representing months into months
2
+ # convert an integer value representing months (or years) into months
3
3
  # @return [Integer] the number of months
4
4
  # @example
5
5
  # 360.months #=> 360
6
- # @api public
7
- def months
8
- self
9
- end
10
-
11
- # convert an integer value representing years into months
12
- # @return [Integer] the number of months
13
- # @example
14
6
  # 30.years #=> 360
15
7
  # @api public
16
- def years
17
- self * 12
8
+ def method_missing(name, *args, &block)
9
+ return self if name.to_s == "months"
10
+ return self * 12 if name.to_s == "years"
11
+ super
18
12
  end
19
13
  end
@@ -11,7 +11,9 @@ class TestCashflows < Test::Unit::TestCase
11
11
  context "an array of numeric cashflows" do
12
12
  should "have an Internal Rate of Return" do
13
13
  assert_equal D("0.143"), [-4000,1200,1410,1875,1050].irr.round(3)
14
+ assert_raises(ArgumentError) { [10,20,30].irr }
14
15
  end
16
+
15
17
  should "have a Net Present Value" do
16
18
  assert_equal D("49.211"), [-100.0, 60, 60, 60].npv(0.1).round(3)
17
19
  end
@@ -26,6 +28,7 @@ class TestCashflows < Test::Unit::TestCase
26
28
 
27
29
  should "have an Internal Rate of Return" do
28
30
  assert_equal D("0.024851"), @xactions.xirr.effective.round(6)
31
+ assert_raises(ArgumentError) { @xactions[1,2].xirr }
29
32
  end
30
33
 
31
34
  should "have a Net Present Value" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-14 00:00:00.000000000 Z
12
+ date: 2012-06-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: flt
16
- requirement: &2151813040 !ruby/object:Gem::Requirement
16
+ requirement: &2152191000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 1.3.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2151813040
24
+ version_requirements: *2152191000
25
25
  description: The finance library provides a Ruby interface for working with interest
26
26
  rates, mortgage amortization, and cashflows (NPV, IRR, etc.).
27
27
  email: wkranec@gmail.com
@@ -40,7 +40,6 @@ files:
40
40
  - lib/finance/amortization.rb
41
41
  - lib/finance/cashflows.rb
42
42
  - lib/finance/decimal.rb
43
- - lib/finance/gistfile1.rb
44
43
  - lib/finance/interval.rb
45
44
  - lib/finance/rates.rb
46
45
  - lib/finance/transaction.rb
@@ -1,25 +0,0 @@
1
- require_relative 'cashflows'
2
- require_relative 'transaction'
3
-
4
- require 'time'
5
- include Finance
6
-
7
- trans = [
8
- Transaction.new(-1000, :date => Time.parse("2011-01-01")),
9
- Transaction.new(100, :date => Time.parse("2011-02-11")),
10
- Transaction.new(100, :date => Time.parse("2011-03-11")),
11
- Transaction.new(100, :date => Time.parse("2011-04-11")),
12
- Transaction.new(100, :date => Time.parse("2011-05-11")),
13
- Transaction.new(100, :date => Time.parse("2011-06-11")),
14
- Transaction.new(100, :date => Time.parse("2011-07-11")),
15
- Transaction.new(100, :date => Time.parse("2011-08-11")),
16
- Transaction.new(100, :date => Time.parse("2011-09-11")),
17
- Transaction.new(100, :date => Time.parse("2011-10-11")),
18
- Transaction.new(100, :date => Time.parse("2011-11-11")),
19
- Transaction.new(100, :date => Time.parse("2011-12-11")),
20
- Transaction.new(100, :date => Time.parse("2012-01-11"))
21
- # Transaction.new(-100, :date => Time.parse("2011-01-11")),
22
- # Transaction.new(150, :date => Time.parse("2012-01-11"))
23
- ]
24
-
25
- puts trans.xirr.effective.round(9)