Sha256: bf57bad9f0927685c6a7df271a353f491ddae6c67c01950d8a342dcd53bf40ae

Contents?: true

Size: 974 Bytes

Versions: 1

Compression:

Stored size: 974 Bytes

Contents

require "bigdecimal/newton"

module CashFlowAnalysis
  module Calculator
    module Xirr
      extend Newton

      # Calculates the {http://en.wikipedia.org/wiki/Internal_rate_of_return XIRR}, assuming irregularly timed cash flows.
      #
      # @param cash_flow_items [Array] items which have `#date` ([Date]) and `#amount` ([Numeric]) properties
      # @return [Numeric] the XIRR for the cash flow
      def self.calculate(cash_flow_items)
        return nil if cash_flow_items.map(&:amount).none? { |amount| amount >= 0 }
        return nil if cash_flow_items.map(&:amount).none? { |amount| amount < 0 }

        objective_function = Util::ObjectiveFunction.new(Xnpv, cash_flow_items)
        discount_rate_vector = [objective_function.one]

        begin
          nlsolve(objective_function, discount_rate_vector)
        rescue RuntimeError => ex
          raise CalculationError.new(ex)
        end

        discount_rate_vector.first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cash_flow_analysis-0.1.0 lib/cash_flow_analysis/calculator/xirr.rb