Module: MathUtil

Defined in:
lib/math_util.rb,
lib/math_util/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.annual_tax_day_equivalent(annual_tax) ⇒ Object



29
30
31
# File 'lib/math_util.rb', line 29

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

.mean(arr) ⇒ Object



4
5
6
# File 'lib/math_util.rb', line 4

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

.population_variance(population) ⇒ Object



8
9
10
11
12
# File 'lib/math_util.rb', line 8

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

.sharpe(fund, bmark, volatility) ⇒ Object



22
23
24
25
26
27
# File 'lib/math_util.rb', line 22

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

.volatility(population) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/math_util.rb', line 14

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