Module: Concerns::Statistical

Included in:
PriceManager
Defined in:
lib/concerns/concerns.rb

Instance Method Summary collapse

Instance Method Details

#basic_statsObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/concerns/concerns.rb', line 101

def basic_stats
  values = yield.collect{|item| item.price}
  if values != []
    @stats[:volume] = values.count
    @stats[:mean] = values.reduce(:+)/@stats[:volume]
    @stats[:min] = values.min
    @stats[:max] = values.max
    @stats[:range] = @stats[:max] - @stats[:min]
  end
  @stats
end

#quartileObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/concerns/concerns.rb', line 113

def quartile
  values = yield.collect{|item| item.price}
  if values != []
    @stat[:volume] = values.length #TODO clean up stuff double check on rounding math for indeces
    if @stats[:volume] % 2 == 0
      @stats[:median] = (values[@stats[:volume]/2] + values[@stats[:volume]/2 - 1]) / 2
      q1 = values[@stats[:volume]/4]
      q3 = values[@stats[:volume]/2 + @stats[:volume]/4]
    else
      mid = @stats[:volume]/2
      @stats[:median] = values[mid]
      q1 = values[mid/2]
      q3 = values[mid + mid/2]
    end
  end
  iqr = q3 - q1
  iqr1 = q1 - 1.5 * iqr
  iqr3 = q3 + 1.5 * iqr
  values.collect{|value| value if value.between?(iqr1,iqr3)}
end