http://www.perlmonks.org?node_id=899752
dwm042 has asked for the wisdom of the Perl Monks concerning the following question:

I started blogging sports recently and while doing so I've been running into algorithms and procedures in the area of sports analytics (what used to be known as sabermetrics). Sports analytics received a huge shot in the arm with the publication of Michael Lewis's book "Moneyball", and these days, according to Mark Cuban, perhaps 2/3 of all US basketball teams have an analytics team. That said, some of us like to do this on an amateur basis. One important algorithm is Doug Drinen's simple rankings system. I've implemented this one 5 different ways now. A particularly clean version can be written in PDL.
sub srs_full_matrix { my $mov = shift; my $played = shift; my $opt = shift || (); my $epsilon = 0.001; my $maxiter = 1000000; my $debug = 0; $epsilon = $opt->{epsilon} if ( $opt->{epsilon} ); $maxiter = $opt->{maxiter} if ( $opt->{maxiter} ); $debug = $opt->{debug} if ( $opt->{debug} ); my $srs = $mov->copy(); my $oldsrs = $srs->copy(); my $delta = 10.0; my $iter = 0; while ( $delta > $epsilon and $iter < $maxiter ) { my $wt = 1.0 / sumover $played; my $prod = $srs x $played; my $sos = $wt * $prod; $srs = $mov + $sos; $delta = max abs ( $srs - $oldsrs ); $oldsrs = $srs->copy(); $iter++; } print "iter = $iter\n" if $debug; print "epsilon = $epsilon\n" if $debug; printf "delta = %7.4f\n", $delta if $debug; my $offset = sum $srs; $srs -= ( $offset / $mov->nelem ); return $srs->slice(":,(0)"); }
I have this function wrapped in Module::Starter with a working test harness now, and getting something like this into CPAN is the reason I just asked for a PAUSE account. Just, I don't see this code as CPAN ready.
1. There needs to be a non-pdl version.
2. The non-PDL version needs to accept very simple input and give a simple answer; input as an array of games, output as a hash of team named mapped to SRS values.
3. error checking on input.
4. helper functions? yes or no? Is something like this okay packaged with the PDL version?
sub load_srs_data { my $games = shift; my %team; for ( @$games ) { my ( $visitor, $visit_score, $home_team, $home_score ) = split + "\,", $_; my $diff = $home_score - $visit_score; $team{$visitor}{games_played}++; $team{$home_team}{games_played}++; $team{$visitor}{points} -= $diff; $team{$home_team}{points} += $diff; push @{$team{$visitor}{played}}, $home_team; push @{$team{$home_team}{played}}, $visitor; } my $total_team = scalar keys %team; my $played = zeroes $total_team, $total_team; my $mov = zeroes $total_team; my %team_map; my $ii = 0; for ( sort keys %team ) { my $team_diff = $team{$_}{points} / $team{$_}{games_played}; $team_map{$_} = $ii; $mov->set( $ii, $team_diff ); $ii++; } for ( keys %team ) { my $i = $team_map{$_}; for my $opp (@{$team{$_}{played}}) { my $j = $team_map{$opp}; my $a = $played->at ( $i, $j ); $a++; $played->set( $i, $j, $a ); } } return \%team, \%team_map, $mov, $played; }
What I'm trying to do is some "design on paper" before I upload..
and yes..
5. Name space. I was using Sports::Analytics::SRS but a simple ranking function isn't the "System". SimpleRanking spelled out with Analytics or SportsAnalytics as a predecessor seems more appropriate.
Sports isn't an initial name anywhere in CPAN that I can see.

Algorithm::SportsAnalytics::SimpleRanking, perhaps, with Algorithm::SportsAnalytics::SimpleRanking::PDL for a PDL implementation?