Module: Finance::Cashflow

Included in:
Array
Defined in:
lib/finance/cashflows.rb

Overview

Provides methods for working with cash flows (collections of transactions)

Instance Method Summary collapse

Instance Method Details

#irr(iterations = 100) ⇒ DecNum

calculate the internal rate of return for a sequence of cash flows

Examples:

[-4000,1200,1410,1875,1050].irr #=> 0.143

Returns:

  • (DecNum)

    the internal rate of return

See Also:



13
14
15
16
17
18
19
20
21
22
# File 'lib/finance/cashflows.rb', line 13

def irr(iterations=100)
  self.collect! { |entry| entry.to_d }

  rate, investment = 1.to_d, self[0]
  iterations.times do
    rate *= (1 - self.npv(rate) / investment)
  end
  
  rate
end

#npv(rate) ⇒ DecNum

calculate the net present value of a sequence of cash flows

Examples:

[-100.0, 60, 60, 60].npv(0.1) #=> 49.211

Parameters:

  • rate (Numeric)

    the discount rate to be applied

Returns:

  • (DecNum)

    the net present value

See Also:



31
32
33
34
35
36
37
38
39
40
# File 'lib/finance/cashflows.rb', line 31

def npv(rate)
  self.collect! { |entry| entry.to_d }

  rate, total = rate.to_d, 0.to_d
  self.each_with_index do |cashflow, index|
    total += cashflow / (1 + rate) ** index
  end

  total
end

#sumNumeric

Returns the total value of a sequence of cash flows.

Returns:

  • (Numeric)

    the total value of a sequence of cash flows



44
45
46
# File 'lib/finance/cashflows.rb', line 44

def sum
  self.inject(:+)
end

#xirrObject



48
49
# File 'lib/finance/cashflows.rb', line 48

def xirr
end