#include <vector>
#include <algorithm>
+#include <complex>
+#include <fftw3.h>
+
// step sizes
-static const double int_step_size = 50.0;
-static const double pdf_step_size = 10.0;
+static const double int_step_size = 75.0;
// rating constant (see below)
static const double rating_constant = 455.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);
+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);
+
-// probability of match ending 10-a when winnerR - loserR = RD
+// probability of match ending k-a (k>a) when winnerR - loserR = RD
//
// +inf
// /
// |
-// | Poisson[lambda1, t](a) * Erlang[lambda2, 10](t) dt
+// | Poisson[lambda1, t](a) * Erlang[lambda2, k](t) dt
// |
// /
// -inf
// 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)
+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)
{
- return prob_score_real(a, prodai(a), rd/rating_constant);
+ double result = 1.0;
+
+ while (a > 0) {
+ if (a & 1) {
+ result *= x;
+ }
+ a >>= 1;
+ x *= x;
+ }
+
+ return result;
}
-// 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)
+// 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)
{
- 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);
+ 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..9) (see above).
-double prodai(double a)
+// Calculates Product(a+i, i=1..k-1) (see above).
+double prodai(int k, int a)
{
- return (a+1)*(a+2)*(a+3)*(a+4)*(a+5)*(a+6)*(a+7)*(a+8)*(a+9);
+ double prod = 1.0;
+ for (int i = 1; i < k; ++i)
+ prod *= (a+i);
+ return prod;
}
-//
-// 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).
-//
-static inline double evaluate_int_point(double a, double prodai_precompute, double r1, double mu2, double sigma2, double winfac, double x);
+double fac(int x)
+{
+ double prod = 1.0;
+ for (int i = 2; i <= x; ++i)
+ prod *= i;
+ return prod;
+}
-double opponent_rating_pdf(double a, double r1, double mu2, double sigma2, double winfac)
+void convolve(int size)
{
- double prodai_precompute = prodai(a);
- winfac /= rating_constant;
+}
- int n = int(3000.0 / int_step_size + 0.5);
- double h = 3000.0 / double(n);
- double sum = evaluate_int_point(a, prodai_precompute, r1, mu2, sigma2, winfac, 0.0);
+void compute_opponent_rating_pdf(int k, int a, double mu2, double sigma2, double winfac, vector<pair<double, double> > &result)
+{
+ double binomial_precompute = prodai(k, a) / fac(k-1);
+ winfac /= rating_constant;
- for (int i = 1; i < n; i += 2) {
- sum += 4.0 * evaluate_int_point(a, prodai_precompute, r1, mu2, sigma2, winfac, i * h);
+ int sz = (6000.0 - 0.0) / int_step_size;
+ double h = (6000.0 - 0.0) / sz;
+
+ fftw_plan f1, f2, b;
+ complex<double> *func1, *func2, *res;
+
+ func1 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
+ func2 = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
+ res = reinterpret_cast<complex<double> *>(fftw_malloc(sz*2*sizeof(complex<double>)));
+ f1 = fftw_plan_dft_1d(sz*2,
+ reinterpret_cast<fftw_complex*>(func1),
+ reinterpret_cast<fftw_complex*>(func1),
+ FFTW_FORWARD,
+ FFTW_MEASURE);
+ f2 = fftw_plan_dft_1d(sz*2,
+ reinterpret_cast<fftw_complex*>(func2),
+ reinterpret_cast<fftw_complex*>(func2),
+ FFTW_FORWARD,
+ FFTW_MEASURE);
+ b = fftw_plan_dft_1d(sz*2,
+ reinterpret_cast<fftw_complex*>(res),
+ reinterpret_cast<fftw_complex*>(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 = 2; i < n; i += 2) {
- sum += 2.0 * evaluate_int_point(a, prodai_precompute, r1, mu2, sigma2, winfac, i * h);
+
+ 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);
}
- sum += evaluate_int_point(a, prodai_precompute, r1, mu2, sigma2, winfac, 3000.0);
- return (h/3.0) * sum;
-}
+ result.reserve(sz*2);
-static inline double evaluate_int_point(double a, double prodai_precompute, double r1, double mu2, double sigma2, double winfac, double x)
-{
- double probscore = prob_score_real(a, prodai_precompute, (r1 - x)*winfac);
- double z = (x - mu2)/sigma2;
- double gaussian = exp(-(z*z/2.0));
- return probscore * gaussian;
+ // 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
{
vector<pair<double, double> > curve;
- if (score1 == 10) {
- 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, r1, mu2, sigma2, 1.0)));
- }
+ if (score1 > score2) {
+ 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(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;
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)
+{
+ vector<pair<double, double> > curve, newcurve;
+ double mu_t = mu3 + mu4;
+ double sigma_t = sqrt(sigma3*sigma3 + sigma4*sigma4);
+
+ if (score1 > score2) {
+ compute_opponent_rating_pdf(score1, score2, mu_t, sigma_t, -1.0, curve);
+ } else {
+ 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));
+ 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(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]);
double sigma2 = atof(argv[4]);
- if (argc > 5) {
+ if (argc > 10) {
+ double mu3 = atof(argv[5]);
+ double sigma3 = atof(argv[6]);
+ double mu4 = atof(argv[7]);
+ double sigma4 = atof(argv[8]);
+ int score1 = atoi(argv[9]);
+ 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);
+ } else if (argc > 8) {
+ double mu3 = atof(argv[5]);
+ double sigma3 = atof(argv[6]);
+ double mu4 = atof(argv[7]);
+ double sigma4 = atof(argv[8]);
+ int k = atoi(argv[9]);
+
+ // assess all possible scores
+ for (int i = 0; i < k; ++i) {
+ double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
+ double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
+ compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, k, i, newmu1_1, newsigma1_1);
+ compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, k, i, newmu1_2, newsigma1_2);
+ compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, i, k, newmu2_1, newsigma2_1);
+ compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, i, k, newmu2_2, newsigma2_2);
+ printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
+ k, i, prob_score(k, i, mu3+mu4-(mu1+mu2)), newmu1_1-mu1, newmu1_2-mu2,
+ newmu2_1-mu3, newmu2_2-mu4);
+ }
+ for (int i = k; i --> 0; ) {
+ double newmu1_1, newmu1_2, newmu2_1, newmu2_2;
+ double newsigma1_1, newsigma1_2, newsigma2_1, newsigma2_2;
+ compute_new_double_rating(mu1, sigma1, mu2, sigma2, mu3, sigma3, mu4, sigma4, i, k, newmu1_1, newsigma1_1);
+ compute_new_double_rating(mu2, sigma2, mu1, sigma1, mu3, sigma3, mu4, sigma4, i, k, newmu1_2, newsigma1_2);
+ compute_new_double_rating(mu3, sigma3, mu4, sigma4, mu1, sigma1, mu2, sigma2, k, i, newmu2_1, newsigma2_1);
+ compute_new_double_rating(mu4, sigma4, mu3, sigma3, mu1, sigma1, mu2, sigma2, k, i, newmu2_2, newsigma2_2);
+ printf("%u-%u,%f,%+f,%+f,%+f,%+f\n",
+ i, k, prob_score(k, i, mu1+mu2-(mu3+mu4)), newmu1_1-mu1, newmu1_2-mu2,
+ newmu2_1-mu3, newmu2_2-mu4);
+ }
+ } else if (argc > 6) {
int score1 = atoi(argv[5]);
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);
} else {
+ int k = atoi(argv[5]);
+
// assess all possible scores
- for (int i = 0; i <= 9; ++i) {
+ for (int i = 0; i < k; ++i) {
double newmu1, newmu2, newsigma1, newsigma2;
- compute_new_rating(mu1, sigma1, mu2, sigma2, 10, i, newmu1, newsigma1);
- compute_new_rating(mu2, sigma2, mu1, sigma1, i, 10, newmu2, newsigma2);
- printf("10-%u,%f,%+f,%+f\n",
- i, prob_score(i, mu1-mu2), newmu1-mu1, newmu2-mu2);
+ compute_new_rating(mu1, sigma1, mu2, sigma2, k, i, newmu1, newsigma1);
+ compute_new_rating(mu2, sigma2, mu1, sigma1, i, k, newmu2, newsigma2);
+ printf("%u-%u,%f,%+f,%+f\n",
+ k, i, prob_score(k, i, mu2-mu1), newmu1-mu1, newmu2-mu2);
}
- for (int i = 10; i --> 0; ) {
+ for (int i = k; i --> 0; ) {
double newmu1, newmu2, newsigma1, newsigma2;
- compute_new_rating(mu1, sigma1, mu2, sigma2, i, 10, newmu1, newsigma1);
- compute_new_rating(mu2, sigma2, mu1, sigma1, 10, i, newmu2, newsigma2);
- printf("%u-10,%f,%+f,%+f\n",
- i, prob_score(i, mu2-mu1), newmu1-mu1, newmu2-mu2);
+ compute_new_rating(mu1, sigma1, mu2, sigma2, i, k, newmu1, newsigma1);
+ compute_new_rating(mu2, sigma2, mu1, sigma1, k, i, newmu2, newsigma2);
+ printf("%u-%u,%f,%+f,%+f\n",
+ 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);
+ }
}