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 prodai, double kfac, 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 Product(a+i, i=1..k-1) and (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 prodai, double kfac, double rd_norm)
67 double nom = prodai * pow(2.0, rd_norm * a);
68 double denom = kfac * 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 prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac;
115 ProbScoreEvaluator(int k, double a, double prodai_precompute, double kfac_precompute, double r1, double mu2, double sigma2, double winfac)
116 : k(k), a(a), prodai_precompute(prodai_precompute), kfac_precompute(kfac_precompute), r1(r1), mu2(mu2), sigma2(sigma2), winfac(winfac) {}
117 inline double operator() (double x) const
119 double probscore = prob_score_real(k, a, prodai_precompute, kfac_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 prodai_precompute = prodai(k, a);
129 double kfac_precompute = fac(k-1);
130 winfac /= rating_constant;
132 return simpson_integrate(ProbScoreEvaluator(k, a, prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac), 0.0, 3000.0, int_step_size);
135 // normalize the curve so we know that A ~= 1
136 void normalize(vector<pair<double, double> > &curve)
139 for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
140 peak = max(peak, i->second);
143 double invpeak = 1.0 / peak;
144 for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
145 i->second *= invpeak;
149 // computes matA * matB
150 void mat_mul(double *matA, unsigned ah, unsigned aw,
151 double *matB, unsigned bh, unsigned bw,
155 for (unsigned y = 0; y < bw; ++y) {
156 for (unsigned x = 0; x < ah; ++x) {
158 for (unsigned c = 0; c < aw; ++c) {
159 sum += matA[c*ah + x] * matB[y*bh + c];
161 result[y*bw + x] = sum;
166 // computes matA^T * matB
167 void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
168 double *matB, unsigned bh, unsigned bw,
172 for (unsigned y = 0; y < bw; ++y) {
173 for (unsigned x = 0; x < aw; ++x) {
175 for (unsigned c = 0; c < ah; ++c) {
176 sum += matA[x*ah + c] * matB[y*bh + c];
178 result[y*bw + x] = sum;
183 void print3x3(double *M)
185 printf("%f %f %f\n", M[0], M[3], M[6]);
186 printf("%f %f %f\n", M[1], M[4], M[7]);
187 printf("%f %f %f\n", M[2], M[5], M[8]);
190 void print3x1(double *M)
192 printf("%f\n", M[0]);
193 printf("%f\n", M[1]);
194 printf("%f\n", M[2]);
197 // solves Ax = B by Gauss-Jordan elimination, where A is a 3x3 matrix,
198 // x is a column vector of length 3 and B is a row vector of length 3.
199 // Destroys its input in the process.
200 void solve3x3(double *A, double *x, double *B)
202 // row 1 -= row 0 * (a1/a0)
204 double f = A[1] / A[0];
212 // row 2 -= row 0 * (a2/a0)
214 double f = A[2] / A[0];
222 // row 2 -= row 1 * (a5/a4)
224 double f = A[5] / A[4];
233 // row 1 -= row 2 * (a7/a8)
235 double f = A[7] / A[8];
241 // row 0 -= row 2 * (a6/a8)
243 double f = A[6] / A[8];
249 // row 0 -= row 1 * (a3/a4)
251 double f = A[3] / A[4];
263 // Give an OK starting estimate for the least squares, by numerical integration
264 // of statistical moments.
265 void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
267 double h = (curve.back().first - curve.front().first) / (curve.size() - 1);
269 double area = curve.front().second;
270 double ex = curve.front().first * curve.front().second;
271 double ex2 = curve.front().first * curve.front().first * curve.front().second;
273 for (unsigned i = 1; i < curve.size() - 1; i += 2) {
274 double x = curve[i].first;
275 double y = curve[i].second;
278 ex2 += 4.0 * x * x * y;
280 for (unsigned i = 2; i < curve.size() - 1; i += 2) {
281 double x = curve[i].first;
282 double y = curve[i].second;
285 ex2 += 2.0 * x * x * y;
288 area += curve.back().second;
289 ex += curve.back().first * curve.back().second;
290 ex2 += curve.back().first * curve.back().first * curve.back().second;
292 area = (h/3.0) * area;
293 ex = (h/3.0) * ex / area;
294 ex2 = (h/3.0) * ex2 / area;
297 sigma_result = sqrt(ex2 - ex * ex);
300 // Find best fit of the data in curves to a Gaussian pdf, based on the
301 // given initial estimates. Works by nonlinear least squares, iterating
302 // until we're below a certain threshold.
304 // Note that the algorithm blows up quite hard if the initial estimate is
305 // not good enough. Use estimate_musigma to get a reasonable starting
307 void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
311 double sigma = sigma1;
314 double matA[curve.size() * 3]; // N x 3
315 double dbeta[curve.size()]; // N x 1
317 // A^T * A: 3xN * Nx3 = 3x3
320 // A^T * dβ: 3xN * Nx1 = 3x1
326 //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
328 // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
329 for (unsigned i = 0; i < curve.size(); ++i) {
330 double x = curve[i].first;
333 matA[i + 0 * curve.size()] =
334 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
337 matA[i + 1 * curve.size()] =
338 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
341 matA[i + 2 * curve.size()] =
342 matA[i + 1 * curve.size()] * (x-mu)/sigma;
346 for (unsigned i = 0; i < curve.size(); ++i) {
347 double x = curve[i].first;
348 double y = curve[i].second;
350 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 solve3x3(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)
375 vector<pair<double, double> > curve;
377 if (score1 > score2) {
378 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
379 double z = (r1 - mu1) / sigma1;
380 double gaussian = exp(-(z*z/2.0));
381 curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score1, score2, r1, mu2, sigma2, 1.0)));
384 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
385 double z = (r1 - mu1) / sigma1;
386 double gaussian = exp(-(z*z/2.0));
387 curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score2, score1, r1, mu2, sigma2, -1.0)));
391 double mu_est, sigma_est;
393 estimate_musigma(curve, mu_est, sigma_est);
394 least_squares(curve, mu_est, sigma_est, mu, sigma);
397 int main(int argc, char **argv)
399 double mu1 = atof(argv[1]);
400 double sigma1 = atof(argv[2]);
401 double mu2 = atof(argv[3]);
402 double sigma2 = atof(argv[4]);
405 int score1 = atoi(argv[5]);
406 int score2 = atoi(argv[6]);
408 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
409 printf("%f %f\n", mu, sigma);
411 int k = atoi(argv[5]);
413 // assess all possible scores
414 for (int i = 0; i < k; ++i) {
415 double newmu1, newmu2, newsigma1, newsigma2;
416 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
417 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
418 printf("%u-%u,%f,%+f,%+f\n",
419 k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
421 for (int i = k; i --> 0; ) {
422 double newmu1, newmu2, newsigma1, newsigma2;
423 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
424 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
425 printf("%u-%u,%f,%+f,%+f\n",
426 i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);