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