]> git.sesse.net Git - foosball/blob - foosrank.cpp
d295c212ed04187a5ba42995a2d2a5852bf65150
[foosball] / foosrank.cpp
1 #include <cstdio>
2 #include <cmath>
3 #include <cassert>
4
5 #include <vector>
6 #include <algorithm>
7
8 #include <complex>
9 #include <fftw3.h>
10
11 #define USE_LOGISTIC_DISTRIBUTION 0
12
13 // step sizes
14 static const double int_step_size = 75.0;
15
16 // rating constant (see below)
17 static const double rating_constant = 455.0;
18
19 #if USE_LOGISTIC_DISTRIBUTION
20 // constant used in the logistic pdf
21 static const double l_const = M_PI / (2.0 * sqrt(3.0));
22 #endif
23
24 using namespace std;
25
26 static double prob_score(int k, int a, double rd);
27 static double prob_score_real(int k, int a, double binomial, double rd_norm);
28 static double prodai(int k, int a);
29 static double fac(int x);
30
31 #if USE_LOGISTIC_DISTRIBUTION
32 // sech²(x)
33 static double sech2(double x)
34 {
35         double e = exp(2.0 * x);
36         return 4.0 * e / ((e+1.0) * (e+1.0));
37 }
38 #endif
39
40 // probability of match ending k-a (k>a) when winnerR - loserR = RD
41 //
42 //   +inf  
43 //     / 
44 //    |
45 //    | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
46 //    |
47 //   /
48 // -inf
49 //
50 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
51 //
52 // The constant of 455 is chosen carefully so to match with the
53 // Glicko/Bradley-Terry assumption that a player rated 400 points over
54 // his/her opponent will win with a probability of 10/11 =~ 0.90909. 
55 //
56 static double prob_score(int k, int a, double rd)
57 {
58         return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant);
59 }
60
61 // computes x^a, probably more efficiently than pow(x, a) (but requires that a
62 // is n unsigned integer)
63 static double intpow(double x, unsigned a)
64 {
65         double result = 1.0;
66
67         while (a > 0) {
68                 if (a & 1) {
69                         result *= x;
70                 }
71                 a >>= 1;
72                 x *= x;
73         }
74
75         return result;
76 }
77
78 // Same, but takes in binomial(a+k-1, k-1) as an argument in
79 // addition to a. Faster if you already have that precomputed, and assumes rd
80 // is already divided by 455.
81 static double prob_score_real(int k, int a, double binomial, double rd_norm)
82 {
83         double nom = binomial * intpow(pow(2.0, rd_norm), a); 
84         double denom = intpow(1.0 + pow(2.0, rd_norm), k+a);
85         return nom/denom;
86 }
87
88 // Calculates Product(a+i, i=1..k-1) (see above).
89 static double prodai(int k, int a)
90 {
91         double prod = 1.0;
92         for (int i = 1; i < k; ++i)
93                 prod *= (a+i);
94         return prod;
95 }
96
97 static double fac(int x)
98 {
99         double prod = 1.0;
100         for (int i = 2; i <= x; ++i)
101                 prod *= i;
102         return prod;
103 }
104
105 static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector<pair<double, double> > &result)
106 {
107         double binomial_precompute = prodai(k, a) / fac(k-1);
108         winfac /= rating_constant;
109
110         int sz = (6000.0 - 0.0) / int_step_size;
111         double h = (6000.0 - 0.0) / sz;
112
113         fftw_plan f1, f2, b;
114         complex<double> *func1, *func2, *res;
115
116         func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
117         func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
118         res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
119         f1 = fftw_plan_dft_1d(sz*2,
120                 reinterpret_cast<fftw_complex*>(func1),
121                 reinterpret_cast<fftw_complex*>(func1),
122                 FFTW_FORWARD,
123                 FFTW_MEASURE);
124         f2 = fftw_plan_dft_1d(sz*2,
125                 reinterpret_cast<fftw_complex*>(func2),
126                 reinterpret_cast<fftw_complex*>(func2),
127                 FFTW_FORWARD,
128                 FFTW_MEASURE);
129         b = fftw_plan_dft_1d(sz*2,
130                 reinterpret_cast<fftw_complex*>(res),
131                 reinterpret_cast<fftw_complex*>(res),
132                 FFTW_BACKWARD,
133                 FFTW_MEASURE);
134         
135         // start off by zero
136         for (int i = 0; i < sz*2; ++i) {
137                 func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0;
138         }
139
140 #if USE_LOGISTIC_DISTRIBUTION
141         double invsigma2 = 1.0 / sigma2;
142 #else
143         double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
144 #endif
145         for (int i = 0; i < sz; ++i) {
146                 double x1 = 0.0 + h*i;
147
148                 // opponent's pdf
149 #if USE_LOGISTIC_DISTRIBUTION
150                 double z = (x1 - mu2) * invsigma2;
151                 double ch = cosh(l_const * z);
152                 func1[i].real() = 1.0 / (ch * ch);
153 #else
154                 double z = (x1 - mu2) * invsq2sigma2;
155                 func1[i].real() = exp(-z*z);
156 #endif
157
158                 double x2 = -3000.0 + h*i;
159                 func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
160         }
161
162         result.reserve(sz*2);
163
164         // convolve
165         fftw_execute(f1);
166         fftw_execute(f2);
167         for (int i = 0; i < sz*2; ++i) {
168                 res[i] = func1[i] * func2[i];
169         }
170         fftw_execute(b);
171
172         result.reserve(sz);
173         for (int i = 0; i < sz; ++i) {
174                 double r1 = i*h;
175                 result.push_back(make_pair(r1, abs(res[i])));
176         }
177 }
178
179 // normalize the curve so we know that A ~= 1
180 static void normalize(vector<pair<double, double> > &curve)
181 {
182         double peak = 0.0;
183         for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
184                 peak = max(peak, i->second);
185         }
186
187         double invpeak = 1.0 / peak;
188         for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
189                 i->second *= invpeak;
190         }
191 }
192
193 // computes matA^T * matB
194 static void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
195                           double *matB, unsigned bh, unsigned bw,
196                           double *result)
197 {
198         assert(ah == bh);
199         for (unsigned y = 0; y < bw; ++y) {
200                 for (unsigned x = 0; x < aw; ++x) {
201                         double sum = 0.0;
202                         for (unsigned c = 0; c < ah; ++c) {
203                                 sum += matA[x*ah + c] * matB[y*bh + c];
204                         }
205                         result[y*bw + x] = sum;
206                 }
207         }
208 }
209
210 // solves Ax = B by Gauss-Jordan elimination, where A is an NxN matrix,
211 // x is a column vector of length N and B is a row vector of length N.
212 // Destroys its input in the process.
213 template<int N>
214 static void solve_matrix(double *A, double *x, double *B)
215 {
216         for (int i = 0; i < N; ++i) {
217                 for (int j = i+1; j < N; ++j) {
218                         // row j -= row i * (a[i,j] / a[i,i])
219                         double f = A[j+i*N] / A[i+i*N];
220
221                         A[j+i*N] = 0.0;
222                         for (int k = i+1; k < N; ++k) {
223                                 A[j+k*N] -= A[i+k*N] * f;
224                         }
225
226                         B[j] -= B[i] * f;
227                 }
228         }
229
230         // back-substitute
231         for (int i = N; i --> 0; ) {
232                 for (int j = i; j --> 0; ) {
233                         // row j -= row i * (a[j,j] / a[j,i])
234                         double f = A[i+j*N] / A[j+j*N];
235                         
236                         // A[j+i*N] = 0.0;
237                         B[j] -= B[i] * f;
238                 }
239         }
240
241         // normalize
242         for (int i = 0; i < N; ++i) {
243                 x[i] = B[i] / A[i+i*N];
244         }
245 }
246
247 // Give an OK starting estimate for the least squares, by numerical integration
248 // of statistical moments.
249 static void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
250 {
251         double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
252
253         double area = curve.front().second;
254         double ex = curve.front().first * curve.front().second;
255         double ex2 = curve.front().first * curve.front().first * curve.front().second;
256
257         for (unsigned i = 1; i < curve.size() - 1; i += 2) {
258                 double x = curve[i].first;
259                 double y = curve[i].second;
260                 area += 4.0 * y;
261                 ex += 4.0 * x * y;
262                 ex2 += 4.0 * x * x * y;
263         }
264         for (unsigned i = 2; i < curve.size() - 1; i += 2) {
265                 double x = curve[i].first;
266                 double y = curve[i].second;
267                 area += 2.0 * y;
268                 ex += 2.0 * x * y;
269                 ex2 += 2.0 * x * x * y;
270         }
271         
272         area += curve.back().second;
273         ex += curve.back().first * curve.back().second;
274         ex2 += curve.back().first * curve.back().first * curve.back().second;
275
276         area = (h/3.0) * area;
277         ex = (h/3.0) * ex / area;
278         ex2 = (h/3.0) * ex2 / area;
279
280         mu_result = ex;
281         sigma_result = sqrt(ex2 - ex * ex);
282 }
283         
284 // Find best fit of the data in curves to a Gaussian pdf, based on the
285 // given initial estimates. Works by nonlinear least squares, iterating
286 // until we're below a certain threshold.
287 //
288 // Note that the algorithm blows up quite hard if the initial estimate is
289 // not good enough. Use estimate_musigma to get a reasonable starting
290 // estimate.
291 static void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
292 {
293         double A = 1.0;
294         double mu = mu1;
295         double sigma = sigma1;
296
297         // column-major
298         double matA[curve.size() * 3];  // N x 3
299         double dbeta[curve.size()];     // N x 1
300
301         // A^T * A: 3xN * Nx3 = 3x3
302         double matATA[3*3];
303
304         // A^T * dβ: 3xN * Nx1 = 3x1
305         double matATdb[3];
306
307         double dlambda[3];
308
309         for ( ;; ) {
310                 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
311
312                 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
313                 for (unsigned i = 0; i < curve.size(); ++i) {
314                         double x = curve[i].first;
315
316 #if USE_LOGISTIC_DISTRIBUTION
317                         // df/dA(x_i)
318                         matA[i + 0 * curve.size()] = sech2(l_const * (x-mu)/sigma);
319
320                         // df/dµ(x_i)
321                         matA[i + 1 * curve.size()] = 2.0 * l_const * A * matA[i + 0 * curve.size()]
322                                 * tanh(l_const * (x-mu)/sigma) / sigma;
323
324                         // df/dσ(x_i)
325                         matA[i + 2 * curve.size()] = 
326                                 matA[i + 1 * curve.size()] * (x-mu)/sigma;
327 #else
328                         // df/dA(x_i)
329                         matA[i + 0 * curve.size()] = 
330                                 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
331
332                         // df/dµ(x_i)
333                         matA[i + 1 * curve.size()] =
334                                 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
335
336                         // df/dσ(x_i)
337                         matA[i + 2 * curve.size()] = 
338                                 matA[i + 1 * curve.size()] * (x-mu)/sigma;
339 #endif
340                 }
341
342                 // find dβ
343                 for (unsigned i = 0; i < curve.size(); ++i) {
344                         double x = curve[i].first;
345                         double y = curve[i].second;
346
347 #if USE_LOGISTIC_DISTRIBUTION
348                         dbeta[i] = y - A * sech2(l_const * (x-mu)/sigma);
349 #else
350                         dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
351 #endif
352                 }
353
354                 // compute a and b
355                 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
356                 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
357
358                 // solve
359                 solve_matrix<3>(matATA, dlambda, matATdb);
360
361                 A += dlambda[0];
362                 mu += dlambda[1];
363                 sigma += dlambda[2];
364
365                 // terminate when we're down to three digits
366                 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
367                         break;
368         }
369
370         mu_result = mu;
371         sigma_result = sigma;
372 }
373
374 static void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma)
375 {
376         vector<pair<double, double> > curve;
377
378         if (score1 > score2) {
379                 compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, curve);
380         } else {
381                 compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, curve);
382         }
383
384         // multiply in the gaussian
385         for (unsigned i = 0; i < curve.size(); ++i) {
386                 double r1 = curve[i].first;
387
388                 // my pdf
389                 double z = (r1 - mu1) / sigma1;
390 #if USE_LOGISTIC_DISTRIBUTION
391                 double ch = cosh(l_const * z);
392                 curve[i].second /= (ch * ch);
393 #else
394                 double gaussian = exp(-(z*z/2.0));
395                 curve[i].second *= gaussian;
396 #endif
397         }
398
399         double mu_est, sigma_est;
400         normalize(curve);
401         estimate_musigma(curve, mu_est, sigma_est);
402         least_squares(curve, mu_est, sigma_est, mu, sigma);
403 }
404
405 static void compute_new_double_rating(double mu1, double sigma1, double mu2, double sigma2, double mu3, double sigma3, double mu4, double sigma4, int score1, int score2, double &mu, double &sigma)
406 {
407         vector<pair<double, double> > curve, newcurve;
408         double mu_t = mu3 + mu4;
409         double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
410                         
411         if (score1 > score2) {
412                 compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, curve);
413         } else {
414                 compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, curve);
415         }
416
417         newcurve.reserve(curve.size());
418
419         // iterate over r1
420         double h = 3000.0 / curve.size();
421         for (unsigned i = 0; i < curve.size(); ++i) {
422                 double sum = 0.0;
423
424                 // could be anything, but this is a nice start
425                 //double r1 = curve[i].first;
426                 double r1 = i * h;
427
428                 // iterate over r2
429 #if USE_LOGISTIC_DISTRIBUTION
430                 double invsigma2 = 1.0 / sigma2;
431 #else
432                 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
433 #endif
434                 for (unsigned j = 0; j < curve.size(); ++j) {
435                         double r1plusr2 = curve[j].first;
436                         double r2 = r1plusr2 - r1;
437
438 #if USE_LOGISTIC_DISTRIBUTION
439                         double z = (r2 - mu2) * invsigma2;
440                         double gaussian = sech2(l_const * z);
441 #else   
442                         double z = (r2 - mu2) * invsq2sigma2;
443                         double gaussian = exp(-z*z);
444 #endif
445                         sum += curve[j].second * gaussian;
446                 }
447
448 #if USE_LOGISTIC_DISTRIBUTION
449                 double z = (r1 - mu1) / sigma1;
450                 double gaussian = sech2(l_const * z);
451 #else
452                 double z = (r1 - mu1) / sigma1;
453                 double gaussian = exp(-(z*z/2.0));
454 #endif
455                 newcurve.push_back(make_pair(r1, gaussian * sum));
456         }
457
458
459         double mu_est, sigma_est;
460         normalize(newcurve);
461         estimate_musigma(newcurve, mu_est, sigma_est);
462         least_squares(newcurve, mu_est, sigma_est, mu, sigma);
463 }
464
465 int main(int argc, char **argv)
466 {
467         FILE *fp = fopen("fftw-wisdom", "rb");
468         if (fp != NULL) {
469                 fftw_import_wisdom_from_file(fp);
470                 fclose(fp);
471         }
472
473         double mu1 = atof(argv[1]);
474         double sigma1 = atof(argv[2]);
475         double mu2 = atof(argv[3]);
476         double sigma2 = atof(argv[4]);
477
478         if (argc > 10) {
479                 double mu3 = atof(argv[5]);
480                 double sigma3 = atof(argv[6]);
481                 double mu4 = atof(argv[7]);
482                 double sigma4 = atof(argv[8]);
483                 int score1 = atoi(argv[9]);
484                 int score2 = atoi(argv[10]);
485                 double mu, sigma;
486                 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, mu, sigma);
487                 if (score1 > score2) {
488                         printf("%f %f %f\n", mu, sigma, prob_score(score1, score2, mu3+mu4-(mu1+mu2)));
489                 } else {
490                         printf("%f %f %f\n", mu, sigma, prob_score(score2, score1, mu1+mu2-(mu3+mu4)));
491                 }
492         } else if (argc > 8) {
493                 double mu3 = atof(argv[5]);
494                 double sigma3 = atof(argv[6]);
495                 double mu4 = atof(argv[7]);
496                 double sigma4 = atof(argv[8]);
497                 int k = atoi(argv[9]);
498                 
499                 // assess all possible scores
500                 for (int i = 0; i < k; ++i) {
501                         double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
502                         double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
503                         compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, newmu1_1, newsigma1_1);
504                         compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, newmu1_2, newsigma1_2);
505                         compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, newmu2_1, newsigma2_1);
506                         compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, newmu2_2, newsigma2_2);
507                         printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
508                                 k, i, prob_score(k, i, mu3+mu4-(mu1+mu2)), newmu1_1-mu1, newmu1_2-mu2,
509                                 newmu2_1-mu3, newmu2_2-mu4);
510                 }
511                 for (int i = k; i --> 0; ) {
512                         double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
513                         double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
514                         compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, newmu1_1, newsigma1_1);
515                         compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, newmu1_2, newsigma1_2);
516                         compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, newmu2_1, newsigma2_1);
517                         compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, newmu2_2, newsigma2_2);
518                         printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
519                                 i, k, prob_score(k, i, mu1+mu2-(mu3+mu4)), newmu1_1-mu1, newmu1_2-mu2,
520                                 newmu2_1-mu3, newmu2_2-mu4);
521                 }
522         } else if (argc > 6) {
523                 int score1 = atoi(argv[5]);
524                 int score2 = atoi(argv[6]);
525                 double mu, sigma;
526                 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
527
528                 if (score1 > score2) {
529                         printf("%f %f %f\n", mu, sigma, prob_score(score1, score2, mu2-mu1));
530                 } else {
531                         printf("%f %f %f\n", mu, sigma, prob_score(score2, score1, mu1-mu2));
532                 }
533         } else {
534                 int k = atoi(argv[5]);
535
536                 // assess all possible scores
537                 for (int i = 0; i < k; ++i) {
538                         double newmu1, newmu2, newsigma1, newsigma2;
539                         compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
540                         compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
541                         printf("%u-%u,%f,%+f,%+f\n",
542                                 k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
543                 }
544                 for (int i = k; i --> 0; ) {
545                         double newmu1, newmu2, newsigma1, newsigma2;
546                         compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
547                         compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
548                         printf("%u-%u,%f,%+f,%+f\n",
549                                 i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);
550                 }
551         }
552         
553         fp = fopen("fftw-wisdom", "wb");
554         if (fp != NULL) {
555                 fftw_export_wisdom_to_file(fp);
556                 fclose(fp);
557         }
558 }
559