Class: Saulabs::TrueSkill::Factors::WeightedSum

Inherits:
Base
  • Object
show all
Defined in:
lib/saulabs/trueskill/factors/weighted_sum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#bind, #message_count, #reset_marginals, #send_message_at

Constructor Details

#initialize(variable, ratings, weights) ⇒ WeightedSum

Returns a new instance of WeightedSum.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/saulabs/trueskill/factors/weighted_sum.rb', line 11

def initialize(variable, ratings, weights)
  super()
  @weights = [weights]
  @weights_squared = [@weights.first.map { |w| w**2 }]
  @index_order = [(0..weights.size+1).to_a]
  (1..weights.size).each do |idx|
    dest_idx = 0
    @weights[idx] = []
    @weights_squared[idx] = []
    @index_order << [idx]
    (0..ratings.size-1).each do |src_idx|
      next if src_idx == idx-1
      weight = weights[idx-1] == 0 ? 0.0 : -weights[src_idx] / weights[idx-1]
      @weights[idx][dest_idx] = weight
      @weights_squared[idx][dest_idx] = weight**2
      @index_order.last[dest_idx+1] = src_idx+1
      dest_idx += 1
    end
    final_weight = weights[idx-1] == 0 ? 0.0 : 1.0 / weights[idx-1]
    @weights[idx][dest_idx] = final_weight
    @weights_squared[idx][dest_idx] = final_weight**2
    @index_order.last[weights.size] = 0
  end
  bind(variable)
  ratings.each { |v| bind(v) }
end

Instance Attribute Details

#index_orderObject (readonly)

Returns the value of attribute index_order.



9
10
11
# File 'lib/saulabs/trueskill/factors/weighted_sum.rb', line 9

def index_order
  @index_order
end

#weightsObject (readonly)

Returns the value of attribute weights.



9
10
11
# File 'lib/saulabs/trueskill/factors/weighted_sum.rb', line 9

def weights
  @weights
end

#weights_squaredObject (readonly)

Returns the value of attribute weights_squared.



9
10
11
# File 'lib/saulabs/trueskill/factors/weighted_sum.rb', line 9

def weights_squared
  @weights_squared
end

Instance Method Details

#log_normalizationObject



50
51
52
53
54
55
56
# File 'lib/saulabs/trueskill/factors/weighted_sum.rb', line 50

def log_normalization
  res = 0
  (1..@variables.size-1).each do |i|
    res += Gauss::Distribution.log_ratio_normalization(@variables[i], @messages[i])
  end
  res
end

#update_message_at(index) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/saulabs/trueskill/factors/weighted_sum.rb', line 38

def update_message_at(index)
  raise "illegal message index: #{index}" if index < 0 || index >= @messages.count
  indices = @index_order[index]
  updated_messages = []
  updated_variables = []
  @messages.each_index do |i|
    updated_messages << @messages[indices[i]]
    updated_variables << @variables[indices[i]]
  end
  update_helper(@weights[index], @weights_squared[index], updated_messages, updated_variables)
end