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
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 |
#avg ⇒ Object
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_sq ⇒ Object
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
125 |
# File 'lib/statval/statval.rb', line 125 def bounded? ; ! (abs_is_infinite(@min) || abs_is_infinite(@max)) end |
#each_pair ⇒ Object 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
123 |
# File 'lib/statval/statval.rb', line 123 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
135 |
# File 'lib/statval/statval.rb', line 135 def std ; Math.sqrt(var) end |
#std_ratio ⇒ Object
160 |
# File 'lib/statval/statval.rb', line 160 def std_ratio ; std / avg end |
#time ⇒ Object
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_s ⇒ Object
121 |
# File 'lib/statval/statval.rb', line 121 def to_s ; to_hash.to_s end |
#values ⇒ Object
61 62 63 64 |
# File 'lib/statval/statval.rb', line 61 def values this = self keys.map { |key| this[key] } end |
#var ⇒ Object
133 |
# File 'lib/statval/statval.rb', line 133 def var ; avg_sq - (avg * avg) end |