Class: Glicko2::Rater

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

Overview

A Rater calculates a new Rating based on actual game outcomes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rating) ⇒ Rater

Returns a new instance of Rater.



6
7
8
9
10
# File 'lib/glicko2/rater.rb', line 6

def initialize(rating)
  @rating = rating
  @v_pre = 0.0
  @delta_pre = 0.0
end

Instance Attribute Details

#ratingObject (readonly)

Returns the value of attribute rating.



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

def rating
  @rating
end

Instance Method Details

#add(other_rating, score) ⇒ Object

Add an outcome against the rating



13
14
15
16
17
# File 'lib/glicko2/rater.rb', line 13

def add(other_rating, score)
  g, e = other_rating.gravity_expected_score(rating.mean)
  @v_pre += g**2 * e * (1 - e)
  @delta_pre += g * (score - e)
end

#rate(tau) ⇒ Object

Rate calculates Rating as at the start of the following period based on game outcomes



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

def rate(tau)
  v = @v_pre**-1
  delta2 = (@delta_pre * v)**2
  sd2 = rating.sd**2
  a = Math.log(rating.volatility**2)
  if v.finite?
    f = lambda do |x|
      expX = Math.exp(x)
      (expX * (delta2 - sd2 - v - expX)) / (2 * (sd2 + v + expX)**2) - (x - a) / tau**2
    end
    if delta2 > sd2 + v
      b = Math.log(delta2 - sd2 - v)
    else
      k = 1
      k += 1 while f.call(a - k * tau) < 0
      b = a - k * tau
    end
    a = Util.illinois_method(a, b, &f)
  end
  volatility = Math.exp(a / 2.0)
  sd_pre = Math.sqrt(sd2 + volatility**2)
  sd = 1 / Math.sqrt(1.0 / sd_pre**2 + 1 / v)
  mean = rating.mean + sd**2 * @delta_pre
  Rating.new(mean, sd, volatility)
end