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:
initial commit
chriseppstein (author)
Sat May 10 15:07:02 -0700 2008
commit  55d1b8cb12b138f030a7978b46345673b4e9ab69
tree    b9b2bfdf2b55814c97f483231ebaa1917968a42d
0
...
 
...
1
0
@@ -0,0 +1 @@
0
+I use this stats module to do math stuff on things.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
0
@@ -0,0 +1,247 @@
0
+module Stats
0
+ class Counter
0
+ include Enumerable
0
+
0
+ def initialize(enumerable = nil)
0
+ @hash = Hash.new
0
+ @counts = 0
0
+ add_all(enumerable) unless enumerable.nil?
0
+ end
0
+ def add_with_count(key,c)
0
+ @hash[key] = (@hash[key] || 0) + c
0
+ @counts += c
0
+ end
0
+ def add_all(enumerable)
0
+ enumerable.each {|key| self << key }
0
+ end
0
+ def <<(key)
0
+ add_with_count(key,1)
0
+ end
0
+ def size
0
+ @hash.size
0
+ end
0
+ def [](key)
0
+ @hash[key]
0
+ end
0
+ def each
0
+ @hash.each_key { |k|
0
+ yield k
0
+ }
0
+ end
0
+
0
+ def delete_if
0
+ @hash.delete_if do |k,v|
0
+ returning(yield(k,v)) do |deleted|
0
+ @counts -= v if deleted
0
+ end
0
+ end
0
+ end
0
+
0
+ def first
0
+ [(k = @hash.keys.first),@hash[k]]
0
+ end
0
+
0
+ def each_count
0
+ @hash.each_value { |v|
0
+ yield v
0
+ }
0
+ end
0
+ def each_with_count
0
+ each { |k|
0
+ yield k, self[k]
0
+ }
0
+ end
0
+ def total
0
+ return @counts
0
+ end
0
+ def max_count
0
+ each_to_a(:each_count).max
0
+ end
0
+ def count_histogram
0
+ return nil if @total == 0
0
+ max = max_count
0
+ returning(Histogram.new(max,max+1, 1)) do |histogram|
0
+ each_count do |c|
0
+ histogram << c
0
+ end
0
+ end
0
+ end
0
+
0
+ def each_t_value
0
+ xbar = mean
0
+ s = standard_deviation
0
+ each_count do |c|
0
+ yield compute_t_value(c, xbar, s)
0
+ end
0
+ end
0
+
0
+ def compute_t_value(c, xbar = mean, s = standard_deviation)
0
+ Math::sqrt(size)*(xbar - c)/s
0
+ end
0
+
0
+ def normal?(allowed_variance = 0.1)
0
+ lr = LinearRegression.new
0
+ x = -1
0
+ each_t_value do |t|
0
+ lr.add_point(x += 1, t)
0
+ end
0
+ puts "slope is #{lr.slope}"
0
+ m = lr.slope
0
+ m < allowed_variance && m > -allowed_variance
0
+ end
0
+
0
+ def sum_of_squares
0
+ sos = 0
0
+ each_count {|c| sos += c*c}
0
+ return sos
0
+ end
0
+
0
+ def zscores(width = 3, center = 0, &block)
0
+ silence_warnings do
0
+ each_to_a(:each_with_zscore, width, center, &block)
0
+ end
0
+ end
0
+
0
+ def mean
0
+ total.to_f / size
0
+ end
0
+
0
+ def variance
0
+ (sum_of_squares - total*mean)/(size)
0
+ end
0
+
0
+ def standard_deviation
0
+ Math::sqrt(variance)
0
+ end
0
+
0
+ def each_with_zscore(width = 3, center = 0, &block)
0
+ if size == 0
0
+ elsif size == 1
0
+ each {|k|
0
+ yield k, center
0
+ }
0
+ else
0
+ each_with_count { |k,v|
0
+ zscore = (v - mean) / standard_deviation
0
+ zscore = 0 if zscore.nan?
0
+ block.call k, zscore*(width/3)+center
0
+ }
0
+ end
0
+ nil
0
+ end
0
+
0
+ def poisson_lambda
0
+ total/size
0
+ end
0
+ end
0
+
0
+ class Histogram
0
+ attr_accessor :bucket_count, :max_value, :min_value
0
+ def initialize(bucket_count, max_value, min_value = 0, counts = nil)
0
+ @bucket_count = bucket_count
0
+ @min_value = min_value
0
+ @max_value = max_value
0
+ @counts = counts || [0] * bucket_count
0
+ end
0
+
0
+ def bucket_width
0
+ @bucket_width ||= (@max_value - @min_value)/@bucket_count
0
+ end
0
+
0
+ def bucket_index_for(floor_bucket_value)
0
+ ((floor_bucket_value - min_value).to_f/bucket_width).floor
0
+ end
0
+
0
+ def count_for(floor_bucket_value)
0
+ @counts[bucket_index_for(floor_bucket_value)] || 0
0
+ end
0
+
0
+ def +(other)
0
+ raise ArgumentError.new("incompatible histograms (#{self.bucket_width} != #{other.bucket_width})") unless self.bucket_width == other.bucket_width
0
+ new_max = [max_value, other.max_value].max
0
+ new_min = [min_value, other.min_value].min
0
+ returning(Histogram.new((new_max-new_min)/bucket_width, new_max, new_min)) do |h|
0
+ bv = new_min
0
+ while bv < new_max
0
+ h[h.bucket_index_for(bv)] = self.count_for(bv) + other.count_for(bv)
0
+ bv += bucket_width
0
+ end
0
+ end
0
+ end
0
+
0
+ def bucket_index(value)
0
+ (@bucket_count *(value - @min_value).to_f / (@max_value - @min_value)).floor
0
+ end
0
+
0
+ def <<(value)
0
+ returning(bucket_index(value)) do |c|
0
+ @counts[c] += 1
0
+ end
0
+ end
0
+
0
+ def [](i)
0
+ @counts[i]
0
+ end
0
+
0
+ def []=(i,v)
0
+ @counts[i] = v
0
+ end
0
+
0
+ def to_s(options = {})
0
+ if options[:max_width]
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
+ else
0
+ scale = 1
0
+ end
0
+ @counts.map {|c| "| "+"*"*(c*scale).ceil}.join("\n")
0
+ end
0
+ end
0
+
0
+ class LinearRegression
0
+ def initialize(points = [])
0
+ @points = points
0
+ end
0
+
0
+ def add_point(x,y)
0
+ @points << [x,y]
0
+ end
0
+
0
+ def sum(index)
0
+ @points.inject(0){|s, p| s+p[index]}
0
+ end
0
+
0
+ def mean(index)
0
+ sum(index) / @points.size
0
+ end
0
+
0
+ def sum_x; sum(0); end
0
+ def mean_x; mean(0); end
0
+ def sum_y; sum(1); end
0
+ def mean_y; mean(1); end
0
+
0
+
0
+ def sum_of_product_of_deviations
0
+ my = mean_y
0
+ mx = mean_x
0
+ @points.inject(0) {|s,p| s + ((p[0] - mx) * (p[1] - my))}
0
+ end
0
+
0
+ def sum_of_squares_of_deviations(index = 0)
0
+ m = mean(index)
0
+ @points.inject(0){|s,p| s+((p[index] - m)**2)}
0
+ end
0
+
0
+ def slope
0
+ sum_of_product_of_deviations / sum_of_squares_of_deviations
0
+ end
0
+
0
+ def y_intercept
0
+ mean_y - mean_x * slope
0
+ end
0
+ end
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.