14 #define PRIOR_WEIGHT 1.0
15 #define MAX_PLAYERS 4096
18 float mu[MAX_PLAYERS];
19 float global_sigma = 70.0f;
20 float prior_sigma = 70.0f;
25 * L(mu_vec, sigma_vec, matches) = product[ L(mu_A, sigma_A, mu_B, sigma_B, score_AB - score_BA) ]
26 * log-likelihood = sum[ log( L(mu_A, sigma_A, mu_B, sigma_B, score_AB - score_BA) ) ]
28 * L(mu1, sigma1, mu2, sigma2, score2 - score1) = sigmoid(mu2 - mu1, sqrt(sigma1² + sigma2²), (score2 - score1))
30 * pdf := 1/(sigma * sqrt(2*Pi)) * exp(-(x - mu)^2 / (2 * sigma^2));
31 * pdfs := subs({ mu = mu1 - mu2, sigma = sqrt(sigma1^2 + sigma2^2) }, pdf);
32 * diff(log(pdfs), mu1);
36 int player, other_player;
40 map<int, vector<match> > matches_for_player;
41 vector<match> all_matches;
43 void dump_scores(const vector<string> &players, const float *mu, int num_players)
46 for (int i = 0; i < num_players; ++i) {
47 printf("%s=[%5.1f, %4.1f] ", players[i].c_str(), mu[i], sigma[i]);
51 for (int i = 0; i < num_players; ++i) {
52 printf("%5.1f ", mu[i]);
56 for (int i = 0; i < num_players; ++i) {
57 printf("%f %s\n", mu[i], players[i].c_str());
63 * diff(logL, mu1) = -w * (mu1 - mu2 - x) / sigma_c^2
64 * maximizer for mu1 is given by: sum_i[ (w_i/sigma_c_i)^2 (mu1 - mu2_i - x_i) ] = 0
65 * sum_i[ (w_i/sigma_c_i)^2 mu1 ] = sum_i [ (w_i/sigma_c_i)^2 ( mu2_i + x_i ) ]
66 * mu1 = sum_i [ (w_i/sigma_c_i)^2 ( mu2_i + x_i ) ] / sum_i[ (w_i/sigma_c_i)^2 ]
68 void update_mu(float *mu, int player_num, const vector<match> &matches)
70 if (matches.empty()) {
74 float nom = 0.0f, denom = 0.0f;
78 float inv_sigma2 = 1.0f / (prior_sigma * prior_sigma);
79 nom += PRIOR_WEIGHT * PRIOR_MU * inv_sigma2;
80 denom += PRIOR_WEIGHT * inv_sigma2;
84 for (unsigned i = 0; i < matches.size(); ++i) {
85 float inv_sigma_c2 = matches[i].weight / (global_sigma * global_sigma);
86 float x = matches[i].margin; // / 70.0f;
88 nom += (mu[matches[i].other_player] + x) * inv_sigma_c2;
89 denom += inv_sigma_c2;
91 mu[player_num] = nom / denom;
94 void dump_raw(const float *mu, int num_players)
96 for (unsigned i = 0; i < all_matches.size(); ++i) {
97 const match& m = all_matches[i];
99 float mu1 = mu[m.player];
100 float mu2 = mu[m.other_player];
101 float sigma = global_sigma;
102 float mu = mu1 - mu2;
106 printf("%f %f\n", (x - mu) / sigma, w);
111 * diff(logL, sigma) = w ( (x - mu)² - sigma² ) / sigma³
112 * maximizer for sigma is given by: sum_i[ (w_i/sigma)³ ((x - mu)² - sigma²) ] = 0
113 * sum_i[ w_i ( (x - mu)² - sigma² ) ] = 0 |: sigma != 0
114 * sum_i[ w_i (x - mu)² ] = sum[ w_i sigma² ]
115 * sigma = sqrt( sum_i[ w_i (x - mu)² ] / sum[w_i] )
117 void update_global_sigma(float *mu, int num_players)
119 float nom = 0.0f, denom = 0.0f;
120 for (unsigned i = 0; i < all_matches.size(); ++i) {
121 const match& m = all_matches[i];
123 float mu1 = mu[m.player];
124 float mu2 = mu[m.other_player];
125 float mu = mu1 - mu2;
129 nom += w * ((x - mu) * (x - mu));
133 global_sigma = sqrt(nom / denom);
137 * diff(priorlogL, sigma) = w ( (x - mu)² - sigma² ) / sigma³
138 * maximizer for sigma is given by: sum_i[ (w_i/sigma)³ ((x - mu)² - sigma²) ] = 0
139 * sum_i[ w_i ( (x - mu)² - sigma² ) ] = 0 |: sigma != 0
140 * sum_i[ w_i (x - mu)² ] = sum[ w_i sigma² ]
141 * sigma = sqrt( sum_i[ w_i (x - mu)² ] / sum[w_i] )
143 void update_prior_sigma(float *mu, int num_players)
145 float nom = 0.0f, denom = 0.0f;
146 for (int i = 0; i < num_players; ++i) {
149 nom += ((mu1 - PRIOR_MU) * (mu1 - PRIOR_MU));
153 prior_sigma = sqrt(nom / denom);
154 if (!(prior_sigma > 40.0f)) {
159 float compute_logl(float z)
161 return -0.5 * (log(2.0f / M_PI) + z * z);
164 float compute_total_logl(float *mu, int num_players)
166 float total_logl = 0.0f;
169 for (int i = 0; i < num_players; ++i) {
170 total_logl += PRIOR_WEIGHT * compute_logl((mu[i] - PRIOR_MU) / prior_sigma);
174 for (unsigned i = 0; i < all_matches.size(); ++i) {
175 const match& m = all_matches[i];
177 float mu1 = mu[m.player];
178 float mu2 = mu[m.other_player];
179 float sigma = global_sigma;
180 float mu = mu1 - mu2;
184 total_logl += w * compute_logl((x - mu) / sigma);
191 * Compute Hessian matrix of the negative log-likelihood, ie. for each term in logL:
193 * M_ij = D_i D_j (- logL) = -w / sigma² for i != j
194 * w / sigma² for i == j
196 * Note that this does not depend on mu or the margin at all.
198 double hessian[MAX_PLAYERS][MAX_PLAYERS];
199 void construct_hessian(const float *mu, const float *sigma, int num_players)
201 memset(hessian, 0, sizeof(hessian));
203 for (unsigned i = 0; i < all_matches.size(); ++i) {
204 const match &m = all_matches[i];
207 int p2 = m.other_player;
209 double sigma_sq = global_sigma * global_sigma;
212 hessian[p1][p2] -= w / sigma_sq;
213 hessian[p2][p1] -= w / sigma_sq;
215 hessian[p1][p1] += w / sigma_sq;
216 hessian[p2][p2] += w / sigma_sq;
219 for (int i = 0; i < num_players; ++i) {
220 for (int j = 0; j < num_players; ++j) {
221 printf("%.12f ", hessian[i][j]);
227 int main(int argc, char **argv)
230 if (scanf("%d", &num_players) != 1) {
231 fprintf(stderr, "Could't read number of players\n");
235 if (num_players > MAX_PLAYERS) {
236 fprintf(stderr, "Max %d players supported\n", MAX_PLAYERS);
240 vector<string> players;
241 map<string, int> player_map;
243 for (int i = 0; i < num_players; ++i) {
245 if (scanf("%s", buf) != 1) {
246 fprintf(stderr, "Couldn't read player %d\n", i);
250 players.push_back(buf);
256 char pl1[256], pl2[256];
260 if (scanf("%s %s %d %d %f", pl1, pl2, &score1, &score2, &weight) != 5) {
261 //fprintf(stderr, "Read %d matches.\n", num_matches);
267 if (player_map.count(pl1) == 0) {
268 fprintf(stderr, "Unknown player '%s'\n", pl1);
271 if (player_map.count(pl2) == 0) {
272 fprintf(stderr, "Unknown player '%s'\n", pl2);
277 m1.player = player_map[pl1];
278 m1.other_player = player_map[pl2];
279 m1.margin = score1 - score2;
281 matches_for_player[player_map[pl1]].push_back(m1);
284 m2.player = player_map[pl2];
285 m2.other_player = player_map[pl1];
286 m2.margin = score2 - score1;
288 matches_for_player[player_map[pl2]].push_back(m2);
290 all_matches.push_back(m1);
293 float mu[MAX_PLAYERS];
295 for (int i = 0; i < num_players; ++i) {
299 for (int j = 0; j < 1000; ++j) {
300 float old_mu[MAX_PLAYERS];
301 float old_global_sigma = global_sigma;
302 float old_prior_sigma = prior_sigma;
303 memcpy(old_mu, mu, sizeof(mu));
304 for (int i = 0; i < num_players; ++i) {
305 update_mu(mu, i, matches_for_player[i]);
307 update_global_sigma(mu, num_players);
308 update_prior_sigma(mu, num_players);
309 /* for (int i = 0; i < num_players; ++i) {
310 update_sigma(mu, sigma, i, matches_for_player[i]);
311 dump_scores(players, mu, sigma, num_players);
314 float sumdiff = 0.0f;
315 for (int i = 0; i < num_players; ++i) {
316 sumdiff += (mu[i] - old_mu[i]) * (mu[i] - old_mu[i]);
318 sumdiff += (prior_sigma - old_prior_sigma) * (prior_sigma - old_prior_sigma);
319 sumdiff += (global_sigma - old_global_sigma) * (global_sigma - old_global_sigma);
320 if (sumdiff < EPSILON) {
321 //fprintf(stderr, "Converged after %d iterations. Stopping.\n", j);
322 printf("%d -1\n", j + 1);
328 dump_raw(mu, num_players);
330 dump_scores(players, mu, num_players);
331 //fprintf(stderr, "Optimal sigma: %f (two-player: %f)\n", sigma[0], sigma[0] * sqrt(2.0f));
332 printf("%f -2\n", global_sigma / sqrt(2.0f));
333 printf("%f -3\n", prior_sigma);
335 float total_logl = compute_total_logl(mu, num_players);
336 printf("%f -4\n", total_logl);
338 // construct_hessian(mu, sigma, num_players);