-
Notifications
You must be signed in to change notification settings - Fork 1
/
glicko_algo.py
executable file
·70 lines (49 loc) · 2.11 KB
/
glicko_algo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/python2
'''
Implementation of Mark Glickman's Glicko System.
akittredge, July 2011
'''
import math
Q = (math.log(10.0) / 400.0)
QQ = Q ** 2
pi = math.pi
def expected_outcome_given_ratings(player_rating,
other_player_rating,
other_player_rating_deviation):
exponent = -g_func(other_player_rating_deviation) * \
(player_rating - other_player_rating) / 400.0
ret_val = 1 / (1 + 10 ** exponent)
return ret_val
def g_func(rating_deviation):
denominator = math.sqrt(1.0 + 3.0 * QQ * rating_deviation**2.0 / pi**2)
return 1.0 / denominator
def dd(player, other_players):
_sum = 0
for other_player in other_players:
g_func_val = g_func(other_player.rating_deviation)
expected_outcome = expected_outcome_given_ratings(player.rating,
other_player.rating,
other_player.rating_deviation)
_sum += g_func_val**2 * expected_outcome * (1 - expected_outcome)
return 1 / (QQ * _sum)
def post_period_results(player, matches):
d_squared = dd(player, (match['opponent'] for match in matches))
denominator = (1 / player.rating_deviation ** 2 + (1 / d_squared))
factor = Q / denominator
new_deviation = math.sqrt(1 / denominator)
_sum = 0
for match in matches:
opponent_rating = match['opponent'].rating
opponent_rating_deviation = match['opponent'].rating_deviation
match_result = match['result']
expected_result = expected_outcome_given_ratings(player.rating,
opponent_rating,
opponent_rating_deviation)
g_func_val = g_func(opponent_rating_deviation)
_sum += g_func_val * (match_result - expected_result)
return {'rating' : player.rating + factor * _sum,
'deviation' : new_deviation }
class Player(object):
def __init__(self, rating, rating_deviation):
self.rating = rating
self.rating_deviation = rating_deviation