Class: RankAggregation::MarkovChain

Inherits:
Object
  • Object
show all
Defined in:
lib/rank-aggregation/markov.rb

Instance Method Summary collapse

Constructor Details

#initialize(items, transitions, logger) ⇒ MarkovChain

Returns a new instance of MarkovChain.



3
4
5
6
7
# File 'lib/rank-aggregation/markov.rb', line 3

def initialize(items, transitions, logger)
  @logger = logger
  @transitions = transitions
  @items = items
end

Instance Method Details

#stationary_distributionObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rank-aggregation/markov.rb', line 9

def stationary_distribution
  dist = {}

  @items.each{|x| dist[x] = 1.0 / @items.size }

  3.times{ |i|
    @logger.debug "markov chain iteration #{i}"
    new_dist = Hash.new(0.0)

    dist.each{|x, p|
      @transitions[x].each{|y, q|
        new_dist[y] += p * q
      }
    }
    dist = new_dist 
  }

  dist
end