]> git.sesse.net Git - foosball/blobdiff - foosrank.cpp
Make the initial rating/RD constants instead of hard-coded values.
[foosball] / foosrank.cpp
index bdb54f2324b3e6a95e626cf035474fb9c1f97b1f..67aee581cbe05f2d8d2064d5e997ab262453706b 100644 (file)
@@ -8,12 +8,19 @@
 #include <complex>
 #include <fftw3.h>
 
+#define USE_LOGISTIC_DISTRIBUTION 0
+
 // step sizes
 static const double int_step_size = 75.0;
 
 // rating constant (see below)
 static const double rating_constant = 455.0;
 
+#if USE_LOGISTIC_DISTRIBUTION
+// constant used in the logistic pdf
+static const double l_const = M_PI / (2.0 * sqrt(3.0));
+#endif
+
 using namespace std;
 
 static double prob_score(int k, int a, double rd);
@@ -21,6 +28,14 @@ static double prob_score_real(int k, int a, double binomial, double rd_norm);
 static double prodai(int k, int a);
 static double fac(int x);
 
+#if USE_LOGISTIC_DISTRIBUTION
+// sech²(x)
+static double sech2(double x)
+{
+       double c = cosh(x);
+       return 1.0 / (c*c);
+}
+#endif
 
 // probability of match ending k-a (k>a) when winnerR - loserR = RD
 //
@@ -122,10 +137,23 @@ static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2,
                func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0;
        }
 
+#if USE_LOGISTIC_DISTRIBUTION
+       double invsigma2 = 1.0 / sigma2;
+#else
+       double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
+#endif
        for (int i = 0; i < sz; ++i) {
                double x1 = 0.0 + h*i;
-               double z = (x1 - mu2)/sigma2;
-               func1[i].real() = exp(-(z*z/2.0));
+
+               // opponent's pdf
+#if USE_LOGISTIC_DISTRIBUTION
+               double z = (x1 - mu2) * invsigma2;
+               double ch = cosh(l_const * z);
+               func1[i].real() = 1.0 / (ch * ch);
+#else
+               double z = (x1 - mu2) * invsq2sigma2;
+               func1[i].real() = exp(-z*z);
+#endif
 
                double x2 = -3000.0 + h*i;
                func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac);
@@ -140,6 +168,8 @@ static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2,
                res[i] = func1[i] * func2[i];
        }
        fftw_execute(b);
+
+       result.reserve(sz);
        for (int i = 0; i < sz; ++i) {
                double r1 = i*h;
                result.push_back(make_pair(r1, abs(res[i])));
@@ -177,70 +207,41 @@ static void mat_mul_trans(double *matA, unsigned ah, unsigned aw,
        }
 }
 
-// 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.
+// solves Ax = B by Gauss-Jordan elimination, where A is an NxN matrix,
+// x is a column vector of length N and B is a row vector of length N.
 // Destroys its input in the process.
-static void solve3x3(double *A, double *x, double *B)
+template<int N>
+static void solve_matrix(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;
+       for (int i = 0; i < N; ++i) {
+               for (int j = i+1; j < N; ++j) {
+                       // row j -= row i * (a[i,j] / a[i,i])
+                       double f = A[j+i*N] / A[i+i*N];
+
+                       A[j+i*N] = 0.0;
+                       for (int k = i+1; k < N; ++k) {
+                               A[j+k*N] -= A[i+k*N] * f;
+                       }
 
-               B[0] -= B[2] * f;
+                       B[j] -= B[i] * f;
+               }
        }
 
-       // row 0 -= row 1 * (a3/a4)
-       {
-               double f = A[3] / A[4];
-               A[3] = 0.0;
-
-               B[0] -= B[1] * f;
+       // back-substitute
+       for (int i = N; i --> 0; ) {
+               for (int j = i; j --> 0; ) {
+                       // row j -= row i * (a[j,j] / a[j,i])
+                       double f = A[i+j*N] / A[j+j*N];
+                       
+                       // A[j+i*N] = 0.0;
+                       B[j] -= B[i] * f;
+               }
        }
 
        // normalize
-       x[0] = B[0] / A[0];
-       x[1] = B[1] / A[4];
-       x[2] = B[2] / A[8];
+       for (int i = 0; i < N; ++i) {
+               x[i] = B[i] / A[i+i*N];
+       }
 }
 
 // Give an OK starting estimate for the least squares, by numerical integration
@@ -312,17 +313,30 @@ static void least_squares(vector<pair<double, double> > &curve, double mu1, doub
                for (unsigned i = 0; i < curve.size(); ++i) {
                        double x = curve[i].first;
 
+#if USE_LOGISTIC_DISTRIBUTION
+                       // df/dA(x_i)
+                       matA[i + 0 * curve.size()] = sech2(l_const * (x-mu)/sigma);
+
+                       // df/dµ(x_i)
+                       matA[i + 1 * curve.size()] = 2.0 * l_const * A * matA[i + 0 * curve.size()]
+                               * tanh(l_const * (x-mu)/sigma) / sigma;
+
+                       // df/dσ(x_i)
+                       matA[i + 2 * curve.size()] = 
+                               matA[i + 1 * curve.size()] * (x-mu)/sigma;
+#else
                        // 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()] = 
+                       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;
+#endif
                }
 
                // find dβ
@@ -330,7 +344,11 @@ static void least_squares(vector<pair<double, double> > &curve, double mu1, doub
                        double x = curve[i].first;
                        double y = curve[i].second;
 
+#if USE_LOGISTIC_DISTRIBUTION
+                       dbeta[i] = y - A * sech2(l_const * (x-mu)/sigma);
+#else
                        dbeta[i] = y - A * exp(- (x-mu)*(x-mu)/(2.0*sigma*sigma));
+#endif
                }
 
                // compute a and b
@@ -338,7 +356,7 @@ static void least_squares(vector<pair<double, double> > &curve, double mu1, doub
                mat_mul_trans(matA, curve.size(), 3, dbeta, curve.size(), 1, matATdb);
 
                // solve
-               solve3x3(matATA, dlambda, matATdb);
+               solve_matrix<3>(matATA, dlambda, matATdb);
 
                A += dlambda[0];
                mu += dlambda[1];
@@ -366,9 +384,16 @@ static void compute_new_rating(double mu1, double sigma1, double mu2, double sig
        // multiply in the gaussian
        for (unsigned i = 0; i < curve.size(); ++i) {
                double r1 = curve[i].first;
+
+               // my pdf
                double z = (r1 - mu1) / sigma1;
+#if USE_LOGISTIC_DISTRIBUTION
+               double ch = cosh(l_const * z);
+               curve[i].second /= (ch * ch);
+#else
                double gaussian = exp(-(z*z/2.0));
                curve[i].second *= gaussian;
+#endif
        }
 
        double mu_est, sigma_est;
@@ -389,6 +414,8 @@ static void compute_new_double_rating(double mu1, double sigma1, double mu2, dou
                compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, curve);
        }
 
+       newcurve.reserve(curve.size());
+
        // iterate over r1
        double h = 3000.0 / curve.size();
        for (unsigned i = 0; i < curve.size(); ++i) {
@@ -399,17 +426,32 @@ static void compute_new_double_rating(double mu1, double sigma1, double mu2, dou
                double r1 = i * h;
 
                // iterate over r2
+#if USE_LOGISTIC_DISTRIBUTION
+               double invsigma2 = 1.0 / sigma2;
+#else
+               double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2);
+#endif
                for (unsigned j = 0; j < curve.size(); ++j) {
                        double r1plusr2 = curve[j].first;
                        double r2 = r1plusr2 - r1;
 
-                       double z = (r2 - mu2) / sigma2;
-                       double gaussian = exp(-(z*z/2.0));
+#if USE_LOGISTIC_DISTRIBUTION
+                       double z = (r2 - mu2) * invsigma2;
+                       double gaussian = sech2(l_const * z);
+#else  
+                       double z = (r2 - mu2) * invsq2sigma2;
+                       double gaussian = exp(-z*z);
+#endif
                        sum += curve[j].second * gaussian;
                }
 
+#if USE_LOGISTIC_DISTRIBUTION
+               double z = (r1 - mu1) / sigma1;
+               double gaussian = sech2(l_const * z);
+#else
                double z = (r1 - mu1) / sigma1;
                double gaussian = exp(-(z*z/2.0));
+#endif
                newcurve.push_back(make_pair(r1, gaussian * sum));
        }
 
@@ -442,7 +484,11 @@ int main(int argc, char **argv)
                int score2 = atoi(argv[10]);
                double mu, sigma;
                compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, score1, score2, mu, sigma);
-               printf("%f %f\n", mu, sigma);
+               if (score1 > score2) {
+                       printf("%f %f %f\n", mu, sigma, prob_score(score1, score2, mu3+mu4-(mu1+mu2)));
+               } else {
+                       printf("%f %f %f\n", mu, sigma, prob_score(score2, score1, mu1+mu2-(mu3+mu4)));
+               }
        } else if (argc > 8) {
                double mu3 = atof(argv[5]);
                double sigma3 = atof(argv[6]);
@@ -478,7 +524,12 @@ int main(int argc, char **argv)
                int score2 = atoi(argv[6]);
                double mu, sigma;
                compute_new_rating(mu1, sigma1, mu2, sigma2, score1, score2, mu, sigma);
-               printf("%f %f\n", mu, sigma);
+
+               if (score1 > score2) {
+                       printf("%f %f %f\n", mu, sigma, prob_score(score1, score2, mu2-mu1));
+               } else {
+                       printf("%f %f %f\n", mu, sigma, prob_score(score2, score1, mu1-mu2));
+               }
        } else {
                int k = atoi(argv[5]);