9 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
10 or die "connect: " . $DBI::errstr;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
14 $dbh->do('SET client_min_messages TO WARNING');
16 # Find last completely done season
17 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');
18 my $last_season = $ref->{'sesong'};
21 my $q = $dbh->prepare('
23 deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt
25 Fotballresultater resultater
26 JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie
27 JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie
28 JOIN Fotballserier serier on resultater.Serie=serier.Nr
29 WHERE deltager1.Nr > deltager2.nr
31 $q->execute($last_season);
36 while (my $ref = $q->fetchrow_hashref) {
37 next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0);
38 next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 150);
39 next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 150);
40 next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 0);
41 push @games, { %$ref };
42 $ids{$ref->{'p1'}} = 1;
43 $ids{$ref->{'p2'}} = 1;
47 my $tmpnam = POSIX::tmpnam();
48 open DATA, ">", $tmpnam
51 printf DATA "%d\n", scalar keys %ids;
52 for my $id (keys %ids) {
53 printf DATA "%d\n", $id;
55 for my $ref (@games) {
56 printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'};
63 open RATINGS, "$config::base_dir/bayeswf < $tmpnam |"
68 if ($x[0] eq 'covariance') {
69 push @covariances, (join("\t", @x[1..3]));
71 push @ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
75 $dbh->do('TRUNCATE ratings');
76 $dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
77 $dbh->pg_putcopydata(join("\n", @ratings));
78 $dbh->pg_putcopyend();
80 $dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
81 $dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
82 $dbh->pg_putcopydata(join("\n", @covariances));
83 $dbh->pg_putcopyend();
84 $dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );');
85 $dbh->do('DROP TABLE IF EXISTS covariance');
86 $dbh->do('ALTER TABLE new_covariance RENAME TO covariance');