function baseball ( )

%*****************************************************************************80
%
%% baseball() models a baseball season.
%
%  Discussion:
%
%    This function calculates and plots (as a function of p) the ratio
%    of the probabilities of a team winning at least 81p games out of
%    81 games to the probability of winning at least 162p games out
%    of 162 games, where p is the probability of winning any individual game.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'baseball()\n' );
  fprintf ( 1, '  Calculate and plot (as a function of p) the ratio\n' );
  fprintf ( 1, '  of the probabilities of a team winning at least 81p games out of\n' );
  fprintf ( 1, '  81 games to the probability of winning at least 162p games out\n' );
  fprintf ( 1, '  of 162 games, where p is the probability of winning any individual game.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Present results graphically.\n' );

  res = 0.001;

  for p = res : res : 1 - res

    s81 = ceil ( 81 * p );
    s162 = ceil ( 162 * p );
%
%  Numerator sum.
%
    sumnum = 0;
    for k = s81 : 81
      bc = binomial ( 81, k );
      sumnum = sumnum + bc * ( p ^ k ) * ( ( 1 - p ) ^ ( 81 - k ) );
    end
%
%  Denominator sum.
%
    sumden = 0;
    for k = s162 : 162
      bc = binomial ( 162, k );
      sumden = sumden + bc * ( p ^ k ) * ( ( 1 - p ) ^ ( 162 - k ) );
    end

    index = round ( p / res );
    ratio(index) = sumnum / sumden;
    x(index) = p;

  end
%
%  Graphics.
%
  clf ( );
  plot ( x, ratio, 'linewidth', 3 );
  grid ( 'on' );
  title ( 'Ratio of Probabilities of Winning At Least np Games Out of n, n=81/np=162' );
  xlabel ( 'p, probability of winning any individual game' );
  ylabel ( 'ratio' );
  filename = 'baseball.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end
             
