-
-
Save kenglishhi/1698473 to your computer and use it in GitHub Desktop.
XIRR Function Written for Ruby 1.9.2
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 'date' | |
require 'bigdecimal/newton' | |
require 'bigdecimal/math' | |
include Newton | |
class XIRR | |
def initialize(datevalues) | |
@datevalues = datevalues | |
@zero = BigDecimal::new("0.0") | |
@one = BigDecimal::new("1.0") | |
@two = BigDecimal::new("2.0") | |
@ten = BigDecimal::new("10.0") | |
@eps = BigDecimal::new("1.0e-16") | |
end | |
def eps; @eps end | |
def one; @one end | |
def two; @two end | |
def ten; @ten end | |
def zero; @zero end | |
def values(x) | |
initial = @datevalues[0][:date] | |
xirr = Array.new | |
puts "x = #{x}" | |
xirr << @datevalues.reduce(0) do |acc, transaction| | |
exponent = (transaction[:date] - initial) / BigDecimal::new("365") | |
#nth = (@one + x[0]) ** exponent | |
nth = BigMath.exp(exponent * BigMath.log(@one + x[0], 15), 15) | |
iterant = transaction[:value] / nth | |
acc + iterant | |
end | |
xirr | |
end | |
end | |
trans = Array.new | |
trans << {value: BigDecimal.new('31206'), date: Date.parse('Thu, 31 Mar 2011')} | |
trans << {value: BigDecimal.new('-75580'), date: Date.parse('Thu, 31 Mar 2011')} | |
trans << {value: BigDecimal.new('-5154'), date: Date.parse('Wed, 30 Mar 2011')} | |
xirrfunc = XIRR.new(trans) | |
z = [xirrfunc.zero] | |
begin | |
nlsolve(xirrfunc, z) | |
rescue => e | |
puts "PROBLEM #{e.inspect}" | |
end | |
puts "Final: #{(z[0].to_f*100).round(2)}%" | |
trans = Array.new | |
trans << { value: BigDecimal.new("-1500000"), date: Date.parse("2010-09-07")} | |
trans << { value: BigDecimal.new("1287560"), date: Date.parse("2011-12-27")} | |
xirrfunc = XIRR.new(trans) | |
z = [xirrfunc.zero] | |
nlsolve(xirrfunc, z) | |
puts "Final: #{(z[0].to_f*100).round(2)}%" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment