X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=bayeswf.cpp;h=487774ea394cb993c7b5c4499cd64f3fb6c30d55;hb=1e27ce64b3bd8574c621f1b7d47f94593d99318b;hp=71f60871a578fb180d972c775d0a4a0ba9cb8749;hpb=ca3f74e46ced6222edcb08abd4217f314f2962d6;p=wloh diff --git a/bayeswf.cpp b/bayeswf.cpp index 71f6087..487774e 100644 --- a/bayeswf.cpp +++ b/bayeswf.cpp @@ -11,6 +11,7 @@ using namespace std; #define PRIOR_MU 1500 +#define PRIOR_WEIGHT 1.0 #define MAX_PLAYERS 4096 #define DUMP_RAW 0 @@ -32,11 +33,12 @@ float prior_sigma = 70.0f; */ struct match { - int other_player; + int player, other_player; int margin; float weight; }; map > matches_for_player; +vector all_matches; void dump_scores(const vector &players, const float *mu, const float *sigma, int num_players) { @@ -74,8 +76,8 @@ void update_mu(float *mu, float *sigma, int player_num, const vector &mat // Prior. { float inv_sigma2 = 1.0f / (prior_sigma * prior_sigma); - nom += PRIOR_MU * inv_sigma2; - denom += inv_sigma2; + nom += PRIOR_WEIGHT * PRIOR_MU * inv_sigma2; + denom += PRIOR_WEIGHT * inv_sigma2; } // All matches. @@ -93,26 +95,19 @@ void update_mu(float *mu, float *sigma, int player_num, const vector &mat void dump_raw(const float *mu, const float *sigma, int num_players) { - for (int i = 0; i < num_players; ++i) { - for (unsigned j = 0; j < matches_for_player[i].size(); ++j) { - const match& m = matches_for_player[i][j]; - - // Only count each match once. - if (m.other_player <= i) { - continue; - } - - float mu1 = mu[i]; - float mu2 = mu[m.other_player]; - float sigma1 = sigma[i]; - float sigma2 = sigma[m.other_player]; - float sigma = sqrt(sigma1 * sigma1 + sigma2 * sigma2); - float mu = mu1 - mu2; - float x = m.margin; - float w = m.weight; - - printf("%f %f\n", (x - mu) / sigma, w); - } + for (unsigned i = 0; i < all_matches.size(); ++i) { + const match& m = all_matches[i]; + + float mu1 = mu[m.player]; + float mu2 = mu[m.other_player]; + float sigma1 = sigma[m.player]; + float sigma2 = sigma[m.other_player]; + float sigma = sqrt(sigma1 * sigma1 + sigma2 * sigma2); + float mu = mu1 - mu2; + float x = m.margin; + float w = m.weight; + + printf("%f %f\n", (x - mu) / sigma, w); } } @@ -158,24 +153,17 @@ void update_sigma(float *mu, float *sigma, int player_num, const vector & void update_global_sigma(float *mu, float *sigma, int num_players) { float nom = 0.0f, denom = 0.0f; - for (int i = 0; i < num_players; ++i) { - for (unsigned j = 0; j < matches_for_player[i].size(); ++j) { - const match& m = matches_for_player[i][j]; - - // Only count each match once. - if (m.other_player <= i) { - continue; - } - - float mu1 = mu[i]; - float mu2 = mu[m.other_player]; - float mu = mu1 - mu2; - float x = m.margin; - float w = m.weight; - - nom += w * ((x - mu) * (x - mu)); - denom += w; - } + for (unsigned i = 0; i < all_matches.size(); ++i) { + const match& m = all_matches[i]; + + float mu1 = mu[m.player]; + float mu2 = mu[m.other_player]; + float mu = mu1 - mu2; + float x = m.margin; + float w = m.weight; + + nom += w * ((x - mu) * (x - mu)); + denom += w; } float best_sigma = sqrt(nom / denom) / sqrt(2.0f); // Divide evenly between the two players. @@ -195,23 +183,49 @@ void update_prior_sigma(float *mu, float *sigma, int num_players) { float nom = 0.0f, denom = 0.0f; for (int i = 0; i < num_players; ++i) { - for (unsigned j = 0; j < matches_for_player[i].size(); ++j) { - const match& m = matches_for_player[i][j]; + float mu1 = mu[i]; - // Only count each match once. - if (m.other_player <= i) { - continue; - } + nom += ((mu1 - PRIOR_MU) * (mu1 - PRIOR_MU)); + denom += 1.0f; + } - float mu1 = mu[i]; + prior_sigma = sqrt(nom / denom); + if (!(prior_sigma > 40.0f)) { + prior_sigma = 40.0f; + } +} - float w = m.weight; - nom += w * ((mu1 - PRIOR_MU) * (mu1 - PRIOR_MU)); - denom += w * 1.0f; - } +float compute_logl(float z) +{ + return -0.5 * (log(2.0f / M_PI) + z * z); +} + +float compute_total_logl(float *mu, float *sigma, int num_players) +{ + float total_logl = 0.0f; + + // Prior. + for (int i = 0; i < num_players; ++i) { + total_logl += PRIOR_WEIGHT * compute_logl((mu[i] - PRIOR_MU) / prior_sigma); } - prior_sigma = sqrt(nom / denom); + // Matches. + for (unsigned i = 0; i < all_matches.size(); ++i) { + const match& m = all_matches[i]; + + float mu1 = mu[m.player]; + float mu2 = mu[m.other_player]; + float sigma1 = sigma[m.player]; + float sigma2 = sigma[m.other_player]; + float sigma = sqrt(sigma1 * sigma1 + sigma2 * sigma2); + float mu = mu1 - mu2; + float x = m.margin; + float w = m.weight; + + total_logl += w * compute_logl((x - mu) / sigma); + } + + return total_logl; } /* @@ -227,20 +241,23 @@ void construct_hessian(const float *mu, const float *sigma, int num_players) { memset(hessian, 0, sizeof(hessian)); - for (int i = 0; i < num_players; ++i) { - double sigma1 = sigma[i]; + for (unsigned i = 0; i < all_matches.size(); ++i) { + const match &m = all_matches[i]; - for (unsigned k = 0; k < matches_for_player[i].size(); ++k) { - int j = matches_for_player[i][k].other_player; + int p1 = m.player; + int p2 = m.other_player; - double sigma2 = sigma[j]; - double sigma_sq = sigma1 * sigma1 + sigma2 * sigma2; + double sigma1 = sigma[m.player]; + double sigma2 = sigma[m.other_player]; - float w = matches_for_player[i][k].weight; + double sigma_sq = sigma1 * sigma1 + sigma2 * sigma2; + float w = m.weight; - hessian[i][j] -= w / sigma_sq; - hessian[i][i] += w / sigma_sq; - } + hessian[p1][p2] -= w / sigma_sq; + hessian[p2][p1] -= w / sigma_sq; + + hessian[p1][p1] += w / sigma_sq; + hessian[p2][p2] += w / sigma_sq; } for (int i = 0; i < num_players; ++i) { @@ -301,16 +318,20 @@ int main(int argc, char **argv) } match m1; + m1.player = player_map[pl1]; m1.other_player = player_map[pl2]; m1.margin = score1 - score2; m1.weight = weight; matches_for_player[player_map[pl1]].push_back(m1); match m2; + m2.player = player_map[pl2]; m2.other_player = player_map[pl1]; m2.margin = score2 - score1; m2.weight = weight; matches_for_player[player_map[pl2]].push_back(m2); + + all_matches.push_back(m1); } float mu[MAX_PLAYERS]; @@ -358,6 +379,9 @@ int main(int argc, char **argv) printf("%f -2\n", sigma[0]); printf("%f -3\n", prior_sigma); + float total_logl = compute_total_logl(mu, sigma, num_players); + printf("%f -4\n", total_logl); + // construct_hessian(mu, sigma, num_players); #endif }