function q_bar = sum_estimate ( n )

%*****************************************************************************80
%
%% SUM_ESTIMATE determines the average sum of a pair of dice.
%
%  Discussion:
%
%    A pair of dice are to be thrown N times, and the results (I,J)
%    observed.
%
%    Let F(I,J) be the observed frequency of (I,J), and from this,
%    estimate the probability P(I,J) = F(I,J) / N.
%
%    Let Q(I,J) be the quanity of interest, namely I+J.  If the probabilities
%    were given to us beforehand, then we can determine the expected value of
%    Q.  Since the probabilities are actually estimates from observations, the
%    formula gives us the average value of Q.  In either case, the formula is
%
%      Q_BAR = sum P(I,J) * Q(I,J).
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 February 2013
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of times the dice are rolled.
%
%    Input, integer Q_BAR, the average value of Q, the sum.
%
%
%  Compute n random pairs of integers between 1 and 6.
%
  d = randi ( [ 1, 6 ], n, 2 );
%
%  F(I,J) counts how many times the pair (I,J) occurs.
%
  f = zeros ( 6, 6 );

  for i = 1 : 6
    for j = 1 : 6
      f(i,j) = length ( find ( d(:,1) == i & d(:,2) == j ) );
    end
  end
%
%  P is the estimated probability.
%
  p(1:6,1:6) = f(1:6,1:6) / n;
%
%  Q is the quantity of interest.
%
  q = zeros ( 6, 6 );
  for i = 1 : 6
    for j = 1 : 6
      q(i,j) = i + j;
    end
  end
%
%  Now determine Q_BAR, the average quantity of interest.
%
  q_bar = sum ( sum ( p(1:6,1:6) .* q(1:6,1:6) ) );

  return
end
