Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/rstat/core_ext/array/sum.rb,
lib/rstat/core_ext/array/mean.rb,
lib/rstat/core_ext/array/mode.rb,
lib/rstat/core_ext/array/range.rb,
lib/rstat/core_ext/array/median.rb,
lib/rstat/core_ext/array/product.rb,
lib/rstat/core_ext/array/percentile.rb,
lib/rstat/core_ext/array/standard_deviation.rb

Instance Method Summary collapse

Instance Method Details

#arithmetric_meanObject



6
7
8
# File 'lib/rstat/core_ext/array/mean.rb', line 6

def arithmetric_mean
  self.mean
end

#coefficient_of_variationObject



16
17
18
# File 'lib/rstat/core_ext/array/standard_deviation.rb', line 16

def coefficient_of_variation
  self.standard_deviation / self.mean
end

#geometric_meanObject



10
11
12
# File 'lib/rstat/core_ext/array/mean.rb', line 10

def geometric_mean
  self.product.to_f ** (1.0 / self.length)
end

#harmonic_meanObject



14
15
16
# File 'lib/rstat/core_ext/array/mean.rb', line 14

def harmonic_mean
  self.length / self.map{ |x| 1.0 / x }.inject{ |sum, x| sum + x }.to_f
end

#interquartile_rangeObject



10
11
12
# File 'lib/rstat/core_ext/array/percentile.rb', line 10

def interquartile_range
  self.percentile(75) - self.percentile(25)
end

#iqrObject



14
15
16
# File 'lib/rstat/core_ext/array/percentile.rb', line 14

def iqr
  self.interquartile_range
end

#meanObject



2
3
4
# File 'lib/rstat/core_ext/array/mean.rb', line 2

def mean
  self.sum.to_f / self.length
end

#medianObject



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rstat/core_ext/array/median.rb', line 2

def median
  unless self.length == 0
    copy = self.sort

    if copy.length % 2 == 0
      (copy[copy.length / 2 - 1] + copy[copy.length / 2]) / 2.0
    else
      copy[copy.length / 2]
    end
  else
    nil
  end
end

#middle_fiftyObject



22
23
24
# File 'lib/rstat/core_ext/array/percentile.rb', line 22

def middle_fifty
  self.interquartile_range
end

#midspreadObject



18
19
20
# File 'lib/rstat/core_ext/array/percentile.rb', line 18

def midspread
  self.interquartile_range
end

#modeObject



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rstat/core_ext/array/mode.rb', line 2

def mode
  if self.length <= 1
    return self
  end

  seen = Hash.new(0)

  self.each{ |value| seen[value] += 1 }

  max = seen.values.max

  seen.find_all{ |key, value| value == max }.map{ |key, value| key }
end

#percentile(p) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/rstat/core_ext/array/percentile.rb', line 2

def percentile p
  if p < 0 || p > 100
    nil
  else
    self.sort[((p.to_f/100.0) * self.length.to_f) - 0.5]
  end
end

#power_mean(p = 1) ⇒ Object



22
23
24
# File 'lib/rstat/core_ext/array/mean.rb', line 22

def power_mean(p = 1)
  ((1.0 / self.length) * self.map{ |x| x ** p }.inject{ |sum, x| sum + x }.to_f) ** (1.0 / p)
end

#productObject



2
3
4
# File 'lib/rstat/core_ext/array/product.rb', line 2

def product
  self.inject(:*)
end

#quadratic_meanObject



18
19
20
# File 'lib/rstat/core_ext/array/mean.rb', line 18

def quadratic_mean
  self.power_mean(2)
end

#rangeObject



2
3
4
# File 'lib/rstat/core_ext/array/range.rb', line 2

def range
  self.max - self.min
end

#standard_deviationObject



8
9
10
# File 'lib/rstat/core_ext/array/standard_deviation.rb', line 8

def standard_deviation
  Math.sqrt(self.variance)
end

#std_devObject



12
13
14
# File 'lib/rstat/core_ext/array/standard_deviation.rb', line 12

def std_dev
  self.standard_deviation
end

#sumObject



2
3
4
# File 'lib/rstat/core_ext/array/sum.rb', line 2

def sum
  self.inject(:+)
end

#varianceObject



2
3
4
5
6
# File 'lib/rstat/core_ext/array/standard_deviation.rb', line 2

def variance
  mean = self.mean

  (1.0 / self.length) * self.map{|x| (x - mean) ** 2}.inject(:+)
end