class StatisticalMethods::SummaryStatistic::Location::Average

Public Class Methods

new(array) click to toggle source
# File lib/statistical_methods/summary_statistic/location/average.rb, line 5
def initialize(array)
  @array = array
  @number = @array.size.to_f
  @empty = @number < 1
end

Public Instance Methods

arithmetic_mean() click to toggle source

en.wikipedia.org/wiki/Arithmetic_mean

# File lib/statistical_methods/summary_statistic/location/average.rb, line 12
def arithmetic_mean
  process { @array.addition / @number }
end
geometric_mean() click to toggle source

en.wikipedia.org/wiki/Geometric_mean

# File lib/statistical_methods/summary_statistic/location/average.rb, line 17
def geometric_mean
  process { @array.multiplication**(1.0 / @number) }
end
harmonic_mean() click to toggle source

en.wikipedia.org/wiki/Harmonic_mean

# File lib/statistical_methods/summary_statistic/location/average.rb, line 22
def harmonic_mean
  process { @number / @array.harmonic.addition } unless include_zero?
end
power_mean(exponent) click to toggle source

en.wikipedia.org/wiki/Generalized_mean

# File lib/statistical_methods/summary_statistic/location/average.rb, line 32
def power_mean(exponent)
  process do
    return exception(exponent) if exceptions.include? exponent
    (@array.power(exponent).addition / @number)**(1.0 / exponent)
  end
end
quadratic_mean() click to toggle source

en.wikipedia.org/wiki/Root_mean_square

# File lib/statistical_methods/summary_statistic/location/average.rb, line 27
def quadratic_mean
  process { Math.sqrt(@array.power(2).addition / @number) }
end

Private Instance Methods

exception(exponent) click to toggle source
# File lib/statistical_methods/summary_statistic/location/average.rb, line 53
def exception(exponent)
  case exponent
  when 0 then geometric_mean
  when -Float::INFINITY then @array.min
  when +Float::INFINITY then @array.max
  end
end
exceptions() click to toggle source
# File lib/statistical_methods/summary_statistic/location/average.rb, line 49
def exceptions
  [-Float::INFINITY, 0, +Float::INFINITY]
end
include_zero?() click to toggle source
# File lib/statistical_methods/summary_statistic/location/average.rb, line 45
def include_zero?
  @array.include?(0.0)
end
process(&block) click to toggle source
# File lib/statistical_methods/summary_statistic/location/average.rb, line 41
def process(&block)
  block.call unless @empty
end