+#include <stdio.h>
+#include <math.h>
+#include <assert.h>
+
+#include <vector>
+#include <algorithm>
+
+// integration step size
+static const double step_size = 10.0;
+
+using namespace std;
+
+double prob_score(double a, double rd);
+double prob_score_real(double a, double prodai, double rd_norm);
+double prodai(double a);
+
+// probability of match ending 10-a when winnerR - loserR = RD
+//
+// +inf
+// /
+// |
+// | Poisson[lambda1, t](a) * Erlang[lambda2, 10](t) dt
+// |
+// /
+// -inf
+//
+// where lambda1 = 1.0, lambda2 = 2^(rd/455)
+//
+// The constant of 455 is chosen carefully so to match with the
+// Glicko/Bradley-Terry assumption that a player rated 400 points over
+// his/her opponent will win with a probability of 10/11 =~ 0.90909.
+//
+double prob_score(double a, double rd)
+{
+ return prob_score_real(a, prodai(a), rd/455.0);
+}
+
+// Same, but takes in Product(a+i, i=1..9) as an argument in addition to a. Faster
+// if you already have that precomputed, and assumes rd is already divided by 455.
+double prob_score_real(double a, double prodai, double rd_norm)
+{
+ double nom =
+ pow(2.0, -a*rd_norm) * pow(2.0, 10.0*rd_norm) * pow(pow(2.0, -rd_norm) + 1.0, -a)
+ * prodai;
+ double denom = 362880 * pow(1.0 + pow(2.0, rd_norm), 10.0);
+ return nom/denom;
+}
+
+// Calculates Product(a+i, i=1..9) (see above).
+double prodai(double a)
+{
+ return (a+1)*(a+2)*(a+3)*(a+4)*(a+5)*(a+6)*(a+7)*(a+8)*(a+9);
+}
+
+//
+// Computes the integral
+//
+// +inf
+// /
+// |
+// | ProbScore[a] (r2-r1) Gaussian[mu2, sigma2] (dr2) dr2
+// |
+// /
+// -inf
+//
+// For practical reasons, -inf and +inf are replaced by 0 and 3000, which
+// is reasonable in the this context.
+//
+// The Gaussian is not normalized.
+//
+// Set the last parameter to 1.0 if player 1 won, or -1.0 if player 2 won.
+// In the latter case, ProbScore will be given (r1-r2) instead of (r2-r1).
+//
+double opponent_rating_pdf(double a, double r1, double mu2, double sigma2, double winfac)
+{
+ double sum = 0.0;
+ double prodai_precompute = prodai(a);
+ winfac /= 455.0;
+ for (double r2 = 0.0; r2 < 3000.0; r2 += step_size) {
+ double x = r2 + step_size*0.5;
+ double probscore = prob_score_real(a, prodai_precompute, (r1 - x)*winfac);
+ double z = (x - mu2)/sigma2;
+ double gaussian = exp(-(z*z/2.0));
+ sum += step_size * probscore * gaussian;
+ }
+ return sum;
+}
+
+// normalize the curve so we know that A ~= 1
+void normalize(vector<pair<double, double> > &curve)
+{
+ double peak = 0.0;
+ for (vector<pair<double, double> >::const_iterator i = curve.begin(); i != curve.end(); ++i) {
+ peak = max(peak, i->second);
+ }
+
+ double invpeak = 1.0 / peak;
+ for (vector<pair<double, double> >::iterator i = curve.begin(); i != curve.end(); ++i) {
+ i->second *= invpeak;
+ }
+}
+
+// computes matA * matB
+void mat_mul(double *matA, unsigned ah, unsigned aw,
+ double *matB, unsigned bh, unsigned bw,
+ double *result)
+{
+ assert(aw == bh);
+ for (unsigned y = 0; y < bw; ++y) {
+ for (unsigned x = 0; x < ah; ++x) {
+ double sum = 0.0;
+ for (unsigned c = 0; c < aw; ++c) {
+ sum += matA[c*ah + x] * matB[y*bh + c];
+ }
+ result[y*bw + x] = sum;
+ }
+ }
+}
+
+// computes matA^T * matB
+void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
+ double *matB, unsigned bh, unsigned bw,
+ double *result)
+{
+ assert(ah == bh);
+ for (unsigned y = 0; y < bw; ++y) {
+ for (unsigned x = 0; x < aw; ++x) {
+ double sum = 0.0;
+ for (unsigned c = 0; c < ah; ++c) {
+ sum += matA[x*ah + c] * matB[y*bh + c];
+ }
+ result[y*bw + x] = sum;
+ }
+ }
+}
+
+void print3x3(double *M)
+{
+ printf("%f %f %f\n", M[0], M[3], M[6]);
+ printf("%f %f %f\n", M[1], M[4], M[7]);
+ printf("%f %f %f\n", M[2], M[5], M[8]);
+}
+
+void print3x1(double *M)
+{
+ printf("%f\n", M[0]);
+ printf("%f\n", M[1]);
+ printf("%f\n", M[2]);
+}
+
+// solves Ax = B by Gauss-Jordan elimination, where A is a 3x3 matrix,
+// x is a column vector of length 3 and B is a row vector of length 3.
+// Destroys its input in the process.
+void solve3x3(double *A, double *x, double *B)
+{
+ // row 1 -= row 0 * (a1/a0)
+ {
+ double f = A[1] / A[0];
+ A[1] = 0.0;
+ A[4] -= A[3] * f;
+ A[7] -= A[6] * f;
+
+ B[1] -= B[0] * f;
+ }
+
+ // row 2 -= row 0 * (a2/a0)
+ {
+ double f = A[2] / A[0];
+ A[2] = 0.0;
+ A[5] -= A[3] * f;
+ A[8] -= A[6] * f;
+
+ B[2] -= B[0] * f;
+ }
+
+ // row 2 -= row 1 * (a5/a4)
+ {
+ double f = A[5] / A[4];
+ A[5] = 0.0;
+ A[8] -= A[7] * f;
+
+ B[2] -= B[1] * f;
+ }
+
+ // back substitute:
+
+ // row 1 -= row 2 * (a7/a8)
+ {
+ double f = A[7] / A[8];
+ A[7] = 0.0;
+
+ B[1] -= B[2] * f;
+ }
+
+ // row 0 -= row 2 * (a6/a8)
+ {
+ double f = A[6] / A[8];
+ A[6] = 0.0;
+
+ B[0] -= B[2] * f;
+ }
+
+ // row 0 -= row 1 * (a3/a4)
+ {
+ double f = A[3] / A[4];
+ A[3] = 0.0;
+
+ B[0] -= B[1] * f;
+ }
+
+ // normalize
+ x[0] = B[0] / A[0];
+ x[1] = B[1] / A[4];
+ x[2] = B[2] / A[8];
+}
+
+// Give an OK starting estimate for the least squares, by numerical integration
+// of x*f(x) and x^2 * f(x). Somehow seems to underestimate sigma, though.
+void estimate_musigma(vector<pair<double, double> > &curve, double &mu_result, double &sigma_result)
+{
+ double mu = 0.0;
+ double sigma = 0.0;
+ double sum_area = 0.0;
+
+ for (unsigned i = 1; i < curve.size(); ++i) {
+ double x1 = curve[i].first;
+ double x0 = curve[i-1].first;
+ double y1 = curve[i].second;
+ double y0 = curve[i-1].second;
+ double xm = 0.5 * (x0 + x1);
+ double ym = 0.5 * (y0 + y1);
+ sum_area += (x1-x0) * ym;
+ mu += (x1-x0) * xm * ym;
+ sigma += (x1-x0) * xm * xm * ym;
+ }
+
+ mu_result = mu / sum_area;
+ sigma_result = sqrt(sigma) / sum_area;
+}
+
+// Find best fit of the data in curves to a Gaussian pdf, based on the
+// given initial estimates. Works by nonlinear least squares, iterating
+// until we're below a certain threshold.
+void least_squares(vector<pair<double, double> > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result)
+{
+ double A = 1.0;
+ double mu = mu1;
+ double sigma = sigma1;
+
+ // column-major
+ double matA[curve.size() * 3]; // N x 3
+ double dbeta[curve.size()]; // N x 1
+
+ // A^T * A: 3xN * Nx3 = 3x3
+ double matATA[3*3];
+
+ // A^T * dβ: 3xN * Nx1 = 3x1
+ double matATdb[3];
+
+ double dlambda[3];
+
+ for ( ;; ) {
+ //printf("A=%f mu=%f sigma=%f\n", A, mu, sigma);
+
+ // fill in A (depends only on x_i, A, mu, sigma -- not y_i)
+ for (unsigned i = 0; i < curve.size(); ++i) {
+ double x = curve[i].first;
+
+ // df/dA(x_i)
+ matA[i + 0 * curve.size()] =
+ exp(-(x-mu)*(x-mu)/(2.0*sigma*sigma));
+
+ // df/dµ(x_i)
+ matA[i + 1 * curve.size()] =
+ A * (x-mu)/(sigma*sigma) * matA[i + 0 * curve.size()];
+
+ // df/dσ(x_i)
+ matA[i + 2 * curve.size()] =
+ matA[i + 1 * curve.size()] * (x-mu)/sigma;
+ }
+
+ // find dβ
+ for (unsigned i = 0; i < curve.size(); ++i) {
+ double x = curve[i].first;
+ double y = curve[i].second;
+
+ dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
+ }
+
+ // compute a and b
+ mat_mul_trans(matA, curve.size(), 3, matA, curve.size(), 3, matATA);
+ mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
+
+ // solve
+ solve3x3(matATA, dlambda, matATdb);
+
+ A += dlambda[0];
+ mu += dlambda[1];
+ sigma += dlambda[2];
+
+ // terminate when we're down to three digits
+ if (fabs(dlambda[0]) <= 1e-3 && fabs(dlambda[1]) <= 1e-3 && fabs(dlambda[2]) <= 1e-3)
+ break;
+ }
+
+ mu_result = mu;
+ sigma_result = sigma;
+}
+
+int main(int argc, char **argv)
+{
+ double mu1 = atof(argv[1]);
+ double sigma1 = atof(argv[2]);
+ double mu2 = atof(argv[3]);
+ double sigma2 = atof(argv[4]);
+ int score1 = atoi(argv[5]);
+ int score2 = atoi(argv[6]);
+ vector<pair<double, double> > curve;
+
+ if (score1 == 10) {
+ for (double r1 = 0.0; r1 < 3000.0; r1 += step_size) {
+ double z = (r1 - mu1) / sigma1;
+ double gaussian = exp(-(z*z/2.0));
+ curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score2, r1, mu2, sigma2, 1.0)));
+ }
+ } else {
+ for (double r1 = 0.0; r1 < 3000.0; r1 += step_size) {
+ double z = (r1 - mu1) / sigma1;
+ double gaussian = exp(-(z*z/2.0));
+ curve.push_back(make_pair(r1, gaussian * opponent_rating_pdf(score1, r1, mu2, sigma2, -1.0)));
+ }
+ }
+
+ double mu_est, sigma_est, mu, sigma;
+ normalize(curve);
+ estimate_musigma(curve, mu_est, sigma_est);
+ least_squares(curve, mu_est, sigma_est, mu, sigma);
+ printf("%f %f\n", mu, sigma);
+}