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
-
#irr(iterations = 100) ⇒ DecNum
calculate the internal rate of return for a sequence of cash flows.
-
#npv(rate) ⇒ DecNum
calculate the net present value of a sequence of cash flows.
-
#sum ⇒ Numeric
The total value of a sequence of cash flows.
- #xirr ⇒ Object
Instance Method Details
#irr(iterations = 100) ⇒ DecNum
calculate the internal rate of return for a sequence of cash flows
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
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 |
#sum ⇒ Numeric
Returns the total value of a sequence of cash flows.
44 45 46 |
# File 'lib/finance/cashflows.rb', line 44 def sum self.inject(:+) end |
#xirr ⇒ Object
48 49 |
# File 'lib/finance/cashflows.rb', line 48 def xirr end |