function [ av, var ] = trstat ( pdf, parin )

%*****************************************************************************80
%
%% trstat() returns the mean and variance for distributions.
%
%  Discussion:
%
%    This procedure returns the mean and variance for a number of statistical
%    distributions as a function of their parameters.
%
%    The input vector PARIN is used to pass in the parameters necessary
%    to specify the distribution.  The number of these parameters varies
%    per distribution, and it is necessary to specify an ordering for the
%    parameters used to a given distribution.  The ordering chosen here
%    is as follows:
%
%    bet
%      PARIN(1) is A
%      PARIN(2) is B
%    bin
%      PARIN(1) is Number of trials
%      PARIN(2) is Prob Event at Each Trial
%    chi
%      PARIN(1) = df
%    exp
%      PARIN(1) = mu
%    f
%      PARIN(1) is df numerator
%      PARIN(2) is df denominator
%    gam
%      PARIN(1) is A
%      PARIN(2) is R
%    nbn
%      PARIN(1) is N
%      PARIN(2) is P
%    nch
%      PARIN(1) is df
%      PARIN(2) is noncentrality parameter
%    nf
%      PARIN(1) is df numerator
%      PARIN(2) is df denominator
%      PARIN(3) is noncentrality parameter
%    nor
%      PARIN(1) is mean
%      PARIN(2) is standard deviation
%    poi
%      PARIN(1) is Mean
%    unf
%      PARIN(1) is LOW bound
%      PARIN(2) is HIGH bound
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 March 2013
%
%  Author:
%
%    Original FORTRAN77 version by Barry Brown, James Lovato.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    string PDF, indicates the distribution:
%    'bet'  beta distribution
%    'bin'  binomial
%    'chi'  chisquare
%    'exp'  exponential
%    'f'    F (variance ratio)
%    'gam'  gamma
%    'nbn'  negative binomial
%    'nch'  noncentral chisquare
%    'nf'   noncentral f
%    'nor'  normal
%    'poi'  Poisson
%    'unf'  uniform
%
%    real PARIN(*), the parameters of the distribution.
%
%  Output:
%
%    real AV, the mean of the specified distribution.
%
%    real VAR, the variance of the specified distribuion.
%
  if ( strcmpi ( pdf, 'bet' ) )

    av = parin(1) / ( parin(1) + parin(2) );
    var = ( av * parin(2) ) / ( ( parin(1) + parin(2) ) * ...
      ( parin(1) + parin(2) + 1.0 ) );

  elseif ( strcmpi ( pdf, 'bin' ) )

    n = floor ( parin(1) );
    p = parin(2);
    av = n * p;
    var = n * p * ( 1.0 - p );

  elseif ( strcmpi ( pdf, 'chi' ) )

    av = parin(1);
    var = 2.0 * parin(1);

  elseif ( strcmpi ( pdf, 'exp' ) )

    av = parin(1);
    var = av ^ 2;

  elseif ( strcmpi ( pdf, 'f' ) )

    if ( parin(2) <= 2.0001 )
      av = -1.0;
    else
      av = parin(2) / ( parin(2) - 2.0 );
    end

    if ( parin(2) <= 4.0001 )
      var = -1.0;
    else
      var = ( 2.0 * parin(2) ^ 2 * ( parin(1) + parin(2) - 2.0 ) ) / ...
        ( parin(1) * ( parin(2) - 2.0 ) ^ 2 * ( parin(2) - 4.0 ) );
    end

  elseif ( strcmpi ( pdf, 'gam' ) )

    a = parin(1);
    r = parin(2);
    av = r / a;
    var = r / a ^ 2;

  elseif ( strcmpi ( pdf, 'nbn' ) )

    n = floor ( parin(1) );
    p = parin(2);
    av = n * ( 1.0 - p ) / p;
    var = n * ( 1.0 - p ) / p ^ 2;

  elseif ( strcmpi ( pdf, 'nch' ) )

    a = parin(1) + parin(2);
    b = parin(2) / a;
    av = a;
    var = 2.0 * a * ( 1.0 + b );

  elseif ( strcmpi ( pdf, 'nf' ) )

    if ( parin(2) <= 2.0001 )
      av = -1.0;
    else
      av = ( parin(2) * ( parin(1) + parin(3) ) ) ...
        / ( ( parin(2) - 2.0 ) * parin(1) );
    end

    if ( parin(2) <= 4.0001 )
      var = -1.0;
    else
      a = ( parin(1) + parin(3) ) ^ 2 ...
        + ( parin(1) + 2.0 * parin(3) ) * ( parin(2) - 2.0 );
      b = ( parin(2) - 2.0 ) ^ 2 * ( parin(2) - 4.0 );
      var = 2.0 * ( parin(2) / parin(1) ) ^ 2 * ( a / b );
    end

  elseif ( strcmpi ( pdf, 'nor' ) )

    av = parin(1);
    var = parin(2) ^ 2;

  elseif ( strcmpi ( pdf, 'poi' ) )

    av = parin(1);
    var = parin(1);

  elseif ( strcmpi ( pdf, 'unf' ) )

    width = parin(2) - parin(1);
    av = parin(1) + width / 2.0;
    var = width ^ 2 / 12.0;

  else

    fprintf ( 1, ' \n' );
    fprintf ( 1, 'TRSTAT - Fatal error!\n' );
    fprintf ( 1, '  Illegal input value for PDF.\n' );
    error ( 'TRSTAT - Fatal error!\n' );

  end

  return
end
