Class: SumFactor

Inherits:
Factor show all
Defined in:
lib/TrueSkill/FactorGraph/SumFactor.rb

Instance Attribute Summary

Attributes inherited from Factor

#vars

Instance Method Summary collapse

Methods inherited from Factor

#var

Constructor Details

#initialize(sum_var, term_vars, coeffs) ⇒ SumFactor


6
7
8
9
10
11
# File 'lib/TrueSkill/FactorGraph/SumFactor.rb', line 6

def initialize(sum_var,term_vars,coeffs)
  super([sum_var]+term_vars)
  @sum=sum_var
  @terms=term_vars
  @coeffs=coeffs
end

Instance Method Details

#downObject


13
14
15
16
17
18
19
20
# File 'lib/TrueSkill/FactorGraph/SumFactor.rb', line 13

def down
 vals=@terms
 msgs=[]
 vals.each do |var|
  msgs << var[self]
 end
 update(@sum,vals,msgs,@coeffs)
end

#to_sObject


63
64
65
# File 'lib/TrueSkill/FactorGraph/SumFactor.rb', line 63

def to_s
  return "<SumFactor "+self.object_id.to_s+">"
end

#up(index = 0) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/TrueSkill/FactorGraph/SumFactor.rb', line 22

def up(index=0)
  coeff=@coeffs[index]
  x=0
  coeffs=[]
  @coeffs.each do |c|
    if x!=index
      coeffs << -c/coeff
    end
    x+=1
  end
  coeffs.insert(index,1.0/coeff)
  vals=@terms.dup
  vals[index]=@sum
  msgs=[]
  vals.each do |var|
    msgs << var[self]
  end
  return update(@terms[index],vals,msgs,coeffs)
end

#update(var, vals, msgs, coeffs) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/TrueSkill/FactorGraph/SumFactor.rb', line 42

def update(var,vals,msgs,coeffs)
  size=coeffs.length-1
  divs=[]
  (0..size).each do |x|
    vl=vals[x]
    ms=msgs[x]
    divs << vl/ms
  end
  pisum=[]
  (0..size).each do |x|
    pisum << coeffs[x]**2/divs[x].pi
  end
  pi=1.0/pisum.inject{|sum,x| sum + x }
  
  tausum=[]
  (0..size).each do |x|
    tausum << coeffs[x]*divs[x].mu
  end
  tau=pi*tausum.inject{|sum,x| sum + x }
  return var.update_message(self,pi,tau)
end