6 use CGI::Carp qw(fatalsToBrowser);
9 my $dbh = foosball::db_connect();
10 $dbh->{AutoCommit} = 0;
12 $dbh->do('delete from single_rating');
15 my $q = $dbh->prepare('select *,extract(epoch from gametime) as eptime from single_results order by gametime');
18 # Combined log-likelihood
21 while (my $ref = $q->fetchrow_hashref) {
22 for my $user (($ref->{'username1'}, $ref->{'username2'})) {
23 if (!exists($ratings{$user})) {
24 $ratings{$user} = [ 1500, 350, 0 ];
28 my $rating1 = $ratings{$ref->{'username1'}}->[0];
29 my $rd1 = $ratings{$ref->{'username1'}}->[1];
30 my $age1 = $ref->{'eptime'} - $ratings{$ref->{'username1'}}->[2];
32 my $rating2 = $ratings{$ref->{'username2'}}->[0];
33 my $rd2 = $ratings{$ref->{'username2'}}->[1];
34 my $age2 = $ref->{'eptime'} - $ratings{$ref->{'username2'}}->[2];
36 my $score1 = $ref->{'score1'};
37 my $score2 = $ref->{'score2'};
39 $rd1 = foosball::apply_aging($rd1, $age1 / 86400.0);
40 $rd2 = foosball::apply_aging($rd2, $age2 / 86400.0);
42 my ($newr1, $newrd1, $likelihood) = foosball::calc_rating($rating1, $rd1, $rating2, $rd2, $score1, $score2);
43 my ($newr2, $newrd2) = foosball::calc_rating($rating2, $rd2, $rating1, $rd1, $score2, $score1);
45 $cll += log($likelihood);
47 printf("%-10s - %-10s: %u - %u, new ratings %u/%u %u/%u [$likelihood]\n",
48 $ref->{'username1'}, $ref->{'username2'}, $ref->{'score1'},
49 $ref->{'score2'}, $newr1, $newrd1, $newr2, $newrd2);
50 $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
51 $ref->{'username1'}, $ref->{'gametime'}, $newr1, $newrd1, $newr1-$rating1);
52 $dbh->do('insert into single_rating values (?,?,?,?,?)', undef,
53 $ref->{'username2'}, $ref->{'gametime'}, $newr2, $newrd2, $newr2-$rating2);
55 $ratings{$ref->{'username1'}} = [ $newr1, $newrd1, $ref->{'eptime'} ];
56 $ratings{$ref->{'username2'}} = [ $newr2, $newrd2, $ref->{'eptime'} ];
62 printf "\nCombined negative log-likelihood (smaller value means a better model): %f\n",