Skip to content

Commit

Permalink
New Bisection Method
Browse files Browse the repository at this point in the history
  • Loading branch information
tubedude committed Aug 8, 2014
1 parent 3a026e5 commit 1e60e3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## Version 0.2.6

* New Bisection method to avoid not converging to negative IRRs if not enough precision

## Version 0.2.5

* Default XIRR does not raise an Exception if cashflow is invalid


## Version 0.2.4

* Cashflow Invalid Messages are now public.
Expand Down
21 changes: 19 additions & 2 deletions lib/xirr/bisection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def xirr(midpoint = nil)
while ((right - left).abs > Xirr::EPS && runs < Xirr.config.iteration_limit.to_i) do

runs += 1
npv_positive?(midpoint) == npv_positive?(left) ? left = midpoint : right = midpoint
midpoint = format_irr(left, right)
left, midpoint, right = bisection(left, midpoint, right)

end

Expand All @@ -35,6 +34,24 @@ def xirr(midpoint = nil)

private

# @param left [BigDecimal]
# @param midpoint [BigDecimal]
# @param right [BigDecimal]
# @return [Array]
# Calculates the Bisections
def bisection(left, midpoint, right)
_left, _mid = npv_positive?(left), npv_positive?(midpoint)
if _left && _mid
return left, left, left if npv_positive?(right) # Not Enough Precision in the left to find the IRR
else
if _left == _mid
return midpoint, format_irr(midpoint, right), right # Result is to the Right
else
return left, format_irr(left, midpoint), midpoint # Result is to the Left
end
end
end

# @param midpoint [Float]
# @return [Bolean]
# Returns true if result is to the right ot the range
Expand Down
2 changes: 1 addition & 1 deletion lib/xirr/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Xirr
# Version of the Gem
VERSION = "0.2.5"
VERSION = "0.2.6"
end

0 comments on commit 1e60e3c

Please sign in to comment.