Class: StatVal::StatVal

Inherits:
Object
  • Object
show all
Defined in:
lib/statval/statval.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ StatVal

Returns a new instance of StatVal.



11
# File 'lib/statval/statval.rb', line 11

def initialize(options = {}) ; reset(options) end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



7
8
9
# File 'lib/statval/statval.rb', line 7

def max
  @max
end

#minObject

Returns the value of attribute min.



6
7
8
# File 'lib/statval/statval.rb', line 6

def min
  @min
end

#numObject

Returns the value of attribute num.



5
6
7
# File 'lib/statval/statval.rb', line 5

def num
  @num
end

#sq_sumObject

Returns the value of attribute sq_sum.



9
10
11
# File 'lib/statval/statval.rb', line 9

def sq_sum
  @sq_sum
end

#sumObject

Returns the value of attribute sum.



8
9
10
# File 'lib/statval/statval.rb', line 8

def sum
  @sum
end

Instance Method Details

#+(value) ⇒ Object



65
# File 'lib/statval/statval.rb', line 65

def +(value) ; self.class.new << self << value end

#<<(value, *rest) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/statval/statval.rb', line 67

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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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 :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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/statval/statval.rb', line 41

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

#avgObject



126
# File 'lib/statval/statval.rb', line 126

def avg ; if empty? then zero_if_unbounded(abs_div(@max - @min, 2)) else abs_div(@sum, @num) end end

#avg_sqObject



128
129
130
# File 'lib/statval/statval.rb', line 128

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?Boolean

Returns:

  • (Boolean)


124
# File 'lib/statval/statval.rb', line 124

def bounded? ; ! (abs_is_infinite(@min) || abs_is_infinite(@max)) end

#each_pairObject Also known as: each



53
54
55
56
# File 'lib/statval/statval.rb', line 53

def each_pair
  this = self
  keys.each { | key| yield key, this[key] }
end

#empty?Boolean

Returns:

  • (Boolean)


122
# File 'lib/statval/statval.rb', line 122

def empty? ; @num == 0 end

#keysObject



23
# File 'lib/statval/statval.rb', line 23

def keys ; ::StatVal.keys(:default) end

#reset(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# 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

#stdObject



134
# File 'lib/statval/statval.rb', line 134

def std ; Math.sqrt(var) end

#std_ratioObject



159
# File 'lib/statval/statval.rb', line 159

def std_ratio ; std / avg end

#timeObject



106
107
108
109
110
111
112
113
114
# File 'lib/statval/statval.rb', line 106

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) ⇒ Object



116
117
118
# File 'lib/statval/statval.rb', line 116

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_sObject



120
# File 'lib/statval/statval.rb', line 120

def to_s ; to_hash.to_s end

#valuesObject



60
61
62
63
# File 'lib/statval/statval.rb', line 60

def values
  this = self
  keys.map { |key| this[key] }
end

#varObject



132
# File 'lib/statval/statval.rb', line 132

def var ; avg_sq - (avg * avg) end