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