9 static const double int_step_size = 50.0;
10 static const double pdf_step_size = 10.0;
12 // rating constant (see below)
13 static const double rating_constant = 455.0;
17 double prob_score(int k, double a, double rd);
18 double prob_score_real(int k, double a, double binomial, double rd_norm);
19 double prodai(int k, double a);
22 // Numerical integration using Simpson's rule
24 double simpson_integrate(const T &evaluator, double from, double to, double step)
26 int n = int((to - from) / step + 0.5);
27 double h = (to - from) / n;
28 double sum = evaluator(from);
30 for (int i = 1; i < n; i += 2) {
31 sum += 4.0 * evaluator(from + i * h);
33 for (int i = 2; i < n; i += 2) {
34 sum += 2.0 * evaluator(from + i * h);
41 // probability of match ending k-a (k>a) when winnerR - loserR = RD
46 // | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
51 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
53 // The constant of 455 is chosen carefully so to match with the
54 // Glicko/Bradley-Terry assumption that a player rated 400 points over
55 // his/her opponent will win with a probability of 10/11 =~ 0.90909.
57 double prob_score(int k, double a, double rd)
59 return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant);
62 // Same, but takes in binomial(a+k-1, k-1) as an argument in
63 // addition to a. Faster if you already have that precomputed, and assumes rd
64 // is already divided by 455.
65 double prob_score_real(int k, double a, double binomial, double rd_norm)
67 double nom = binomial * pow(2.0, rd_norm * a);
68 double denom = pow(1.0 + pow(2.0, rd_norm), k+a);
72 // Calculates Product(a+i, i=1..k-1) (see above).
73 double prodai(int k, double a)
76 for (int i = 1; i < k; ++i)
84 for (int i = 2; i <= x; ++i)
90 // Computes the integral
95 // | ProbScore[a] (r2-r1) Gaussian[mu2, sigma2] (dr2) dr2
100 // For practical reasons, -inf and +inf are replaced by 0 and 3000, which
101 // is reasonable in the this context.
103 // The Gaussian is not normalized.
105 // Set the last parameter to 1.0 if player 1 won, or -1.0 if player 2 won.
106 // In the latter case, ProbScore will be given (r1-r2) instead of (r2-r1).
108 class ProbScoreEvaluator {
112 double binomial_precompute, r1, mu2, sigma2, winfac;
115 ProbScoreEvaluator(int k, double a, double binomial_precompute, double r1, double mu2, double sigma2, double winfac)
116 : k(k), a(a), binomial_precompute(binomial_precompute), r1(r1), mu2(mu2), sigma2(sigma2), winfac(winfac) {}
117 inline double operator() (double x) const
119 double probscore = prob_score_real(k, a, binomial_precompute, (x - r1)*winfac);
120 double z = (x - mu2)/sigma2;
121 double gaussian = exp(-(z*z/2.0));
122 return probscore * gaussian;
126 double opponent_rating_pdf(int k, double a, double r1, double mu2, double sigma2, double winfac)
128 double binomial_precompute = prodai(k, a) / fac(k-1);
129 winfac /= rating_constant;
131 return simpson_integrate(ProbScoreEvaluator(k, a, binomial_precompute, r1, mu2, sigma2, winfac), 0.0, 3000.0, int_step_size);
134 // normalize the curve so we know that A ~= 1
135 void normalize(vector<pair<double, double> > &curve)
138 for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
139 peak = max(peak, i->second);
142 double invpeak = 1.0 / peak;
143 for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
144 i->second *= invpeak;
148 // computes matA * matB
149 void mat_mul(double *matA, unsigned ah, unsigned aw,
150 double *matB, unsigned bh, unsigned bw,
154 for (unsigned y = 0; y < bw; ++y) {
155 for (unsigned x = 0; x < ah; ++x) {
157 for (unsigned c = 0; c < aw; ++c) {
158 sum += matA[c*ah + x] * matB[y*bh + c];
160 result[y*bw + x] = sum;
165 // computes matA^T * matB
166 void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
167 double *matB, unsigned bh, unsigned bw,
171 for (unsigned y = 0; y < bw; ++y) {
172 for (unsigned x = 0; x < aw; ++x) {
174 for (unsigned c = 0; c < ah; ++c) {
175 sum += matA[x*ah + c] * matB[y*bh + c];
177 result[y*bw + x] = sum;
182 void print3x3(double *M)
184 printf("%f %f %f\n", M[0], M[3], M[6]);
185 printf("%f %f %f\n", M[1], M[4], M[7]);
186 printf("%f %f %f\n", M[2], M[5], M[8]);
189 void print3x1(double *M)
191 printf("%f\n", M[0]);
192 printf("%f\n", M[1]);
193 printf("%f\n", M[2]);
196 // solves Ax = B by Gauss-Jordan elimination, where A is a 3x3 matrix,
197 // x is a column vector of length 3 and B is a row vector of length 3.
198 // Destroys its input in the process.
199 void solve3x3(double *A, double *x, double *B)
201 // row 1 -= row 0 * (a1/a0)
203 double f = A[1] / A[0];
211 // row 2 -= row 0 * (a2/a0)
213 double f = A[2] / A[0];
221 // row 2 -= row 1 * (a5/a4)
223 double f = A[5] / A[4];
232 // row 1 -= row 2 * (a7/a8)
234 double f = A[7] / A[8];
240 // row 0 -= row 2 * (a6/a8)
242 double f = A[6] / A[8];
248 // row 0 -= row 1 * (a3/a4)
250 double f = A[3] / A[4];
262 // Give an OK starting estimate for the least squares, by numerical integration
263 // of statistical moments.
264 void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
266 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
268 double area = curve.front().second;
269 double ex = curve.front().first * curve.front().second;
270 double ex2 = curve.front().first * curve.front().first * curve.front().second;
272 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
273 double x = curve[i].first;
274 double y = curve[i].second;
277 ex2 += 4.0 * x * x * y;
279 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
280 double x = curve[i].first;
281 double y = curve[i].second;
284 ex2 += 2.0 * x * x * y;
287 area += curve.back().second;
288 ex += curve.back().first * curve.back().second;
289 ex2 += curve.back().first * curve.back().first * curve.back().second;
291 area = (h/3.0) * area;
292 ex = (h/3.0) * ex / area;
293 ex2 = (h/3.0) * ex2 / area;
296 sigma_result = sqrt(ex2 - ex * ex);
299 // Find best fit of the data in curves to a Gaussian pdf, based on the
300 // given initial estimates. Works by nonlinear least squares, iterating
301 // until we're below a certain threshold.
303 // Note that the algorithm blows up quite hard if the initial estimate is
304 // not good enough. Use estimate_musigma to get a reasonable starting
306 void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
310 double sigma = sigma1;
313 double matA[curve.size() * 3]; // N x 3
314 double dbeta[curve.size()]; // N x 1
316 // A^T * A: 3xN * Nx3 = 3x3
319 // A^T * dβ: 3xN * Nx1 = 3x1
325 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
327 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
328 for (unsigned i = 0; i < curve.size(); ++i) {
329 double x = curve[i].first;
332 matA[i + 0 * curve.size()] =
333 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
336 matA[i + 1 * curve.size()] =
337 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
340 matA[i + 2 * curve.size()] =
341 matA[i + 1 * curve.size()] * (x-mu)/sigma;
345 for (unsigned i = 0; i < curve.size(); ++i) {
346 double x = curve[i].first;
347 double y = curve[i].second;
349 dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
353 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
354 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
357 solve3x3(matATA, dlambda, matATdb);
363 // terminate when we're down to three digits
364 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
369 sigma_result = sigma;
372 void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma)
374 vector<pair<double, double> > curve;
376 if (score1 > score2) {
377 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
378 double z = (r1 - mu1) / sigma1;
379 double gaussian = exp(-(z*z/2.0));
380 curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score1, score2, r1, mu2, sigma2, 1.0)));
383 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
384 double z = (r1 - mu1) / sigma1;
385 double gaussian = exp(-(z*z/2.0));
386 curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score2, score1, r1, mu2, sigma2, -1.0)));
390 double mu_est, sigma_est;
392 estimate_musigma(curve, mu_est, sigma_est);
393 least_squares(curve, mu_est, sigma_est, mu, sigma);
396 int main(int argc, char **argv)
398 double mu1 = atof(argv[1]);
399 double sigma1 = atof(argv[2]);
400 double mu2 = atof(argv[3]);
401 double sigma2 = atof(argv[4]);
404 int score1 = atoi(argv[5]);
405 int score2 = atoi(argv[6]);
407 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
408 printf("%f %f\n", mu, sigma);
410 int k = atoi(argv[5]);
412 // assess all possible scores
413 for (int i = 0; i < k; ++i) {
414 double newmu1, newmu2, newsigma1, newsigma2;
415 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
416 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
417 printf("%u-%u,%f,%+f,%+f\n",
418 k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
420 for (int i = k; i --> 0; ) {
421 double newmu1, newmu2, newsigma1, newsigma2;
422 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
423 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
424 printf("%u-%u,%f,%+f,%+f\n",
425 i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);