Class: Saulabs::TrueSkill::Factors::Within

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

Instance Method Summary collapse

Methods inherited from Base

#bind, #message_count, #reset_marginals, #send_message_at

Constructor Details

#initialize(epsilon, variable) ⇒ Within

Returns a new instance of Within.



10
11
12
13
14
# File 'lib/saulabs/trueskill/factors/within.rb', line 10

def initialize(epsilon, variable)
  super()
  @epsilon = epsilon
  bind(variable) 
end

Instance Method Details

#log_normalizationObject



16
17
18
19
20
21
22
# File 'lib/saulabs/trueskill/factors/within.rb', line 16

def log_normalization
 msg = @variables[0] / @messages[0]
 mean = msg.mean
 dev = msg.deviation
 z = Gauss::Distribution.cdf((@epsilon - mean) / dev) - Gauss::Distribution.cdf((-@epsilon - mean) / dev)
 -Gauss::Distribution.log_product_normalization(msg, @messages[0]) + Math.log(z)
end

#update_message_at(index) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/saulabs/trueskill/factors/within.rb', line 24

def update_message_at(index)
  message = @messages[index]
  variable = @variables[index]
  msg = variable / message
  c = msg.precision
  d = msg.precision_mean
  sqrt_c = Math.sqrt(c)
  d_sqrt_c = sqrt_c == 0 ? 0.0 : d / sqrt_c
  e_sqrt_c = @epsilon * sqrt_c
  denom = 1.0 - Gauss::TruncatedCorrection.w_within_margin(d_sqrt_c, e_sqrt_c)
  new_precision = c / denom
  new_precision_mean = (d + sqrt_c * Gauss::TruncatedCorrection.v_within_margin(d_sqrt_c, e_sqrt_c)) / denom
  new_marginal = Gauss::Distribution.with_precision(new_precision_mean, new_precision)
  new_message = message * new_marginal / variable
  diff = new_marginal - variable
  message.replace(new_message)
  variable.replace(new_marginal)
  return diff
end