-
Notifications
You must be signed in to change notification settings - Fork 9
/
class.Glicko2Player.php
199 lines (152 loc) · 5.15 KB
/
class.Glicko2Player.php
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/*******************************************************************************
Corrected implementation of Glicko2
License: LGPLv3
Modified by Guangcong Luo on 2012 Sept 28
Modified from Noah Smith's implementation, which contains a number of mathematical
errors.
His original documentation is reproduced below.
*******************************************************************************
glicko-2 ranking system
Written by Noah Smith 2011, June 7
megiddo ( @t ) thirdform ( dot ) com
Based on http://www.glicko.net/glicko/glicko2.doc/example.html
Usage
Glicko2Player([$rating = 1500 [, $rd = 350 [, $volatility = 0.06 [, $mu [, $phi [, $sigma [, $systemconstant = 0.75 ]]]]]]]
For new players, use the default values for rating, rd, and volatility.
The systemconstant should be between 0.3 and 1.2, depending on system itself (this is game dependent, and must be set
by estimation or experimentation)
Updating a Glicko2Player
Add wins, losses, and draws to a player:
$Alice = new Glicko2Player();
$Bob = new Glicko2Player();
$Charlie = new Glicko2Player();
$David = new Glicko2Player();
$Alice->AddWin($Bob);
$Alice->AddWin($Charlie)
$Bob->AddLoss($Alice);
$Bob->AddWin($Charlie);
$Charlie->AddLoss($Alice);
$Charlie->AddLoss($Bob);
$Alice->Update();
$Bob->Update();
$Charlie->Update();
$David->Update(); // David did not participate, but must be updated
This message and the following may not be removed or modified:
Caveat Emptor
I make no assertions that either Glicko-2 or this code are correct. Use at your own risk.
*******************************************************************************/
class Glicko2Player {
public $rating;
public $rd;
public $sigma;
public $mu;
public $phi;
public $tau;
private $pi2 = 9.8696044;
var $M = array();
function __construct($rating = 1500, $rd = 350, $volatility = 0.06, $mu = null, $phi = null, $sigma = null, $systemconstant = 0.75) {
// Step 1
$this->rating = $rating;
$this->rd = $rd;
// volatility
if (is_null($sigma)) {
$this->sigma = $volatility;
} else {
$this->sigma = $sigma;
}
// System Constant
$this->tau = $systemconstant;
// Step 2
// Rating
if (is_null($mu)) {
$this->mu = ( $this->rating - 1500 ) / 173.7178;
} else {
$this->mu = $mu;
}
// Rating Deviation
if (is_null($phi)) {
$this->phi = $this->rd / 173.7178;
} else {
$this->phi = $phi;
}
}
function AddWin($OtherPlayer) {
$this->M[] = $OtherPlayer->MatchElement(1);
}
function AddLoss($OtherPlayer) {
$this->M[] = $OtherPlayer->MatchElement(0);
}
function AddDraw($OtherPlayer) {
$this->M[] = $OtherPlayer->MatchElement(0.5);
}
function Update() {
$Results = $this->AddMatches($this->M);
$this->rating = $Results['r'];
$this->rd = $Results['RD'];
$this->mu = $Results['mu'];
$this->phi = $Results['phi'];
$this->sigma = $Results['sigma'];
$this->M = array();
}
function MatchElement($score) {
return array( 'mu' => $this->mu, 'phi' => $this->phi, 'score' => $score );
}
function AddMatches($M) {
// This is where the Glicko2 rating calculation actually happens
// Follow along the steps using: http://www.glicko.net/glicko/glicko2.pdf
if (count($M) == 0) {
$phi_p = sqrt( ( $this->phi * $this->phi ) + ( $this->sigma * $this->sigma ) );
return array( 'r' => $this->rating, 'RD' => 173.7178 * $phi_p, 'mu' => $this->mu, 'phi' => $phi_p, 'sigma' => $this->sigma ) ;
}
// summation parts of Step 3 & 4 & 7
$v_sum = 0;
$delta_sum = 0;
$mu_p_sum = 0;
for ($j = 0; $j < count($M); $j++) {
$E = $this->E( $this->mu, $M[$j]['mu'], $M[$j]['phi'] );
$g = $this->g( $M[$j]['phi'] );
$v_sum += ( $g * $g * $E * ( 1 - $E ) );
$delta_sum += $g * ( $M[$j]['score'] - $E );
$mu_p_sum += $g * ( $M[$j]['score'] - $E );
}
// Step 3
// Estimated variance
$v = 1.0 / $v_sum;
// Step 4
// Estimated improvment in rating
$delta = $v * $delta_sum;
// Step 5
$a = log( $this->sigma * $this->sigma );
$x_prev = $a;
$x = $x_prev;
$tausq = $this->tau * $this->tau;
$phisq = $this->phi * $this->phi;
$deltasq = $delta * $delta;
do {
$exp_xp = exp( $x_prev );
$d = $this->phi * $this->phi + $v + $exp_xp;
$deltadsq = $deltasq / ($d * $d);
$h1 = -( $x_prev - $a ) / ( $tausq ) - ( 0.5 * $exp_xp / $d ) + ( 0.5 * $exp_xp * $deltadsq );
$h2 = ( -1.0 / $tausq ) - ( ( 0.5 * $exp_xp ) * ( $phisq + $v ) / ( $d * $d ) ) + ( 0.5 * $deltasq * $exp_xp * ( $phisq + $v - $exp_xp ) / ( $d * $d * $d ) );
$tmp_x = $x;
$x = $x_prev - ( $h1 / $h2 );
$x_prev = $tmp_x;
} while (abs($x - $x_prev) > 0.1);
$sigma_p = exp( $x / 2 );
// Step 6
$phi_star = sqrt( $phisq + ( $sigma_p * $sigma_p ) );
// Step 7
$phi_p = 1.0 / ( sqrt( ( 1.0 / ( $phi_star * $phi_star ) ) + ( 1.0 / $v ) ) );
// New mu
$mu_p = $this->mu + $phi_p * $phi_p * $mu_p_sum;
return array( 'r' => ( 173.7178 * $mu_p ) + 1500, 'RD' => 173.7178 * $phi_p, 'mu' => $mu_p, 'phi' => $phi_p, 'sigma' => $sigma_p );
}
function g($phi) {
return 1.0 / ( sqrt( 1.0 + ( 3.0 * $phi * $phi) / ( $this->pi2 ) ) );
}
function E($mu, $mu_j, $phi_j) {
return 1.0 / ( 1.0 + exp( -$this->g($phi_j) * ( $mu - $mu_j ) ) );
}
}