Module: OrderedList

Defined in:
lib/rbbt/statistics/random_walk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#total_weightsObject

Returns the value of attribute total_weights.



338
339
340
# File 'lib/rbbt/statistics/random_walk.rb', line 338

def total_weights
  @total_weights
end

#weightsObject

Returns the value of attribute weights.



338
339
340
# File 'lib/rbbt/statistics/random_walk.rb', line 338

def weights
  @weights
end

Class Method Details

.draw_hits(list, set, filename = nil, options = {}) ⇒ Object



360
361
362
363
# File 'lib/rbbt/statistics/random_walk.rb', line 360

def self.draw_hits(list, set, filename = nil, options = {})
  hits = OrderedList.hits(list, set)
  RandomWalk.draw_hits(hits, list.length, filename, options)
end

.hits(list, set) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/rbbt/statistics/random_walk.rb', line 351

def self.hits(list, set)
  set = Set.new(set) unless Set === set
  hits = []
  list.each_with_index do |e,i|
    hits << i + 1 if set.include? e # count from 1
  end
  hits
end

.setup(list, weights = nil, total_weights = nil) ⇒ Object



340
341
342
343
344
345
346
347
348
349
# File 'lib/rbbt/statistics/random_walk.rb', line 340

def self.setup(list, weights = nil, total_weights = nil)
  list.extend OrderedList
  list.weights = weights
  if weights and total_weights.nil?
    list.total_weights = Misc.sum(weights)
  else
    list.total_weights = total_weights
  end
  list
end

Instance Method Details

#draw_hits(set, filename = nil, options = {}) ⇒ Object



382
383
384
# File 'lib/rbbt/statistics/random_walk.rb', line 382

def draw_hits(set, filename = nil, options = {})
  OrderedList.draw_hits(self, set, filename, options)
end

#hits(set) ⇒ Object



365
366
367
# File 'lib/rbbt/statistics/random_walk.rb', line 365

def hits(set)
  OrderedList.hits(self, set)
end

#pvalue(set, cutoff = 0.1, options = {}) ⇒ Object

def pvalue(set, options = {})

set = Set.new(set.compact) unless Set === set
options = Misc.add_defaults options, :permutations => 10000, :missing => 0
hits = hits(set)
score = RandomWalk.score(hits.sort, self.length, 0)
permutations = RandomWalk.permutations(set.length, self.length, options[:missing], options[:permutations])
RandomWalk.pvalue(permutations, score)

end



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/rbbt/statistics/random_walk.rb', line 395

def pvalue(set, cutoff = 0.1, options = {})
  set = Set.new(set.compact) unless Set === set
  options = Misc.add_defaults options, :permutations => 10000, :missing => 0
  permutations, missing, persist_permutations = Misc.process_options options, :permutations, :missing, :persist_permutations

  hits = hits(set)
 
  return 1.0 if hits.empty?

  target_score = RandomWalk.score(hits.sort, self.length, missing)

  if persist_permutations
    permutations = RandomWalk.persisted_permutations(set.length, self.length, missing, permutations)
    RandomWalk.pvalue(permutations, target_score)
  else
    # P-value computation
    target_score_abs = target_score.abs

    max = (permutations.to_f * cutoff).ceil

    size = set.length
    total = self.length
    better_permutation_score_count = 1
    if size == 0
      1.0
    else
      (1..permutations).each do
        p= []
        RandomWalk.sample_without_replacement(total, size, p)

        permutation_score = RandomWalk.score(p.sort, total, missing).abs
        if permutation_score.abs > target_score_abs
          better_permutation_score_count += 1
        end

        return 1.0 if better_permutation_score_count > max
      end
      p = (better_permutation_score_count.to_f + 1) / permutations
      p = -p if target_score < 0
      p
    end
  end
end

#pvalue_weights(set, cutoff = 0.1, options = {}) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/rbbt/statistics/random_walk.rb', line 439

def pvalue_weights(set, cutoff = 0.1, options = {})
  raise "No weight defined" if @weights.nil?
  @total_weights ||= Misc.sum(@weights)

  set = Set.new(set.compact) unless Set === set
  options = Misc.add_defaults options, :permutations => 10000, :missing => 0
  permutations, missing = Misc.process_options options, :permutations, :missing

  hits = hits(set)

  return 1.0 if hits.empty?

  target_score = RandomWalk.score_weights(hits.sort, @weights, @total_weights, self.length, 0)
  target_score_abs = target_score.abs

  max = (permutations.to_f * cutoff).ceil

  size = set.length
  total = self.length
  better_permutation_score_count = 1
  if size == 0
    1.0
  else
    (1..permutations).each do
      p= []
      RandomWalk.sample_without_replacement(total, size, p)

      permutation_score = RandomWalk.score_weights(p.sort, @weights, @total_weights, total, missing).abs
      if permutation_score.abs > target_score_abs
        better_permutation_score_count += 1
      end

      return 1.0 if better_permutation_score_count > max
    end
    p = (better_permutation_score_count.to_f + 1) / permutations
    p = -p if target_score < 0
    p
  end
end

#score(set) ⇒ Object



369
370
371
372
# File 'lib/rbbt/statistics/random_walk.rb', line 369

def score(set)
  hits = hits(set)
  RandomWalk.score(hits.sort, self.length, 0)
end

#score_weights(set) ⇒ Object



374
375
376
377
378
379
# File 'lib/rbbt/statistics/random_walk.rb', line 374

def score_weights(set)
  raise "No weight defined" if @weights.nil?
  @total_weights ||= Misc.sum(@weights)
  hits = hits(set)
  RandomWalk.score_weights(hits.sort, @weights, @total_weights, self.length, 0)
end