Class: Finance::Calculations
- Inherits:
-
Object
- Object
- Finance::Calculations
- Defined in:
- lib/finance/calculations.rb
Defined Under Namespace
Classes: Function
Class Method Summary collapse
-
.irr(values) ⇒ Float
(also: internal_return_rate)
IRR computes the Rate of Interest per period.
-
.mirr(values, rate, reinvest_rate) ⇒ Float
MIRR computes the modified Rate of Interest.
-
.npv(rate, values) ⇒ Numeric
(also: net_present_value)
Npv computes the Net Present Value of a cash flow series.
Class Method Details
.irr(values) ⇒ Float Also known as: internal_return_rate
IRR computes the Rate of Interest per period.
48 49 50 51 52 53 54 55 56 |
# File 'lib/finance/calculations.rb', line 48 def irr(values) return 0.0 unless correct_cashflows?(values) func = BigDecimal.limit(100) func = Function.new(values) rate = [ func.one ] nlsolve(func, rate) rate[0].to_f end |
.mirr(values, rate, reinvest_rate) ⇒ Float
MIRR computes the modified Rate of Interest.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/finance/calculations.rb', line 73 def mirr(values, rate, reinvest_rate) inflows = []; outflows = []; # We prefer manual enumeration over the partition # because of the need to replace outflows with zeros. values.each do |val| if val >= 0 inflows << val outflows << 0.0 else outflows << val inflows << 0.0 end end if outflows.all?(0.0) || inflows.all?(0.0) return 0.0 end fv = npv(reinvest_rate, inflows).abs pv = npv(rate, outflows).abs return (fv/pv) ** (1.0/(values.size - 1)) * (1 + reinvest_rate) - 1 end |
.npv(rate, values) ⇒ Numeric Also known as: net_present_value
Npv computes the Net Present Value of a cash flow series.
25 26 27 28 29 30 31 |
# File 'lib/finance/calculations.rb', line 25 def npv(rate, values) npv_value = 0.0 values.each_with_index do |current_value, pow_index| npv_value += current_value / (1.0 + rate) ** pow_index end npv_value end |