]> git.sesse.net Git - foosball/blob - foosrank.cpp
ba24d0254e69fc32a58f7deca9c08856dc5d6b39
[foosball] / foosrank.cpp
1 #include <stdio.h>
2 #include <math.h>
3 #include <assert.h>
4
5 #include <vector>
6 #include <algorithm>
7
8 // step sizes
9 static const double int_step_size = 50.0;
10 static const double pdf_step_size = 10.0;
11
12 // rating constant (see below)
13 static const double rating_constant = 455.0;
14
15 using namespace std;
16
17 double prob_score(int k, double a, double rd);
18 double prob_score_real(int k, double a, double prodai, double kfac, double rd_norm);
19 double prodai(int k, double a);
20 double fac(int x);
21
22 // Numerical integration using Simpson's rule
23 template<class T>
24 double simpson_integrate(const T &evaluator, double from, double to, double step)
25 {
26         int n = int((to - from) / step + 0.5);
27         double h = (to - from) / n;
28         double sum = evaluator(from);
29
30         for (int i = 1; i < n; i += 2) {
31                 sum += 4.0 * evaluator(from + i * h);
32         }
33         for (int i = 2; i < n; i += 2) {
34                 sum += 2.0 * evaluator(from + i * h);
35         }
36         sum += evaluator(to);
37
38         return (h/3.0) * sum;
39 }
40
41 // probability of match ending k-a (k>a) when winnerR - loserR = RD
42 //
43 //   +inf  
44 //     / 
45 //    |
46 //    | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
47 //    |
48 //   /
49 // -inf
50 //
51 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
52 //
53 // The constant of 455 is chosen carefully so to match with the
54 // Glicko/Bradley-Terry assumption that a player rated 400 points over
55 // his/her opponent will win with a probability of 10/11 =~ 0.90909. 
56 //
57 double prob_score(int k, double a, double rd)
58 {
59         return prob_score_real(k, a, prodai(k, a), fac(k-1), rd/rating_constant);
60 }
61
62 // Same, but takes in Product(a+i, i=1..k-1) and (k-1)! as an argument in
63 // addition to a. Faster if you already have that precomputed, and assumes rd
64 // is already divided by 455.
65 double prob_score_real(int k, double a, double prodai, double kfac, double rd_norm)
66 {
67         double nom = prodai * pow(2.0, rd_norm * a); 
68         double denom = kfac * pow(1.0 + pow(2.0, rd_norm), k+a);
69         return nom/denom;
70 }
71
72 // Calculates Product(a+i, i=1..k-1) (see above).
73 double prodai(int k, double a)
74 {
75         double prod = 1.0;
76         for (int i = 1; i < k; ++i)
77                 prod *= (a+i);
78         return prod;
79 }
80
81 double fac(int x)
82 {
83         double prod = 1.0;
84         for (int i = 2; i <= x; ++i)
85                 prod *= i;
86         return prod;
87 }
88
89 // 
90 // Computes the integral
91 //
92 //   +inf
93 //    /
94 //    |
95 //    | ProbScore[a] (r2-r1) Gaussian[mu2, sigma2] (dr2) dr2
96 //    |
97 //   /
98 // -inf
99 //
100 // For practical reasons, -inf and +inf are replaced by 0 and 3000, which
101 // is reasonable in the this context.
102 //
103 // The Gaussian is not normalized.
104 //
105 // Set the last parameter to 1.0 if player 1 won, or -1.0 if player 2 won.
106 // In the latter case, ProbScore will be given (r1-r2) instead of (r2-r1).
107 //
108 class ProbScoreEvaluator {
109 private:
110         int k;
111         double a;
112         double prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac;
113
114 public:
115         ProbScoreEvaluator(int k, double a, double prodai_precompute, double kfac_precompute, double r1, double mu2, double sigma2, double winfac)
116                 : k(k), a(a), prodai_precompute(prodai_precompute), kfac_precompute(kfac_precompute), r1(r1), mu2(mu2), sigma2(sigma2), winfac(winfac) {}
117         inline double operator() (double x) const
118         {
119                 double probscore = prob_score_real(k, a, prodai_precompute, kfac_precompute, (x - r1)*winfac);
120                 double z = (x - mu2)/sigma2;
121                 double gaussian = exp(-(z*z/2.0));
122                 return probscore * gaussian;
123         }
124 };
125
126 double opponent_rating_pdf(int k, double a, double r1, double mu2, double sigma2, double winfac)
127 {
128         double prodai_precompute = prodai(k, a);
129         double kfac_precompute = fac(k-1);
130         winfac /= rating_constant;
131
132         return simpson_integrate(ProbScoreEvaluator(k, a, prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac), 0.0, 3000.0, int_step_size);
133 }
134
135 // normalize the curve so we know that A ~= 1
136 void normalize(vector<pair<double, double> > &curve)
137 {
138         double peak = 0.0;
139         for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
140                 peak = max(peak, i->second);
141         }
142
143         double invpeak = 1.0 / peak;
144         for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
145                 i->second *= invpeak;
146         }
147 }
148
149 // computes matA * matB
150 void mat_mul(double *matA, unsigned ah, unsigned aw,
151              double *matB, unsigned bh, unsigned bw,
152              double *result)
153 {
154         assert(aw == bh);
155         for (unsigned y = 0; y < bw; ++y) {
156                 for (unsigned x = 0; x < ah; ++x) {
157                         double sum = 0.0;
158                         for (unsigned c = 0; c < aw; ++c) {
159                                 sum += matA[c*ah + x] * matB[y*bh + c];
160                         }
161                         result[y*bw + x] = sum;
162                 }
163         }
164 }
165                 
166 // computes matA^T * matB
167 void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
168                    double *matB, unsigned bh, unsigned bw,
169                    double *result)
170 {
171         assert(ah == bh);
172         for (unsigned y = 0; y < bw; ++y) {
173                 for (unsigned x = 0; x < aw; ++x) {
174                         double sum = 0.0;
175                         for (unsigned c = 0; c < ah; ++c) {
176                                 sum += matA[x*ah + c] * matB[y*bh + c];
177                         }
178                         result[y*bw + x] = sum;
179                 }
180         }
181 }
182
183 void print3x3(double *M)
184 {
185         printf("%f %f %f\n", M[0], M[3], M[6]);
186         printf("%f %f %f\n", M[1], M[4], M[7]);
187         printf("%f %f %f\n", M[2], M[5], M[8]);
188 }
189
190 void print3x1(double *M)
191 {
192         printf("%f\n", M[0]);
193         printf("%f\n", M[1]);
194         printf("%f\n", M[2]);
195 }
196
197 // solves Ax = B by Gauss-Jordan elimination, where A is a 3x3 matrix,
198 // x is a column vector of length 3 and B is a row vector of length 3.
199 // Destroys its input in the process.
200 void solve3x3(double *A, double *x, double *B)
201 {
202         // row 1 -= row 0 * (a1/a0)
203         {
204                 double f = A[1] / A[0];
205                 A[1] = 0.0;
206                 A[4] -= A[3] * f;
207                 A[7] -= A[6] * f;
208
209                 B[1] -= B[0] * f;
210         }
211
212         // row 2 -= row 0 * (a2/a0)
213         {
214                 double f = A[2] / A[0];
215                 A[2] = 0.0;
216                 A[5] -= A[3] * f;
217                 A[8] -= A[6] * f;
218
219                 B[2] -= B[0] * f;
220         }
221
222         // row 2 -= row 1 * (a5/a4)
223         {
224                 double f = A[5] / A[4];
225                 A[5] = 0.0;
226                 A[8] -= A[7] * f;
227                 
228                 B[2] -= B[1] * f;
229         }
230
231         // back substitute:
232
233         // row 1 -= row 2 * (a7/a8)
234         {
235                 double f = A[7] / A[8];
236                 A[7] = 0.0;
237
238                 B[1] -= B[2] * f;
239         }
240
241         // row 0 -= row 2 * (a6/a8)
242         {
243                 double f = A[6] / A[8];
244                 A[6] = 0.0;
245
246                 B[0] -= B[2] * f;
247         }
248
249         // row 0 -= row 1 * (a3/a4)
250         {
251                 double f = A[3] / A[4];
252                 A[3] = 0.0;
253
254                 B[0] -= B[1] * f;
255         }
256
257         // normalize
258         x[0] = B[0] / A[0];
259         x[1] = B[1] / A[4];
260         x[2] = B[2] / A[8];
261 }
262
263 // Give an OK starting estimate for the least squares, by numerical integration
264 // of statistical moments.
265 void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
266 {
267         double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
268
269         double area = curve.front().second;
270         double ex = curve.front().first * curve.front().second;
271         double ex2 = curve.front().first * curve.front().first * curve.front().second;
272
273         for (unsigned i = 1; i < curve.size() - 1; i += 2) {
274                 double x = curve[i].first;
275                 double y = curve[i].second;
276                 area += 4.0 * y;
277                 ex += 4.0 * x * y;
278                 ex2 += 4.0 * x * x * y;
279         }
280         for (unsigned i = 2; i < curve.size() - 1; i += 2) {
281                 double x = curve[i].first;
282                 double y = curve[i].second;
283                 area += 2.0 * y;
284                 ex += 2.0 * x * y;
285                 ex2 += 2.0 * x * x * y;
286         }
287         
288         area += curve.back().second;
289         ex += curve.back().first * curve.back().second;
290         ex2 += curve.back().first * curve.back().first * curve.back().second;
291
292         area = (h/3.0) * area;
293         ex = (h/3.0) * ex / area;
294         ex2 = (h/3.0) * ex2 / area;
295
296         mu_result = ex;
297         sigma_result = sqrt(ex2 - ex * ex);
298 }
299         
300 // Find best fit of the data in curves to a Gaussian pdf, based on the
301 // given initial estimates. Works by nonlinear least squares, iterating
302 // until we're below a certain threshold.
303 //
304 // Note that the algorithm blows up quite hard if the initial estimate is
305 // not good enough. Use estimate_musigma to get a reasonable starting
306 // estimate.
307 void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
308 {
309         double A = 1.0;
310         double mu = mu1;
311         double sigma = sigma1;
312
313         // column-major
314         double matA[curve.size() * 3];  // N x 3
315         double dbeta[curve.size()];     // N x 1
316
317         // A^T * A: 3xN * Nx3 = 3x3
318         double matATA[3*3];
319
320         // A^T * dβ: 3xN * Nx1 = 3x1
321         double matATdb[3];
322
323         double dlambda[3];
324
325         for ( ;; ) {
326                 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
327
328                 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
329                 for (unsigned i = 0; i < curve.size(); ++i) {
330                         double x = curve[i].first;
331
332                         // df/dA(x_i)
333                         matA[i + 0 * curve.size()] = 
334                                 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
335
336                         // df/dµ(x_i)
337                         matA[i + 1 * curve.size()] = 
338                                 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
339
340                         // df/dσ(x_i)
341                         matA[i + 2 * curve.size()] = 
342                                 matA[i + 1 * curve.size()] * (x-mu)/sigma;
343                 }
344
345                 // find dβ
346                 for (unsigned i = 0; i < curve.size(); ++i) {
347                         double x = curve[i].first;
348                         double y = curve[i].second;
349
350                         dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
351                 }
352
353                 // compute a and b
354                 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
355                 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
356
357                 // solve
358                 solve3x3(matATA, dlambda, matATdb);
359
360                 A += dlambda[0];
361                 mu += dlambda[1];
362                 sigma += dlambda[2];
363
364                 // terminate when we're down to three digits
365                 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
366                         break;
367         }
368
369         mu_result = mu;
370         sigma_result = sigma;
371 }
372
373 void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma)
374 {
375         vector<pair<double, double> > curve;
376
377         if (score1 > score2) {
378                 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
379                         double z = (r1 - mu1) / sigma1;
380                         double gaussian = exp(-(z*z/2.0));
381                         curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score1, score2, r1, mu2, sigma2, 1.0)));
382                 }
383         } else {
384                 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
385                         double z = (r1 - mu1) / sigma1;
386                         double gaussian = exp(-(z*z/2.0));
387                         curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score2, score1, r1, mu2, sigma2, -1.0)));
388                 }
389         }
390
391         double mu_est, sigma_est;
392         normalize(curve);
393         estimate_musigma(curve, mu_est, sigma_est);
394         least_squares(curve, mu_est, sigma_est, mu, sigma);
395 }
396
397 int main(int argc, char **argv)
398 {
399         double mu1 = atof(argv[1]);
400         double sigma1 = atof(argv[2]);
401         double mu2 = atof(argv[3]);
402         double sigma2 = atof(argv[4]);
403
404         if (argc > 6) {
405                 int score1 = atoi(argv[5]);
406                 int score2 = atoi(argv[6]);
407                 double mu, sigma;
408                 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
409                 printf("%f %f\n", mu, sigma);
410         } else {
411                 int k = atoi(argv[5]);
412
413                 // assess all possible scores
414                 for (int i = 0; i < k; ++i) {
415                         double newmu1, newmu2, newsigma1, newsigma2;
416                         compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
417                         compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
418                         printf("%u-%u,%f,%+f,%+f\n",
419                                 k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
420                 }
421                 for (int i = k; i --> 0; ) {
422                         double newmu1, newmu2, newsigma1, newsigma2;
423                         compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
424                         compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
425                         printf("%u-%u,%f,%+f,%+f\n",
426                                 i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);
427                 }
428         }
429 }
430