-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathglicko2.js
370 lines (333 loc) · 11.9 KB
/
glicko2.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const { Glicko2 } = require('../glicko2');
// Generate normally distributed random numbers
// cf. https://mika-s.github.io/javascript/random/normal-distributed/2019/05/15/generating-normally-distributed-random-numbers-in-javascript.html
function boxMullerTransform() {
const u1 = Math.random();
const u2 = Math.random();
const z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2);
const z1 = Math.sqrt(-2.0 * Math.log(u1)) * Math.sin(2.0 * Math.PI * u2);
return { z0, z1 };
}
function getNormallyDistributedRandomNumber(mean, stddev) {
const { z0, _ } = boxMullerTransform();
return z0 * stddev + mean;
}
describe('Glicko2', function() {
describe('makePlayer()', function() {
it('should make a default player when passed no settings', function() {
// const glicko = new Glicko2();
const glicko = new Glicko2();
const player = glicko.makePlayer();
player.getRating().should.equal(1500);
player.getRd().should.equal(350);
player.getVol().should.equal(0.06);
});
it('should support setting individual settings', function() {
const glicko = new Glicko2({
rating: 1600
});
const player = glicko.makePlayer();
player.getRating().should.equal(1600);
player.getRd().should.equal(350);
player.getVol().should.equal(0.06);
});
it('should not be affected by newer instances of Glicko2', function() {
const glicko = new Glicko2({
rating: 1400
});
const player = glicko.makePlayer();
const newerGlicko = new Glicko2({
rating: 1600
});
player.getRating().should.equal(1400);
});
});
describe('getPlayers()', function() {
it('should retrieve all players with ids', function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
// const glicko = new Glicko2(settings);
const player = glicko.makePlayer();
const pl1 = glicko.makePlayer(1400, 30, 0.06);
const pl3 = glicko.makePlayer(1550, 100, 0.06);
const players = glicko.getPlayers();
players.length.should.equal(3);
players[1].id.should.equal(1);
players[1].getRating().should.equal(1400);
});
});
describe('updateRatings()', function() {
it('should calculate new ratings', function() {
// Following the example at:
// http://math.bu.edu/people/mg/glicko/glicko2.doc/example.html
// Pretend Ryan (of rating 1500 and rating deviation 350) plays players of ratings 1400, 1550 and 1700
// and rating deviations 30, 100 and 300 respectively with outcomes 1, 0 and 0.
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
const Ryan = glicko.makePlayer();
const Bob = glicko.makePlayer(1400, 30, 0.06);
const John = glicko.makePlayer(1550, 100, 0.06);
const Mary = glicko.makePlayer(1700, 300, 0.06);
const matches = [];
matches.push([Ryan, Bob, 1]); //Ryan won over Bob
matches.push([Ryan, John, 0]); //Ryan lost against John
matches.push([Ryan, Mary, 0]); //Ryan lost against Mary
/*Perfs testing
const players = [Bob, John, Mary];
const ind = 0;
while (ind++ < 50){
players.push(glicko.makePlayer());
}
const nbpl = players.length;
const pl1, pl2;
for (const i=0; i<1000;i++){
pl1 = players[Math.floor(Math.random() * nbpl)];
pl2 = players[Math.floor(Math.random() * nbpl)];
matches.push([pl1, pl2, Math.floor(Math.random() * 3) / 2]);
}
//End perfs
*/
glicko.updateRatings(matches);
(Math.abs(Ryan.getRating() - 1464.06) < 0.01).should.equal(true);
(Math.abs(Ryan.getRd() - 151.52) < 0.01).should.equal(true);
(Math.abs(Ryan.getVol() - 0.05999) < 0.00001).should.equal(true);
});
it('should allow to be called multiple times', function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
const Ryan = glicko.makePlayer();
const Bob = glicko.makePlayer(1400, 30, 0.06);
const John = glicko.makePlayer(1550, 100, 0.06);
const Mary = glicko.makePlayer(1700, 300, 0.06);
let matches = [];
matches.push([Ryan, Bob, 1]); //Ryan won over Bob
matches.push([Ryan, John, 0]); //Ryan lost against John
matches.push([Ryan, Mary, 0]); //Ryan lost against Mary
glicko.updateRatings(matches);
//We initiate a new ranking instance with the actual values of the first one
const glicko_new = new Glicko2(settings);
const Ryan_new = glicko_new.makePlayer(Ryan.getRating(), Ryan.getRd(), Ryan.getVol());
const Bob_new = glicko_new.makePlayer(Bob.getRating(), Bob.getRd(), Bob.getVol());
const John_new = glicko_new.makePlayer(John.getRating(), John.getRd(), John.getVol());
const Mary_new = glicko_new.makePlayer(Mary.getRating(), Mary.getRd(), Mary.getVol());
//Second tournament for the first ranking instance
matches = [];
matches.push([Ryan, Bob, 0]);
matches.push([Ryan, John, 1]);
matches.push([Mary, Bob, 1]);
glicko.updateRatings(matches);
//console.log('nb players: ' + glicko.getPlayers().length);
//Fist tournament for the second ranking instance, with the same matches
const matches_new = [];
matches_new.push([Ryan_new, Bob_new, 0]);
matches_new.push([Ryan_new, John_new, 1]);
matches_new.push([Mary_new, Bob_new, 1]);
glicko_new.updateRatings(matches_new);
//The ratings in both systems should be the same
(Math.abs(Ryan.getRating() - Ryan_new.getRating()) < 0.1).should.equal(true);
(Math.abs(Ryan.getRd() - Ryan_new.getRd()) < 0.1).should.equal(true);
(Math.abs(Ryan.getVol() - Ryan_new.getVol()) < 0.00001).should.equal(true);
});
it('should be able to update ratings when a player did not play', function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
const Ryan = glicko.makePlayer();
const matches = [];
glicko.updateRatings(matches);
});
it('should accept Race objects', function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
const Ryan = glicko.makePlayer();
const Bob = glicko.makePlayer(1400, 30, 0.06);
const John = glicko.makePlayer(1550, 100, 0.06);
const Mary = glicko.makePlayer(1700, 300, 0.06);
const race = glicko.makeRace(
[
[Ryan], //Ryan won the race
[Bob, John], //Bob and John 2nd position ex-aequo
[Mary] // Mary 4th position
]
);
glicko.updateRatings(race);
// console.log(Ryan.getRating(), Ryan.getRd(), Ryan.getVol());
//v1
(Math.abs(Ryan.getRating() - 1685.7) < 0.1).should.equal(true);
(Math.abs(Ryan.getRd() - 151.52) < 0.01).should.equal(true);
(Math.abs(Ryan.getVol() - 0.06000) < 0.00001).should.equal(true);
//v2
// (Math.abs(Ryan.getRating() - 1563.6) < 0.1).should.equal(true);
// (Math.abs(Ryan.getRd() - 175.40) < 0.01).should.equal(true);
// (Math.abs(Ryan.getVol() - 0.06) < 0.00001).should.equal(true);
});
});
describe('predict()', function() {
it('should calculate expected outcome', function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
const p1 = glicko.makePlayer(1500, 100, 0.06);
const p2 = glicko.makePlayer(1300, 100, 0.06);
const expected = p1.predict(p2);
(Math.abs(expected - 0.74) < 0.001).should.equal(true);
const gexpected = glicko.predict(p1, p2);
gexpected.should.equal(expected);
});
});
});
describe("Race", function() {
describe("getMatches", function() {
it("Should convert a race to a list of matches", function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const glicko = new Glicko2(settings);
const Ryan = glicko.makePlayer();
const Bob = glicko.makePlayer(1400, 30, 0.06);
const John = glicko.makePlayer(1550, 100, 0.06);
const Mary = glicko.makePlayer(1700, 300, 0.06);
const race = glicko.makeRace(
[
[Ryan], //Ryan won the race
[Bob, John], //Bob and John 2nd position ex-aequo
[Mary] // Mary 4th position
]
);
const matches = race.getMatches();
//v1
matches.should.eql([
[Ryan, Bob, 1],
[Ryan, John, 1],
[Ryan, Mary, 1],
[Bob, John, 0.5],
[Bob, Mary, 1],
[John, Mary, 1]
]);
//v2
// matches.should.eql([
// [Ryan, Bob, 1],
// [Bob, John, 0.5],
// [John, Mary, 1]
// ]);
})
});
function calculatePrecision(scoreRatings) {
let predictionOk = 0;
let predictionCount = 0;
while (scoreRating = scoreRatings.pop()) {
scoreRatings.forEach(adversary => {
predictedWin = scoreRating.rating > adversary.rating;
realWin = scoreRating.score > adversary.score;
if (predictedWin == realWin) {
predictionOk += 1;
}
predictionCount += 1;
});
}
return predictionOk / predictionCount;
}
function makeRound(ranking, players, realRatings) {
let playerScores = realRatings.map(function(real, i) {
let score = Math.ceil(getNormallyDistributedRandomNumber(real, 150));
return {
real,
score,
player: players[i]
};
}).sort((a, b) => b.score - a.score);
let scoreRatings = playerScores.map(pscore => ({
score: pscore.score,
rating: pscore.player.getRating(),
}));
let idealRatings = playerScores.map(pscore => ({
score: pscore.score,
rating: pscore.real,
}));
let realPrecision = calculatePrecision(idealRatings);
let rankingPrecision = calculatePrecision(scoreRatings);
let raceResults = [];
let current = [];
playerScores.forEach(function(pscore) {
if (current.length && current[0].score == pscore.score) {
current.push(pscore);
} else {
raceResults.push(current.map((currScore) => currScore.player));
current = [pscore];
}
});
raceResults.push(current.map((currScore) => currScore.player));
const race = ranking.makeRace(raceResults);
ranking.updateRatings(race);
return {
realPrecision,
rankingPrecision
};
}
// XXX : trying to replicate http://www.tckerrigan.com/Misc/Multiplayer_Elo/ procedure to test accuracy of the algorithm
describe("evaluateAlgorithm", function() {
it("Should approximate reality", function() {
const settings = {
tau: 0.5,
rpd: 604800,
rating: 1500,
rd: 200,
vol: 0.06
};
const ranking = new Glicko2(settings);
let nbPlayers = 10;
const players = [];
const realRatings = [];
let diff = 1; // add or substract 1 alternatively
for (let idx = 0; idx < nbPlayers; idx++) {
realRatings.push(1100 + 100 * idx);
players.push(ranking.makePlayer(1500 + diff, 200, 0.06));
diff = 0 - diff;
}
let precisions = [];
for (let numRound = 1; numRound < 50; numRound++) {
precisions = makeRound(ranking, players, realRatings);
// console.log(precisions.realPrecision, precisions.rankingPrecision);
}
// (Math.abs(precisions.realPrecision - precisions.rankingPrecision) < 0.01).should.equal(true);
});
})
})