Class: TrueSkillObject

Inherits:
Object
  • Object
show all
Defined in:
lib/TrueSkill/TrueSkill.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mu = MU, sigma = SIGMA, beta = BETA, tau = TAU, draw_probability = DRAW_PROBABILITY) ⇒ TrueSkillObject

Returns a new instance of TrueSkillObject


12
13
14
15
16
17
18
# File 'lib/TrueSkill/TrueSkill.rb', line 12

def initialize(mu=MU, sigma=SIGMA, beta=BETA, tau=TAU,draw_probability=DRAW_PROBABILITY)
  @mu=mu
  @sigma=sigma
  @beta=beta
  @tau=tau
  @draw_probability=draw_probability
end

Instance Attribute Details

#betaObject

Returns the value of attribute beta


4
5
6
# File 'lib/TrueSkill/TrueSkill.rb', line 4

def beta
  @beta
end

#draw_probabilityObject

Returns the value of attribute draw_probability


4
5
6
# File 'lib/TrueSkill/TrueSkill.rb', line 4

def draw_probability
  @draw_probability
end

#muObject

Returns the value of attribute mu


4
5
6
# File 'lib/TrueSkill/TrueSkill.rb', line 4

def mu
  @mu
end

#sigmaObject

Returns the value of attribute sigma


4
5
6
# File 'lib/TrueSkill/TrueSkill.rb', line 4

def sigma
  @sigma
end

#tauObject

Returns the value of attribute tau


4
5
6
# File 'lib/TrueSkill/TrueSkill.rb', line 4

def tau
  @tau
end

Instance Method Details

#build_factor_graph(rating_groups, ranks) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/TrueSkill/TrueSkill.rb', line 48

def build_factor_graph(rating_groups,ranks)
  ratings=rating_groups.flatten
  size=ratings.length
  group_size=rating_groups.length
  rating_vars=[]
  size.times { rating_vars << Variable.new}
  
  perf_vars=[]
  size.times { perf_vars << Variable.new}
  
  teamperf_vars=[]
  group_size.times { teamperf_vars << Variable.new}
  
  teamdiff_vars=[]
  (group_size-1).times { teamdiff_vars << Variable.new}
  
  team_sizes=_team_sizes(rating_groups)
  get_perf_vars_by_team=lambda do |team|
    if team > 0
      start=team_sizes[team-1]
    else
      start=0
    end
    endv=team_sizes[team]
    return perf_vars[start,endv]
  end
  
  build_rating_layer=lambda do
    Enumerator.new do |yielder|
    
      rating_vars.zip(ratings).each do |rating_var,rating|
        yielder.yield(PriorFactor.new(rating_var,rating,@tau))
      end
    end
  end
  build_perf_layer=lambda do
    Enumerator.new do |yielder|
    
      rating_vars.zip(perf_vars).each do |rating_var,perf_var|
        yielder.yield(LikelihoodFactor.new(rating_var,perf_var,@beta**2))
      end
    end
  end
  build_teamperf_layer=lambda do
   Enumerator.new do |yielder|
     teamperf_vars.each_with_index do |teamperf_var,team|
        child_perf_vars = get_perf_vars_by_team.call(team)
        yielder.yield(SumFactor.new(teamperf_var,child_perf_vars,[1]*child_perf_vars.length))
     end
   end
 end
 build_teamdiff_layer=lambda do
  Enumerator.new do |yielder|
    teamdiff_vars.each_with_index do |teamdiff_var,team|
      yielder.yield(SumFactor.new(teamdiff_var,teamperf_vars[team,team=2],[+1,-1]))
    end
  end
 end
 build_trunc_layer=lambda do
  Enumerator.new do |yielder|
    teamdiff_vars.each_with_index do |teamdiff_var,x|
      size=0
      rating_groups[x,x+2].each do |group|
        size+=group.length
      end
      draw_margin=calc_draw_margin(@draw_probability,@beta,size)
      
      if ranks[x]==ranks[x+1]
        v_func,w_func=$v_drawFunc,$w_drawFunc
      else
        v_func,w_func=$vFunc,$wFunc
      end
      yielder.yield(TruncateFactor.new(teamdiff_var,v_func,w_func,draw_margin))
    end
  end
 end
 
 return Array(build_rating_layer.call),Array(build_perf_layer.call),Array(build_teamperf_layer.call),Array(build_teamdiff_layer.call),Array(build_trunc_layer.call)
end

#make_as_globalObject


31
32
33
# File 'lib/TrueSkill/TrueSkill.rb', line 31

def make_as_global
  return setup(nil,nil,nil,nil,nil,self)
end

#match_quality(rating_groups) ⇒ Object


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/TrueSkill/TrueSkill.rb', line 211

def match_quality(rating_groups)
  rating_groups=validate_rating_groups(rating_groups)
  ratings=rating_groups.flatten
  length=ratings.length
  mean_rows=[]
  ratings.each {|r| mean_rows << [r.mu] }
  mean_matrix=Matrix.new(mean_rows)
  variance_matrix=Proc.new do |width,height|
    Enumerator.new do |yielder|
      sig=[]
      ratings.each {|x| sig << (x.sigma**2)}
      sig.each_with_index do |variance,x|
        yielder.yield [x,x],variance
      end
    end
  end
  variance_matrix=Matrix.new(nil,length,length,:func=>variance_matrix)
  #pp variance_matrix 
  rotated_a_matrix=Proc.new do |set_width,set_height|
    Enumerator.new do |yielder|
        t=0
        r2=0
        x2=0
        rating_groups.take(rating_groups.size-1).zip(rating_groups.drop(1)).each_with_index do |y,r|
          cur=y[0]
          nex=y[1]
          z=0
          (t...t+cur.length).each do |x|
            yielder.yield [r,x],1
            t+=1
            z=x
          end
          z+=1
          (z...z+nex.length).each do |x|
            pp 
            yielder.yield [r,x],-1
            z=x
          end
          r2=r
          x2=z
        end
        set_width.call(x2+1)
        set_height.call(r2+1)
    end
  end
  rotated_a_matrix=Matrix.new(nil,nil,nil,:func=>rotated_a_matrix)
  a_matrix = rotated_a_matrix.transpose()
  _ata = (@beta ** 2) * rotated_a_matrix * a_matrix
  _atsa=rotated_a_matrix*(variance_matrix*a_matrix)
  start=mean_matrix.transpose()*a_matrix
  middle=_ata+_atsa
  endv=rotated_a_matrix*mean_matrix
  e_arg=(-0.5*start*middle.inverse()*endv).determinant()
  s_arg=_ata.determinant()/middle.determinant()
  return Math.exp(e_arg)*Math.sqrt(s_arg)
end

#Rating(mu = nil, sigma = nil) ⇒ Object


21
22
23
24
25
26
27
28
29
# File 'lib/TrueSkill/TrueSkill.rb', line 21

def Rating(mu=nil,sigma=nil)
  if mu.nil?
    mu=@mu
  end
  if sigma.nil?
    sigma=@sigma
  end
  return Rating.new mu,sigma
end

#run_schedule(rating_layer, perf_layer, teamperf_layer, teamdiff_layer, trunc_layer, min_delta = DELTA) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/TrueSkill/TrueSkill.rb', line 128

def run_schedule(rating_layer, perf_layer, teamperf_layer,teamdiff_layer, trunc_layer, min_delta=DELTA)
  [rating_layer,perf_layer,teamperf_layer].flatten.each do |f|
    f.down
  end
  teamdiff_len=teamdiff_layer.length
  (10).times do |x|
    if teamdiff_len==1
      teamdiff_layer[0].down
      delta=trunc_layer[0].up
    else
      delta=0
      (teamdiff_len-1).times do |x2|
        teamdiff_layer[x2].down
        delta=[delta,trunc_layer[x2].up].max
        teamdiff_layer[x2].up(1)
      end
      (teamdiff_len-1).step(0,-1) do |x2|
        teamdiff_layer[x2].down
        delta=[delta,trunc_layer[x2].up].max
        teamdiff_layer[x2].up(0)
      end
    end
    if delta<=min_delta
      break
    end
  end
  teamdiff_layer.first.up(0)
  teamdiff_layer.last.up(1)
  teamperf_layer.each do |f|
    (f.vars.length-1).times do |x|
      f.up(x)
    end
  end
  perf_layer.each do |f|
    f.up
  end
end

#transform_ratings(rating_groups, ranks = nil, min_delta = DELTA) ⇒ Object


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/TrueSkill/TrueSkill.rb', line 165

def transform_ratings(rating_groups, ranks=nil, min_delta=DELTA)
  rating_groups=validate_rating_groups(rating_groups)
  #pp "Start groups"
  #pp rating_groups
  group_size=rating_groups.length
  if ranks.nil?
    ranks=Array(0..group_size-1)
  end
  sorting=ranks.zip(rating_groups).each_with_index.to_a.map {|x| x.reverse}.sort { |x,y| x[1][0] <=> y[1][0]}
  sorted_groups=[]
  sorting.each do |x,g|
    sorted_groups << g[1]
  end
  sorted_ranks=ranks.sort()
  unsorting_hint=[]
  sorting.each do |x,g|
    unsorting_hint << x
  end
  #pp "Sorted Groups"
  #pp sorted_groups
  #pp "Sorted Ranks"
  #pp sorted_ranks
  layers=build_factor_graph(sorted_groups,sorted_ranks)
  run_schedule(layers[0],layers[1],layers[2],layers[3],layers[4])
  rating_layer,team_sizes=layers[0],_team_sizes(sorted_groups)
  #pp "Team Sizes"
  #pp team_sizes
  #pp "Rating Layer"
  #pp rating_layer
  transformed_groups=[]
  ([0]+team_sizes.take(team_sizes.size - 1)).zip(team_sizes).each do |start,ending|
    group=[]
    rating_layer[start...ending].each do |f|
      group << Rating.new(f.var.mu,f.var.sigma)
    end
    transformed_groups << Array(group)
  end
  #pp "Transformed"
  #pp transformed_groups
  unsorting=unsorting_hint.zip(transformed_groups).sort { |x,y| x[0]<=>y[0]}
  output=[]
  unsorting.each do |x,g|
    output << g
  end
  return output
end

#validate_rating_groups(rating_groups) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/TrueSkill/TrueSkill.rb', line 34

def validate_rating_groups(rating_groups)
  rating_groups.each do |group|
    if group.is_a? Rating
      group=[group,]
    end
    if group.length==0
      raise "each group must contain multiple ratings"
    end
  end
  if rating_groups.length<2
    raise "need multiple rating groups"
  end
  return rating_groups
end