Module: CashFlowAnalysis::Calculator::Xnpv

Defined in:
lib/cash_flow_analysis/calculator/xnpv.rb

Class Method Summary collapse

Class Method Details

.calculate(discount_rate, cash_flow_items) ⇒ Numeric

Calculates the XNPV, assuming irregularly timed cash flows.

Parameters:

  • discount_rate (Numeric)

    the discount rate for the calculation

  • cash_flow_items (Array)

    items which have `#date` ([Date]) and `#amount` ([Numeric]) properties

Returns:

  • (Numeric)

    the XNPV for the cash flow


9
10
11
12
13
14
15
16
# File 'lib/cash_flow_analysis/calculator/xnpv.rb', line 9

def self.calculate(discount_rate, cash_flow_items)
  start_date = cash_flow_items.first.date

  cash_flow_items.inject(0) do |sum, item|
    days_since_start = (item.date - start_date).to_i
    sum += item.amount / ((1 + discount_rate) ** (days_since_start / 365.0))
  end
end