class StatVal::StatVal

Constants

NEG_INFINITY
POS_INFINITY

Attributes

max[R]
min[R]
num[R]
sq_sum[R]
sum[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/statval/statval.rb, line 11
def initialize(options = {}) ; reset(options) end

Public Instance Methods

+(value) click to toggle source
# File lib/statval/statval.rb, line 66
def +(value) ; self.class.new << self << value end
<<(value, *rest) click to toggle source
# File lib/statval/statval.rb, line 68
def <<(value, *rest)
  this = self
  if value.kind_of? StatVal
    if empty?
      reset(value.to_hash(:writable))
    else
      begin
        @num    += value.num
        @sum    += value.sum
        @sq_sum += value.sq_sum
        val_min  = value.min
        val_max  = value.max
        @min     = val_min if val_min < @min
        @max     = val_max if val_max > @max
      end unless value.empty?
    end
  else
    if value.kind_of? Numeric
      @sum       += value
      @sq_sum    += value * value
      @num       += 1
      @min        = value if value < @min
      @max        = value if value > @max
    else
      if value.respond_to?(:each_pair)
        value.each_pair { |k, v| this << v }
      else
        if value.respond_to?(:each)
          value.each { |v| this << v }
        else
          raise ArgumentError
        end
      end
    end
  end
  rest.each { |v| this << v } if rest
  this
end
[](key) click to toggle source
# File lib/statval/statval.rb, line 25
def [](key)
  case key
    when :num then num
    when :min then min
    when :max then max
    when :sum then sum
    when :sq_sum then sq_sum
    when :std_ratio then std_ratio
    when :avg then avg
    when :std then std
    when :avg_sq then avg_sq
    when :var then var
    else
      raise ArgumentError
  end
end
[]=(key, new_val) click to toggle source
# File lib/statval/statval.rb, line 42
def []=(key, new_val)
  case key
    when :num then self.num = new_val
    when :min then self.min = new_val
    when :max then self.max = new_val
    when :sum then self.sum = new_val
    when :sq_sum then self.sq_sum = new_val
    else
      raise ArgumentError
  end
end
avg() click to toggle source
# File lib/statval/statval.rb, line 127
def avg ; if empty? then zero_if_unbounded(abs_div(@max - @min, 2)) else abs_div(@sum, @num) end end
avg_sq() click to toggle source
# File lib/statval/statval.rb, line 129
def avg_sq
  if empty? then zero_if_unbounded(abs_div((@max*@max) - (@min*@min), 2)) else abs_div(@sq_sum, @num) end
end
bounded?() click to toggle source
# File lib/statval/statval.rb, line 125
def bounded? ; ! (abs_is_infinite(@min) || abs_is_infinite(@max)) end
each()
Alias for: each_pair
each_pair() { |key, this| ... } click to toggle source
# File lib/statval/statval.rb, line 54
def each_pair
  this = self
  keys.each { | key| yield key, this[key] }
end
Also aliased as: each
empty?() click to toggle source
# File lib/statval/statval.rb, line 123
def empty? ; @num == 0 end
keys() click to toggle source
# File lib/statval/statval.rb, line 23
def keys ; ::StatVal.keys(:default) end
max=(new_val) click to toggle source
# File lib/statval/statval.rb, line 156
def max=(new_val)
  @max = if new_val then new_val else (if empty? then NEG_INFINITY else avg+std end) end
end
min=(new_val) click to toggle source
# File lib/statval/statval.rb, line 152
def min=(new_val)
  @min = if new_val then new_val else (if empty? then POS_INFINITY else avg-std end) end
end
num=(new_val) click to toggle source
# File lib/statval/statval.rb, line 137
def num=(new_val)
  raise ArgumentError if new_val < 0
  @num = new_val
end
reset(options = {}) click to toggle source
# File lib/statval/statval.rb, line 13
def reset(options = {})
  options[:num] = 0 unless options[:num]
  options[:min] = nil unless options[:min]
  options[:max] = nil unless options[:max]
  options[:sum] = 0 unless options[:sum]
  options[:sq_sum] = 0 unless options[:sq_sum]
  options.each_pair { |k, v| self[k] = v}
  options
end
sq_sum=(new_val) click to toggle source
# File lib/statval/statval.rb, line 147
def sq_sum=(new_val)
  raise ArgumentError if new_val < 0
  @sq_sum= new_val
end
std() click to toggle source
# File lib/statval/statval.rb, line 135
def std ; Math.sqrt(var) end
std_ratio() click to toggle source
# File lib/statval/statval.rb, line 160
def std_ratio ; std / avg end
sum=(new_val) click to toggle source
# File lib/statval/statval.rb, line 142
def sum=(new_val)
  raise ArgumentError if new_val < 0
  @sum= new_val
end
time() { || ... } click to toggle source
# File lib/statval/statval.rb, line 107
def time
  start = Time.now
  begin
    yield
  ensure
    stop = Time.now
    self << (stop-start)
  end
end
to_hash(which_keys = nil, convert_to_s = false) click to toggle source
# File lib/statval/statval.rb, line 117
def to_hash(which_keys = nil, convert_to_s = false)
  ::StatVal.key_hash(which_keys).inject({}) { |h, (attr, name)| h[(if convert_to_s then name.to_s else name end)] = self[attr]; h }
end
to_s() click to toggle source
# File lib/statval/statval.rb, line 121
def to_s ; to_hash.to_s end
values() click to toggle source
# File lib/statval/statval.rb, line 61
def values
  this = self
  keys.map { |key| this[key] }
end
var() click to toggle source
# File lib/statval/statval.rb, line 133
def var ; avg_sq - (avg * avg) end

Private Instance Methods

abs_div(nom, denom) click to toggle source
# File lib/statval/statval.rb, line 169
def abs_div(nom, denom) ; nom.abs.to_f / denom end
abs_is_infinite(val) click to toggle source
# File lib/statval/statval.rb, line 167
def abs_is_infinite(val) ; val.abs.to_f === POS_INFINITY end
zero_if_unbounded(val) click to toggle source
# File lib/statval/statval.rb, line 168
def zero_if_unbounded(val) ; if bounded? then val else 0.0 end end