#!/usr/bin/perl
#
# analyze correlations between various team statistics and winning
#     percentage.  Make a nice plot for each one.
#
# MWR 3/22/2009

$debug = 1;

# extra space at edges of graphs, as fraction of total range
$extra_frac = 0.05;

#############################################################################
# runs_scored
$infile = "./calc_correlations.runs_scored.out";
$cmd  = "awk '{ print \$10, \$8 }' $infile ";
$cmd .= "  | fitline ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, " " . $ret);
$intercept = $words[1];
$slope = $words[2];
$syx = $words[5];
$r_coeff = $words[11];
if ($debug > 0) {
  printf "  inter %10.5e  slope %10.5e  syx %7.4f   r %6.3f \n",
        $intercept, $slope, $syx, $r_coeff;
}

# make a graph for runs scored
  $title = "Which statistics explain winning percentage -- runs scored? ";
  $output_file = "./correlate_runs_scored.ps";
  $term_options = "postscript color ";
  $cmdfile = "./correlate_plots.gnu";

  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $min = `sort -nr +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  $max = `sort -n +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $minx = $min - ($max - $min)*$extra_frac;
  $maxx = $max + ($max - $min)*$extra_frac;
  $min = `sort -nr +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  $max = `sort -n +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $miny = $min - ($max - $min)*$extra_frac;
  $maxy = $max + ($max - $min)*$extra_frac;
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Runs scored during the season ' \n";
  printf CMDFILE "set ylabel 'Winning percentage ' \n";
  printf CMDFILE "set title '$title'\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] '$infile' ";
  $cmd .= "      using (\$10):(\$8) with points lt 1 pt 4 ps 0.8 lw 3  ";
  $cmd .= "      t '' ";
  $cmd .= " , ";
  $label_str = sprintf "R %5.3f  syx %6.4f", $r_coeff, $syx;
  $cmd .= " $intercept+$slope*x with lines lc -1 lt 1 t '$label_str' ";
  $cmd .= " , ";
  $cmd .= " ($intercept+0.0593)+$slope*x with lines lc -1 lt 2 t '' ";
  $cmd .= " , ";
  $cmd .= " ($intercept-0.0593)+$slope*x with lines lc -1 lt 2 t '' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }



#############################################################################
# runs_allowed
$infile = "./calc_correlations.runs_allowed.out";
$cmd  = "awk '{ print \$10, \$8 }' $infile ";
$cmd .= "  | fitline ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, " " . $ret);
$intercept = $words[1];
$slope = $words[2];
$syx = $words[5];
$r_coeff = $words[11];
if ($debug > 0) {
  printf "  inter %10.5e  slope %10.5e  syx %7.4f   r %6.3f \n",
        $intercept, $slope, $syx, $r_coeff;
}

# make a graph for runs scored
  $title = "Which statistics explain winning percentage -- runs allowed? ";
  $output_file = "./correlate_runs_allowed.ps";
  $term_options = "postscript color ";
  $cmdfile = "./correlate_plots.gnu";

  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $min = `sort -nr +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  $max = `sort -n +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $minx = $min - ($max - $min)*$extra_frac;
  $maxx = $max + ($max - $min)*$extra_frac;
  $min = `sort -nr +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  $max = `sort -n +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $miny = $min - ($max - $min)*$extra_frac;
  $maxy = $max + ($max - $min)*$extra_frac;
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xlabel 'Runs allowed during the season ' \n";
  printf CMDFILE "set ylabel 'Winning percentage ' \n";
  printf CMDFILE "set title '$title'\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] '$infile' ";
  $cmd .= "      using (\$10):(\$8) with points lt 1 pt 4 ps 0.8 lw 3  ";
  $cmd .= "      t '' ";
  $cmd .= " , ";
  $label_str = sprintf "R %5.3f  syx %6.4f", $r_coeff, $syx;
  $cmd .= " $intercept+$slope*x with lines lc -1 lt 1 t '$label_str' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




#############################################################################
# batters striking out
$infile = "./calc_correlations.so.out";
$cmd  = "awk '{ print \$10, \$8 }' $infile ";
$cmd .= "  | fitline ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, " " . $ret);
$intercept = $words[1];
$slope = $words[2];
$syx = $words[5];
$r_coeff = $words[11];
if ($debug > 0) {
  printf "  inter %10.5e  slope %10.5e  syx %7.4f   r %6.3f \n",
        $intercept, $slope, $syx, $r_coeff;
}

# make a graph for runs scored
  $title = "Which statistics explain winning percentage -- batting strikeouts? ";
  $output_file = "./correlate_so.ps";
  $term_options = "postscript color ";
  $cmdfile = "./correlate_plots.gnu";

  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $min = `sort -nr +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  $max = `sort -n +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $minx = $min - ($max - $min)*$extra_frac;
  $maxx = $max + ($max - $min)*$extra_frac;
  $min = `sort -nr +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  $max = `sort -n +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $miny = $min - ($max - $min)*$extra_frac;
  $maxy = $max + ($max - $min)*$extra_frac;
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top right \n";
  printf CMDFILE "set xlabel 'Batting strikeouts during the season' \n";
  printf CMDFILE "set ylabel 'Winning percentage ' \n";
  printf CMDFILE "set title '$title'\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] '$infile' ";
  $cmd .= "      using (\$10):(\$8) with points lt 1 pt 4 ps 0.8 lw 3  ";
  $cmd .= "      t '' ";
  $cmd .= " , ";
  $label_str = sprintf "R %5.3f  syx %6.4f", $r_coeff, $syx;
  $cmd .= " $intercept+$slope*x with lines lc -1 lt 1 t '$label_str' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }




#############################################################################
# batters walking
$infile = "./calc_correlations.bb.out";
$cmd  = "awk '{ print \$10, \$8 }' $infile ";
$cmd .= "  | fitline ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, " " . $ret);
$intercept = $words[1];
$slope = $words[2];
$syx = $words[5];
$r_coeff = $words[11];
if ($debug > 0) {
  printf "  inter %10.5e  slope %10.5e  syx %7.4f   r %6.3f \n",
        $intercept, $slope, $syx, $r_coeff;
}

# make a graph for runs scored
  $title = "Which statistics explain winning percentage -- walks earned? ";
  $output_file = "./correlate_bb.ps";
  $term_options = "postscript color ";
  $cmdfile = "./correlate_plots.gnu";

  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $min = `sort -nr +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  $max = `sort -n +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $minx = $min - ($max - $min)*$extra_frac;
  $maxx = $max + ($max - $min)*$extra_frac;
  $min = `sort -nr +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  $max = `sort -n +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $miny = $min - ($max - $min)*$extra_frac;
  $maxy = $max + ($max - $min)*$extra_frac;
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Walks earned during the season' \n";
  printf CMDFILE "set ylabel 'Winning percentage ' \n";
  printf CMDFILE "set title '$title'\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] '$infile' ";
  $cmd .= "      using (\$10):(\$8) with points lt 1 pt 4 ps 0.8 lw 3  ";
  $cmd .= "      t '' ";
  $cmd .= " , ";
  $label_str = sprintf "R %5.3f  syx %6.4f", $r_coeff, $syx;
  $cmd .= " $intercept+$slope*x with lines lc -1 lt 1 t '$label_str' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }


#############################################################################
# batters grounding into double plays
$infile = "./calc_correlations.gdp.out";
$cmd  = "awk '{ print \$10, \$8 }' $infile ";
$cmd .= "  | fitline ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, " " . $ret);
$intercept = $words[1];
$slope = $words[2];
$syx = $words[5];
$r_coeff = $words[11];
if ($debug > 0) {
  printf "  inter %10.5e  slope %10.5e  syx %7.4f   r %6.3f \n",
        $intercept, $slope, $syx, $r_coeff;
}

# make a graph for runs scored
  $title = "Which statistics explain winning percentage -- GIDP? ";
  $output_file = "./correlate_gdp.ps";
  $term_options = "postscript color ";
  $cmdfile = "./correlate_plots.gnu";

  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $min = `sort -nr +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  $max = `sort -n +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $minx = $min - ($max - $min)*$extra_frac;
  $maxx = $max + ($max - $min)*$extra_frac;
  $min = `sort -nr +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  $max = `sort -n +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $miny = $min - ($max - $min)*$extra_frac;
  $maxy = $max + ($max - $min)*$extra_frac;
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Double plays grounded into during the season' \n";
  printf CMDFILE "set ylabel 'Winning percentage ' \n";
  printf CMDFILE "set title '$title'\n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] '$infile' ";
  $cmd .= "      using (\$10):(\$8) with points lt 1 pt 4 ps 0.8 lw 3  ";
  $cmd .= "      t '' ";
  $cmd .= " , ";
  $label_str = sprintf "R %5.3f  syx %6.4f", $r_coeff, $syx;
  $cmd .= " $intercept+$slope*x with lines lc -1 lt 1 t '$label_str' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }





#############################################################################
# team index -- should show no correlation
$infile = "./calc_correlations.index.out";
$cmd  = "awk '{ print \$10, \$8 }' $infile ";
$cmd .= "  | fitline ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, " " . $ret);
$intercept = $words[1];
$slope = $words[2];
$syx = $words[5];
$r_coeff = $words[11];
if ($debug > 0) {
  printf "  inter %10.5e  slope %10.5e  syx %7.4f   r %6.3f \n",
        $intercept, $slope, $syx, $r_coeff;
}

# make a graph for runs scored
  $title = "Which statistics explain winning percentage? ";
  $output_file = "./correlate_index.ps";
  $term_options = "postscript color ";
  $cmdfile = "./correlate_plots.gnu";

  # determine the limits of the graph
  #    note that the y-axis is logarithmic
  $min = `sort -nr +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  $max = `sort -n +9 $infile | tail -1 | awk '{ printf \"%.4f\", \$10 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $minx = $min - ($max - $min)*$extra_frac;
  $maxx = $max + ($max - $min)*$extra_frac;
  $min = `sort -nr +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  $max = `sort -n +7 $infile | tail -1 | awk '{ printf \"%.4f\", \$8 }' `;
  if ($debug > 0) {
     printf " min %10.5f  max %10.5f \n", $min, $max;
  }
  $miny = $min - ($max - $min)*$extra_frac;
  $maxy = $max + ($max - $min)*$extra_frac;
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'Index of team in list' \n";
  printf CMDFILE "set ylabel 'Winning percentage ' \n";
  printf CMDFILE "set title '$title'\n";
  printf CMDFILE "set label 1 at 2, 0.32 'mean 0.501 stdev 0.080' \n";
  printf CMDFILE "set object 1 rect from -7,0.420 to 173,0.580 lw 0.5 fc rgbcolor '#f1f1f1' behind \n";
  
  $cmd = "plot [$minx:$maxx][$miny:$maxy] '$infile' ";
  $cmd .= "      using (\$10):(\$8) with points lt 1 pt 4 ps 0.8 lw 3  ";
  $cmd .= "      t '' ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;

  # create a GIF copy of any Postscript file
  if ($term_options =~ "postscript") {
    # printf "yes, term_options ..%s.. has postscript \n", $term_options;
    $gif_file = $output_file;
    $gif_file =~ s/.ps$/.gif/;
    $cmd = "convert -rotate 90 $output_file $gif_file ";
    printf "cmd is ..$cmd.. \n";
    $retval = `$cmd`;
  }







exit 0;




##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}

