Class: StatVal::StatVal
- Inherits:
-
Object
- Object
- StatVal::StatVal
- Defined in:
- lib/statval/statval.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
-
#num ⇒ Object
Returns the value of attribute num.
-
#sq_sum ⇒ Object
Returns the value of attribute sq_sum.
-
#sum ⇒ Object
Returns the value of attribute sum.
Instance Method Summary collapse
- #+(value) ⇒ Object
- #<<(value, *rest) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, new_val) ⇒ Object
- #avg ⇒ Object
- #avg_sq ⇒ Object
- #bounded? ⇒ Boolean
- #each_pair ⇒ Object (also: #each)
- #empty? ⇒ Boolean
-
#initialize(options = {}) ⇒ StatVal
constructor
A new instance of StatVal.
- #keys ⇒ Object
- #reset(options = {}) ⇒ Object
- #std ⇒ Object
- #std_ratio ⇒ Object
- #time ⇒ Object
- #to_hash(which_keys = nil, convert_to_s = false) ⇒ Object
- #to_s ⇒ Object
- #values ⇒ Object
- #var ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ StatVal
Returns a new instance of StatVal.
11 |
# File 'lib/statval/statval.rb', line 11 def initialize( = {}) ; reset() end |
Instance Attribute Details
#max ⇒ Object
Returns the value of attribute max.
7 8 9 |
# File 'lib/statval/statval.rb', line 7 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
6 7 8 |
# File 'lib/statval/statval.rb', line 6 def min @min end |
#num ⇒ Object
Returns the value of attribute num.
5 6 7 |
# File 'lib/statval/statval.rb', line 5 def num @num end |
#sq_sum ⇒ Object
Returns the value of attribute sq_sum.
9 10 11 |
# File 'lib/statval/statval.rb', line 9 def sq_sum @sq_sum end |
#sum ⇒ Object
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 |
#avg ⇒ Object
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_sq ⇒ Object
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
124 |
# File 'lib/statval/statval.rb', line 124 def bounded? ; ! (abs_is_infinite(@min) || abs_is_infinite(@max)) end |
#each_pair ⇒ Object 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
122 |
# File 'lib/statval/statval.rb', line 122 def empty? ; @num == 0 end |
#reset(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/statval/statval.rb', line 13 def reset( = {}) [:num] = 0 unless [:num] [:min] = nil unless [:min] [:max] = nil unless [:max] [:sum] = 0 unless [:sum] [:sq_sum] = 0 unless [:sq_sum] .each_pair { |k, v| self[k] = v} end |
#std ⇒ Object
134 |
# File 'lib/statval/statval.rb', line 134 def std ; Math.sqrt(var) end |
#std_ratio ⇒ Object
159 |
# File 'lib/statval/statval.rb', line 159 def std_ratio ; std / avg end |
#time ⇒ Object
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_s ⇒ Object
120 |
# File 'lib/statval/statval.rb', line 120 def to_s ; to_hash.to_s end |
#values ⇒ Object
60 61 62 63 |
# File 'lib/statval/statval.rb', line 60 def values this = self keys.map { |key| this[key] } end |
#var ⇒ Object
132 |
# File 'lib/statval/statval.rb', line 132 def var ; avg_sq - (avg * avg) end |