Sha256: d724d648dd17d23e9fee3a1aabd24a5d4f99c49f47f68ded3b615f0e3837373c

Contents?: true

Size: 838 Bytes

Versions: 1

Compression:

Stored size: 838 Bytes

Contents

require "bigdecimal"

module MathUtil
  def self.mean(arr)
    arr.inject(:+) / arr.length.to_f
  end

  def self.population_variance(population)
    mean = mean(population)
    sum = population.map { |element| ((element - mean) ** 2) }.inject(:+)
    (sum/population.length)
  end

  def self.volatility(population)
    population.compact!
    if population.is_a?(Array) && population.any?
      variance = MathUtil.population_variance(population)
      (((variance * 252) ** 0.5) * 100).round(4)
    end
  end

  def self.sharpe(fund, bmark, volatility)
    sharpe = 100 * (
      ((1 + mean(fund)) ** 252 - 1) -
      ((1 + mean(bmark)) ** 252 - 1)) / volatility
    sharpe.round(4) if sharpe > 0
  end

  def self.annual_tax_day_equivalent(annual_tax)
    BigDecimal.new((1 + annual_tax) ** (1/252.0) - 1, 16) if annual_tax
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
math_util-0.1.1 lib/math_util.rb