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;
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));
26 static double prob_score_real(int k, int a, double binomial, double rd_norm);
27 static double prodai(int k, int a);
28 static double fac(int x);
30 #if USE_LOGISTIC_DISTRIBUTION
32 static double sech2(double x)
34 double e = exp(2.0 * x);
35 return 4.0 * e / ((e+1.0) * (e+1.0));
40 // probability of match ending k-a (k>a) when winnerR - loserR = RD
45 // | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
50 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
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.
56 static double prob_score(int k, int a, double rd)
58 return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant);
62 // computes x^a, probably more efficiently than pow(x, a) (but requires that a
63 // is n unsigned integer)
64 static double intpow(double x, unsigned a)
79 // Same, but takes in binomial(a+k-1, k-1) as an argument in
80 // addition to a. Faster if you already have that precomputed, and assumes rd
81 // is already divided by 455.
82 static double prob_score_real(int k, int a, double binomial, double rd_norm)
84 double nom = binomial * intpow(pow(2.0, rd_norm), a);
85 double denom = intpow(1.0 + pow(2.0, rd_norm), k+a);
89 // Calculates Product(a+i, i=1..k-1) (see above).
90 static double prodai(int k, int a)
93 for (int i = 1; i < k; ++i)
98 static double fac(int x)
101 for (int i = 2; i <= x; ++i)
106 static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector<pair<double, double> > &result)
108 double binomial_precompute = prodai(k, a) / fac(k-1);
109 winfac /= rating_constant;
111 int sz = (6000.0 - 0.0) / int_step_size;
112 double h = (6000.0 - 0.0) / sz;
115 complex<double> *func1, *func2, *res;
117 func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
118 func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
119 res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
120 f1 = fftw_plan_dft_1d(sz*2,
121 reinterpret_cast<fftw_complex*>(func1),
122 reinterpret_cast<fftw_complex*>(func1),
125 f2 = fftw_plan_dft_1d(sz*2,
126 reinterpret_cast<fftw_complex*>(func2),
127 reinterpret_cast<fftw_complex*>(func2),
130 b = fftw_plan_dft_1d(sz*2,
131 reinterpret_cast<fftw_complex*>(res),
132 reinterpret_cast<fftw_complex*>(res),
137 for (int i = 0; i < sz*2; ++i) {
138 func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0;
141 #if USE_LOGISTIC_DISTRIBUTION
142 double invsigma2 = 1.0 / sigma2;
144 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
146 for (int i = 0; i < sz; ++i) {
147 double x1 = 0.0 + h*i;
150 #if USE_LOGISTIC_DISTRIBUTION
151 double z = (x1 - mu2) * invsigma2;
152 double ch = cosh(l_const * z);
153 func1[i].real() = 1.0 / (ch * ch);
155 double z = (x1 - mu2) * invsq2sigma2;
156 func1[i].real() = exp(-z*z);
159 double x2 = -3000.0 + h*i;
160 func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
163 result.reserve(sz*2);
168 for (int i = 0; i < sz*2; ++i) {
169 res[i] = func1[i] * func2[i];
174 for (int i = 0; i < sz; ++i) {
176 result.push_back(make_pair(r1, abs(res[i])));
180 // normalize the curve so we know that A ~= 1
181 static void normalize(vector<pair<double, double> > &curve)
184 for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
185 peak = max(peak, i->second);
188 double invpeak = 1.0 / peak;
189 for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
190 i->second *= invpeak;
194 // computes matA^T * matB
195 static void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
196 double *matB, unsigned bh, unsigned bw,
200 for (unsigned y = 0; y < bw; ++y) {
201 for (unsigned x = 0; x < aw; ++x) {
203 for (unsigned c = 0; c < ah; ++c) {
204 sum += matA[x*ah + c] * matB[y*bh + c];
206 result[y*bw + x] = sum;
211 // solves Ax = B by Gauss-Jordan elimination, where A is an NxN matrix,
212 // x is a column vector of length N and B is a row vector of length N.
213 // Destroys its input in the process.
215 static void solve_matrix(double *A, double *x, double *B)
217 for (int i = 0; i < N; ++i) {
218 for (int j = i+1; j < N; ++j) {
219 // row j -= row i * (a[i,j] / a[i,i])
220 double f = A[j+i*N] / A[i+i*N];
223 for (int k = i+1; k < N; ++k) {
224 A[j+k*N] -= A[i+k*N] * f;
232 for (int i = N; i --> 0; ) {
233 for (int j = i; j --> 0; ) {
234 // row j -= row i * (a[j,j] / a[j,i])
235 double f = A[i+j*N] / A[j+j*N];
243 for (int i = 0; i < N; ++i) {
244 x[i] = B[i] / A[i+i*N];
248 // Give an OK starting estimate for the least squares, by numerical integration
249 // of statistical moments.
250 static void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
252 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
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;
258 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
259 double x = curve[i].first;
260 double y = curve[i].second;
263 ex2 += 4.0 * x * x * y;
265 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
266 double x = curve[i].first;
267 double y = curve[i].second;
270 ex2 += 2.0 * x * x * y;
273 area += curve.back().second;
274 ex += curve.back().first * curve.back().second;
275 ex2 += curve.back().first * curve.back().first * curve.back().second;
277 area = (h/3.0) * area;
278 ex = (h/3.0) * ex / area;
279 ex2 = (h/3.0) * ex2 / area;
282 sigma_result = sqrt(ex2 - ex * ex);
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.
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
292 static void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
296 double sigma = sigma1;
299 double matA[curve.size() * 3]; // N x 3
300 double dbeta[curve.size()]; // N x 1
302 // A^T * A: 3xN * Nx3 = 3x3
305 // A^T * dβ: 3xN * Nx1 = 3x1
311 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
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;
317 #if USE_LOGISTIC_DISTRIBUTION
319 matA[i + 0 * curve.size()] = sech2(l_const * (x-mu)/sigma);
322 matA[i + 1 * curve.size()] = 2.0 * l_const * A * matA[i + 0 * curve.size()]
323 * tanh(l_const * (x-mu)/sigma) / sigma;
326 matA[i + 2 * curve.size()] =
327 matA[i + 1 * curve.size()] * (x-mu)/sigma;
330 matA[i + 0 * curve.size()] =
331 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
334 matA[i + 1 * curve.size()] =
335 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
338 matA[i + 2 * curve.size()] =
339 matA[i + 1 * curve.size()] * (x-mu)/sigma;
344 for (unsigned i = 0; i < curve.size(); ++i) {
345 double x = curve[i].first;
346 double y = curve[i].second;
348 #if USE_LOGISTIC_DISTRIBUTION
349 dbeta[i] = y - A * sech2(l_const * (x-mu)/sigma);
351 dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
356 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
357 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
360 solve_matrix<3>(matATA, dlambda, matATdb);
366 // terminate when we're down to three digits
367 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
372 sigma_result = sigma;
375 static void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma, double &probability)
377 vector<pair<double, double> > curve;
379 if (score1 > score2) {
380 compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, curve);
382 compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, curve);
385 // multiply in the gaussian
386 for (unsigned i = 0; i < curve.size(); ++i) {
387 double r1 = curve[i].first;
390 double z = (r1 - mu1) / sigma1;
391 #if USE_LOGISTIC_DISTRIBUTION
392 double ch = cosh(l_const * z);
393 curve[i].second /= (ch * ch);
395 double gaussian = exp(-(z*z/2.0));
396 curve[i].second *= gaussian;
400 // Compute the overall probability of the given result, by integrating
401 // the entire resulting pdf. Note that since we're actually evaluating
402 // a double integral, we'll need to multiply by h² instead of h.
403 // (TODO: Use Simpson's rule here.)
405 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
407 for (unsigned i = 0; i < curve.size(); ++i) {
408 sum += h * h * curve[i].second;
411 // FFT convolution multiplication factor (FFTW computes unnormalized
413 sum /= (curve.size() * 2);
415 // pdf normalization factors
416 #if USE_LOGISTIC_DISTRIBUTION
417 sum *= M_PI / (sigma1 * 4.0 * sqrt(3.0));
418 sum *= M_PI / (sigma2 * 4.0 * sqrt(3.0));
420 sum /= (sigma1 * sqrt(2.0 * M_PI));
421 sum /= (sigma2 * sqrt(2.0 * M_PI));
427 double mu_est, sigma_est;
429 estimate_musigma(curve, mu_est, sigma_est);
430 least_squares(curve, mu_est, sigma_est, mu, sigma);
433 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)
435 vector<pair<double, double> > curve, newcurve;
436 double mu_t = mu3 + mu4;
437 double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
439 if (score1 > score2) {
440 compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, curve);
442 compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, curve);
445 newcurve.reserve(curve.size());
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 #if USE_LOGISTIC_DISTRIBUTION
458 double invsigma2 = 1.0 / sigma2;
460 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
462 for (unsigned j = 0; j < curve.size(); ++j) {
463 double r1plusr2 = curve[j].first;
464 double r2 = r1plusr2 - r1;
466 #if USE_LOGISTIC_DISTRIBUTION
467 double z = (r2 - mu2) * invsigma2;
468 double gaussian = sech2(l_const * z);
470 double z = (r2 - mu2) * invsq2sigma2;
471 double gaussian = exp(-z*z);
473 sum += curve[j].second * gaussian;
476 #if USE_LOGISTIC_DISTRIBUTION
477 double z = (r1 - mu1) / sigma1;
478 double gaussian = sech2(l_const * z);
480 double z = (r1 - mu1) / sigma1;
481 double gaussian = exp(-(z*z/2.0));
483 newcurve.push_back(make_pair(r1, gaussian * sum));
486 // Compute the overall probability of the given result, by integrating
487 // the entire resulting pdf. Note that since we're actually evaluating
488 // a triple integral, we'll need to multiply by 4h³ (no idea where the
489 // 4 factor comes from, probably from the 0..6000 range somehow) instead
490 // of h. (TODO: Use Simpson's rule here.)
492 double h = (newcurve.back().first - newcurve.front().first) / (newcurve.size() - 1);
494 for (unsigned i = 0; i < newcurve.size(); ++i) {
495 sum += 4.0 * h * h * h * newcurve[i].second;
498 // FFT convolution multiplication factor (FFTW computes unnormalized
500 sum /= (newcurve.size() * 2);
502 // pdf normalization factors
503 #if USE_LOGISTIC_DISTRIBUTION
504 sum *= M_PI / (sigma1 * 4.0 * sqrt(3.0));
505 sum *= M_PI / (sigma2 * 4.0 * sqrt(3.0));
506 sum *= M_PI / (sigma_t * 4.0 * sqrt(3.0));
508 sum /= (sigma1 * sqrt(2.0 * M_PI));
509 sum /= (sigma2 * sqrt(2.0 * M_PI));
510 sum /= (sigma_t * sqrt(2.0 * M_PI));
516 double mu_est, sigma_est;
518 estimate_musigma(newcurve, mu_est, sigma_est);
519 least_squares(newcurve, mu_est, sigma_est, mu, sigma);
522 int main(int argc, char **argv)
524 FILE *fp = fopen("fftw-wisdom", "rb");
526 fftw_import_wisdom_from_file(fp);
530 double mu1 = atof(argv[1]);
531 double sigma1 = atof(argv[2]);
532 double mu2 = atof(argv[3]);
533 double sigma2 = atof(argv[4]);
536 double mu3 = atof(argv[5]);
537 double sigma3 = atof(argv[6]);
538 double mu4 = atof(argv[7]);
539 double sigma4 = atof(argv[8]);
540 int score1 = atoi(argv[9]);
541 int score2 = atoi(argv[10]);
542 double mu, sigma, probability;
543 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, mu, sigma, probability);
544 if (score1 > score2) {
545 printf("%f %f %f\n", mu, sigma, probability);
547 printf("%f %f %f\n", mu, sigma, probability);
549 } else if (argc > 8) {
550 double mu3 = atof(argv[5]);
551 double sigma3 = atof(argv[6]);
552 double mu4 = atof(argv[7]);
553 double sigma4 = atof(argv[8]);
554 int k = atoi(argv[9]);
556 // assess all possible scores
557 for (int i = 0; i < k; ++i) {
558 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
559 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
561 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, newmu1_1, newsigma1_1, probability);
562 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, newmu1_2, newsigma1_2, probability);
563 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, newmu2_1, newsigma2_1, probability);
564 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, newmu2_2, newsigma2_2, probability);
565 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
566 k, i, probability, newmu1_1-mu1, newmu1_2-mu2,
567 newmu2_1-mu3, newmu2_2-mu4);
569 for (int i = k; i --> 0; ) {
570 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
571 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
573 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, newmu1_1, newsigma1_1, probability);
574 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, newmu1_1, newsigma1_1, probability);
575 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, newmu1_2, newsigma1_2, probability);
576 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, newmu2_1, newsigma2_1, probability);
577 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, newmu2_2, newsigma2_2, probability);
578 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
579 i, k, probability, newmu1_1-mu1, newmu1_2-mu2,
580 newmu2_1-mu3, newmu2_2-mu4);
582 } else if (argc > 6) {
583 int score1 = atoi(argv[5]);
584 int score2 = atoi(argv[6]);
585 double mu, sigma, probability;
586 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma, probability);
588 if (score1 > score2) {
589 printf("%f %f %f\n", mu, sigma, probability);
591 printf("%f %f %f\n", mu, sigma, probability);
594 int k = atoi(argv[5]);
596 // assess all possible scores
597 for (int i = 0; i < k; ++i) {
598 double newmu1, newmu2, newsigma1, newsigma2, probability;
599 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1, probability);
600 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2, probability);
601 printf("%u-%u,%f,%+f,%+f\n",
602 k, i, probability, newmu1-mu1, newmu2-mu2);
604 for (int i = k; i --> 0; ) {
605 double newmu1, newmu2, newsigma1, newsigma2, probability;
606 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1, probability);
607 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2, probability);
608 printf("%u-%u,%f,%+f,%+f\n",
609 i, k, probability, newmu1-mu1, newmu2-mu2);
613 fp = fopen("fftw-wisdom", "wb");
615 fftw_export_wisdom_to_file(fp);