14 #define PRIOR_SIGMA 100
15 #define MAX_PLAYERS 4096
17 float mu[MAX_PLAYERS];
18 float sigma[MAX_PLAYERS];
23 * L(mu_vec, sigma_vec, matches) = product[ L(mu_A, sigma_A, mu_B, sigma_B, score_AB - score_BA) ]
24 * log-likelihood = sum[ log( L(mu_A, sigma_A, mu_B, sigma_B, score_AB - score_BA) ) ]
26 * L(mu1, sigma1, mu2, sigma2, score2 - score1) = sigmoid(mu2 - mu1, sqrt(sigma1² + sigma2²), (score2 - score1))
28 * pdf := 1/(sigma * sqrt(2*Pi)) * exp(-(x - mu)^2 / (2 * sigma^2));
29 * pdfs := subs({ mu = mu1 - mu2, sigma = sqrt(sigma1^2 + sigma2^2) }, pdf);
30 * diff(log(pdfs), mu1);
38 map<int, vector<match> > matches_for_player;
40 void dump_scores(const vector<string> &players, const float *mu, const float *sigma, int num_players)
43 for (int i = 0; i < num_players; ++i) {
44 printf("%s=[%5.1f, %4.1f] ", players[i].c_str(), mu[i], sigma[i]);
48 for (int i = 0; i < num_players; ++i) {
49 printf("%5.1f ", mu[i]);
53 for (int i = 0; i < num_players; ++i) {
54 printf("%f %s\n", mu[i], players[i].c_str());
60 * diff(logL, mu1) = -w * (mu1 - mu2 - x) / sigma_c^2
61 * maximizer for mu1 is given by: sum_i[ (w_i/sigma_c_i)^2 (mu1 - mu2_i - x_i) ] = 0
62 * sum_i[ (w_i/sigma_c_i)^2 mu1 ] = sum_i [ (w_i/sigma_c_i)^2 ( mu2_i + x_i ) ]
63 * mu1 = sum_i [ (w_i/sigma_c_i)^2 ( mu2_i + x_i ) ] / sum_i[ (w_i/sigma_c_i)^2 ]
65 void update_mu(float *mu, float *sigma, int player_num, const vector<match> &matches)
67 if (matches.empty()) {
71 float nom = 0.0f, denom = 0.0f;
75 float inv_sigma2 = 1.0f / (PRIOR_SIGMA * PRIOR_SIGMA);
76 nom += PRIOR_MU * inv_sigma2;
81 for (unsigned i = 0; i < matches.size(); ++i) {
82 float sigma1 = sigma[player_num];
83 float sigma2 = sigma[matches[i].other_player];
84 float inv_sigma_c2 = matches[i].weight / (sigma1 * sigma1 + sigma2 * sigma2);
85 float x = matches[i].margin; // / 70.0f;
87 nom += (mu[matches[i].other_player] + x) * inv_sigma_c2;
88 denom += inv_sigma_c2;
90 mu[player_num] = nom / denom;
94 * diff(logL, sigma1) = sigma1 (-sigma1² - sigma2² + (x - mu)²) / sigma_c²
95 * maximizer for sigma1 is given by: sum_i[ (1/sigma_c_i)² sigma1 ((x - mu)² - (sigma1² + sigma2²) ] = 0
96 * sum_i[ (x - mu)² - sigma1² - sigma2² ] = 0 |: sigma1 != 0, sigma2 != 0
97 * sum_i[ (x - mu)² - sigma2² ] = sum[ sigma1² ]
98 * sigma1 = sqrt( sum_i[ (x - mu)² - sigma2² ] / N )
100 void update_sigma(float *mu, float *sigma, int player_num, const vector<match> &matches)
102 if (matches.size() < 2) {
107 for (unsigned i = 0; i < matches.size(); ++i) {
108 float mu1 = mu[player_num];
109 float mu2 = mu[matches[i].other_player];
110 float mu = mu1 - mu2;
111 float sigma2 = sigma[matches[i].other_player];
112 float x = matches[i].margin;
114 //fprintf(stderr, "x=%f mu=%f sigma2=%f add %f-%f = %f\n", x, mu, sigma2, (x-mu)*(x-mu), sigma2*sigma2, (x - mu) * (x - mu) - sigma2 * sigma2);
115 sum += (x - mu) * (x - mu) - sigma2 * sigma2;
121 //fprintf(stderr, "sum=%f\n", sum);
122 sigma[player_num] = sqrt(sum / matches.size());
126 * diff(logL, sigma) = w ( (x - mu)² - sigma² ) / sigma³
127 * maximizer for sigma is given by: sum_i[ (w_i/sigma)³ ((x - mu)² - sigma²) ] = 0
128 * sum_i[ w_i ( (x - mu)² - sigma² ) ] = 0 |: sigma != 0
129 * sum_i[ w_i (x - mu)² ] = sum[ w_i sigma² ]
130 * sigma = sqrt( sum_i[ w_i (x - mu)² ] / sum[w_i] )
132 void update_global_sigma(float *mu, float *sigma, int num_players)
134 float nom = 0.0f, denom = 0.0f;
135 for (int i = 0; i < num_players; ++i) {
136 for (unsigned j = 0; j < matches_for_player[i].size(); ++j) {
137 const match& m = matches_for_player[i][j];
139 // Only count each match once.
140 if (m.other_player <= i) {
145 float mu2 = mu[m.other_player];
146 float mu = mu1 - mu2;
150 nom += w * ((x - mu) * (x - mu));
155 float best_sigma = sqrt(nom / denom) / sqrt(2.0f); // Divide evenly between the two players.
156 for (int i = 0; i < num_players; ++i) {
157 sigma[i] = best_sigma;
161 void renormalize(float *mu, float *sigma, int num_players)
164 for (int i = 0; i < num_players; ++i) {
167 float corr = 1500.0f - avg / num_players;
168 for (int i = 0; i < num_players; ++i) {
174 * Compute Hessian matrix of the negative log-likelihood, ie. for each term in logL:
176 * M_ij = D_i D_j (- logL) = -w / sigma² for i != j
177 * w / sigma² for i == j
179 * Note that this does not depend on mu or the margin at all.
181 double hessian[MAX_PLAYERS][MAX_PLAYERS];
182 void construct_hessian(const float *mu, const float *sigma, int num_players)
184 memset(hessian, 0, sizeof(hessian));
186 for (int i = 0; i < num_players; ++i) {
187 double sigma1 = sigma[i];
189 for (unsigned k = 0; k < matches_for_player[i].size(); ++k) {
190 int j = matches_for_player[i][k].other_player;
192 double sigma2 = sigma[j];
193 double sigma_sq = sigma1 * sigma1 + sigma2 * sigma2;
195 float w = matches_for_player[i][k].weight;
197 hessian[i][j] -= w / sigma_sq;
198 hessian[i][i] += w / sigma_sq;
202 for (int i = 0; i < num_players; ++i) {
203 for (int j = 0; j < num_players; ++j) {
204 printf("%.12f ", hessian[i][j]);
210 int main(int argc, char **argv)
213 if (scanf("%d", &num_players) != 1) {
214 fprintf(stderr, "Could't read number of players\n");
218 if (num_players > MAX_PLAYERS) {
219 fprintf(stderr, "Max %d players supported\n", MAX_PLAYERS);
223 vector<string> players;
224 map<string, int> player_map;
226 for (int i = 0; i < num_players; ++i) {
228 if (scanf("%s", buf) != 1) {
229 fprintf(stderr, "Couldn't read player %d\n", i);
233 players.push_back(buf);
239 char pl1[256], pl2[256];
243 if (scanf("%s %s %d %d %f", pl1, pl2, &score1, &score2, &weight) != 5) {
244 //fprintf(stderr, "Read %d matches.\n", num_matches);
250 if (player_map.count(pl1) == 0) {
251 fprintf(stderr, "Unknown player '%s'\n", pl1);
254 if (player_map.count(pl2) == 0) {
255 fprintf(stderr, "Unknown player '%s'\n", pl2);
260 m1.other_player = player_map[pl2];
261 m1.margin = score1 - score2;
263 matches_for_player[player_map[pl1]].push_back(m1);
266 m2.other_player = player_map[pl1];
267 m2.margin = score2 - score1;
269 matches_for_player[player_map[pl2]].push_back(m2);
272 float mu[MAX_PLAYERS];
273 float sigma[MAX_PLAYERS];
275 for (int i = 0; i < num_players; ++i) {
277 sigma[i] = 70.0f / sqrt(2.0f);
279 renormalize(mu, sigma, num_players);
281 for (int j = 0; j < 1000; ++j) {
282 float old_mu[MAX_PLAYERS];
283 float old_sigma[MAX_PLAYERS];
284 memcpy(old_mu, mu, sizeof(mu));
285 memcpy(old_sigma, sigma, sizeof(sigma));
286 for (int i = 0; i < num_players; ++i) {
287 update_mu(mu, sigma, i, matches_for_player[i]);
288 renormalize(mu, sigma, num_players);
290 update_global_sigma(mu, sigma, num_players);
291 /* for (int i = 0; i < num_players; ++i) {
292 update_sigma(mu, sigma, i, matches_for_player[i]);
293 dump_scores(players, mu, sigma, num_players);
296 float sumdiff = 0.0f;
297 for (int i = 0; i < num_players; ++i) {
298 sumdiff += (mu[i] - old_mu[i]) * (mu[i] - old_mu[i]);
299 sumdiff += (sigma[i] - old_sigma[i]) * (sigma[i] - old_sigma[i]);
301 if (sumdiff < EPSILON) {
302 //fprintf(stderr, "Converged after %d iterations. Stopping.\n", j);
303 printf("%d -1\n", j + 1);
307 dump_scores(players, mu, sigma, num_players);
308 //fprintf(stderr, "Optimal sigma: %f (two-player: %f)\n", sigma[0], sigma[0] * sqrt(2.0f));
309 printf("%f -2\n", sigma[0]);
311 // construct_hessian(mu, sigma, num_players);