Class: WholeHistoryRating::PlayerDay

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player, day) ⇒ PlayerDay

Returns a new instance of PlayerDay.



5
6
7
8
9
10
11
# File 'lib/whole_history_rating/player_day.rb', line 5

def initialize(player, day)
  @day = day
  @player = player
  @is_first_day = false
  @won_games = []
  @lost_games = []
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



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

def day
  @day
end

#is_first_dayObject

Returns the value of attribute is_first_day.



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

def is_first_day
  @is_first_day
end

#lost_gamesObject

Returns the value of attribute lost_games.



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

def lost_games
  @lost_games
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#playerObject

Returns the value of attribute player.



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

def player
  @player
end

#rObject

Returns the value of attribute r.



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

def r
  @r
end

#uncertaintyObject

Returns the value of attribute uncertainty.



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

def uncertainty
  @uncertainty
end

#won_gamesObject

Returns the value of attribute won_games.



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

def won_games
  @won_games
end

Instance Method Details

#add_game(game) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/whole_history_rating/player_day.rb', line 99

def add_game(game)
  if (game.winner == "W" && game.white_player == @player) ||
     (game.winner == "B" && game.black_player == @player)
    @won_games << game
  else
    @lost_games << game
  end
end

#clear_game_terms_cacheObject



29
30
31
32
# File 'lib/whole_history_rating/player_day.rb', line 29

def clear_game_terms_cache
  @won_game_terms = nil
  @lost_game_terms = nil
end

#eloObject



25
26
27
# File 'lib/whole_history_rating/player_day.rb', line 25

def elo
  (@r * 400.0)/(Math.log(10))
end

#elo=(elo) ⇒ Object



21
22
23
# File 'lib/whole_history_rating/player_day.rb', line 21

def elo=(elo)
  @r = elo * (Math.log(10)/400.0)
end

#gammaObject



17
18
19
# File 'lib/whole_history_rating/player_day.rb', line 17

def gamma
  Math.exp(@r)
end

#gamma=(gamma) ⇒ Object



13
14
15
# File 'lib/whole_history_rating/player_day.rb', line 13

def gamma=(gamma)
  @r = Math.log(gamma)
end

#log_likelihoodObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/whole_history_rating/player_day.rb', line 86

def log_likelihood
  tally = 0.0
  won_game_terms.each do |a,b,c,d|
    tally += Math.log(a*gamma)
    tally -= Math.log(c*gamma + d)
  end
  lost_game_terms.each do |a,b,c,d|
    tally += Math.log(b)
    tally -= Math.log(c*gamma + d)
  end
  tally
end

#log_likelihood_derivativeObject



78
79
80
81
82
83
84
# File 'lib/whole_history_rating/player_day.rb', line 78

def log_likelihood_derivative    
  tally = 0.0
  (won_game_terms + lost_game_terms).each do |a,b,c,d|
    tally += c/(c*gamma + d)
  end 
  won_game_terms.count - gamma * tally
end

#log_likelihood_second_derivativeObject



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

def log_likelihood_second_derivative
  sum = 0.0
  (won_game_terms + lost_game_terms).each do |a,b,c,d|
    sum += (c*d) / ((c*gamma + d)**2.0)
  end 
  if gamma.nan? || sum.nan?
    puts "won_game_terms = #{won_game_terms}"
    puts "lost_game_terms = #{lost_game_terms}"
  end
  -1 * gamma * sum
end

#lost_game_termsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/whole_history_rating/player_day.rb', line 50

def lost_game_terms
  if @lost_game_terms.nil?
    @lost_game_terms = @lost_games.map do |g|
      other_gamma = g.opponents_adjusted_gamma(player)
      if other_gamma == 0 || other_gamma.nan? || other_gamma.infinite?
        puts "other_gamma (#{g.opponent(player).inspect}) = #{other_gamma}"
      end
      [0.0,other_gamma,1.0,other_gamma]
    end
    if is_first_day
      @lost_game_terms << [0.0,1.0,1.0,1.0]  # loss against virtual player ranked with gamma = 1.0
    end
  end
  @lost_game_terms
end

#update_by_1d_newtons_methodObject



108
109
110
111
112
113
114
115
116
# File 'lib/whole_history_rating/player_day.rb', line 108

def update_by_1d_newtons_method
  dlogp = log_likelihood_derivative
  d2logp = log_likelihood_second_derivative
  dr = (log_likelihood_derivative / log_likelihood_second_derivative)
  new_r = @r - dr
  #new_r = [0, @r - dr].max
  #puts "(#{player.name}) #{new_r} = #{@r} - (#{log_likelihood_derivative}/#{log_likelihood_second_derivative})"
  @r = new_r
end

#won_game_termsObject



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

def won_game_terms
  if @won_game_terms.nil?
    @won_game_terms = @won_games.map do |g|
      other_gamma = g.opponents_adjusted_gamma(player)
      if other_gamma == 0 || other_gamma.nan? || other_gamma.infinite?
        puts "other_gamma (#{g.opponent(player).inspect}) = #{other_gamma}"
      end
      [1.0,0.0,1.0,other_gamma]
    end
    if is_first_day
      @won_game_terms << [1.0,0.0,1.0,1.0]  # win against virtual player ranked with gamma = 1.0
    end
  end
  @won_game_terms
end