public
Description: Histograms, Linear Regression, Normal Distribution Analysis, Counting Occurances. I'm sure someone has made a better stats module than this one.
Clone URL: git://github.com/chriseppstein/lame_stats.git
Search Repo:
Added vertical plotting.

Or horizontal plotting, depending on your point of view.
dustin (author)
Sat May 10 16:32:30 -0700 2008
commit  ea3ce0c94ec5c11ffea39222d474dbf3f4d91f0b
tree    5678da59c502f0ab3e1eeaa4891a4fbd1b2b9250
parent  55d1b8cb12b138f030a7978b46345673b4e9ab69
...
186
187
188
189
 
190
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
193
194
195
196
197
 
198
199
 
200
201
202
203
204
...
186
187
188
 
189
190
 
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
 
 
 
 
 
215
216
 
217
218
 
219
220
221
0
@@ -186,19 +186,36 @@ module Stats
0
     def []=(i,v)
0
       @counts[i] = v
0
     end
0
-
0
+
0
     def to_s(options = {})
0
- if options[:max_width]
0
+ if options[:orientation] == :vertical
0
+ plot_vertically options
0
+ else
0
+ plot_horizontally options
0
+ end
0
+ end
0
+
0
+ private
0
+
0
+ def plot_vertically(options = {})
0
+ scale = scale_to options[:max_height]
0
+ height = (@counts.max * scale).ceil
0
+ rows = (0..height).to_a.reverse.map do |h|
0
+ @counts.map{|c| (c * scale).ceil >= h ? "*" : " " }.join(' ')
0
+ end.join("\n") + "\n" + ("_" * ((@counts.size * 3) - 2))
0
+ end
0
+
0
+ def plot_horizontally(options = {})
0
+ @counts.map {|c| "| "+"*"*(c*scale_to(options[:max_width])).ceil}.join("\n")
0
+ end
0
+
0
+ def scale_to(val)
0
+ if val
0
         max = @counts.max
0
- if max > options[:max_width]
0
- scale = options[:max_width] / max.to_f
0
- else
0
- scale = 1
0
- end
0
+ max > val ? options[:max_width] / max.to_f : 1
0
       else
0
- scale = 1
0
+ 1
0
       end
0
- @counts.map {|c| "| "+"*"*(c*scale).ceil}.join("\n")
0
     end
0
   end
0
   

Comments

    No one has commented yet.