Sha256: 124202286c93b075dd9995dcc0247cabbc305768ffc60ec0d3ef8b85893c6d5e

Contents?: true

Size: 762 Bytes

Versions: 2

Compression:

Stored size: 762 Bytes

Contents

module CashFlowAnalysis
  module Calculator
    module Xnpv
      # Calculates the {http://en.wikipedia.org/wiki/Net_present_value XNPV}, assuming irregularly timed cash flows.
      #
      # @param discount_rate [Numeric] the discount rate for the calculation
      # @param cash_flow_items [Array] items which have `#date` ([Date]) and `#amount` ([Numeric]) properties
      # @return [Numeric] the XNPV for the cash flow
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cash_flow_analysis-0.2.0 lib/cash_flow_analysis/calculator/xnpv.rb
cash_flow_analysis-0.1.0 lib/cash_flow_analysis/calculator/xnpv.rb