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.



343
344
345
# File 'lib/rbbt/statistics/random_walk.rb', line 343

def total_weights
  @total_weights
end

#weightsObject

Returns the value of attribute weights.



343
344
345
# File 'lib/rbbt/statistics/random_walk.rb', line 343

def weights
  @weights
end

Class Method Details

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



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

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



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

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



345
346
347
348
349
350
351
352
353
354
# File 'lib/rbbt/statistics/random_walk.rb', line 345

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



387
388
389
# File 'lib/rbbt/statistics/random_walk.rb', line 387

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

#hits(set) ⇒ Object



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

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



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
438
439
440
441
442
# File 'lib/rbbt/statistics/random_walk.rb', line 400

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



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
478
479
480
481
482
# File 'lib/rbbt/statistics/random_walk.rb', line 444

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



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

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

#score_weights(set) ⇒ Object



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

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