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 GNU LGPL license.
#
#  Modified:
#
#    08 February 2023
#
#  Author:
#
#    Original MATLAB version by Paul Nahin.
#    This version by John Burkardt.
#
#  Reference:
#
#    Paul Nahin,
#    Dueling Idiots and Other Probability Puzzlers,
#    Princeton, 2012,
#    ISBN: 978-0691155005.
#
  print ( '' )
  print ( 'baseball()' )
  print ( '  Calculate and plot (as a function of p) the ratio' )
  print ( '  of the probabilities of a team winning at least 81p games out of' )
  print ( '  81 games to the probability of winning at least 162p games out' )
  print ( '  of 162 games, where p is the probability of winning any individual game.' )
  print ( '' )
  print ( '  Present results graphically.' )

  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 ) )

    index = round ( p / res )
    ratio(index) = sumnum / sumden
    x(index) = p
#
#  Graphics.
#
  plt.clf ( )
  plt.plot ( x, ratio )
  plt.grid ( True )
  plt.title ( 'Ratio of Probabilities of Winning At Least np Games Out of n, n=81/np=162' )
  plt.xlabel ( 'p, probability of winning any individual game' )
  plt.ylabel ( 'ratio' )
  filename = 'baseball.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  return
             
