Module: RandomWalk
- Defined in:
- lib/rbbt/statistics/random_walk.rb
Constant Summary collapse
- COLORS =
{ :red => PNG::Color::Red, :green => PNG::Color::Green, :white => PNG::Color::White, :black => PNG::Color::Black, :gray => PNG::Color::Gray, }
Class Attribute Summary collapse
-
.scoring_method ⇒ Object
Returns the value of attribute scoring_method.
Class Method Summary collapse
- .combine(up, down) ⇒ Object
- .draw_hits(hits, total, filename = nil, options = {}) ⇒ Object
- .permutations(size, total, missing = 0, times = 10_000) ⇒ Object
- .permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000) ⇒ Object
-
.persisted_permutations(size, total, missing = 0, times = 10_000) ⇒ Object
def self.persisted_permutations(size, total, missing = 0, times = 10_000) repo_file = “/tmp/rw_repo7” repo = Persist.open_tokyocabinet(repo_file, false, :float_array) key = Misc.digest([size, total, missing, times, scoring_method].inspect) begin repo.read if repo repo else p = permutations(size, total, missing, times) repo.write_and_close do repo = p end p end ensure repo.close end end.
- .persisted_permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000) ⇒ Object
- .pvalue(permutations, score) ⇒ Object
-
.score_up_down(up, down, total, missing = 0) ⇒ Object
Two sided.
- .set_scoring(method) ⇒ Object
Class Attribute Details
.scoring_method ⇒ Object
Returns the value of attribute scoring_method.
215 216 217 |
# File 'lib/rbbt/statistics/random_walk.rb', line 215 def scoring_method @scoring_method end |
Class Method Details
.combine(up, down) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rbbt/statistics/random_walk.rb', line 227 def self.combine(up, down) return down if up == 0 return up if down == 0 return up - down if (up > 0) == (down > 0) return 0 else up - down end end |
.draw_hits(hits, total, filename = nil, options = {}) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/rbbt/statistics/random_walk.rb', line 340 def self.draw_hits(hits, total, filename = nil, = {}) update = [:update] size = [:size] || total bg_color = [:bg_color] || :white width = [:width] || 20 sections = [:sections] || [] size = [size, total].min canvas = PNG::Canvas.new size, width, COLORS[bg_color] || PNG::Color.from(bg_color) hits = hits.collect{|h| h - 1} if size < total hits = hits.collect{|h| (h.to_f * size / total).to_i} end sections.each{|color, info| start = info[0] finish = info[1] (start..finish).each{|x| (0..width - 1).each{|y| canvas[x,y] = COLORS[color] } } } hits.each{|hit| canvas.line hit, 0, hit , width - 1, PNG::Color::Black } png = PNG.new canvas if filename png.save filename else png.to_blob end end |
.permutations(size, total, missing = 0, times = 10_000) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rbbt/statistics/random_walk.rb', line 247 def self.permutations(size, total, missing = 0, times = 10_000) if size == 0 [0] * times else (1..times).collect do p = [] sample_without_replacement(total, size, p) score(p, total, missing).abs end end end |
.permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/rbbt/statistics/random_walk.rb', line 260 def self.permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000) if up_size == 0 or down_size == 0 [0] * times else (1..times).collect do up_p = [] sample_without_replacement(total, up_size, up_p) down_p = [] sample_without_replacement(total, down_size, down_p) score_up_down(up_p, down_p, total, missing).abs end end end |
.persisted_permutations(size, total, missing = 0, times = 10_000) ⇒ Object
def self.persisted_permutations(size, total, missing = 0, times = 10_000)
repo_file = "/tmp/rw_repo7"
repo = Persist.open_tokyocabinet(repo_file, false, :float_array)
key = Misc.digest([size, total, missing, times, scoring_method].inspect)
begin
repo.read
if repo[key]
repo[key]
else
p = permutations(size, total, missing, times)
repo.write_and_close do
repo[key] = p
end
p
end
ensure
repo.close
end
end
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/rbbt/statistics/random_walk.rb', line 296 def self.persisted_permutations(size, total, missing = 0, times = 10_000) require 'rbbt/util/tc_cache' repo_file = "/tmp/rw_repo9_" << scoring_method.to_s key = Misc.digest([size, total, missing, times, scoring_method].inspect) cache = TCCache.open(repo_file, :float_array) p = cache.cache(key) do permutations(size, total, missing, times) end p end |
.persisted_permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000) ⇒ Object
307 308 309 310 311 312 313 314 315 316 |
# File 'lib/rbbt/statistics/random_walk.rb', line 307 def self.persisted_permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000) require 'rbbt/util/tc_cache' repo_file = "/tmp/rw_repo9_" << scoring_method.to_s key = Misc.digest([up_size, down_size, total, missing, times, scoring_method].inspect) cache = TCCache.open(repo_file, :float_array) p = cache.cache(key) do permutations_up_down(up_size, down_size, total, missing, times) end p end |
.pvalue(permutations, score) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/rbbt/statistics/random_walk.rb', line 319 def self.pvalue(permutations, score) positive = score > 0 score = score.abs count = permutations.select{|per| per.abs >= score.abs }.length pvalue = (0.0 + count) / (permutations.length) pvalue = 1.0/permutations.length if pvalue == 0.0 positive ? pvalue : - pvalue end |
.score_up_down(up, down, total, missing = 0) ⇒ Object
Two sided
240 241 242 243 244 245 |
# File 'lib/rbbt/statistics/random_walk.rb', line 240 def self.score_up_down(up, down, total, missing = 0) scores_up = score(up, total, missing) scores_down = score(down, total, missing) combine(scores_up, scores_down) end |
.set_scoring(method) ⇒ Object
217 218 219 220 |
# File 'lib/rbbt/statistics/random_walk.rb', line 217 def set_scoring(method) scoring_method = method class << self; self end.send(:alias_method, :score, method.to_sym) end |