12 static const double int_step_size = 75.0;
14 // rating constant (see below)
15 static const double rating_constant = 455.0;
19 double prob_score(int k, double a, double rd);
20 double prob_score_real(int k, double a, double binomial, double rd_norm);
21 double prodai(int k, double a);
25 // probability of match ending k-a (k>a) when winnerR - loserR = RD
30 // | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
35 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
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.
41 double prob_score(int k, double a, double rd)
43 return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant);
46 // Same, but takes in binomial(a+k-1, k-1) as an argument in
47 // addition to a. Faster if you already have that precomputed, and assumes rd
48 // is already divided by 455.
49 double prob_score_real(int k, double a, double binomial, double rd_norm)
51 double nom = binomial * pow(2.0, rd_norm * a);
52 double denom = pow(1.0 + pow(2.0, rd_norm), k+a);
56 // Calculates Product(a+i, i=1..k-1) (see above).
57 double prodai(int k, double a)
60 for (int i = 1; i < k; ++i)
68 for (int i = 2; i <= x; ++i)
74 // Computes the integral
79 // | ProbScore[a] (r1-r2) Gaussian[mu2, sigma2] (r2) dr2
84 // For practical reasons, -inf and +inf are replaced by 0 and 3000, which
85 // is reasonable in the this context.
87 // The Gaussian is not normalized.
89 // Set the last parameter to 1.0 if player 1 won, or -1.0 if player 2 won.
90 // In the latter case, ProbScore will be given (r2-r1) instead of (r1-r2).
92 class ProbScoreEvaluator {
96 double binomial_precompute, r1, mu2, sigma2, winfac;
99 ProbScoreEvaluator(int k, double a, double binomial_precompute, double r1, double mu2, double sigma2, double winfac)
100 : k(k), a(a), binomial_precompute(binomial_precompute), r1(r1), mu2(mu2), sigma2(sigma2), winfac(winfac) {}
101 inline double operator() (double x) const
103 double probscore = prob_score_real(k, a, binomial_precompute, (r1 - x)*winfac);
104 double z = (x - mu2)/sigma2;
105 double gaussian = exp(-(z*z/2.0));
106 return probscore * gaussian;
110 void convolve(int size)
114 void compute_opponent_rating_pdf(int k, double a, double mu2, double sigma2, double winfac, vector<pair<double, double> > &result)
116 double binomial_precompute = prodai(k, a) / fac(k-1);
117 winfac /= rating_constant;
119 int sz = (6000.0 - 0.0) / int_step_size;
120 double h = (6000.0 - 0.0) / sz;
123 complex<double> *func1, *func2, *res;
125 func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
126 func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
127 res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
128 f1 = fftw_plan_dft_1d(sz*2,
129 reinterpret_cast<fftw_complex*>(func1),
130 reinterpret_cast<fftw_complex*>(func1),
133 f2 = fftw_plan_dft_1d(sz*2,
134 reinterpret_cast<fftw_complex*>(func2),
135 reinterpret_cast<fftw_complex*>(func2),
138 b = fftw_plan_dft_1d(sz*2,
139 reinterpret_cast<fftw_complex*>(res),
140 reinterpret_cast<fftw_complex*>(res),
145 for (int i = 0; i < sz*2; ++i) {
146 func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0;
149 for (int i = 0; i < sz; ++i) {
150 double x1 = 0.0 + h*i;
151 double z = (x1 - mu2)/sigma2;
152 func1[i].real() = exp(-(z*z/2.0));
154 double x2 = -3000.0 + h*i;
155 func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
158 result.reserve(sz*2);
163 for (int i = 0; i < sz*2; ++i) {
164 res[i] = func1[i] * func2[i];
167 for (int i = 0; i < sz; ++i) {
169 result.push_back(make_pair(r1, abs(res[i])));
173 // normalize the curve so we know that A ~= 1
174 void normalize(vector<pair<double, double> > &curve)
177 for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
178 peak = max(peak, i->second);
181 double invpeak = 1.0 / peak;
182 for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
183 i->second *= invpeak;
187 // computes matA * matB
188 void mat_mul(double *matA, unsigned ah, unsigned aw,
189 double *matB, unsigned bh, unsigned bw,
193 for (unsigned y = 0; y < bw; ++y) {
194 for (unsigned x = 0; x < ah; ++x) {
196 for (unsigned c = 0; c < aw; ++c) {
197 sum += matA[c*ah + x] * matB[y*bh + c];
199 result[y*bw + x] = sum;
204 // computes matA^T * matB
205 void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
206 double *matB, unsigned bh, unsigned bw,
210 for (unsigned y = 0; y < bw; ++y) {
211 for (unsigned x = 0; x < aw; ++x) {
213 for (unsigned c = 0; c < ah; ++c) {
214 sum += matA[x*ah + c] * matB[y*bh + c];
216 result[y*bw + x] = sum;
221 void print3x3(double *M)
223 printf("%f %f %f\n", M[0], M[3], M[6]);
224 printf("%f %f %f\n", M[1], M[4], M[7]);
225 printf("%f %f %f\n", M[2], M[5], M[8]);
228 void print3x1(double *M)
230 printf("%f\n", M[0]);
231 printf("%f\n", M[1]);
232 printf("%f\n", M[2]);
235 // solves Ax = B by Gauss-Jordan elimination, where A is a 3x3 matrix,
236 // x is a column vector of length 3 and B is a row vector of length 3.
237 // Destroys its input in the process.
238 void solve3x3(double *A, double *x, double *B)
240 // row 1 -= row 0 * (a1/a0)
242 double f = A[1] / A[0];
250 // row 2 -= row 0 * (a2/a0)
252 double f = A[2] / A[0];
260 // row 2 -= row 1 * (a5/a4)
262 double f = A[5] / A[4];
271 // row 1 -= row 2 * (a7/a8)
273 double f = A[7] / A[8];
279 // row 0 -= row 2 * (a6/a8)
281 double f = A[6] / A[8];
287 // row 0 -= row 1 * (a3/a4)
289 double f = A[3] / A[4];
301 // Give an OK starting estimate for the least squares, by numerical integration
302 // of statistical moments.
303 void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
305 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
307 double area = curve.front().second;
308 double ex = curve.front().first * curve.front().second;
309 double ex2 = curve.front().first * curve.front().first * curve.front().second;
311 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
312 double x = curve[i].first;
313 double y = curve[i].second;
316 ex2 += 4.0 * x * x * y;
318 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
319 double x = curve[i].first;
320 double y = curve[i].second;
323 ex2 += 2.0 * x * x * y;
326 area += curve.back().second;
327 ex += curve.back().first * curve.back().second;
328 ex2 += curve.back().first * curve.back().first * curve.back().second;
330 area = (h/3.0) * area;
331 ex = (h/3.0) * ex / area;
332 ex2 = (h/3.0) * ex2 / area;
335 sigma_result = sqrt(ex2 - ex * ex);
338 // Find best fit of the data in curves to a Gaussian pdf, based on the
339 // given initial estimates. Works by nonlinear least squares, iterating
340 // until we're below a certain threshold.
342 // Note that the algorithm blows up quite hard if the initial estimate is
343 // not good enough. Use estimate_musigma to get a reasonable starting
345 void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
349 double sigma = sigma1;
352 double matA[curve.size() * 3]; // N x 3
353 double dbeta[curve.size()]; // N x 1
355 // A^T * A: 3xN * Nx3 = 3x3
358 // A^T * dβ: 3xN * Nx1 = 3x1
364 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
366 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
367 for (unsigned i = 0; i < curve.size(); ++i) {
368 double x = curve[i].first;
371 matA[i + 0 * curve.size()] =
372 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
375 matA[i + 1 * curve.size()] =
376 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
379 matA[i + 2 * curve.size()] =
380 matA[i + 1 * curve.size()] * (x-mu)/sigma;
384 for (unsigned i = 0; i < curve.size(); ++i) {
385 double x = curve[i].first;
386 double y = curve[i].second;
388 dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
392 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
393 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
396 solve3x3(matATA, dlambda, matATdb);
402 // terminate when we're down to three digits
403 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
408 sigma_result = sigma;
411 void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma)
413 vector<pair<double, double> > curve;
415 if (score1 > score2) {
416 compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, curve);
418 compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, curve);
421 // multiply in the gaussian
422 for (unsigned i = 0; i < curve.size(); ++i) {
423 double r1 = curve[i].first;
424 double z = (r1 - mu1) / sigma1;
425 double gaussian = exp(-(z*z/2.0));
426 curve[i].second *= gaussian;
429 double mu_est, sigma_est;
431 estimate_musigma(curve, mu_est, sigma_est);
432 least_squares(curve, mu_est, sigma_est, mu, sigma);
435 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)
437 vector<pair<double, double> > curve, newcurve;
438 double mu_t = mu3 + mu4;
439 double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
441 if (score1 > score2) {
442 compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, curve);
444 compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, curve);
448 double h = 3000.0 / curve.size();
449 for (unsigned i = 0; i < curve.size(); ++i) {
452 // could be anything, but this is a nice start
453 //double r1 = curve[i].first;
457 for (unsigned j = 0; j < curve.size(); ++j) {
458 double r1plusr2 = curve[j].first;
459 double r2 = r1plusr2 - r1;
461 double z = (r2 - mu2) / sigma2;
462 double gaussian = exp(-(z*z/2.0));
463 sum += curve[j].second * gaussian;
466 double z = (r1 - mu1) / sigma1;
467 double gaussian = exp(-(z*z/2.0));
468 newcurve.push_back(make_pair(r1, gaussian * sum));
472 double mu_est, sigma_est;
474 estimate_musigma(newcurve, mu_est, sigma_est);
475 least_squares(newcurve, mu_est, sigma_est, mu, sigma);
478 int main(int argc, char **argv)
480 FILE *fp = fopen("fftw-wisdom", "rb");
482 fftw_import_wisdom_from_file(fp);
486 double mu1 = atof(argv[1]);
487 double sigma1 = atof(argv[2]);
488 double mu2 = atof(argv[3]);
489 double sigma2 = atof(argv[4]);
492 double mu3 = atof(argv[5]);
493 double sigma3 = atof(argv[6]);
494 double mu4 = atof(argv[7]);
495 double sigma4 = atof(argv[8]);
496 int score1 = atoi(argv[9]);
497 int score2 = atoi(argv[10]);
499 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, mu, sigma);
500 printf("%f %f\n", mu, sigma);
501 } else if (argc > 8) {
502 double mu3 = atof(argv[5]);
503 double sigma3 = atof(argv[6]);
504 double mu4 = atof(argv[7]);
505 double sigma4 = atof(argv[8]);
506 int k = atoi(argv[9]);
508 // assess all possible scores
509 for (int i = 0; i < k; ++i) {
510 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
511 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
512 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, newmu1_1, newsigma1_1);
513 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, newmu1_2, newsigma1_2);
514 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, newmu2_1, newsigma2_1);
515 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, newmu2_2, newsigma2_2);
516 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
517 k, i, prob_score(k, i, mu3+mu4-(mu1+mu2)), newmu1_1-mu1, newmu1_2-mu2,
518 newmu2_1-mu3, newmu2_2-mu4);
520 for (int i = k; i --> 0; ) {
521 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
522 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
523 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, newmu1_1, newsigma1_1);
524 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, newmu1_2, newsigma1_2);
525 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, newmu2_1, newsigma2_1);
526 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, newmu2_2, newsigma2_2);
527 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
528 i, k, prob_score(k, i, mu1+mu2-(mu3+mu4)), newmu1_1-mu1, newmu1_2-mu2,
529 newmu2_1-mu3, newmu2_2-mu4);
531 } else if (argc > 6) {
532 int score1 = atoi(argv[5]);
533 int score2 = atoi(argv[6]);
535 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
536 printf("%f %f\n", mu, sigma);
538 int k = atoi(argv[5]);
540 // assess all possible scores
541 for (int i = 0; i < k; ++i) {
542 double newmu1, newmu2, newsigma1, newsigma2;
543 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
544 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
545 printf("%u-%u,%f,%+f,%+f\n",
546 k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
548 for (int i = k; i --> 0; ) {
549 double newmu1, newmu2, newsigma1, newsigma2;
550 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
551 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
552 printf("%u-%u,%f,%+f,%+f\n",
553 i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);
557 fp = fopen("fftw-wisdom", "wb");
559 fftw_export_wisdom_to_file(fp);