-
Notifications
You must be signed in to change notification settings - Fork 1
/
glicko.py
148 lines (129 loc) · 4.77 KB
/
glicko.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Copyright (c) 201 by Matt Sewall.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
# Glicko
# python 3.4.3
# Copyright (c) 2016 by Matt Sewall.
# All rights reserved.
import math
# Background information - https://en.wikipedia.org/wiki/Glicko_rating_system
# Based on this equation - http://www.glicko.net/glicko/glicko2.pdf
INITRAT = 1500.0
_MAXSIZE = 186
_MAXMULTI = .272
_MULTISLOPE = .00391
_WIN = 1.0
_LOSS = 0
_CATCH = .5
_VOL = .05
_CONV = 173.7178
_EPS = 0.0001
class GlickoPlayer:
def __init__(self, name, place, rating, confidence, volatility):
self.name = name
self.place = place
self.rating = rating
self.confidence = confidence
self.volatility = volatility
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
def findSigma(mu, phi, sigma, change, v):
alpha = math.log(sigma ** 2)
def f(x):
tmp = phi ** 2 + v + math.exp(x)
a = math.exp(x) * (change ** 2 - tmp) / (2 * tmp ** 2)
b = (x - alpha) / (_VOL ** 2)
return a - b
a = alpha
if change ** 2 > phi ** 2 + v:
b = math.log(change ** 2 - phi ** 2 - v)
else:
k = 1
while f(alpha - k * _VOL) < 0:
k += 1
b = alpha - k * _VOL
fa = f(a)
fb = f(b)
# Larger _EPS used to speed iterations up slightly
while abs(b - a) > _EPS:
c = a + (a - b) * fa / (fb - fa)
fc = f(c)
if fc * fb < 0:
a = b
fa = fb
else:
fa /= 2
b = c
fb = fc
return math.e ** (a / 2)
def calculateGlicko(players, contest_weight=1.0, negative_damping=1.0):
N = len(players)
if N > _MAXSIZE:
multi = _MAXMULTI
else:
multi = _WIN - _MULTISLOPE * N
# compare every head to head matchup in a given compeition
for i in players:
mu = (i.rating - INITRAT) / _CONV
phi = i.confidence / _CONV
sigma = i.volatility
v_inv = 0
delta = 0
for j in players:
if i is not j:
oppMu = (j.rating - INITRAT) / _CONV
oppPhi = j.confidence / _CONV
if i.place > j.place:
S = _LOSS
elif i.place < j.place:
S = _WIN
else:
S = _CATCH
# Change the weight of the matchup based on opponent confidence
weighted = 1 / math.sqrt(1 + 3 * oppPhi ** 2 / math.pi ** 2)
# Change the weight of the matchup based on competition size
weighted = weighted * multi * contest_weight
expected_score = 1 / (1 + math.exp(-weighted * (mu - oppMu)))
v_inv += weighted ** 2 * expected_score * \
(1 - expected_score)
d = weighted * (S - expected_score)
if d < 0:
d *= negative_damping
delta += d
if v_inv != 0:
v = 1 / v_inv
change = v * delta
newSigma = findSigma(mu, phi, sigma, change, v)
phiAst = math.sqrt(phi ** 2 + newSigma ** 2)
# New confidence based on competitors volatility and v
newPhi = 1 / math.sqrt(1 / phiAst ** 2 + 1 / v)
newMu = mu + newPhi ** 2 * delta
i.rating = newMu * _CONV + INITRAT
i.confidence = newPhi * _CONV
i.volatility = newSigma