Class: Xirr::NewtonMethod

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/xirr/newton_method.rb

Overview

Class to calculate IRR using Newton Method

Defined Under Namespace

Classes: Function

Instance Attribute Summary

Attributes included from Base

#cf

Instance Method Summary collapse

Methods included from Base

#initialize, #periods_from_start, #xnpv

Instance Method Details

#xirr(guess, options) ⇒ BigDecimal

Calculates XIRR using Newton method

Parameters:

  • guess (Float)

Returns:

  • (BigDecimal)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/xirr/newton_method.rb', line 47

def xirr guess, options
  func = Function.new(self, :xnpv)
  rate = [guess || cf.irr_guess]
  begin
    nlsolve(func, rate)
    (rate[0] <= -1 || rate[0].nan?) ? nil : rate[0].round(Xirr::PRECISION)

      # rate[0].round(Xirr::PRECISION)
  rescue
    nil
  end
end