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((1 + annual_tax) ** (1/252.0) - 1, 16) if annual_tax end end