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 // probability of match ending k-a (k>a) when winnerR - loserR = RD
27 // | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
32 // where lambda1 = 1.0, lambda2 = 2^(rd/455)
34 // The constant of 455 is chosen carefully so to match with the
35 // Glicko/Bradley-Terry assumption that a player rated 400 points over
36 // his/her opponent will win with a probability of 10/11 =~ 0.90909.
38 double prob_score(int k, double a, double rd)
40 return prob_score_real(k, a, prodai(k, a), fac(k-1), rd/rating_constant);
43 // Same, but takes in Product(a+i, i=1..k-1) and (k-1)! as an argument in
44 // addition to a. Faster if you already have that precomputed, and assumes rd
45 // is already divided by 455.
46 double prob_score_real(int k, double a, double prodai, double kfac, double rd_norm)
48 double nom = prodai * pow(2.0, -rd_norm * a);
49 double denom = kfac * pow(1.0 + pow(2.0, -rd_norm), k+a);
53 // Calculates Product(a+i, i=1..k-1) (see above).
54 double prodai(int k, double a)
57 for (int i = 1; i < k; ++i)
65 for (int i = 2; i <= x; ++i)
71 // Computes the integral
76 // | ProbScore[a] (r2-r1) Gaussian[mu2, sigma2] (dr2) dr2
81 // For practical reasons, -inf and +inf are replaced by 0 and 3000, which
82 // is reasonable in the this context.
84 // The Gaussian is not normalized.
86 // Set the last parameter to 1.0 if player 1 won, or -1.0 if player 2 won.
87 // In the latter case, ProbScore will be given (r1-r2) instead of (r2-r1).
89 static inline double evaluate_int_point(int k, double a, double prodai_precompute, double kfac_precompute, double r1, double mu2, double sigma2, double winfac, double x);
91 double opponent_rating_pdf(int k, double a, double r1, double mu2, double sigma2, double winfac)
93 double prodai_precompute = prodai(k, a);
94 double kfac_precompute = fac(k-1);
95 winfac /= rating_constant;
97 int n = int(3000.0 / int_step_size + 0.5);
98 double h = 3000.0 / double(n);
99 double sum = evaluate_int_point(k, a, prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac, 0.0);
101 for (int i = 1; i < n; i += 2) {
102 sum += 4.0 * evaluate_int_point(k, a, prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac, i * h);
104 for (int i = 2; i < n; i += 2) {
105 sum += 2.0 * evaluate_int_point(k, a, prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac, i * h);
107 sum += evaluate_int_point(k, a, prodai_precompute, kfac_precompute, r1, mu2, sigma2, winfac, 3000.0);
109 return (h/3.0) * sum;
112 static inline double evaluate_int_point(int k, double a, double prodai_precompute, double kfac_precompute, double r1, double mu2, double sigma2, double winfac, double x)
114 double probscore = prob_score_real(k, a, prodai_precompute, kfac_precompute, (r1 - x)*winfac);
115 double z = (x - mu2)/sigma2;
116 double gaussian = exp(-(z*z/2.0));
117 return probscore * gaussian;
120 // normalize the curve so we know that A ~= 1
121 void normalize(vector<pair<double, double> > &curve)
124 for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
125 peak = max(peak, i->second);
128 double invpeak = 1.0 / peak;
129 for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
130 i->second *= invpeak;
134 // computes matA * matB
135 void mat_mul(double *matA, unsigned ah, unsigned aw,
136 double *matB, unsigned bh, unsigned bw,
140 for (unsigned y = 0; y < bw; ++y) {
141 for (unsigned x = 0; x < ah; ++x) {
143 for (unsigned c = 0; c < aw; ++c) {
144 sum += matA[c*ah + x] * matB[y*bh + c];
146 result[y*bw + x] = sum;
151 // computes matA^T * matB
152 void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
153 double *matB, unsigned bh, unsigned bw,
157 for (unsigned y = 0; y < bw; ++y) {
158 for (unsigned x = 0; x < aw; ++x) {
160 for (unsigned c = 0; c < ah; ++c) {
161 sum += matA[x*ah + c] * matB[y*bh + c];
163 result[y*bw + x] = sum;
168 void print3x3(double *M)
170 printf("%f %f %f\n", M[0], M[3], M[6]);
171 printf("%f %f %f\n", M[1], M[4], M[7]);
172 printf("%f %f %f\n", M[2], M[5], M[8]);
175 void print3x1(double *M)
177 printf("%f\n", M[0]);
178 printf("%f\n", M[1]);
179 printf("%f\n", M[2]);
182 // solves Ax = B by Gauss-Jordan elimination, where A is a 3x3 matrix,
183 // x is a column vector of length 3 and B is a row vector of length 3.
184 // Destroys its input in the process.
185 void solve3x3(double *A, double *x, double *B)
187 // row 1 -= row 0 * (a1/a0)
189 double f = A[1] / A[0];
197 // row 2 -= row 0 * (a2/a0)
199 double f = A[2] / A[0];
207 // row 2 -= row 1 * (a5/a4)
209 double f = A[5] / A[4];
218 // row 1 -= row 2 * (a7/a8)
220 double f = A[7] / A[8];
226 // row 0 -= row 2 * (a6/a8)
228 double f = A[6] / A[8];
234 // row 0 -= row 1 * (a3/a4)
236 double f = A[3] / A[4];
248 // Give an OK starting estimate for the least squares, by numerical integration
249 // of statistical moments.
250 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 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;
318 matA[i + 0 * curve.size()] =
319 exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
322 matA[i + 1 * curve.size()] =
323 A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
326 matA[i + 2 * curve.size()] =
327 matA[i + 1 * curve.size()] * (x-mu)/sigma;
331 for (unsigned i = 0; i < curve.size(); ++i) {
332 double x = curve[i].first;
333 double y = curve[i].second;
335 dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
339 mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
340 mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
343 solve3x3(matATA, dlambda, matATdb);
349 // terminate when we're down to three digits
350 if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
355 sigma_result = sigma;
358 void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma)
360 vector<pair<double, double> > curve;
362 if (score1 > score2) {
363 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
364 double z = (r1 - mu1) / sigma1;
365 double gaussian = exp(-(z*z/2.0));
366 curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score1, score2, r1, mu2, sigma2, 1.0)));
369 for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) {
370 double z = (r1 - mu1) / sigma1;
371 double gaussian = exp(-(z*z/2.0));
372 curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score2, score1, r1, mu2, sigma2, -1.0)));
376 double mu_est, sigma_est;
378 estimate_musigma(curve, mu_est, sigma_est);
379 least_squares(curve, mu_est, sigma_est, mu, sigma);
382 int main(int argc, char **argv)
384 double mu1 = atof(argv[1]);
385 double sigma1 = atof(argv[2]);
386 double mu2 = atof(argv[3]);
387 double sigma2 = atof(argv[4]);
390 int score1 = atoi(argv[5]);
391 int score2 = atoi(argv[6]);
393 compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
394 printf("%f %f\n", mu, sigma);
396 int k = atoi(argv[5]);
398 // assess all possible scores
399 for (int i = 0; i < k; ++i) {
400 double newmu1, newmu2, newsigma1, newsigma2;
401 compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
402 compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
403 printf("%u-%u,%f,%+f,%+f\n",
404 k, i, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2);
406 for (int i = k; i --> 0; ) {
407 double newmu1, newmu2, newsigma1, newsigma2;
408 compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
409 compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
410 printf("%u-%u,%f,%+f,%+f\n",
411 i, k, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);