Class: WholeHistoryRating::Player

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Player

Returns a new instance of Player.



7
8
9
10
11
12
# File 'lib/whole_history_rating/player.rb', line 7

def initialize(name, config)
  @name = name
  @debug = config[:debug]
  @w2 = (Math.sqrt(config[:w2])*Math.log(10)/400)**2  # Convert from elo^2 to r^2
  @days = []
end

Instance Attribute Details

#anchor_gammaObject

Returns the value of attribute anchor_gamma.



5
6
7
# File 'lib/whole_history_rating/player.rb', line 5

def anchor_gamma
  @anchor_gamma
end

#daysObject

Returns the value of attribute days.



5
6
7
# File 'lib/whole_history_rating/player.rb', line 5

def days
  @days
end

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/whole_history_rating/player.rb', line 5

def debug
  @debug
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/whole_history_rating/player.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/whole_history_rating/player.rb', line 5

def name
  @name
end

#w2Object

Returns the value of attribute w2.



5
6
7
# File 'lib/whole_history_rating/player.rb', line 5

def w2
  @w2
end

Instance Method Details

#add_game(game) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/whole_history_rating/player.rb', line 234

def add_game(game)
  if days.last.nil? || days.last.day != game.day
    new_pday = PlayerDay.new(self, game.day)
    if days.empty?
      new_pday.is_first_day = true
      new_pday.gamma = 1
    else
      new_pday.gamma = days.last.gamma
    end
    days << new_pday
  end
  if (game.white_player == self)
    game.wpd = days.last
  else 
    game.bpd = days.last
  end
  days.last.add_game(game)
end

#compute_sigma2Object



90
91
92
93
94
95
96
# File 'lib/whole_history_rating/player.rb', line 90

def compute_sigma2
  sigma2 = []
  days.each_cons(2) do |d1,d2|
    sigma2 << (d2.day - d1.day).abs * @w2
  end
  sigma2
end

#covarianceObject



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
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/whole_history_rating/player.rb', line 169

def covariance
  r = days.map(&:r)

  sigma2 = compute_sigma2
  h = hessian(days, sigma2)
  g = gradient(r, days, sigma2)

  n = days.count

  a = []
  d = [h[0,0]]
  b = [h[0,1]]

  n = r.size    
  (1..(n-1)).each do |i|
    a[i] = h[i,i-1] / d[i-1]
    d[i] = h[i,i] - a[i] * b[i-1]
    b[i] = h[i,i+1]
  end

  dp = []
  dp[n-1] = h[n-1,n-1]    
  bp = []
  bp[n-1] = h[n-1,n-2]
  ap = []
  (n-2).downto(0) do |i|
    ap[i] = h[i,i+1] / dp[i+1]
    dp[i] = h[i,i] - ap[i]*bp[i+1]
    bp[i] = h[i,i-1]
  end

  v = []
  0.upto(n-2) do |i|
    v[i] = dp[i+1]/(b[i]*bp[i+1] - d[i]*dp[i+1])
  end
  v[n-1] = -1/d[n-1]

  #puts "a = #{a}"
  #puts "b = #{b}"
  #puts "bp = #{bp}"
  #puts "d = #{d}"
  #puts "dp = #{dp}"
  #puts "v = #{v}" 

  Matrix.build(n) do |row,col|
    if row == col
      v[row]
    elsif row == col-1
      -1*a[col]*v[col]
    else
      0
    end
  end
end

#gradient(r, days, sigma2) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/whole_history_rating/player.rb', line 63

def gradient(r, days, sigma2)
  g = []
  n = days.count
  days.each_with_index do |day,idx|
    prior = 0
    prior += -(r[idx]-r[idx+1])/sigma2[idx] if idx < (n-1)
    prior += -(r[idx]-r[idx-1])/sigma2[idx-1] if idx > 0
    if @debug
      puts "g[#{idx}] = #{day.log_likelihood_derivative} + #{prior}"
    end
    g << day.log_likelihood_derivative + prior
  end
  g
end

#hessian(days, sigma2) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/whole_history_rating/player.rb', line 45

def hessian(days, sigma2)
  n = days.count
  Matrix.build(n) do |row,col|
    if row == col
      prior = 0
      prior += -1.0/sigma2[row] if row < (n-1)
      prior += -1.0/sigma2[row-1] if row > 0
      days[row].log_likelihood_second_derivative + prior - 0.001
    elsif row == col-1
      1.0/sigma2[row]
    elsif row == col+1
      1.0/sigma2[col]
    else
      0
    end
  end
end

#inspectObject



14
15
16
# File 'lib/whole_history_rating/player.rb', line 14

def inspect
  "#{self}:(#{name})"
end

#log_likelihoodObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/whole_history_rating/player.rb', line 18

def log_likelihood
  sum = 0.0
  sigma2 = compute_sigma2
  n = days.count
  0.upto(n-1) do |i|
    prior = 0
    if i < (n-1)
      rd = days[i].r - days[i+1].r
      prior += (1/(Math.sqrt(2*Math::PI*sigma2[i]))) * Math.exp(-(rd**2)/2*sigma2[i]) 
    end
    if i > 0
      rd = days[i].r - days[i-1].r
      prior += (1/(Math.sqrt(2*Math::PI*sigma2[i-1]))) * Math.exp(-(rd**2)/2*sigma2[i-1]) 
    end
    if prior == 0
      sum += days[i].log_likelihood
    else
      if (days[i].log_likelihood.infinite? || Math.log(prior).infinite?) 
        puts "Infinity at #{inspect}: #{days[i].log_likelihood} + #{Math.log(prior)}: prior = #{prior}, days = #{days.inspect}"
        exit
      end
      sum += days[i].log_likelihood + Math.log(prior)
    end
  end
  sum
end

#run_one_newton_iterationObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/whole_history_rating/player.rb', line 78

def run_one_newton_iteration
  days.each do |day|
    day.clear_game_terms_cache
  end

  if days.count == 1
    days[0].update_by_1d_newtons_method
  elsif days.count > 1
    update_by_ndim_newton
  end
end

#update_by_ndim_newtonObject



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
127
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
165
166
167
# File 'lib/whole_history_rating/player.rb', line 98

def update_by_ndim_newton
  # r
  r = days.map(&:r)

  if @debug
    puts "Updating #{inspect}"
    days.each do |day|
      puts "day[#{day.day}] r = #{day.r}"
      puts "day[#{day.day}] win terms = #{day.won_game_terms}"
      puts "day[#{day.day}] win games = #{day.won_games}"
      puts "day[#{day.day}] lose terms = #{day.lost_game_terms}"
      puts "day[#{day.day}] lost games = #{day.lost_games}"
      puts "day[#{day.day}] log(p) = #{day.log_likelihood}"
      puts "day[#{day.day}] dlp = #{day.log_likelihood_derivative}"
      puts "day[#{day.day}] dlp2 = #{day.log_likelihood_second_derivative}"
    end
  end

  # sigma squared (used in the prior)
  sigma2 = compute_sigma2

  h = hessian(days, sigma2)
  g = gradient(r, days, sigma2)

  a = []
  d = [h[0,0]]
  b = [h[0,1]]

  n = r.size    
  (1..(n-1)).each do |i|
    a[i] = h[i,i-1] / d[i-1]
    d[i] = h[i,i] - a[i] * b[i-1]
    b[i] = h[i,i+1]
  end


  y = [g[0]]
  (1..(n-1)).each do |i|
    y[i] = g[i] - a[i] * y[i-1]
  end

  x = []
  x[n-1] = y[n-1] / d[n-1]
  (n-2).downto(0) do |i|
    x[i] = (y[i] - b[i] * x[i+1]) / d[i]
  end    

  new_r = r.zip(x).map {|ri,xi| ri-xi}

  new_r.each do |r|
    if r > 650
      raise WHR::UnstableRatingException, "Unstable r (#{new_r}) on player #{inspect}"
    end
  end

  if @debug
    puts "Hessian = #{h}"
    puts "gradient = #{g}"
    puts "a = #{a}"
    puts "d = #{d}"
    puts "b = #{b}"
    puts "y = #{y}"
    puts "x = #{x}"
    puts "#{inspect} (#{r}) => (#{new_r})"
  end

  days.each_with_index do |day,idx|
    day.r = day.r - x[idx]
  end
end

#update_uncertaintyObject



224
225
226
227
228
229
230
231
232
# File 'lib/whole_history_rating/player.rb', line 224

def update_uncertainty
  if days.count > 0
    c = covariance
    u = (0..(days.count-1)).collect{|i| c[i,i]}
    days.zip(u) {|d,u| d.uncertainty = u}
  else
    5
  end
end