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(int k, int a, double rd);
27 static double prob_score_real(int k, int a, double binomial, double rd_norm);
28 static double prodai(int k, int a);
29 static double fac(int x);
31 #if USE_LOGISTIC_DISTRIBUTION
33 static double sech2(double x)
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);
61 // computes x^a, probably more efficiently than pow(x, a) (but requires that a
62 // is n unsigned integer)
63 static double intpow(double x, unsigned a)
78 // Same, but takes in binomial(a+k-1, k-1) as an argument in
79 // addition to a. Faster if you already have that precomputed, and assumes rd
80 // is already divided by 455.
81 static double prob_score_real(int k, int a, double binomial, double rd_norm)
83 double nom = binomial * intpow(pow(2.0, rd_norm), a);
84 double denom = intpow(1.0 + pow(2.0, rd_norm), k+a);
88 // Calculates Product(a+i, i=1..k-1) (see above).
89 static double prodai(int k, int a)
92 for (int i = 1; i < k; ++i)
97 static double fac(int x)
100 for (int i = 2; i <= x; ++i)
105 static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector<pair<double, double> > &result)
107 double binomial_precompute = prodai(k, a) / fac(k-1);
108 winfac /= rating_constant;
110 int sz = (6000.0 - 0.0) / int_step_size;
111 double h = (6000.0 - 0.0) / sz;
114 complex<double> *func1, *func2, *res;
116 func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
117 func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
118 res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
119 f1 = fftw_plan_dft_1d(sz*2,
120 reinterpret_cast<fftw_complex*>(func1),
121 reinterpret_cast<fftw_complex*>(func1),
124 f2 = fftw_plan_dft_1d(sz*2,
125 reinterpret_cast<fftw_complex*>(func2),
126 reinterpret_cast<fftw_complex*>(func2),
129 b = fftw_plan_dft_1d(sz*2,
130 reinterpret_cast<fftw_complex*>(res),
131 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 double ch = cosh(l_const * z);
152 func1[i].real() = 1.0 / (ch * ch);
154 double z = (x1 - mu2) * invsq2sigma2;
155 func1[i].real() = exp(-z*z);
158 double x2 = -3000.0 + h*i;
159 func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
162 result.reserve(sz*2);
167 for (int i = 0; i < sz*2; ++i) {
168 res[i] = func1[i] * func2[i];
173 for (int i = 0; i < sz; ++i) {
175 result.push_back(make_pair(r1, abs(res[i])));
179 // normalize the curve so we know that A ~= 1
180 static void normalize(vector<pair<double, double> > &curve)
183 for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
184 peak = max(peak, i->second);
187 double invpeak = 1.0 / peak;
188 for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
189 i->second *= invpeak;
193 // computes matA^T * matB
194 static void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
195 double *matB, unsigned bh, unsigned bw,
199 for (unsigned y = 0; y < bw; ++y) {
200 for (unsigned x = 0; x < aw; ++x) {
202 for (unsigned c = 0; c < ah; ++c) {
203 sum += matA[x*ah + c] * matB[y*bh + c];
205 result[y*bw + x] = sum;
210 // solves Ax = B by Gauss-Jordan elimination, where A is an NxN matrix,
211 // x is a column vector of length N and B is a row vector of length N.
212 // Destroys its input in the process.
214 static void solve_matrix(double *A, double *x, double *B)
216 for (int i = 0; i < N; ++i) {
217 for (int j = i+1; j < N; ++j) {
218 // row j -= row i * (a[i,j] / a[i,i])
219 double f = A[j+i*N] / A[i+i*N];
222 for (int k = i+1; k < N; ++k) {
223 A[j+k*N] -= A[i+k*N] * f;
231 for (int i = N; i --> 0; ) {
232 for (int j = i; j --> 0; ) {
233 // row j -= row i * (a[j,j] / a[j,i])
234 double f = A[i+j*N] / A[j+j*N];
242 for (int i = 0; i < N; ++i) {
243 x[i] = B[i] / A[i+i*N];
247 // Give an OK starting estimate for the least squares, by numerical integration
248 // of statistical moments.
249 static void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
251 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
253 double area = curve.front().second;
254 double ex = curve.front().first * curve.front().second;
255 double ex2 = curve.front().first * curve.front().first * curve.front().second;
257 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
258 double x = curve[i].first;
259 double y = curve[i].second;
262 ex2 += 4.0 * x * x * y;
264 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
265 double x = curve[i].first;
266 double y = curve[i].second;
269 ex2 += 2.0 * x * x * y;
272 area += curve.back().second;
273 ex += curve.back().first * curve.back().second;
274 ex2 += curve.back().first * curve.back().first * curve.back().second;
276 area = (h/3.0) * area;
277 ex = (h/3.0) * ex / area;
278 ex2 = (h/3.0) * ex2 / area;
281 sigma_result = sqrt(ex2 - ex * ex);
284 // Find best fit of the data in curves to a Gaussian pdf, based on the
285 // given initial estimates. Works by nonlinear least squares, iterating
286 // until we're below a certain threshold.
288 // Note that the algorithm blows up quite hard if the initial estimate is
289 // not good enough. Use estimate_musigma to get a reasonable starting
291 static void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
295 double sigma = sigma1;
298 double matA[curve.size() * 3]; // N x 3
299 double dbeta[curve.size()]; // N x 1
301 // A^T * A: 3xN * Nx3 = 3x3
304 // A^T * dβ: 3xN * Nx1 = 3x1
310 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
312 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
313 for (unsigned i = 0; i < curve.size(); ++i) {
314 double x = curve[i].first;
316 #if USE_LOGISTIC_DISTRIBUTION
318 matA[i + 0 * curve.size()] = sech2(l_const * (x-mu)/sigma);
321 matA[i + 1 * curve.size()] = 2.0 * l_const * A * matA[i + 0 * curve.size()]
322 * tanh(l_const * (x-mu)/sigma) / sigma;
325 matA[i + 2 * curve.size()] =
326 matA[i + 1 * curve.size()] * (x-mu)/sigma;
329 matA[i + 0 * curve.size()] =
330 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
333 matA[i + 1 * curve.size()] =
334 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
337 matA[i + 2 * curve.size()] =
338 matA[i + 1 * curve.size()] * (x-mu)/sigma;
343 for (unsigned i = 0; i < curve.size(); ++i) {
344 double x = curve[i].first;
345 double y = curve[i].second;
347 #if USE_LOGISTIC_DISTRIBUTION
348 dbeta[i] = y - A * sech2(l_const * (x-mu)/sigma);
350 dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
355 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
356 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
359 solve_matrix<3>(matATA, dlambda, matATdb);
365 // terminate when we're down to three digits
366 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
371 sigma_result = sigma;
374 static void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma)
376 vector<pair<double, double> > curve;
378 if (score1 > score2) {
379 compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, curve);
381 compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, curve);
384 // multiply in the gaussian
385 for (unsigned i = 0; i < curve.size(); ++i) {
386 double r1 = curve[i].first;
389 double z = (r1 - mu1) / sigma1;
390 #if USE_LOGISTIC_DISTRIBUTION
391 double ch = cosh(l_const * z);
392 curve[i].second /= (ch * ch);
394 double gaussian = exp(-(z*z/2.0));
395 curve[i].second *= gaussian;
399 double mu_est, sigma_est;
401 estimate_musigma(curve, mu_est, sigma_est);
402 least_squares(curve, mu_est, sigma_est, mu, sigma);
405 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)
407 vector<pair<double, double> > curve, newcurve;
408 double mu_t = mu3 + mu4;
409 double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
411 if (score1 > score2) {
412 compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, curve);
414 compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, curve);
417 newcurve.reserve(curve.size());
420 double h = 3000.0 / curve.size();
421 for (unsigned i = 0; i < curve.size(); ++i) {
424 // could be anything, but this is a nice start
425 //double r1 = curve[i].first;
429 #if USE_LOGISTIC_DISTRIBUTION
430 double invsigma2 = 1.0 / sigma2;
432 double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
434 for (unsigned j = 0; j < curve.size(); ++j) {
435 double r1plusr2 = curve[j].first;
436 double r2 = r1plusr2 - r1;
438 #if USE_LOGISTIC_DISTRIBUTION
439 double z = (r2 - mu2) * invsigma2;
440 double gaussian = sech2(l_const * z);
442 double z = (r2 - mu2) * invsq2sigma2;
443 double gaussian = exp(-z*z);
445 sum += curve[j].second * gaussian;
448 #if USE_LOGISTIC_DISTRIBUTION
449 double z = (r1 - mu1) / sigma1;
450 double gaussian = sech2(l_const * z);
452 double z = (r1 - mu1) / sigma1;
453 double gaussian = exp(-(z*z/2.0));
455 newcurve.push_back(make_pair(r1, gaussian * sum));
459 double mu_est, sigma_est;
461 estimate_musigma(newcurve, mu_est, sigma_est);
462 least_squares(newcurve, mu_est, sigma_est, mu, sigma);
465 int main(int argc, char **argv)
467 FILE *fp = fopen("fftw-wisdom", "rb");
469 fftw_import_wisdom_from_file(fp);
473 double mu1 = atof(argv[1]);
474 double sigma1 = atof(argv[2]);
475 double mu2 = atof(argv[3]);
476 double sigma2 = atof(argv[4]);
479 double mu3 = atof(argv[5]);
480 double sigma3 = atof(argv[6]);
481 double mu4 = atof(argv[7]);
482 double sigma4 = atof(argv[8]);
483 int score1 = atoi(argv[9]);
484 int score2 = atoi(argv[10]);
486 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, mu, sigma);
487 printf("%f %f\n", mu, sigma);
488 } else if (argc > 8) {
489 double mu3 = atof(argv[5]);
490 double sigma3 = atof(argv[6]);
491 double mu4 = atof(argv[7]);
492 double sigma4 = atof(argv[8]);
493 int k = atoi(argv[9]);
495 // assess all possible scores
496 for (int i = 0; i < k; ++i) {
497 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
498 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
499 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, newmu1_1, newsigma1_1);
500 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, newmu1_2, newsigma1_2);
501 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, newmu2_1, newsigma2_1);
502 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, newmu2_2, newsigma2_2);
503 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
504 k, i, prob_score(k, i, mu3+mu4-(mu1+mu2)), newmu1_1-mu1, newmu1_2-mu2,
505 newmu2_1-mu3, newmu2_2-mu4);
507 for (int i = k; i --> 0; ) {
508 double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
509 double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
510 compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, newmu1_1, newsigma1_1);
511 compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, newmu1_2, newsigma1_2);
512 compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, newmu2_1, newsigma2_1);
513 compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, newmu2_2, newsigma2_2);
514 printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
515 i, k, prob_score(k, i, mu1+mu2-(mu3+mu4)), newmu1_1-mu1, newmu1_2-mu2,
516 newmu2_1-mu3, newmu2_2-mu4);
518 } else if (argc > 6) {
519 int score1 = atoi(argv[5]);
520 int score2 = atoi(argv[6]);
522 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
523 printf("%f %f\n", mu, sigma);
525 int k = atoi(argv[5]);
527 // assess all possible scores
528 for (int i = 0; i < k; ++i) {
529 double newmu1, newmu2, newsigma1, newsigma2;
530 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
531 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
532 printf("%u-%u,%f,%+f,%+f\n",
533 k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
535 for (int i = k; i --> 0; ) {
536 double newmu1, newmu2, newsigma1, newsigma2;
537 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
538 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
539 printf("%u-%u,%f,%+f,%+f\n",
540 i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);
544 fp = fopen("fftw-wisdom", "wb");
546 fftw_export_wisdom_to_file(fp);