Module: Finrb::Cashflow
- Included in:
- Array
- Defined in:
- lib/finrb/cashflows.rb
Overview
Provides methods for working with cash flows (collections of transactions)
Defined Under Namespace
Classes: Function
Instance Method Summary collapse
-
#irr(guess = nil) ⇒ Flt::DecNum
calculate the internal rate of return for a sequence of cash flows.
- #method_missing(name, *args, &block) ⇒ Object
-
#npv(rate) ⇒ Flt::DecNum
calculate the net present value of a sequence of cash flows.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#xirr(guess = nil) ⇒ Rate
calculate the internal rate of return for a sequence of cash flows with dates @param Initial guess rate, Deafults to 1.0.
-
#xnpv(rate) ⇒ Flt::DecNum
calculate the net present value of a sequence of cash flows.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
61 62 63 64 65 |
# File 'lib/finrb/cashflows.rb', line 61 def method_missing(name, *args, &block) return sum if name.to_s == 'sum' super end |
Instance Method Details
#irr(guess = nil) ⇒ Flt::DecNum
calculate the internal rate of return for a sequence of cash flows
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/finrb/cashflows.rb', line 50 def irr(guess = nil) # Make sure we have a valid sequence of cash flows. positives, negatives = partition { |i| i >= 0 } raise(ArgumentError, 'Calculation does not converge.') if positives.empty? || negatives.empty? func = Function.new(self, :npv) rate = [valid(guess)] nlsolve(func, rate) rate[0] end |
#npv(rate) ⇒ Flt::DecNum
calculate the net present value of a sequence of cash flows
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/finrb/cashflows.rb', line 78 def npv(rate) cashflows = map { |entry| Flt::DecNum.new(entry.to_s) } rate = Flt::DecNum.new(rate.to_s) total = Flt::DecNum.new(0.to_s) cashflows.each_with_index do |cashflow, index| total += cashflow / ((rate + 1)**index) end total end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
67 68 69 |
# File 'lib/finrb/cashflows.rb', line 67 def respond_to_missing?(name, include_private = false) name.to_s == 'sum' || super end |
#xirr(guess = nil) ⇒ Rate
calculate the internal rate of return for a sequence of cash flows with dates @param Initial guess rate, Deafults to 1.0
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/finrb/cashflows.rb', line 100 def xirr(guess = nil) # Make sure we have a valid sequence of cash flows. positives, negatives = partition { |t| t.amount >= 0 } if positives.empty? || negatives.empty? raise( ArgumentError, 'Calculation does not converge. Cashflow needs to have a least one positive and one negative value.' ) end func = Function.new(self, :xnpv) rate = [valid(guess)] nlsolve(func, rate) Rate.new(rate[0], :apr, compounds: Finrb.config.periodic_compound ? :continuously : :annually) end |
#xnpv(rate) ⇒ Flt::DecNum
calculate the net present value of a sequence of cash flows
125 126 127 128 129 130 131 |
# File 'lib/finrb/cashflows.rb', line 125 def xnpv(rate) rate = Flt::DecNum.new(rate.to_s) sum do |t| t.amount / ((rate + 1)**(date_diff(start, t.date) / days_in_period)) end end |