Class: Wads::Stats
- Inherits:
-
Object
- Object
- Wads::Stats
- Defined in:
- lib/wads/data_structures.rb
Overview
Stats allows you to maintain sets of data values, identified by a key, or data set name. You can then use Stats methods to get the count, average, sum, or percentiles for these keys.
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add(key, value) ⇒ Object
- #average(key) ⇒ Object
- #count(key) ⇒ Object
- #display_counts ⇒ Object
- #halfway(b, e) ⇒ Object
- #increment(key) ⇒ Object
-
#initialize(name) ⇒ Stats
constructor
A new instance of Stats.
- #keys ⇒ Object
- #max(key) ⇒ Object
- #min(key) ⇒ Object
- #most_common(key) ⇒ Object
- #pad(str, size, left_align = false) ⇒ Object
- #percentile(key, pct) ⇒ Object
- #report(report_keys = keys) ⇒ Object
- #sample_variance(key) ⇒ Object
- #std_dev(key) ⇒ Object
- #sum(key) ⇒ Object
Constructor Details
#initialize(name) ⇒ Stats
Returns a new instance of Stats.
82 83 84 85 |
# File 'lib/wads/data_structures.rb', line 82 def initialize(name) @name = name @data = {} end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
80 81 82 |
# File 'lib/wads/data_structures.rb', line 80 def data @data end |
#name ⇒ Object
Returns the value of attribute name.
79 80 81 |
# File 'lib/wads/data_structures.rb', line 79 def name @name end |
Instance Method Details
#add(key, value) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wads/data_structures.rb', line 87 def add(key, value) data_set = @data[key] if data_set data_set << value else data_set = [] data_set << value @data[key] = data_set end end |
#average(key) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/wads/data_structures.rb', line 120 def average(key) data_set = @data[key] if data_set return (sum(key) / count(key)).round(5) else return 0 end end |
#count(key) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/wads/data_structures.rb', line 102 def count(key) data_set = @data[key] if data_set return data_set.size else return 0 end end |
#display_counts ⇒ Object
235 236 237 238 239 240 241 242 |
# File 'lib/wads/data_structures.rb', line 235 def display_counts puts "#{pad(@name, 20)} Value" puts "#{'-' * 20} #{'-' * 10}" @data.keys.each do |key| #data_set = @data[key] puts "#{pad(key, 20)} #{count(key)}" end end |
#halfway(b, e) ⇒ Object
159 160 161 162 163 |
# File 'lib/wads/data_structures.rb', line 159 def halfway(b, e) d = e - b m = b + (d / 2).to_i m end |
#increment(key) ⇒ Object
98 99 100 |
# File 'lib/wads/data_structures.rb', line 98 def increment(key) add(key, 1) end |
#keys ⇒ Object
244 245 246 |
# File 'lib/wads/data_structures.rb', line 244 def keys @data.keys end |
#max(key) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/wads/data_structures.rb', line 138 def max(key) data_set = @data[key] if data_set return data_set.max else return 0 end end |
#min(key) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/wads/data_structures.rb', line 129 def min(key) data_set = @data[key] if data_set return data_set.min else return 0 end end |
#most_common(key) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/wads/data_structures.rb', line 202 def most_common(key) value_counts = {} data_set = @data[key] data_set.each do |data| c = value_counts[data] if c.nil? value_counts[data] = 1 else value_counts[data] = value_counts[data] + 1 end end largest_count = 0 largest_key = "none" value_counts.keys.each do |key| key_count = value_counts[key] if key_count > largest_count largest_count = key_count largest_key = key end end largest_key end |
#pad(str, size, left_align = false) ⇒ Object
226 227 228 229 230 231 232 233 |
# File 'lib/wads/data_structures.rb', line 226 def pad(str, size, left_align = false) str = str.to_s if left_align str[0, size].ljust(size, ' ') else str[0, size].rjust(size, ' ') end end |
#percentile(key, pct) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/wads/data_structures.rb', line 165 def percentile(key, pct) data_set = @data[key] if data_set sorted_data_set = data_set.sort pct_index = (data_set.length - 1).to_f * pct mod = pct_index.modulo(1.0) adj = pct_index if mod > 0.9 adj = pct_index.ceil elsif mod < 0.1 adj = pct_index.floor else # We want halfway between the two indices low = pct_index.floor high = pct_index.ceil if low < 0 low = 0 end if high > data_set.size - 1 high = data_set.size - 1 end result = halfway(sorted_data_set[low], sorted_data_set[high]) return result end if adj < 0 adj = 0 elsif adj > data_set.size - 1 adj = data_set.size - 1 end result = sorted_data_set[adj] return result else return 0 end end |
#report(report_keys = keys) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/wads/data_structures.rb', line 248 def report(report_keys = keys) puts "#{pad(@name, 10)}#{SPACER}#{pad('Count', 7)}#{SPACER}#{pad('Min', VALUE_WIDTH)}#{SPACER}#{pad('Avg', VALUE_WIDTH)}#{SPACER}#{pad('StdDev', VALUE_WIDTH)}#{SPACER}#{pad('Max', VALUE_WIDTH)}#{SPACER}| #{pad('p1', VALUE_WIDTH)}#{SPACER}#{pad('p10', VALUE_WIDTH)}#{SPACER}#{pad('p50', VALUE_WIDTH)}#{SPACER}#{pad('p90', VALUE_WIDTH)}#{SPACER}#{pad('p99', VALUE_WIDTH)}" puts "#{'-' * 10}#{SPACER}#{'-' * 7}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}| #{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}#{SPACER}#{'-' * VALUE_WIDTH}" if report_keys.nil? report_keys = @data.keys end report_keys.each do |key| data_set = @data[key] m1 = min(key).round(5) a = average(key).round(5) sd = std_dev(key).round(5) m2 = max(key).round(5) p1 = percentile(key, 0.01).round(5) p10 = percentile(key, 0.1).round(5) p50 = percentile(key, 0.5).round(5) p90 = percentile(key, 0.90).round(5) p99 = percentile(key, 0.99).round(5) puts "#{pad(key, 10)}#{SPACER}#{pad(count(key), 7)}#{SPACER}#{pad(m1, VALUE_WIDTH)}#{SPACER}#{pad(a, VALUE_WIDTH)}#{SPACER}#{pad(sd, VALUE_WIDTH)}#{SPACER}#{pad(m2, VALUE_WIDTH)}#{SPACER}| #{pad(p1, VALUE_WIDTH)}#{SPACER}#{pad(p10, VALUE_WIDTH)}#{SPACER}#{pad(p50, VALUE_WIDTH)}#{SPACER}#{pad(p90, VALUE_WIDTH)}#{SPACER}#{pad(p99, VALUE_WIDTH)}" end end |
#sample_variance(key) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/wads/data_structures.rb', line 147 def sample_variance(key) data_set = @data[key] return 0 unless data_set m = average(key) s = data_set.inject(0.0){|accum, i| accum +(i-m)**2 } s/(data_set.length - 1).to_f end |
#std_dev(key) ⇒ Object
155 156 157 |
# File 'lib/wads/data_structures.rb', line 155 def std_dev(key) Math.sqrt(sample_variance(key)) end |
#sum(key) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/wads/data_structures.rb', line 111 def sum(key) data_set = @data[key] if data_set return data_set.inject(0.to_f){|sum,x| sum + x } else return 0 end end |