xirr_newton_calculator 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,7 +13,7 @@ class XirrNewtonCalculator
13
13
 
14
14
  def initialize(flows, init_rate, max_iteration=1000)
15
15
  @flows = flows
16
- @x_n = init_rate < 1 ? 1 / (1 + init_rate) : 0.5
16
+ @x_n = init_rate
17
17
  @max_iteration = max_iteration
18
18
  end
19
19
 
@@ -25,7 +25,7 @@ class XirrNewtonCalculator
25
25
  @x_n = next_value(@x_n)
26
26
  iteration += 1
27
27
  end
28
- discount_factor_to_irr(@x_n)
28
+ @x_n
29
29
  end
30
30
 
31
31
  private
@@ -39,14 +39,14 @@ class XirrNewtonCalculator
39
39
  def dfdx(x)
40
40
  @flows[1..-1].inject(0) do |result, flow|
41
41
  diff_date = (Date.parse(flow.date.to_s) - Date.parse(@flows[0].date.to_s))/365
42
- result += flow.amount* diff_date * x.to_f ** (diff_date -1)
42
+ result += flow.amount * (-diff_date) / ((1.0 + x) ** (diff_date + 1))
43
43
  end
44
44
  end
45
45
 
46
46
  def f(x)
47
47
  @f_xn = @flows.inject(0) do |result, flow|
48
48
  diff_date = (Date.parse(flow.date.to_s) - Date.parse(@flows[0].date.to_s))/365
49
- result += flow.amount * x.to_f ** diff_date
49
+ result += flow.amount / ((1 + x) ** diff_date)
50
50
  end
51
51
  end
52
52
 
@@ -68,21 +68,21 @@ describe "XirrNewtonCalculator" do
68
68
 
69
69
  describe "#f" do
70
70
  it "returns 4 for x = 1" do
71
- expect(xirr_calculator.send(:f, 1)).to eq 4
71
+ expect(xirr_calculator.send(:f, 0.2).round(1)).to eq 2.7
72
72
  end
73
73
 
74
74
  it "returns -9.3 for x = 0.1" do
75
- expect(xirr_calculator.send(:f, 0.1).round(1)).to eq -9.3
75
+ expect(xirr_calculator.send(:f, 0.1).round(1)).to eq 3.3
76
76
  end
77
77
  end
78
78
 
79
79
  describe '#dfdx' do
80
- it "returns 44.9 for x = 0.1" do
81
- expect(xirr_calculator.send(:dfdx, 0.1).round(1)).to eq 44.9
80
+ it "returns -6.5 for x = 0.1" do
81
+ expect(xirr_calculator.send(:dfdx, 0.1).round(1)).to eq -6.5
82
82
  end
83
83
 
84
- it "returns 7.3 for x = 1" do
85
- expect(xirr_calculator.send(:dfdx, 1).round(1)).to eq 7.3
84
+ it "returns -3.1 for x = 1" do
85
+ expect(xirr_calculator.send(:dfdx, 1).round(1)).to eq -3.1
86
86
  end
87
87
  end
88
88
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xirr_newton_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: