11 #define USE_LOGISTIC_DISTRIBUTION 0
14 static const double int_step_size = 75.0;
16 // rating constant (see below)
17 static const double rating_constant = 455.0;
21 static double prob_score_real(int k, int a, double binomial, double rd_norm);
22 static double prodai(int k, int a);
23 static double fac(int x);
25 #if USE_LOGISTIC_DISTRIBUTION
27 static double sech2(double x)
29 double e = exp(2.0 * x);
30 return 4.0 * e / ((e+1.0) * (e+1.0));
35 // probability of match ending k-a (k>a) when winnerR - loserR = RD
40 // | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
45 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
47 // The constant of 455 is chosen carefully so to match with the
48 // Glicko/Bradley-Terry assumption that a player rated 400 points over
49 // his/her opponent will win with a probability of 10/11 =~ 0.90909.
51 static double prob_score(int k, int a, double rd)
53 return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant);
57 // computes x^a, probably more efficiently than pow(x, a) (but requires that a
58 // is n unsigned integer)
59 static double intpow(double x, unsigned a)
74 // Same, but takes in binomial(a+k-1, k-1) as an argument in
75 // addition to a. Faster if you already have that precomputed, and assumes rd
76 // is already divided by 455.
77 static double prob_score_real(int k, int a, double binomial, double rd_norm)
79 double nom = binomial * intpow(pow(2.0, rd_norm), a);
80 double denom = intpow(1.0 + pow(2.0, rd_norm), k+a);
84 // Calculates Product(a+i, i=1..k-1) (see above).
85 static double prodai(int k, int a)
88 for (int i = 1; i < k; ++i)
93 static double fac(int x)
96 for (int i = 2; i <= x; ++i)
101 static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector<pair<double, double> > *result)
103 double binomial_precompute = prodai(k, a) / fac(k-1);
104 winfac /= rating_constant;
106 int sz = (6000.0 - 0.0) / int_step_size;
107 double h = (6000.0 - 0.0) / sz;
109 static bool inited = false;
110 static fftw_plan f1, f2, b;
111 static complex<double> *func1, *func2, *res;
114 func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
115 func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
116 res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
117 f1 = fftw_plan_dft_1d(sz*2,
118 reinterpret_cast<fftw_complex*>(func1),
119 reinterpret_cast<fftw_complex*>(func1),
122 f2 = fftw_plan_dft_1d(sz*2,
123 reinterpret_cast<fftw_complex*>(func2),
124 reinterpret_cast<fftw_complex*>(func2),
127 b = fftw_plan_dft_1d(sz*2,
128 reinterpret_cast<fftw_complex*>(res),
129 reinterpret_cast<fftw_complex*>(res),
136 for (int i = 0; i < sz*2; ++i) {
137 func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0;
140 #if USE_LOGISTIC_DISTRIBUTION
141 double invsigma2 = 1.0 / sigma2;
143 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
145 for (int i = 0; i < sz; ++i) {
146 double x1 = 0.0 + h*i;
149 #if USE_LOGISTIC_DISTRIBUTION
150 double z = (x1 - mu2) * invsigma2;
151 func1[i].real() = sech2(0.5 * z);
153 double z = (x1 - mu2) * invsq2sigma2;
154 func1[i].real() = exp(-z*z);
157 double x2 = -3000.0 + h*i;
158 func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
161 result->reserve(sz*2);
166 for (int i = 0; i < sz*2; ++i) {
167 res[i] = func1[i] * func2[i];
172 for (int i = 0; i < sz; ++i) {
174 result->push_back(make_pair(r1, abs(res[i])));
178 // normalize the curve so we know that A ~= 1
179 static void normalize(vector<pair<double, double> > *curve)
182 for (vector<pair<double, double> >::const_iterator i = curve->begin(); i != curve->end(); ++i) {
183 peak = max(peak, i->second);
186 double invpeak = 1.0 / peak;
187 for (vector<pair<double, double> >::iterator i = curve->begin(); i != curve->end(); ++i) {
188 i->second *= invpeak;
192 // computes matA^T * matB
193 static void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
194 double *matB, unsigned bh, unsigned bw,
198 for (unsigned y = 0; y < bw; ++y) {
199 for (unsigned x = 0; x < aw; ++x) {
201 for (unsigned c = 0; c < ah; ++c) {
202 sum += matA[x*ah + c] * matB[y*bh + c];
204 result[y*bw + x] = sum;
209 // solves Ax = B by Gauss-Jordan elimination, where A is an NxN matrix,
210 // x is a column vector of length N and B is a row vector of length N.
211 // Destroys its input in the process.
213 static void solve_matrix(double *A, double *x, double *B)
215 for (int i = 0; i < N; ++i) {
216 for (int j = i+1; j < N; ++j) {
217 // row j -= row i * (a[i,j] / a[i,i])
218 double f = A[j+i*N] / A[i+i*N];
221 for (int k = i+1; k < N; ++k) {
222 A[j+k*N] -= A[i+k*N] * f;
230 for (int i = N; i --> 0; ) {
231 for (int j = i; j --> 0; ) {
232 // row j -= row i * (a[j,j] / a[j,i])
233 double f = A[i+j*N] / A[j+j*N];
241 for (int i = 0; i < N; ++i) {
242 x[i] = B[i] / A[i+i*N];
246 // Give an OK starting estimate for the least squares, by numerical integration
247 // of statistical moments.
248 static void estimate_musigma(const vector<pair<double, double> > &curve, double *mu_result, double *sigma_result)
250 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
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;
256 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
257 double x = curve[i].first;
258 double y = curve[i].second;
261 ex2 += 4.0 * x * x * y;
263 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
264 double x = curve[i].first;
265 double y = curve[i].second;
268 ex2 += 2.0 * x * x * y;
271 area += curve.back().second;
272 ex += curve.back().first * curve.back().second;
273 ex2 += curve.back().first * curve.back().first * curve.back().second;
275 area = (h/3.0) * area;
276 ex = (h/3.0) * ex / area;
277 ex2 = (h/3.0) * ex2 / area;
280 *sigma_result = sqrt(ex2 - ex * ex);
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.
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
290 static void least_squares(const vector<pair<double, double> > &curve, double mu1, double sigma1, double *mu_result, double *sigma_result)
294 double sigma = sigma1;
297 double matA[curve.size() * 3]; // N x 3
298 double dbeta[curve.size()]; // N x 1
300 // A^T * A: 3xN * Nx3 = 3x3
303 // A^T * dβ: 3xN * Nx1 = 3x1
309 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
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;
315 #if USE_LOGISTIC_DISTRIBUTION
317 matA[i + 0 * curve.size()] = sech2(0.5 * (x-mu)/sigma);
320 matA[i + 1 * curve.size()] = A * matA[i + 0 * curve.size()]
321 * tanh(0.5 * (x-mu)/sigma) / sigma;
324 matA[i + 2 * curve.size()] =
325 matA[i + 1 * curve.size()] * (x-mu)/sigma;
328 matA[i + 0 * curve.size()] =
329 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
332 matA[i + 1 * curve.size()] =
333 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
336 matA[i + 2 * curve.size()] =
337 matA[i + 1 * curve.size()] * (x-mu)/sigma;
342 for (unsigned i = 0; i < curve.size(); ++i) {
343 double x = curve[i].first;
344 double y = curve[i].second;
346 #if USE_LOGISTIC_DISTRIBUTION
347 dbeta[i] = y - A * sech2(0.5 * (x-mu)/sigma);
349 dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
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);
358 solve_matrix<3>(matATA, dlambda, matATdb);
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)
370 *sigma_result = sigma;
373 void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double *mu, double *sigma, double *probability)
375 vector<pair<double, double> > curve;
377 if (score1 > score2) {
378 compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, &curve);
380 compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, &curve);
383 // multiply in the gaussian
384 for (unsigned i = 0; i < curve.size(); ++i) {
385 double r1 = curve[i].first;
388 double z = (r1 - mu1) / sigma1;
389 #if USE_LOGISTIC_DISTRIBUTION
390 curve[i].second *= sech2(0.5 * z);
392 double gaussian = exp(-(z*z/2.0));
393 curve[i].second *= gaussian;
397 // Compute the overall probability of the given result, by integrating
398 // the entire resulting pdf. Note that since we're actually evaluating
399 // a double integral, we'll need to multiply by h² instead of h.
401 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
402 double sum = curve.front().second;
403 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
404 sum += 4.0 * curve[i].second;
406 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
407 sum += 2.0 * curve[i].second;
409 sum += curve.back().second;
412 // FFT convolution multiplication factor (FFTW computes unnormalized
414 sum /= (curve.size() * 2);
416 // pdf normalization factors
417 #if USE_LOGISTIC_DISTRIBUTION
418 sum /= (sigma1 * 4.0);
419 sum /= (sigma2 * 4.0);
421 sum /= (sigma1 * sqrt(2.0 * M_PI));
422 sum /= (sigma2 * sqrt(2.0 * M_PI));
428 double mu_est, sigma_est;
430 estimate_musigma(curve, &mu_est, &sigma_est);
431 least_squares(curve, mu_est, sigma_est, mu, sigma);
434 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, double *probability)
436 vector<pair<double, double> > curve, newcurve;
437 double mu_t = mu3 + mu4;
438 double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
440 if (score1 > score2) {
441 compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, &curve);
443 compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, &curve);
446 newcurve.reserve(curve.size());
449 double h = 3000.0 / curve.size();
450 for (unsigned i = 0; i < curve.size(); ++i) {
453 // could be anything, but this is a nice start
454 //double r1 = curve[i].first;
458 #if USE_LOGISTIC_DISTRIBUTION
459 double invsigma2 = 1.0 / sigma2;
461 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
463 for (unsigned j = 0; j < curve.size(); ++j) {
464 double r1plusr2 = curve[j].first;
465 double r2 = r1plusr2 - r1;
467 #if USE_LOGISTIC_DISTRIBUTION
468 double z = (r2 - mu2) * invsigma2;
469 double gaussian = sech2(0.5 * z);
471 double z = (r2 - mu2) * invsq2sigma2;
472 double gaussian = exp(-z*z);
474 sum += curve[j].second * gaussian;
477 #if USE_LOGISTIC_DISTRIBUTION
478 double z = (r1 - mu1) / sigma1;
479 double gaussian = sech2(0.5 * z);
481 double z = (r1 - mu1) / sigma1;
482 double gaussian = exp(-(z*z/2.0));
484 newcurve.push_back(make_pair(r1, gaussian * sum));
487 // Compute the overall probability of the given result, by integrating
488 // the entire resulting pdf. Note that since we're actually evaluating
489 // a triple integral, we'll need to multiply by 4h³ (no idea where the
490 // 4 factor comes from, probably from the 0..6000 range somehow) instead
493 double h = (newcurve.back().first - newcurve.front().first) / (newcurve.size() - 1);
494 double sum = newcurve.front().second;
495 for (unsigned i = 1; i < newcurve.size() - 1; i += 2) {
496 sum += 4.0 * newcurve[i].second;
498 for (unsigned i = 2; i < newcurve.size() - 1; i += 2) {
499 sum += 2.0 * newcurve[i].second;
501 sum += newcurve.back().second;
503 sum *= 4.0 * h * h * h / 3.0;
505 // FFT convolution multiplication factor (FFTW computes unnormalized
507 sum /= (newcurve.size() * 2);
509 // pdf normalization factors
510 #if USE_LOGISTIC_DISTRIBUTION
511 sum /= (sigma1 * 4.0);
512 sum /= (sigma2 * 4.0);
513 sum /= (sigma_t * 4.0);
515 sum /= (sigma1 * sqrt(2.0 * M_PI));
516 sum /= (sigma2 * sqrt(2.0 * M_PI));
517 sum /= (sigma_t * sqrt(2.0 * M_PI));
523 double mu_est, sigma_est;
524 normalize(&newcurve);
525 estimate_musigma(newcurve, &mu_est, &sigma_est);
526 least_squares(newcurve, mu_est, sigma_est, mu, sigma);
529 int main(int argc, char **argv)
531 FILE *fp = fopen("fftw-wisdom", "rb");
533 fftw_import_wisdom_from_file(fp);
537 double mu1 = atof(argv[1]);
538 double sigma1 = atof(argv[2]);
539 double mu2 = atof(argv[3]);
540 double sigma2 = atof(argv[4]);
543 double mu3 = atof(argv[5]);
544 double sigma3 = atof(argv[6]);
545 double mu4 = atof(argv[7]);
546 double sigma4 = atof(argv[8]);
547 int score1 = atoi(argv[9]);
548 int score2 = atoi(argv[10]);
549 double mu, sigma, probability;
550 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, &mu, &sigma, &probability);
551 printf("%f %f %f\n", mu, sigma, probability);
552 } else if (argc > 8) {
553 double mu3 = atof(argv[5]);
554 double sigma3 = atof(argv[6]);
555 double mu4 = atof(argv[7]);
556 double sigma4 = atof(argv[8]);
557 int k = atoi(argv[9]);
559 // assess all possible scores
560 for (int i = 0; i < k; ++i) {
561 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
562 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
564 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, &newmu1_1, &newsigma1_1, &probability);
565 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, &newmu1_2, &newsigma1_2, &probability);
566 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, &newmu2_1, &newsigma2_1, &probability);
567 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, &newmu2_2, &newsigma2_2, &probability);
568 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
569 k, i, probability, newmu1_1-mu1, newmu1_2-mu2,
570 newmu2_1-mu3, newmu2_2-mu4);
572 for (int i = k; i --> 0; ) {
573 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
574 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
576 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, &newmu1_1, &newsigma1_1, &probability);
577 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, &newmu1_2, &newsigma1_2, &probability);
578 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, &newmu2_1, &newsigma2_1, &probability);
579 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, &newmu2_2, &newsigma2_2, &probability);
580 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
581 i, k, probability, newmu1_1-mu1, newmu1_2-mu2,
582 newmu2_1-mu3, newmu2_2-mu4);
584 } else if (argc > 6) {
585 int score1 = atoi(argv[5]);
586 int score2 = atoi(argv[6]);
587 double mu, sigma, probability;
588 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, &mu, &sigma, &probability);
590 printf("%f %f %f\n", mu, sigma, probability);
592 int k = atoi(argv[5]);
594 // assess all possible scores
595 for (int i = 0; i < k; ++i) {
596 double newmu1, newmu2, newsigma1, newsigma2, probability;
597 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, &newmu1, &newsigma1, &probability);
598 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, &newmu2, &newsigma2, &probability);
599 printf("%u-%u,%f,%+f,%+f\n",
600 k, i, probability, newmu1-mu1, newmu2-mu2);
602 for (int i = k; i --> 0; ) {
603 double newmu1, newmu2, newsigma1, newsigma2, probability;
604 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, &newmu1, &newsigma1, &probability);
605 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, &newmu2, &newsigma2, &probability);
606 printf("%u-%u,%f,%+f,%+f\n",
607 i, k, probability, newmu1-mu1, newmu2-mu2);
611 fp = fopen("fftw-wisdom", "wb");
613 fftw_export_wisdom_to_file(fp);