class FinancialCalculator::NetPresentValue
Calculates the present value of a series of equally spaced cashflows of unequal amounts
Attributes
cashflows[R]
@return [Arrray<Numeric>] An array of cashflow amounts @api public
rate[R]
@return [Numeric] The rates used for calculating the present value @api public
result[R]
@return [DecNum] Result of the XNPV calculation @api public
Public Class Methods
new(rate, cashflows)
click to toggle source
Create a new net present value calculation @see en.wikipedia.org/wiki/Net_present_value @param [Numeric] rate The discount (interest) rate @param [Array<Numeric>] cashflows An array of cashflows @return [FinancialCalculator::Npv] An instance of a Net Present Value calculation
# File lib/financial_calculator/npv.rb, line 22 def initialize(rate, cashflows) raise ArgumentError.new("Rate must be a Numeric. Got #{rate.class} instead") unless rate.is_a? Numeric @rate = rate @cashflows = cashflows @result = solve(rate, cashflows) end
Public Instance Methods
inspect()
click to toggle source
# File lib/financial_calculator/npv.rb, line 30 def inspect "NPV(#{result})" end
Private Instance Methods
solve(rate, cashflows)
click to toggle source
# File lib/financial_calculator/npv.rb, line 36 def solve(rate, cashflows) cashflows.each_with_index.reduce(0) do |total, (payment, index)| total += payment / (1 + rate) ** (index + 1) end end