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.



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

def total_weights
  @total_weights
end

#weightsObject

Returns the value of attribute weights.



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

def weights
  @weights
end

Class Method Details

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



403
404
405
406
# File 'lib/rbbt/statistics/random_walk.rb', line 403

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



394
395
396
397
398
399
400
401
# File 'lib/rbbt/statistics/random_walk.rb', line 394

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



383
384
385
386
387
388
389
390
391
392
# File 'lib/rbbt/statistics/random_walk.rb', line 383

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



425
426
427
# File 'lib/rbbt/statistics/random_walk.rb', line 425

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

#hits(set) ⇒ Object



408
409
410
# File 'lib/rbbt/statistics/random_walk.rb', line 408

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

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



429
430
431
432
433
434
435
436
437
438
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
# File 'lib/rbbt/statistics/random_walk.rb', line 429

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.length < 3

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

  if persist_permutations 
    permutations = RandomWalk.persisted_permutations(hits.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 = hits.length
    total = self.length
    better_permutation_score_count = 1
    (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

#pvalue_up_down(up_set, down_set, cutoff = 0.1, options = {}) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/rbbt/statistics/random_walk.rb', line 470

def pvalue_up_down(up_set, down_set, cutoff = 0.1, options = {})
  up_set = Set.new(up_set.compact) unless Set === up_set
  down_set = Set.new(down_set.compact) unless Set === down_set

  options = Misc.add_defaults options, :permutations => 10000, :missing => 0
  permutations, missing, persist_permutations = Misc.process_options options, :permutations, :missing, :persist_permutations

  up_hits = hits(up_set)
  down_hits = hits(down_set)
 
  return 1.0 if up_hits.length + down_hits.length < 3

  target_score = RandomWalk.score_up_down(up_hits.sort, down_hits.sort, self.length, missing)

  if persist_permutations 
    permutations = RandomWalk.persisted_permutations_up_down(up_hits.length, down_hits.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

    up_size = up_set.length
    down_size = down_set.length

    total = self.length
    better_permutation_score_count = 1

    if size == 0
      1.0
    else
      (1..permutations).each do
        up_p= []
        RandomWalk.sample_without_replacement(total, up_size, up_p)
        down_p= []
        RandomWalk.sample_without_replacement(total, down_size, down_p)

        permutation_score = RandomWalk.score_up_down(up_p.sort, down_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



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/rbbt/statistics/random_walk.rb', line 523

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.length < 3

  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



412
413
414
415
# File 'lib/rbbt/statistics/random_walk.rb', line 412

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

#score_weights(set) ⇒ Object



417
418
419
420
421
422
# File 'lib/rbbt/statistics/random_walk.rb', line 417

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