Class: Glicko2::Player

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

Overview

Calculates a new Glicko2 ranking based on a seed object and game outcomes.

The example from the Glicko2 paper, where a player wins against the first opponent, but then looses against the next two:

Rating = Struct.new(:rating, :rating_deviation, :volatility)

player_seed = Rating.new(1500, 200, 0.06)
opponent1_seed = Rating.new(1400, 30, 0.06)
opponent2_seed = Rating.new(1550, 100, 0.06)
opponent3_seed = Rating.new(1700, 300, 0.06)

player = Glicko2::Player.from_obj(player_seed)
opponent1 = Glicko2::Player.from_obj(opponent1_seed)
opponent2 = Glicko2::Player.from_obj(opponent2_seed)
opponent3 = Glicko2::Player.from_obj(opponent3_seed)

new_player = player.generate_next([opponent1, opponent2, opponent3],
                                  [1, 0, 0])
new_player.update_obj

puts player_seed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rating, obj = nil) ⇒ Player

Returns a new instance of Player.

Parameters:

  • mean (Numeric)

    player mean

  • sd (Numeric)

    player standard deviation

  • volatility (Numeric)

    player volatility

  • obj (#rating, #rating_deviation, #volatility) (defaults to: nil)

    seed values object



43
44
45
46
# File 'lib/glicko2/player.rb', line 43

def initialize(rating, obj=nil)
  @rating = rating
  @obj = obj
end

Instance Attribute Details

#objObject (readonly)

Returns the value of attribute obj.



26
27
28
# File 'lib/glicko2/player.rb', line 26

def obj
  @obj
end

#ratingObject (readonly)

Returns the value of attribute rating.



26
27
28
# File 'lib/glicko2/player.rb', line 26

def rating
  @rating
end

Class Method Details

.from_obj(obj, config = nil) ⇒ Player

Create a Glicko2::Player from a seed object, converting from Glicko ratings to Glicko2.

Parameters:

Returns:

  • (Player)

    constructed instance.



33
34
35
36
37
# File 'lib/glicko2/player.rb', line 33

def self.from_obj(obj, config=nil)
  rating = Rating.from_glicko_rating(obj.rating, obj.rating_deviation,
                                     obj.volatility, config)
  new(rating, obj)
end

Instance Method Details

#generate_next(others, scores) ⇒ Player

Create new Glicko2::Player with updated values.

This method will not modify any objects that are passed into it.

Parameters:

  • others (Array<Player>)

    list of opponent players

  • scores (Array<Numeric>)

    list of correlating scores (‘0` for a loss, `0.5` for a draw and `1` for a win).

Returns:



56
57
58
59
60
61
62
63
# File 'lib/glicko2/player.rb', line 56

def generate_next(others, scores)
  if others.compact.length < 1
    generate_next_without_games
  else
    others = others.compact.map{ |other| other.rating }
    generate_next_with_games(others, scores)
  end
end

#meanObject



73
74
75
# File 'lib/glicko2/player.rb', line 73

def mean
  rating.mean
end

#standard_deviationObject Also known as: sd



77
78
79
# File 'lib/glicko2/player.rb', line 77

def standard_deviation
  rating.standard_deviation
end

#to_sObject



86
87
88
# File 'lib/glicko2/player.rb', line 86

def to_s
  "#<Player rating=#{rating}, obj=#{obj}>"
end

#update_objObject

Update seed object with this player’s values



66
67
68
69
70
71
# File 'lib/glicko2/player.rb', line 66

def update_obj
  glicko_rating = rating.to_glicko_rating
  @obj.rating = glicko_rating.mean
  @obj.rating_deviation = glicko_rating.standard_deviation
  @obj.volatility = volatility
end

#volatilityObject



82
83
84
# File 'lib/glicko2/player.rb', line 82

def volatility
  rating.volatility
end