]> git.sesse.net Git - wloh/commitdiff
Store covariance matrix when training model.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 20 Mar 2012 20:56:43 +0000 (21:56 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 20 Mar 2012 20:56:43 +0000 (21:56 +0100)
bayeswf.cpp
train.pl

index a691b8fe915ac8f6bb8714e62bd2349c173201b1..f94b1d33f6e213f281fc308ad111ab06a69778ac 100644 (file)
@@ -227,13 +227,22 @@ void construct_hessian(const float *mu, int num_players)
 
 // Compute uncertainty (stddev) of mu estimates, which is sqrt((H^-1)_ii),
 // where H is the Hessian (see construct_hessian()).
-void compute_mu_uncertainty(const float *mu, int num_players)
+void compute_mu_uncertainty(const float *mu, const vector<string> &players)
 {
        // FIXME: Use pseudoinverse if applicable.
        Matrix<float, Dynamic, Dynamic> ih = hessian.inverse();
-       for (int i = 0; i < num_players; ++i) {
+       for (unsigned i = 0; i < players.size(); ++i) {
                mu_stddev[i] = sqrt(ih(i, i));
        }
+
+       for (unsigned i = 0; i < players.size(); ++i) {
+               for (unsigned j = 0; j < players.size(); ++j) {
+                       printf("covariance %s %s %f\n",
+                              players[i].c_str(),
+                              players[j].c_str(),
+                              ih(i, j));
+               }
+       }
 }
 
 int main(int argc, char **argv)
@@ -340,7 +349,7 @@ int main(int argc, char **argv)
        dump_raw(mu, num_players);
 #else
        construct_hessian(mu, num_players);
-       compute_mu_uncertainty(mu, num_players);
+       compute_mu_uncertainty(mu, players);
        dump_scores(players, mu, mu_stddev, num_players);
        //fprintf(stderr, "Optimal sigma: %f (two-player: %f)\n", sigma[0], sigma[0] * sqrt(2.0f));
        printf("%f 0 -2\n", global_sigma / sqrt(2.0f));
index 119b738dd4cddf322fee45107db587cb4df1dd47..9238b61cb73ef1e6945c352a46f5f04f257906e3 100755 (executable)
--- a/train.pl
+++ b/train.pl
@@ -11,6 +11,8 @@ my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config:
 $dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
+$dbh->do('SET client_min_messages TO WARNING');
+
 # Find last completely done season 
 my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier GROUP BY sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) ORDER BY sesong DESC LIMIT 1');
 my $last_season = $ref->{'sesong'};
@@ -54,15 +56,33 @@ for my $ref (@games) {
 }
 close DATA;
 
-$dbh->do('DELETE FROM ratings');
-my $iq = $dbh->prepare('INSERT INTO ratings ( id, rating, rating_stddev ) VALUES (?, ?, ?)');
+my @ratings = ();
+my @covariances = ();
 
 open RATINGS, "$config::base_dir/bayeswf < $tmpnam |"
        or die "bayeswf: $!";
 while (<RATINGS>) {
-       /(.*) (.*) (.*)/ or next;
-       $iq->execute($3, $1, $2);
+       chomp;
+       my @x = split;
+       if ($x[0] eq 'covariance') {
+               push @covariances, (join("\t", @x[1..3]));
+       } else {
+               push @ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
+       }
 }
 
+$dbh->do('TRUNCATE ratings');
+$dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
+$dbh->pg_putcopydata(join("\n", @ratings));
+$dbh->pg_putcopyend();
+
+$dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
+$dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
+$dbh->pg_putcopydata(join("\n", @covariances));
+$dbh->pg_putcopyend();
+$dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );');
+$dbh->do('DROP TABLE IF EXISTS covariance');
+$dbh->do('ALTER TABLE new_covariance RENAME TO covariance');
+
 $dbh->commit;
 unlink($tmpnam);