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.



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

def total_weights
  @total_weights
end

#weightsObject

Returns the value of attribute weights.



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

def weights
  @weights
end

Class Method Details

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



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

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



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

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



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

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



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

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

#hits(set) ⇒ Object



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

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



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
438
# File 'lib/rbbt/statistics/random_walk.rb', line 396

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



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
478
# File 'lib/rbbt/statistics/random_walk.rb', line 440

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



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

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

#score_weights(set) ⇒ Object



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

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