From: Steinar H. Gunderson Date: Sun, 21 Oct 2007 12:14:40 +0000 (+0200) Subject: More cleanups, and statify. X-Git-Url: https://git.sesse.net/?p=foosball;a=commitdiff_plain;h=ae0fbaa3310d3d62c7d6d1e4e967be7bd1051e58 More cleanups, and statify. --- diff --git a/foosrank.cpp b/foosrank.cpp index b711f00..bdb54f2 100644 --- a/foosrank.cpp +++ b/foosrank.cpp @@ -16,10 +16,10 @@ static const double rating_constant = 455.0; using namespace std; -double prob_score(int k, int a, double rd); -double prob_score_real(int k, int a, double binomial, double rd_norm); -double prodai(int k, int 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,14 +38,14 @@ 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, int 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) -double intpow(double x, unsigned a) +static double intpow(double x, unsigned a) { double result = 1.0; @@ -63,7 +63,7 @@ double intpow(double x, unsigned a) // 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, int a, double binomial, double rd_norm) +static double prob_score_real(int k, int a, double binomial, double rd_norm) { double nom = binomial * intpow(pow(2.0, rd_norm), a); double denom = intpow(1.0 + pow(2.0, rd_norm), k+a); @@ -71,7 +71,7 @@ double prob_score_real(int k, int a, double binomial, double rd_norm) } // Calculates Product(a+i, i=1..k-1) (see above). -double prodai(int k, int a) +static double prodai(int k, int a) { double prod = 1.0; for (int i = 1; i < k; ++i) @@ -79,7 +79,7 @@ double prodai(int k, int a) return prod; } -double fac(int x) +static double fac(int x) { double prod = 1.0; for (int i = 2; i <= x; ++i) @@ -87,11 +87,7 @@ double fac(int x) return prod; } -void convolve(int size) -{ -} - -void compute_opponent_rating_pdf(int k, int 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; @@ -151,7 +147,7 @@ void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double } // 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) { @@ -164,27 +160,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) { @@ -198,24 +177,10 @@ 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. // Destroys its input in the process. -void solve3x3(double *A, double *x, double *B) +static void solve3x3(double *A, double *x, double *B) { // row 1 -= row 0 * (a1/a0) { @@ -280,7 +245,7 @@ void solve3x3(double *A, double *x, double *B) // 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); @@ -322,7 +287,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; @@ -388,7 +353,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; @@ -412,7 +377,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;