function [logL,avar] = gamlike(params,data)
%GAMLIKE Negative gamma log-likelihood function.
%   L = GAMLIKE(PARAMS,DATA) returns the negative of the gamma log-likelihood 
%   function for the parameters PARAMS(1) = A and PARAMS(12) = B, given DATA.
%
%   [LOGL, AVAR] = GAMLIKE(PARAMS,DATA) adds the inverse of Fisher's
%   information matrix, AVAR. If the input parameter values in PARAMS
%   are the maximum likelihood estimates, the diagonal elements of AVAR
%   are their asymptotic variances.  AVAR is based on the observed
%   Fisher's information, not the expected information.
%
%   GAMLIKE is a utility function for maximum likelihood estimation. 
%   See also BETALIKE, GAMFIT, NORMLIKE, MLE, WEIBLIKE.

%   B.A. Jones 11-4-94, Z. You 5-25-99
%   Copyright 1993-2000 The MathWorks, Inc. 
%   $Revision: 2.13 $  $Date: 2000/05/26 18:52:53 $

if nargin < 2, 
    error('Requires at least two input arguments'); 
end

[n, m] = size(data);

if nargout == 2 & max(m,n) == 1
    error('To compute the 2nd output, the 2nd input must have at least two elements.');
end

if n == 1
   data = data';
   n = m;
end

a = params(1);
b = params(2);
ld = log(data);
lb = log(b);
la = gammaln(a);

logL = sum(data/b + gammaln(a) + a*lb - (a-1)*ld);

if nargout == 2
  delta = sqrt(eps);
  bd = b + delta;
  J = [la-gammaln(a+delta)+delta*(ld-lb), data*(1/b-1/bd)+a*log(b/bd)]/delta;
  [Q,R]= qr(J,0);
  Rinv = R\eye(2);
  avar = Rinv*Rinv';
end
