Class: Xirr::NewtonMethod

Inherits:
Object
  • Object
show all
Includes:
Newton, 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)


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

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.config.precision)

      # rate[0].round(Xirr.config.precision)
  rescue
    nil
  end
end