X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=foosrank.cpp;h=b0f30424ff7e6e7f7040f14356ff2651193e6211;hb=c9813b4e9d96a16129271afee59d47dd4220fd90;hp=cc660764762885eacd62abf9362e7b7cca54e3c3;hpb=1371406e6263f6aaf6856cfc886e01a19f80f000;p=foosball diff --git a/foosrank.cpp b/foosrank.cpp index cc66076..b0f3042 100644 --- a/foosrank.cpp +++ b/foosrank.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include @@ -16,10 +16,10 @@ static const double rating_constant = 455.0; using namespace std; -double prob_score(int k, double a, double rd); -double prob_score_real(int k, double a, double binomial, double rd_norm); -double prodai(int k, double a); -double fac(int x); +static double prob_score(int k, int a, double rd); +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); // probability of match ending k-a (k>a) when winnerR - loserR = RD @@ -38,23 +38,40 @@ double fac(int x); // 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(int k, double a, double rd) +static double prob_score(int k, int a, double rd) { return prob_score_real(k, a, prodai(k, a) / fac(k-1), rd/rating_constant); } +// computes x^a, probably more efficiently than pow(x, a) (but requires that a +// is n unsigned integer) +static double intpow(double x, unsigned a) +{ + double result = 1.0; + + while (a > 0) { + if (a & 1) { + result *= x; + } + a >>= 1; + x *= x; + } + + return result; +} + // Same, but takes in binomial(a+k-1, k-1) 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(int k, double a, double binomial, double rd_norm) +static double prob_score_real(int k, int a, double binomial, double rd_norm) { - double nom = binomial * pow(2.0, rd_norm * a); - double denom = pow(1.0 + pow(2.0, rd_norm), k+a); + double nom = binomial * intpow(pow(2.0, rd_norm), a); + double denom = intpow(1.0 + pow(2.0, rd_norm), k+a); return nom/denom; } // Calculates Product(a+i, i=1..k-1) (see above). -double prodai(int k, double a) +static double prodai(int k, int a) { double prod = 1.0; for (int i = 1; i < k; ++i) @@ -62,7 +79,7 @@ double prodai(int k, double a) return prod; } -double fac(int x) +static double fac(int x) { double prod = 1.0; for (int i = 2; i <= x; ++i) @@ -70,48 +87,7 @@ double fac(int x) return prod; } -// -// Computes the integral -// -// +inf -// / -// | -// | ProbScore[a] (r1-r2) Gaussian[mu2, sigma2] (r2) 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 (r2-r1) instead of (r1-r2). -// -class ProbScoreEvaluator { -private: - int k; - double a; - double binomial_precompute, r1, mu2, sigma2, winfac; - -public: - ProbScoreEvaluator(int k, double a, double binomial_precompute, double r1, double mu2, double sigma2, double winfac) - : k(k), a(a), binomial_precompute(binomial_precompute), r1(r1), mu2(mu2), sigma2(sigma2), winfac(winfac) {} - inline double operator() (double x) const - { - double probscore = prob_score_real(k, a, binomial_precompute, (r1 - x)*winfac); - double z = (x - mu2)/sigma2; - double gaussian = exp(-(z*z/2.0)); - return probscore * gaussian; - } -}; - -void convolve(int size) -{ -} - -void compute_opponent_rating_pdf(int k, double a, double mu2, double sigma2, double winfac, vector > &result) +static void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector > &result) { double binomial_precompute = prodai(k, a) / fac(k-1); winfac /= rating_constant; @@ -146,10 +122,11 @@ void compute_opponent_rating_pdf(int k, double a, double mu2, double sigma2, dou func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0; } + double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2); 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)); + double z = (x1 - mu2) * invsq2sigma2; + func1[i].real() = exp(-z*z); double x2 = -3000.0 + h*i; func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac); @@ -171,7 +148,7 @@ void compute_opponent_rating_pdf(int k, double a, double mu2, double sigma2, dou } // normalize the curve so we know that A ~= 1 -void normalize(vector > &curve) +static void normalize(vector > &curve) { double peak = 0.0; for (vector >::const_iterator i = curve.begin(); i != curve.end(); ++i) { @@ -184,27 +161,10 @@ void normalize(vector > &curve) } } -// 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) +static 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) { @@ -218,89 +178,46 @@ void mat_mul_trans(double *matA, unsigned ah, unsigned aw, } } -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. +// 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. -void solve3x3(double *A, double *x, double *B) +template +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 // of statistical moments. -void estimate_musigma(vector > &curve, double &mu_result, double &sigma_result) +static void estimate_musigma(vector > &curve, double &mu_result, double &sigma_result) { double h = (curve.back().first - curve.front().first) / (curve.size() - 1); @@ -342,7 +259,7 @@ void estimate_musigma(vector > &curve, double &mu_result, d // Note that the algorithm blows up quite hard if the initial estimate is // not good enough. Use estimate_musigma to get a reasonable starting // estimate. -void least_squares(vector > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result) +static void least_squares(vector > &curve, double mu1, double sigma1, double &mu_result, double &sigma_result) { double A = 1.0; double mu = mu1; @@ -393,7 +310,7 @@ void least_squares(vector > &curve, double mu1, double sigm 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]; @@ -408,7 +325,7 @@ void least_squares(vector > &curve, double mu1, double sigm sigma_result = sigma; } -void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma) +static void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, int score1, int score2, double &mu, double &sigma) { vector > curve; @@ -432,7 +349,7 @@ void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, in least_squares(curve, mu_est, sigma_est, mu, sigma); } -void compute_new_double_rating(double mu1, double sigma1, double mu2, double sigma2, double mu3, double sigma3, double mu4, double sigma4, int score1, int score2, double &mu, double &sigma) +static void compute_new_double_rating(double mu1, double sigma1, double mu2, double sigma2, double mu3, double sigma3, double mu4, double sigma4, int score1, int score2, double &mu, double &sigma) { vector > curve, newcurve; double mu_t = mu3 + mu4; @@ -444,6 +361,8 @@ void compute_new_double_rating(double mu1, double sigma1, double mu2, double sig 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) { @@ -454,12 +373,13 @@ void compute_new_double_rating(double mu1, double sigma1, double mu2, double sig double r1 = i * h; // iterate over r2 + double invsq2sigma2 = 1.0 / (sqrt(2.0) * sigma2); 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)); + double z = (r2 - mu2) * invsq2sigma2; + double gaussian = exp(-z*z); sum += curve[j].second * gaussian; }