rbbt-dm 1.1.19 → 1.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/rbbt/statistics/random_walk.rb +101 -17
 - data/test/rbbt/statistics/test_random_walk.rb +41 -2
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 3ce285713356bad8c3fe55c26a2b3ac42f8c782b
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ea8d760c4c2682df56a94c4772a53421d3ae89f7
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 15d49776f790c74e40b7c6c8f44ba320d35e66aff8525cff2ad7f375bec87fb1843fd2775128f6d06e64f37b371a727220d35b9ff0ce52e88b2c49ac8a998718
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 522aa087395221563801e0a2d1a978b2759ad312ba6eea469049b386c44a7198d939f6f05c63aa59dc905b82727384c60e836ad0c4a188377aef7555562a3be2
         
     | 
| 
         @@ -256,26 +256,42 @@ module RandomWalk 
     | 
|
| 
       256 
256 
     | 
    
         
             
                end
         
     | 
| 
       257 
257 
     | 
    
         
             
              end
         
     | 
| 
       258 
258 
     | 
    
         | 
| 
       259 
     | 
    
         
            -
              def self. 
     | 
| 
       260 
     | 
    
         
            -
                 
     | 
| 
       261 
     | 
    
         
            -
             
     | 
| 
       262 
     | 
    
         
            -
                 
     | 
| 
       263 
     | 
    
         
            -
             
     | 
| 
       264 
     | 
    
         
            -
             
     | 
| 
       265 
     | 
    
         
            -
             
     | 
| 
       266 
     | 
    
         
            -
                     
     | 
| 
       267 
     | 
    
         
            -
             
     | 
| 
       268 
     | 
    
         
            -
             
     | 
| 
       269 
     | 
    
         
            -
                     
     | 
| 
       270 
     | 
    
         
            -
                      repo[key] = p
         
     | 
| 
       271 
     | 
    
         
            -
                    end
         
     | 
| 
       272 
     | 
    
         
            -
                    p
         
     | 
| 
      
 259 
     | 
    
         
            +
              def self.permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000)
         
     | 
| 
      
 260 
     | 
    
         
            +
                if up_size == 0 or down_size == 0
         
     | 
| 
      
 261 
     | 
    
         
            +
                  [0] * times
         
     | 
| 
      
 262 
     | 
    
         
            +
                else
         
     | 
| 
      
 263 
     | 
    
         
            +
                  (1..times).collect do
         
     | 
| 
      
 264 
     | 
    
         
            +
                    up_p = []
         
     | 
| 
      
 265 
     | 
    
         
            +
                    sample_without_replacement(total, up_size, up_p)
         
     | 
| 
      
 266 
     | 
    
         
            +
                    down_p = []
         
     | 
| 
      
 267 
     | 
    
         
            +
                    sample_without_replacement(total, down_size, down_p)
         
     | 
| 
      
 268 
     | 
    
         
            +
             
     | 
| 
      
 269 
     | 
    
         
            +
                    score_up_down(up_p, down_p, total, missing).abs
         
     | 
| 
       273 
270 
     | 
    
         
             
                  end
         
     | 
| 
       274 
     | 
    
         
            -
                ensure
         
     | 
| 
       275 
     | 
    
         
            -
                  repo.close
         
     | 
| 
       276 
271 
     | 
    
         
             
                end
         
     | 
| 
       277 
272 
     | 
    
         
             
              end
         
     | 
| 
       278 
273 
     | 
    
         | 
| 
      
 274 
     | 
    
         
            +
             
     | 
| 
      
 275 
     | 
    
         
            +
              #def self.persisted_permutations(size, total, missing = 0, times = 10_000)
         
     | 
| 
      
 276 
     | 
    
         
            +
              #  repo_file = "/tmp/rw_repo7"
         
     | 
| 
      
 277 
     | 
    
         
            +
              #  repo = Persist.open_tokyocabinet(repo_file, false, :float_array)
         
     | 
| 
      
 278 
     | 
    
         
            +
              #  key = Misc.digest([size, total, missing, times, scoring_method].inspect)
         
     | 
| 
      
 279 
     | 
    
         
            +
              #  begin
         
     | 
| 
      
 280 
     | 
    
         
            +
              #    repo.read
         
     | 
| 
      
 281 
     | 
    
         
            +
              #    if repo[key]
         
     | 
| 
      
 282 
     | 
    
         
            +
              #      repo[key]
         
     | 
| 
      
 283 
     | 
    
         
            +
              #    else
         
     | 
| 
      
 284 
     | 
    
         
            +
              #      p = permutations(size, total, missing, times)
         
     | 
| 
      
 285 
     | 
    
         
            +
              #      repo.write_and_close do
         
     | 
| 
      
 286 
     | 
    
         
            +
              #        repo[key] = p
         
     | 
| 
      
 287 
     | 
    
         
            +
              #      end
         
     | 
| 
      
 288 
     | 
    
         
            +
              #      p
         
     | 
| 
      
 289 
     | 
    
         
            +
              #    end
         
     | 
| 
      
 290 
     | 
    
         
            +
              #  ensure
         
     | 
| 
      
 291 
     | 
    
         
            +
              #    repo.close
         
     | 
| 
      
 292 
     | 
    
         
            +
              #  end
         
     | 
| 
      
 293 
     | 
    
         
            +
              #end
         
     | 
| 
      
 294 
     | 
    
         
            +
             
     | 
| 
       279 
295 
     | 
    
         
             
              def self.persisted_permutations(size, total, missing = 0, times = 10_000)
         
     | 
| 
       280 
296 
     | 
    
         
             
                require 'rbbt/util/tc_cache'
         
     | 
| 
       281 
297 
     | 
    
         
             
                repo_file = "/tmp/rw_repo9"
         
     | 
| 
         @@ -287,12 +303,27 @@ module RandomWalk 
     | 
|
| 
       287 
303 
     | 
    
         
             
                p
         
     | 
| 
       288 
304 
     | 
    
         
             
              end
         
     | 
| 
       289 
305 
     | 
    
         | 
| 
      
 306 
     | 
    
         
            +
              def self.persisted_permutations_up_down(up_size, down_size, total, missing = 0, times = 10_000)
         
     | 
| 
      
 307 
     | 
    
         
            +
                require 'rbbt/util/tc_cache'
         
     | 
| 
      
 308 
     | 
    
         
            +
                repo_file = "/tmp/rw_repo9"
         
     | 
| 
      
 309 
     | 
    
         
            +
                key = Misc.digest([up_size, down_size, total, missing, times, scoring_method].inspect)
         
     | 
| 
      
 310 
     | 
    
         
            +
                cache = TCCache.open(repo_file, :float_array)
         
     | 
| 
      
 311 
     | 
    
         
            +
                p = cache.cache(key) do
         
     | 
| 
      
 312 
     | 
    
         
            +
                  permutations_up_down(up_size, down_size, total, missing, times)
         
     | 
| 
      
 313 
     | 
    
         
            +
                end
         
     | 
| 
      
 314 
     | 
    
         
            +
                p
         
     | 
| 
      
 315 
     | 
    
         
            +
              end
         
     | 
| 
      
 316 
     | 
    
         
            +
             
     | 
| 
      
 317 
     | 
    
         
            +
             
     | 
| 
       290 
318 
     | 
    
         
             
              def self.pvalue(permutations, score)
         
     | 
| 
      
 319 
     | 
    
         
            +
                positive = score > 0
         
     | 
| 
       291 
320 
     | 
    
         
             
                score = score.abs
         
     | 
| 
       292 
     | 
    
         
            -
                permutations.inject(1){|acc, per| 
         
     | 
| 
      
 321 
     | 
    
         
            +
                pvalue = permutations.inject(1){|acc, per| 
         
     | 
| 
       293 
322 
     | 
    
         
             
                  acc += 1 if per > score
         
     | 
| 
       294 
323 
     | 
    
         
             
                  acc
         
     | 
| 
       295 
324 
     | 
    
         
             
                }.to_f / permutations.length
         
     | 
| 
      
 325 
     | 
    
         
            +
             
     | 
| 
      
 326 
     | 
    
         
            +
                positive ? pvalue : - pvalue
         
     | 
| 
       296 
327 
     | 
    
         
             
              end
         
     | 
| 
       297 
328 
     | 
    
         | 
| 
       298 
329 
     | 
    
         
             
              COLORS = {
         
     | 
| 
         @@ -445,6 +476,59 @@ module OrderedList 
     | 
|
| 
       445 
476 
     | 
    
         
             
                end
         
     | 
| 
       446 
477 
     | 
    
         
             
              end
         
     | 
| 
       447 
478 
     | 
    
         | 
| 
      
 479 
     | 
    
         
            +
              def pvalue_up_down(up_set, down_set, cutoff = 0.1, options = {})
         
     | 
| 
      
 480 
     | 
    
         
            +
                up_set = Set.new(up_set.compact) unless Set === up_set
         
     | 
| 
      
 481 
     | 
    
         
            +
                down_set = Set.new(down_set.compact) unless Set === down_set
         
     | 
| 
      
 482 
     | 
    
         
            +
             
     | 
| 
      
 483 
     | 
    
         
            +
                options = Misc.add_defaults options, :permutations => 10000, :missing => 0
         
     | 
| 
      
 484 
     | 
    
         
            +
                permutations, missing, persist_permutations = Misc.process_options options, :permutations, :missing, :persist_permutations
         
     | 
| 
      
 485 
     | 
    
         
            +
             
     | 
| 
      
 486 
     | 
    
         
            +
                up_hits = hits(up_set)
         
     | 
| 
      
 487 
     | 
    
         
            +
                down_hits = hits(down_set)
         
     | 
| 
      
 488 
     | 
    
         
            +
               
         
     | 
| 
      
 489 
     | 
    
         
            +
                return 1.0 if up_hits.empty? or down_hits.empty? # Repasar
         
     | 
| 
      
 490 
     | 
    
         
            +
             
     | 
| 
      
 491 
     | 
    
         
            +
                target_score = RandomWalk.score_up_down(up_hits.sort, down_hits.sort, self.length, missing)
         
     | 
| 
      
 492 
     | 
    
         
            +
             
     | 
| 
      
 493 
     | 
    
         
            +
                if persist_permutations 
         
     | 
| 
      
 494 
     | 
    
         
            +
                  permutations = RandomWalk.persisted_permutations_up_down(up_set.length, down_set.length, self.length, missing, permutations)
         
     | 
| 
      
 495 
     | 
    
         
            +
                  RandomWalk.pvalue(permutations, target_score)
         
     | 
| 
      
 496 
     | 
    
         
            +
                else
         
     | 
| 
      
 497 
     | 
    
         
            +
                  # P-value computation
         
     | 
| 
      
 498 
     | 
    
         
            +
                  target_score_abs = target_score.abs
         
     | 
| 
      
 499 
     | 
    
         
            +
             
     | 
| 
      
 500 
     | 
    
         
            +
                  max = (permutations.to_f * cutoff).ceil
         
     | 
| 
      
 501 
     | 
    
         
            +
             
     | 
| 
      
 502 
     | 
    
         
            +
                  up_size = up_set.length
         
     | 
| 
      
 503 
     | 
    
         
            +
                  down_size = down_set.length
         
     | 
| 
      
 504 
     | 
    
         
            +
             
     | 
| 
      
 505 
     | 
    
         
            +
                  total = self.length
         
     | 
| 
      
 506 
     | 
    
         
            +
                  better_permutation_score_count = 1
         
     | 
| 
      
 507 
     | 
    
         
            +
             
     | 
| 
      
 508 
     | 
    
         
            +
                  if size == 0
         
     | 
| 
      
 509 
     | 
    
         
            +
                    1.0
         
     | 
| 
      
 510 
     | 
    
         
            +
                  else
         
     | 
| 
      
 511 
     | 
    
         
            +
                    (1..permutations).each do
         
     | 
| 
      
 512 
     | 
    
         
            +
                      up_p= []
         
     | 
| 
      
 513 
     | 
    
         
            +
                      RandomWalk.sample_without_replacement(total, up_size, up_p)
         
     | 
| 
      
 514 
     | 
    
         
            +
                      down_p= []
         
     | 
| 
      
 515 
     | 
    
         
            +
                      RandomWalk.sample_without_replacement(total, down_size, down_p)
         
     | 
| 
      
 516 
     | 
    
         
            +
             
     | 
| 
      
 517 
     | 
    
         
            +
                      permutation_score = RandomWalk.score_up_down(up_p.sort, down_p.sort, total, missing).abs
         
     | 
| 
      
 518 
     | 
    
         
            +
                      if permutation_score.abs > target_score_abs
         
     | 
| 
      
 519 
     | 
    
         
            +
                        better_permutation_score_count += 1
         
     | 
| 
      
 520 
     | 
    
         
            +
                      end
         
     | 
| 
      
 521 
     | 
    
         
            +
             
     | 
| 
      
 522 
     | 
    
         
            +
                      return 1.0 if better_permutation_score_count > max
         
     | 
| 
      
 523 
     | 
    
         
            +
                    end
         
     | 
| 
      
 524 
     | 
    
         
            +
                    p = (better_permutation_score_count.to_f + 1) / permutations
         
     | 
| 
      
 525 
     | 
    
         
            +
                    p = -p if target_score < 0
         
     | 
| 
      
 526 
     | 
    
         
            +
                    p
         
     | 
| 
      
 527 
     | 
    
         
            +
                  end
         
     | 
| 
      
 528 
     | 
    
         
            +
                end
         
     | 
| 
      
 529 
     | 
    
         
            +
              end
         
     | 
| 
      
 530 
     | 
    
         
            +
             
     | 
| 
      
 531 
     | 
    
         
            +
             
     | 
| 
       448 
532 
     | 
    
         
             
              def pvalue_weights(set, cutoff = 0.1, options = {})
         
     | 
| 
       449 
533 
     | 
    
         
             
                raise "No weight defined" if @weights.nil?
         
     | 
| 
       450 
534 
     | 
    
         
             
                @total_weights ||= Misc.sum(@weights)
         
     | 
| 
         @@ -18,11 +18,50 @@ class TestRandomWalk < Test::Unit::TestCase 
     | 
|
| 
       18 
18 
     | 
    
         
             
                list = (1..1000).to_a
         
     | 
| 
       19 
19 
     | 
    
         
             
                list.extend OrderedList
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
                assert list.pvalue((1..100).to_a, 0.05) < 0.05
         
     | 
| 
      
 21 
     | 
    
         
            +
                assert list.pvalue((1..100).to_a, 0.05).abs < 0.05
         
     | 
| 
      
 22 
     | 
    
         
            +
                assert list.pvalue((1..100).to_a, 0.05) > 0
         
     | 
| 
      
 23 
     | 
    
         
            +
                
         
     | 
| 
      
 24 
     | 
    
         
            +
                assert list.pvalue((900..1000).to_a, 0.05).abs < 0.05
         
     | 
| 
      
 25 
     | 
    
         
            +
                assert list.pvalue((900..1000).to_a, 0.05) < 0
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
       22 
27 
     | 
    
         
             
                assert list.pvalue([100, 200, 300, 400, 500], 0.05) > 0.05
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                assert list.pvalue((1..100).to_a, 0.05, :persisted_permutations => true).abs < 0.05
         
     | 
| 
      
 30 
     | 
    
         
            +
                assert list.pvalue((1..100).to_a, 0.05, :persisted_permutations => true) > 0
         
     | 
| 
      
 31 
     | 
    
         
            +
                
         
     | 
| 
      
 32 
     | 
    
         
            +
                assert list.pvalue((900..1000).to_a, 0.05, :persisted_permutations => true).abs < 0.05
         
     | 
| 
      
 33 
     | 
    
         
            +
                assert list.pvalue((900..1000).to_a, 0.05, :persisted_permutations => true) < 0
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                assert list.pvalue([100, 200, 300, 400, 500], 0.05, :persisted_permutations => true) > 0.05
         
     | 
| 
      
 36 
     | 
    
         
            +
              end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
              def test_pvalue_up_down
         
     | 
| 
      
 39 
     | 
    
         
            +
                Log.severity = 0
         
     | 
| 
      
 40 
     | 
    
         
            +
                list = (1..1000).to_a
         
     | 
| 
      
 41 
     | 
    
         
            +
                list.extend OrderedList
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                assert list.pvalue_up_down((1..100).to_a, (900..1000).to_a, 0.05, :persist_permutations => false).abs < 0.05
         
     | 
| 
      
 44 
     | 
    
         
            +
                assert list.pvalue_up_down((1..100).to_a, (900..1000).to_a, 0.05, :persist_permutations => false) > 0
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                assert list.pvalue_up_down((900..1000).to_a, (1..100).to_a, 0.05, :persist_permutations => false).abs < 0.05
         
     | 
| 
      
 47 
     | 
    
         
            +
                assert list.pvalue_up_down((900..1000).to_a, (1..100).to_a, 0.05, :persist_permutations => false) < 0
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                rand_top = (1..100).to_a.collect{ rand(1000) }
         
     | 
| 
      
 50 
     | 
    
         
            +
                rand_bottom = (1..100).to_a.collect{ rand(1000) }
         
     | 
| 
      
 51 
     | 
    
         
            +
                assert list.pvalue_up_down(rand_top, rand_bottom, 0.05, :persisted_permutations => false).abs > 0.05
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                assert list.pvalue_up_down((1..100).to_a, (900..1000).to_a, 0.05, :persist_permutations => true).abs < 0.05
         
     | 
| 
      
 54 
     | 
    
         
            +
                assert list.pvalue_up_down((1..100).to_a, (900..1000).to_a, 0.05, :persist_permutations => true) > 0
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                assert list.pvalue_up_down((900..1000).to_a, (1..100).to_a, 0.05, :persist_permutations => true).abs < 0.05
         
     | 
| 
      
 57 
     | 
    
         
            +
                assert list.pvalue_up_down((900..1000).to_a, (1..100).to_a, 0.05, :persist_permutations => true) < 0
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                rand_top = (1..100).to_a.collect{ rand(1000) }
         
     | 
| 
      
 60 
     | 
    
         
            +
                rand_bottom = (1..100).to_a.collect{ rand(1000) }
         
     | 
| 
      
 61 
     | 
    
         
            +
                assert list.pvalue_up_down(rand_top, rand_bottom, 0.05, :persisted_permutations => true).abs > 0.05
         
     | 
| 
       23 
62 
     | 
    
         
             
              end
         
     | 
| 
       24 
63 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
              def  
     | 
| 
      
 64 
     | 
    
         
            +
              def __test_pvalue_weights
         
     | 
| 
       26 
65 
     | 
    
         
             
                list = (1..1000).to_a
         
     | 
| 
       27 
66 
     | 
    
         | 
| 
       28 
67 
     | 
    
         
             
                weights = list.collect{|v| (Misc.mean(list) - v)**2}
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: rbbt-dm
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.1.20
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Miguel Vazquez
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2016-02-11 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: rbbt-util
         
     |