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



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

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

#<<(value, *rest) ⇒ Object



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



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



42
43
44
45
46
47
48
49
50
51
52
# 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

#avgObject



127
# 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_sqObject



129
130
131
# 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?Boolean

Returns:

  • (Boolean)


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

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

#each_pairObject Also known as: each



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

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



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

def std ; Math.sqrt(var) end

#std_ratioObject



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

def std_ratio ; std / avg end

#timeObject



107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



117
118
119
# 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_sObject



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

def to_s ; to_hash.to_s end

#valuesObject



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

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

#varObject



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

def var ; avg_sq - (avg * avg) end