function estimate ( )

%*****************************************************************************80
%
%% ESTIMATE estimates the number of runners in a marathon.
%
%  Discussion:
%
%    N runners participate in a marathon.
%    Each runner wears a tag with their index, from 1 to N.
%    We observe the values of K of these tags.
%    We want to estimate N.
%
%  Modified:
%
%    28 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ESTIMATE:\n' );
  fprintf ( 1, '  N runners participate in a marathon.\n' );
  fprintf ( 1, '  Each runner wears a tag with their index, from 1 to N.\n' );
  fprintf ( 1, '  We observe the values of K of these tags.\n' );
  fprintf ( 1, '  We want to estimate N.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Produce illustrative plots for several cases.\n' );

  clf

  size = [ 2, 5, 10, 20 ];

  for z = 1 : 4

    s = size(z);

    for loop = 1 : 10000

      N = round ( 900 * rand ( ) ) + 100;
      population = randperm ( N );
      n = round ( s * N / 100 );
      new_N = N;
      new_n = n;
      observed = zeros ( 1, n );
      j = 1;

      for k = 1 : N
        p = new_n / new_N;
        new_N = new_N - 1;
        if ( rand ( ) < p )
          observed(j) = population(k);
          j = j + 1;
          new_n = new_n - 1;
        end
      end

      maximum = max ( observed );
      error(loop) = (((n+1)/n)*maximum-1-N)*100/N;
    end

    subplot(2,2,z);
    hist(error,100);
    xlabel ( 'Percent error' );
    ylabel ( 'Number of simulations' );
    title_str = [ 'sample size = ' int2str(size(z)) '%'];
    title ( title_str );

  end

  filename = 'estimate.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
  pause ( 5 );

  return
end
