Class: Glicko2::RatingPeriod

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

Overview

Glicko ratings are calculated in bulk at the end of arbitrary, but fixed length, periods named rating periods. Where a period is fixed to be long enough that the average number of games that each player has played in is about 5 to 10 games. It could be weekly, monthly or more as required.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(players) ⇒ RatingPeriod

Returns a new instance of RatingPeriod.

Parameters:



10
11
12
13
14
15
16
17
18
# File 'lib/glicko2/rating_period.rb', line 10

def initialize(players)
  @players = players
  @cache = players.reduce({}) do |memo, player|
    raise DuplicatePlayerError unless memo[player.obj].nil?
    memo[player.obj] = player
    memo
  end
  @raters = players.map { |p| Rater.new(p.rating) }
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



7
8
9
# File 'lib/glicko2/rating_period.rb', line 7

def players
  @players
end

Class Method Details

.from_objs(objs) ⇒ RatingPeriod

Create rating period from list of seed objects

Parameters:

  • objs (Array<#rating,#rating_deviation,#volatility>)

    seed value objects

Returns:



24
25
26
# File 'lib/glicko2/rating_period.rb', line 24

def self.from_objs(objs)
  new(objs.map { |obj| Player.from_obj(obj) })
end

Instance Method Details

#game(game_seeds, ranks) ⇒ Object

Register a game with this rating period

Parameters:

  • game_seeds (Array<#rating,#rating_deviation,#volatility>)

    ratings participating in a game

  • ranks (Array<Integer>)

    corresponding ranks



32
33
34
35
36
37
38
39
# File 'lib/glicko2/rating_period.rb', line 32

def game(game_seeds, ranks)
  game_seeds.each_with_index do |iseed, i|
    game_seeds.each_with_index do |jseed, j|
      next if i == j
      @raters[i].add(player(jseed).rating, Util.ranks_to_score(ranks[i], ranks[j]))
    end
  end
end

#generate_next(tau) ⇒ RatingPeriod

Generate a new Glicko2::RatingPeriod with a new list of updated Player

Returns:



44
45
46
47
48
49
50
# File 'lib/glicko2/rating_period.rb', line 44

def generate_next(tau)
  p = []
  @raters.each_with_index do |rater, i|
    p << Player.new(rater.rate(tau), @players[i].obj)
  end
  self.class.new(p)
end

#player(obj) ⇒ Player

Fetch the player associated with a seed object

Parameters:

  • obj (#rating, #rating_deviation, #volatility)

    seed object

Returns:



56
57
58
# File 'lib/glicko2/rating_period.rb', line 56

def player(obj)
  @cache[obj]
end

#to_sObject



60
61
62
# File 'lib/glicko2/rating_period.rb', line 60

def to_s
  "#<RatingPeriod players=#{@players}"
end