From: Steinar H. Gunderson Date: Tue, 20 Mar 2012 20:56:43 +0000 (+0100) Subject: Store covariance matrix when training model. X-Git-Url: https://git.sesse.net/?p=wloh;a=commitdiff_plain;h=c5cf5cb78785bfd57ea74b23d72ce4e27439ee2f Store covariance matrix when training model. --- diff --git a/bayeswf.cpp b/bayeswf.cpp index a691b8f..f94b1d3 100644 --- a/bayeswf.cpp +++ b/bayeswf.cpp @@ -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 &players) { // FIXME: Use pseudoinverse if applicable. Matrix 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)); diff --git a/train.pl b/train.pl index 119b738..9238b61 100755 --- 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 () { - /(.*) (.*) (.*)/ 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);