refactoring (some formatting, mostly ruby'ization)

This commit is contained in:
swaits committed 2011-12-19 15:23:11 -08:00
1 parent b240814147
commit 59537258f7
1 file changed
+8 -23
+8 -23
View File
@@ -108,8 +108,8 @@ class Glicko2
# and/or add_draw(). This method is called automatically whenever update() # and/or add_draw(). This method is called automatically whenever update()
# is called. # is called.
def clear_results def clear_results
@opponents = [] @opponents = Array.new
@results = [] @results = Array.new
end end
# Add a result to this rating. Note that no calculation is performed until # Add a result to this rating. Note that no calculation is performed until
@@ -156,28 +156,16 @@ class Glicko2
end end
# compute variance # compute variance
variance = 1.0 / @results.inject(0.0) do |sum,r|
#variance = 0.0
#@results.inject(0.0) do |variance,r|
# g_i = Glicko2.g(r.opponent.g2deviation)
# e_i = Glicko2.E(@g2rating,r.opponent.g2rating,r.opponent.g2deviation)
# g_i ** 2.0 * e_i * (1.0 - e_i)
#end
variance = 0.0
@results.each do |r|
g_i = Glicko2.g(r.opponent.g2deviation) g_i = Glicko2.g(r.opponent.g2deviation)
e_i = Glicko2.E(@g2rating, r.opponent.g2rating, r.opponent.g2deviation) e_i = Glicko2.E(@g2rating, r.opponent.g2rating, r.opponent.g2deviation)
variance += g_i ** 2.0 * e_i * (1.0 - e_i) sum + g_i**2.0 * e_i * (1.0 - e_i)
end end
variance = 1.0 / variance
# compute delta # compute delta
delta = 0.0 delta = variance * @results.inject(0.0) do |sum,r|
@results.each do |r| sum + Glicko2.g(r.opponent.g2deviation) * (r.result - Glicko2.E(@g2rating, r.opponent.g2rating, r.opponent.g2deviation))
delta += Glicko2.g(r.opponent.g2deviation) * (r.result - Glicko2.E(@g2rating, r.opponent.g2rating, r.opponent.g2deviation))
end end
delta *= variance
# determine new volatility # determine new volatility
new_volatility = 0.0 new_volatility = 0.0
@@ -198,12 +186,9 @@ class Glicko2
# update the rating and deviation # update the rating and deviation
new_deviation = 1.0 / (Math.sqrt(1.0/(pre_deviation**2.0) + 1.0 / variance)) new_deviation = 1.0 / (Math.sqrt(1.0/(pre_deviation**2.0) + 1.0 / variance))
new_rating = 0.0 new_rating = @g2rating + new_deviation**2.0 * @results.inject(0.0) do |sum,r|
@results.each do |r| sum + Glicko2.g(r.opponent.g2deviation) * (r.result - Glicko2.E(@g2rating, r.opponent.g2rating, r.opponent.g2deviation))
new_rating += Glicko2.g(r.opponent.g2deviation) * (r.result - Glicko2.E(@g2rating, r.opponent.g2rating, r.opponent.g2deviation))
end end
new_rating = new_rating * new_deviation**2.0
new_rating += @g2rating
# wipe our result lists # wipe our result lists
clear_results clear_results