Sha256: 14a2421036c1bba3dc26e0841dcb9c88f7c172833da99eff19f023de6b5fe9e3
Contents?: true
Size: 1.33 KB
Versions: 8
Compression:
Stored size: 1.33 KB
Contents
require 'bigdecimal/newton' include Newton module Xirr # Class to calculate IRR using Newton Method class NewtonMethod include Base # Base class for working with Newton's Method. # @api private class Function values = { eps: Xirr::EPS, one: "1.0", two: "2.0", ten: "10.0", zero: "0.0" } # define default values values.each do |key, value| define_method key do BigDecimal.new(value, Xirr::PRECISION) end end # @param transactions [Cashflow] # @param function [Symbol] # Initializes the Function with the Cashflow it will use as data source and the funcion to reduce it. def initialize(transactions, function) @transactions = transactions @function = function end # Necessary for #nlsolve # @param x [BigDecimal] def values(x) value = @transactions.send(@function, BigDecimal.new(x[0].to_s, Xirr::PRECISION)) [BigDecimal.new(value.to_s, Xirr::PRECISION)] end end # Calculates XIRR using Newton method # @return [BigDecimal] # @param guess [Float] def xirr(guess=nil) func = Function.new(self, :xnpv) rate = [guess || cf.irr_guess.to_f] nlsolve(func, rate) rate[0].round Xirr::PRECISION end end end
Version data entries
8 entries across 8 versions & 1 rubygems