From bc46a663c308a940cc6545861ae8de8e36a2fe2d Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 17 Oct 2007 18:39:27 +0200 Subject: [PATCH] Switch to FFT-based calculation. Much, much faster! --- foosrank.cpp | 180 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 115 insertions(+), 65 deletions(-) diff --git a/foosrank.cpp b/foosrank.cpp index b3eef1e..cae1dc3 100644 --- a/foosrank.cpp +++ b/foosrank.cpp @@ -5,9 +5,11 @@ #include #include +#include +#include + // step sizes static const double int_step_size = 75.0; -static const double pdf_step_size = 15.0; // rating constant (see below) static const double rating_constant = 455.0; @@ -19,24 +21,6 @@ double prob_score_real(int k, double a, double binomial, double rd_norm); double prodai(int k, double a); double fac(int x); -// Numerical integration using Simpson's rule -template -double simpson_integrate(const T &evaluator, double from, double to, double step) -{ - int n = int((to - from) / step + 0.5); - double h = (to - from) / n; - double sum = evaluator(from); - - for (int i = 1; i < n; i += 2) { - sum += 4.0 * evaluator(from + i * h); - } - for (int i = 2; i < n; i += 2) { - sum += 2.0 * evaluator(from + i * h); - } - sum += evaluator(to); - - return (h/3.0) * sum; -} // probability of match ending k-a (k>a) when winnerR - loserR = RD // @@ -103,7 +87,7 @@ double fac(int x) // 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). +// In the latter case, ProbScore will be given (r2-r1) instead of (r1-r2). // class ProbScoreEvaluator { private: @@ -123,12 +107,67 @@ public: } }; -double opponent_rating_pdf(int k, double a, double r1, double mu2, double sigma2, double winfac) +void convolve(int size) +{ +} + +void compute_opponent_rating_pdf(int k, double a, double mu2, double sigma2, double winfac, vector > &result) { double binomial_precompute = prodai(k, a) / fac(k-1); winfac /= rating_constant; - return simpson_integrate(ProbScoreEvaluator(k, a, binomial_precompute, r1, mu2, sigma2, winfac), 0.0, 6000.0, int_step_size); + int sz = (6000.0 - 0.0) / int_step_size; + double h = (6000.0 - 0.0) / sz; + + fftw_plan f1, f2, b; + complex *func1, *func2, *res; + + func1 = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); + func2 = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); + res = reinterpret_cast *>(fftw_malloc(sz*2*sizeof(complex))); + f1 = fftw_plan_dft_1d(sz*2, + reinterpret_cast(func1), + reinterpret_cast(func1), + FFTW_FORWARD, + FFTW_MEASURE); + f2 = fftw_plan_dft_1d(sz*2, + reinterpret_cast(func2), + reinterpret_cast(func2), + FFTW_FORWARD, + FFTW_MEASURE); + b = fftw_plan_dft_1d(sz*2, + reinterpret_cast(res), + reinterpret_cast(res), + FFTW_BACKWARD, + FFTW_MEASURE); + + // start off by zero + for (int i = 0; i < sz*2; ++i) { + func1[i].real() = func1[i].imag() = func2[i].real() = func2[i].imag() = 0.0; + } + + 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 x2 = -3000.0 + h*i; + func2[(i - sz/2 + sz*2)%(sz*2)].real() = prob_score_real(k, a, binomial_precompute, x2*winfac); + } + + result.reserve(sz*2); + + // convolve + fftw_execute(f1); + fftw_execute(f2); + for (int i = 0; i < sz*2; ++i) { + res[i] = func1[i] * func2[i]; + } + fftw_execute(b); + for (int i = 0; i < sz; ++i) { + double r1 = i*h; + result.push_back(make_pair(r1, abs(res[i]))); + } } // normalize the curve so we know that A ~= 1 @@ -374,17 +413,17 @@ void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, in vector > curve; if (score1 > score2) { - for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_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, score2, r1, mu2, sigma2, -1.0))); - } + compute_opponent_rating_pdf(score1, score2, mu2, sigma2, -1.0, curve); } else { - for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_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, score1, r1, mu2, sigma2, 1.0))); - } + compute_opponent_rating_pdf(score2, score1, mu2, sigma2, 1.0, curve); + } + + // multiply in the gaussian + for (unsigned i = 0; i < curve.size(); ++i) { + double r1 = curve[i].first; + double z = (r1 - mu1) / sigma1; + double gaussian = exp(-(z*z/2.0)); + curve[i].second *= gaussian; } double mu_est, sigma_est; @@ -393,52 +432,57 @@ void compute_new_rating(double mu1, double sigma1, double mu2, double sigma2, in least_squares(curve, mu_est, sigma_est, mu, sigma); } -// int(normpdf[mu2, sigma2](t2) * ..., t2=0..3000); -class OuterIntegralEvaluator { -private: - double theta1, mu2, sigma2, mu_t, sigma_t; - int score1, score2; - double winfac; - -public: - OuterIntegralEvaluator(double theta1, double mu2, double sigma2, double mu3, double sigma3, double mu4, double sigma4, int score1, int score2, double winfac) - : theta1(theta1), mu2(mu2), sigma2(sigma2), mu_t(mu3 + mu4), sigma_t(sqrt(sigma3*sigma3 + sigma4*sigma4)), score1(score1), score2(score2), winfac(winfac) {} - - double operator() (double theta2) const - { - double z = (theta2 - mu2) / sigma2; - double gaussian = exp(-(z*z/2.0)); - double r1 = theta1 + theta2; - return gaussian * opponent_rating_pdf(score1, score2, r1, mu_t, sigma_t, winfac); - } -}; - 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; - + vector > curve, newcurve; + double mu_t = mu3 + mu4; + double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4); + if (score1 > score2) { - for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) { - double z = (r1 - mu1) / sigma1; - double gaussian = exp(-(z*z/2.0)); - curve.push_back(make_pair(r1, gaussian * simpson_integrate(OuterIntegralEvaluator(r1,mu2,sigma2,mu3,sigma3,mu4,sigma4,score1,score2,-1.0), 0.0, 3000.0, int_step_size))); - } + compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, curve); } else { - for (double r1 = 0.0; r1 < 3000.0; r1 += pdf_step_size) { - double z = (r1 - mu1) / sigma1; + compute_opponent_rating_pdf(score2, score1, mu_t, sigma_t, 1.0, curve); + } + + // iterate over r1 + double h = 3000.0 / curve.size(); + for (unsigned i = 0; i < curve.size(); ++i) { + double sum = 0.0; + + // could be anything, but this is a nice start + //double r1 = curve[i].first; + double r1 = i * h; + + // iterate over r2 + 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)); - curve.push_back(make_pair(r1, gaussian * simpson_integrate(OuterIntegralEvaluator(r1,mu2,sigma2,mu3,sigma3,mu4,sigma4,score2,score1,1.0), 0.0, 3000.0, int_step_size))); + sum += curve[j].second * gaussian; } + + double z = (r1 - mu1) / sigma1; + double gaussian = exp(-(z*z/2.0)); + newcurve.push_back(make_pair(r1, gaussian * sum)); } + double mu_est, sigma_est; - normalize(curve); - estimate_musigma(curve, mu_est, sigma_est); - least_squares(curve, mu_est, sigma_est, mu, sigma); + normalize(newcurve); + estimate_musigma(newcurve, mu_est, sigma_est); + least_squares(newcurve, mu_est, sigma_est, mu, sigma); } int main(int argc, char **argv) { + FILE *fp = fopen("fftw-wisdom", "rb"); + if (fp != NULL) { + fftw_import_wisdom_from_file(fp); + fclose(fp); + } + double mu1 = atof(argv[1]); double sigma1 = atof(argv[2]); double mu2 = atof(argv[3]); @@ -479,5 +523,11 @@ int main(int argc, char **argv) i, k, prob_score(k, i, mu1-mu2), newmu1-mu1, newmu2-mu2); } } + + fp = fopen("fftw-wisdom", "wb"); + if (fp != NULL) { + fftw_export_wisdom_to_file(fp); + fclose(fp); + } } -- 2.39.2