Created
July 9, 2010 22:35
-
-
Save jasondew/470191 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'algebra' | |
class IRR | |
def self.calculate(profits) | |
begin | |
function(profits).zero | |
rescue Algebra::MaximumIterationsReached => mir | |
nil | |
end | |
end | |
private | |
def self.function(profits) | |
Algebra::Function.new do |x| | |
sumands = Array.new | |
profits.each_with_index {|profit, index| sumands << profit.to_f / (1 + x) ** index } | |
sumands.inject(0) {|sum, sumand| sum + sumand } | |
end | |
end | |
end | |
puts IRR.calculate([-100, 30, 35, 40, 45]) | |
puts IRR.calculate([-1, 1]) | |
puts IRR.calculate([]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment