]> git.sesse.net Git - wloh/blob - bayeswf.cpp
Only dump scores once, at the very end.
[wloh] / bayeswf.cpp
1 #include <stdio.h>
2 #include <math.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #include <map>
7 #include <vector>
8 #include <string>
9 #include <algorithm>
10
11 using namespace std;
12
13 #define MAX_PLAYERS 4096
14
15 float mu[MAX_PLAYERS];
16 float sigma[MAX_PLAYERS];
17
18 #define EPSILON 1e-3
19
20 /*
21  * L(mu_vec, sigma_vec, matches) = product[ L(mu_A, sigma_A, mu_B, sigma_B, score_AB - score_BA) ]
22  * log-likelihood = sum[ log( L(mu_A, sigma_A, mu_B, sigma_B, score_AB - score_BA) ) ]
23  * 
24  * L(mu1, sigma1, mu2, sigma2, score2 - score1) = sigmoid(mu2 - mu1, sqrt(sigma1² + sigma2²), (score2 - score1))
25  *
26  * pdf := 1/(sigma * sqrt(2*Pi)) * exp(-(x - mu)^2 / (2 * sigma^2));        
27  * pdfs := subs({ mu = mu1 - mu2, sigma = sqrt(sigma1^2 + sigma2^2) }, pdf);
28  * diff(log(pdfs), mu1); 
29  */
30
31 struct match {
32         int other_player;
33         int margin;
34 };
35 map<int, vector<match> > matches_for_player;
36
37 void dump_scores(const vector<string> &players, const float *mu, const float *sigma, int num_players)
38 {
39 #if 0
40         for (int i = 0; i < num_players; ++i) {
41                 printf("%s=[%5.1f, %4.1f] ", players[i].c_str(), mu[i], sigma[i]);
42         }
43         printf("\n");
44 #elif 0
45         for (int i = 0; i < num_players; ++i) {
46                 printf("%5.1f ", mu[i]);
47         }
48         printf("\n");
49 #else
50         for (int i = 0; i < num_players; ++i) {
51                 printf("%5.1f %s\n", mu[i], players[i].c_str());
52         }
53         printf("\n");
54 #endif
55 }
56
57 /*
58  * diff(logL, mu1) = -(mu1 - mu2 - x) / sigma_c^2
59  * maximizer for mu1 is given by: sum_i[ (1/sigma_c_i)^2 (mu1 - mu2_i - x_i) ] = 0
60  *                                sum_i[ (1/sigma_c_i)^2 mu1 ] = sum_i [ (1/sigma_c_i)^2 ( mu2_i + x_i ) ]
61  *                                mu1 = sum_i [ (1/sigma_c_i)^2 ( mu2_i + x_i ) ] / sum_i[ (1/sigma_c_i)^2 ]
62  */
63 void update_mu(float *mu, float *sigma, int player_num, const vector<match> &matches)
64 {
65         if (matches.empty()) {
66                 return;
67         }
68
69         float nom = 0.0f, denom = 0.0f;
70         for (unsigned i = 0; i < matches.size(); ++i) {
71                 float sigma1 = sigma[player_num];
72                 float sigma2 = sigma[matches[i].other_player];
73                 float inv_sigma_c2 = 1.0f / (sigma1 * sigma1 + sigma2 * sigma2);
74                 float x = matches[i].margin; // / 70.0f;
75         
76                 nom += (mu[matches[i].other_player] + x) * inv_sigma_c2;
77                 denom += inv_sigma_c2;
78         }
79         mu[player_num] = nom / denom;
80 }
81
82 /*
83  * diff(logL, sigma1) = sigma1 (-sigma1² - sigma2² + (x - mu)²) / sigma_c²
84  * maximizer for sigma1 is given by: sum_i[ (1/sigma_c_i)² sigma1 ((x - mu)² - (sigma1² + sigma2²) ] = 0
85  *                                   sum_i[ (x - mu)² - sigma1² - sigma2² ] = 0                                  |: sigma1 != 0, sigma2 != 0
86  *                                   sum_i[ (x - mu)² - sigma2² ] = sum[ sigma1² ]
87  *                                   sigma1 = sqrt( sum_i[ (x - mu)² - sigma2² ] / N )
88  */
89 void update_sigma(float *mu, float *sigma, int player_num, const vector<match> &matches)
90 {
91         if (matches.size() < 2) {
92                 return;
93         }
94
95         float sum = 0.0f;
96         for (unsigned i = 0; i < matches.size(); ++i) {
97                 float mu1 = mu[player_num];
98                 float mu2 = mu[matches[i].other_player];
99                 float mu = mu1 - mu2;
100                 float sigma2 = sigma[matches[i].other_player];
101                 float x = matches[i].margin;
102
103                 //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);
104                 sum += (x - mu) * (x - mu) - sigma2 * sigma2;
105         }
106
107         if (sum <= 0) {
108                 return;
109         }
110         //fprintf(stderr, "sum=%f\n", sum);
111         sigma[player_num] = sqrt(sum / matches.size());
112 }
113
114 void renormalize(float *mu, float *sigma, int num_players)
115 {
116         float avg = 0.0f;
117         for (int i = 0; i < num_players; ++i) {
118                 avg += mu[i];
119         }
120         float corr = 1500.0f - avg / num_players;
121         for (int i = 0; i < num_players; ++i) {
122                 mu[i] += corr;
123         }
124 }
125
126 /*
127  * Compute Fisher information matrix of the log-likelihood, evaluated at the MLE,
128 c
129  * ie. M_ij = E[ (D_i logL) (D_j logL) ] = - sum( ( x - (mu_1 - mu_2) )² / sigma_c⁴ )  for i != j
130  *                                       = - sum( 1 / sigma_c² )                     for i == j
131  *
132  * The Hessian matrix is generally zero and thus not as interesting.
133  */
134 void construct_fim(const float *mu, const float *sigma, int num_players)
135 {
136         float fim[MAX_PLAYERS][MAX_PLAYERS];
137         memset(fim, 0, sizeof(fim));
138
139         for (int i = 0; i < num_players; ++i) {
140                 float mu1 = mu[i];
141                 float sigma1 = sigma[i];
142
143                 for (unsigned k = 0; k < matches_for_player[i].size(); ++k) {
144                         int j = matches_for_player[i][k].other_player;
145                         float mu2 = mu[j];
146                         float sigma2 = sigma[j];
147
148                         float x = matches_for_player[i][k].margin;
149                         float sigma_sq = sqrt(sigma1 * sigma1 + sigma2 * sigma2);
150
151                         fprintf(stderr, "exp_diff_sq=%f  sigma_sq=%f\n", (x - (mu1 - mu2)) * (x - (mu1 - mu2)), sigma_sq * sigma_sq);
152
153 #if 1
154                         fim[i][i] += (x - (mu1 - mu2)) * (x - (mu1 - mu2)) / (sigma_sq * sigma_sq);
155                         fim[i][j] -= (x - (mu1 - mu2)) * (x - (mu1 - mu2)) / (sigma_sq * sigma_sq);
156 #else
157                         fim[i][i] -= 1.0f / sigma_sq;
158                         fim[i][j] += 1.0f / sigma_sq;
159 #endif
160                 }
161
162                 for (int j = 0; j < num_players; ++j) {
163                         printf("%f ", fim[i][j]);
164                 }
165                 printf("\n");
166         }
167 }
168
169 int main(int argc, char **argv)
170 {
171         int num_players;
172         if (scanf("%d", &num_players) != 1) {
173                 fprintf(stderr, "Could't read number of players\n");
174                 exit(1);
175         }
176
177         if (num_players > MAX_PLAYERS) {
178                 fprintf(stderr, "Max %d players supported\n", MAX_PLAYERS);
179                 exit(1);
180         }
181
182         vector<string> players;
183         map<string, int> player_map;
184
185         for (int i = 0; i < num_players; ++i) {
186                 char buf[256];
187                 if (scanf("%s", buf) != 1) {
188                         fprintf(stderr, "Couldn't read player %d\n", i);
189                         exit(1);
190                 }
191
192                 players.push_back(buf);
193                 player_map[buf] = i;
194         }
195
196         int num_matches = 0;
197         for ( ;; ) {
198                 char pl1[256], pl2[256];
199                 int score1, score2;
200
201                 if (scanf("%s %s %d %d", pl1, pl2, &score1, &score2) != 4) {
202                         fprintf(stderr, "Read %d matches.\n", num_matches);
203                         break;
204                 }
205
206                 ++num_matches;
207
208                 if (player_map.count(pl1) == 0) {
209                         fprintf(stderr, "Unknown player '%s'\n", pl1);
210                         exit(1);
211                 }
212                 if (player_map.count(pl2) == 0) {
213                         fprintf(stderr, "Unknown player '%s'\n", pl2);
214                         exit(1);
215                 }
216
217                 match m1;
218                 m1.other_player = player_map[pl2];
219                 m1.margin = score1 - score2;
220                 matches_for_player[player_map[pl1]].push_back(m1);
221
222                 match m2;
223                 m2.other_player = player_map[pl1];
224                 m2.margin = score2 - score1;
225                 matches_for_player[player_map[pl2]].push_back(m2);
226         }
227
228         float mu[MAX_PLAYERS];
229         float sigma[MAX_PLAYERS];
230
231         for (int i = 0; i < num_players; ++i) {
232                 mu[i] = 1500.0f;
233                 sigma[i] = 70.0f / sqrt(2.0f);
234         }
235         renormalize(mu, sigma, num_players);
236
237         for (int j = 0; j < 100; ++j) {
238                 float old_mu[MAX_PLAYERS];
239                 float old_sigma[MAX_PLAYERS];
240                 memcpy(old_mu, mu, sizeof(float) * MAX_PLAYERS);
241                 memcpy(old_sigma, sigma, sizeof(float) * MAX_PLAYERS);
242                 for (int i = 0; i < num_players; ++i) {
243                         update_mu(mu, sigma, i, matches_for_player[i]);
244                         renormalize(mu, sigma, num_players);
245                 }
246                 /* for (int i = 0; i < num_players; ++i) {
247                         update_sigma(mu, sigma, i, matches_for_player[i]);
248                         dump_scores(players, mu, sigma, num_players);
249                 } */
250                 bool any_difference = false;
251                 for (int i = 0; i < num_players; ++i) {
252                         if (fabs(mu[i] - old_mu[i]) > EPSILON ||
253                             fabs(sigma[i] - old_sigma[i]) > EPSILON) {
254                                 any_difference = true;
255                                 break;
256                         }
257                 }
258                 if (!any_difference) {
259                         fprintf(stderr, "Converged after %d iterations. Stopping.\n", j);
260                         break;
261                 }
262         }
263         dump_scores(players, mu, sigma, num_players);
264
265 //      construct_fim(mu, sigma, num_players);
266 }