]> git.sesse.net Git - wloh/commitdiff
Compute and store total log-likelihood of model.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 18 Mar 2012 12:34:12 +0000 (13:34 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 18 Mar 2012 12:34:12 +0000 (13:34 +0100)
bayeswf.cpp

index 136a382349ac1528abadf2e5d1f325c93992cb5b..f2047bbf69549d510522bb316b33cf9788db3b5b 100644 (file)
@@ -218,6 +218,46 @@ void update_prior_sigma(float *mu, float *sigma, int num_players)
        }
 }
 
+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);
+       }
+
+       // Matches.
+       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;
+
+                       total_logl += w * compute_logl((x - mu) / sigma);
+               }
+       }
+
+       return total_logl;
+}
+
 /*
  * Compute Hessian matrix of the negative log-likelihood, ie. for each term in logL:
  *
@@ -362,6 +402,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
 }