Module: Xirr::Base

Extended by:
ActiveSupport::Concern
Included in:
Bisection, NewtonMethod
Defined in:
lib/xirr/base.rb

Overview

Base module for XIRR calculation Methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cfObject (readonly)

Returns the value of attribute cf.



8
9
10
# File 'lib/xirr/base.rb', line 8

def cf
  @cf
end

Instance Method Details

#initialize(cf) ⇒ Object

Must provide the calling Cashflow in order to calculate

Parameters:



12
13
14
# File 'lib/xirr/base.rb', line 12

def initialize(cf)
  @cf = cf
end

#periods_from_start(date) ⇒ Rational

Calculates days until last transaction

Parameters:

  • date (Date)

Returns:

  • (Rational)


19
20
21
# File 'lib/xirr/base.rb', line 19

def periods_from_start(date)
  (date - cf.min_date) / cf.period
end

#xnpv(rate) ⇒ BigDecimal

Net Present Value function that will be used to reduce the cashflow

Parameters:

  • rate (BigDecimal)

Returns:

  • (BigDecimal)


26
27
28
29
30
31
# File 'lib/xirr/base.rb', line 26

def xnpv(rate)
  cf.inject(0) do |sum, t|
    # sum + (xnpv_c rate, t.amount, periods_from_start(t.date))
    sum + t.amount / (1+rate.to_f) ** periods_from_start(t.date)
  end
end