function fish_sample ( total, seed )

%*****************************************************************************80
%
%% FISH_SAMPLE estimates the mean and variance of a fish population.
%
%  Discussion:
%
%    The uniform PDF for [0,1] has mean 0.5 and variance 1/12.
%
%    A fish population is created by sampling the uniform PDF for [0,1].
%    This population has a mean and variance which are probably close to,
%    but not equal to, the PDF statistics.
%
%    We can think of each value F as a weight or size for the fish.
%
%    Now we seek to estimate the average weight, and the variance of the weight,
%    by fishing, that is, selecting a small sample of fish, computing their
%    mean and variance, and using that to estimate the statistics of the full
%    population.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 February 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer TOTAL, the total number of fish in the population.
%
%    Input, integer SEED, a seed for the random number generator.
%

%  Initialize.
%
  rng ( seed );
%
%  Generate the data.
%
  f = rand ( total, 1 );
%
%  Display the distribution average and variance.
%
  mu = 1.0 / 2.0;
  mu_sq = 1.0 / 3.0;
  var = 1.0 / 12.0;

  fprintf ( 1, '\n' );
  fprintf ( 1, '                     E(X)          E(X^2)        Error        Variance\n' );
  fprintf ( 1, '                                           estimating E(X)\n' );
  fprintf ( 1, '\n' );

  fprintf ( 1, '           PDF  %12g  %12g                %12g\n', mu, mu_sq, var );
  fprintf ( 1, '\n' );
%
%  Compute the data average and variance.
%
  mu_dat    = sum ( f(1:total)    ) / total;
  mu_dat_sq = sum ( f(1:total).^2 ) / total;

  var = mu_dat_sq - mu_dat^2;

  fprintf ( 1, '  %12d  %12g  %12g                %12g\n', total, mu_dat, mu_dat_sq, var );
  fprintf ( 1, '\n' );
%
%  Choose a sample (with possible repetitition).
%
  total_log = log2 ( total );
  sample = 1;

  for sample_log = 0 : total_log
  
    i = randint ( sample, 1, [1, total ] );
%
%  Estimate the data average and variance.
%
    mu_sam    = sum ( f(i)    ) / sample;
    mu_sq_sam = sum ( f(i).^2 ) / sample;
    e = abs ( mu_sam - mu_dat );

    var_sam = mu_sq_sam - mu_sam^2;

    fprintf ( 1, '  %12d  %12g  %12g  %12g  %12g\n', sample, mu_sam, mu_sq_sam, e, var_sam );

    sample = sample * 2;

  end

  return
end

