PHP Code:
#!perl -w
use strict;
use constant LEAGUE => uc 'MLB';
use constant MIN_GAMES => ( LEAGUE eq 'MLB' ? 162/4 : (LEAGUE eq 'NHL' ? 82/4 : ''));
use constant MIN_DOG_ODDS => +100;
use constant TOTAL_YEAR => 99999999;
my ($wins_r, $losses_r, $tot_bet_r, $tot_win_r, $tot_var_r, $count_r, $tot_util_r, );
my ($tot_bet, $tot_win, ) = (0,0);
my ($date, $vt, $ht, $vml, $hml, $tot, $tadj, $oadj, $uadj, $vsc, $hsc,$year,$month);
while (<>) **
chomp;
if (LEAGUE eq 'NHL') **
($date, $vt, $ht, $vml, $hml, $tot, $oadj, $uadj, $vsc, $hsc) = split/\t/; # nhl
# fmt (tab sep)=> date, visit team, home team, visit ml, home ml, total, over adj, under adj, visit score, home score
** elsif (LEAGUE eq 'MLB') **
($date, $vt, $ht, $vml, $hml, $tot, $tadj, $vsc, $hsc) = split/\t/; # mlb
# fmt (tab sep)=> date, visit team, home team, visit ml, home ml, total, total adj, visit score, home score
**
next unless $date > 0;
($year,$month) = $date =~ /^(....)(..)/;
$year++ if (LEAGUE eq 'NHL' && $month+0 >= 8);
if (
( ($wins_r->**$year**->**$vt** + $losses_r->**$year**->**$vt** < MIN_GAMES) &&
($wins_r->**$year**->**$ht** + $losses_r->**$year**->**$ht** < MIN_GAMES)
)
) **
# not enough games played
next;
**
my ($dog, $fave, $dog_score, $fave_score, $dog_ml, $fave_ml,);
if ($vml > $hml) **
# visitor is dog
$dog = $vt;
$fave = $ht;
$dog_score = $vsc;
$fave_score = $hsc;
$dog_ml = $vml;
$fave_ml = $hml;
** elsif ($hml > $vml) **
# home is dog
$dog = $ht;
$fave = $vt;
$dog_score = $hsc;
$fave_score = $vsc;
$dog_ml = $hml;
$fave_ml = $vml;
** else **
# no dog
next;
**
my ($fave_rec,$dog_rec) = (0,0);
if ($fave_rec + $dog_rec > 0) **
$dog_rec = $wins_r->**$year**->**$dog** / ($wins_r->**$year**->**$dog** + $losses_r->**$year**->**$dog**);
$fave_rec = $wins_r->**$year**->**$fave** / ($wins_r->**$year**->**$fave** + $losses_r->**$year**->**$fave**);
**
if ($fave_rec > $dog_rec) **
# fave record better than dog
next;
** elsif ($dog_ml < MIN_DOG_ODDS) **
# not a big enough dog
next;
**
my $dog_win = ($dog_score > $fave_score ? 1 : 0);
my $dec_dog_odds = &us2dec($dog_ml);
my $edge = 0.01;
# my $bet = $edge/($dec_dog_odds-1);
my $bet = 1;
my $win_amt = $bet * ( $dog_win * $dec_dog_odds - 1 );
my $var = $bet*$bet*($dec_dog_odds-1);
my $util = $bet < 1 ? log(1+$win_amt) : 0;
print "$date\t$dog\t$fave\t$dog_ml\t$fave_ml\t$dog_score\t$fave_score\t$dog_rec\t$fave_rec\t$bet\t$win_amt\t$var";
# print "\t$util";
print "\n";
$tot_var_r->**+TOTAL_YEAR** += $var;
$tot_bet_r->**+TOTAL_YEAR** += $bet;
$tot_win_r->**+TOTAL_YEAR** += $win_amt;
$count_r->**+TOTAL_YEAR**++;
$tot_util_r->**+TOTAL_YEAR** += $util;
$tot_var_r->**$year** += $var;
$tot_bet_r->**$year** += $bet;
$tot_win_r->**$year** += $win_amt;
$tot_util_r->**$year** += $util;
$count_r->**$year**++;
** continue **
if ($vsc > $hsc) **
$wins_r->**$year**->**$vt**++;
$losses_r->**$year**->**$ht**++;
** elsif ($vsc < $hsc) **
$wins_r->**$year**->**$ht**++;
$losses_r->**$year**->**$vt**++;
**
**
foreach my $year (sort keys %$tot_bet_r) **
my $disp_year = $year;
$disp_year = 'TOTAL' if $disp_year == TOTAL_YEAR;
printf STDERR "$disp_year\t$count_r->**$year**\t%0.4f\t%0.4f\t", $tot_win_r->**$year**,$tot_bet_r->**$year**;
printf STDERR "%0.4f%%\t", 100*$tot_win_r->**$year**/$tot_bet_r->**$year**;
printf STDERR "%0.6f", sqrt($tot_var_r->**$year**)/$tot_bet_r->**$year**;
# printf STDERR "%0.3f\t", $tot_win_r->**$year** / sqrt($tot_var_r->**$year**);
# printf STDERR "%0.4f%%\t", 100*exp($tot_util_r->**$year**)-1 if $tot_util_r->**$year**;
printf STDERR "\n";
**
sub us2dec($) **
my $us = 0 + shift;
return (
$us >= 100
? 1 + $us / 100
: ( $us <= -100 ? 1 - 100 / $us : 0 )
);
**