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

Class Method Summary collapse

Class Attribute Details

.scoring_methodObject

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



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rbbt/statistics/random_walk.rb', line 226

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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rbbt/statistics/random_walk.rb', line 302

def self.draw_hits(hits, total, filename = nil, options = {})
  update = options[:update]

  size = options[:size] || total
  bg_color = options[:bg_color] || :white
  width = options[:width] || 20
  sections = options[: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



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rbbt/statistics/random_walk.rb', line 246

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(size_up, size_down, total, missing = 0, times = 10000) ⇒ Object



279
280
281
282
283
# File 'lib/rbbt/statistics/random_walk.rb', line 279

def self.permutations_up_down(size_up, size_down, total, missing = 0, times = 10000)
  (1..times).collect do
    score_up_down(Array.new(size_up){ (rand * total).to_i }.sort, Array.new(size_down){ (rand * total).to_i }.sort, total, missing).abs
  end
end

.persisted_permutations(size, total, missing = 0, times = 10_000) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rbbt/statistics/random_walk.rb', line 259

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

.pvalue(permutations, score) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/rbbt/statistics/random_walk.rb', line 285

def self.pvalue(permutations, score)
  score = score.abs
  permutations.inject(1){|acc, per| 

    acc += 1 if per > score
    acc
  }.to_f / permutations.length
end

.score_up_down(up, down, total, missing = 0) ⇒ Object

Two sided



239
240
241
242
243
244
# File 'lib/rbbt/statistics/random_walk.rb', line 239

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