function [J,dJ]=gmmj(theta);
%GMMJ2 Hansen's (1982) J statistic.
% [J,dJ]=gmmj(theta) computes
%
% J = g(theta)' W g(theta)
%
% and (if iagflg=1 in UNCMIN) dJ=dJ(theta)/dtheta where
%
% g(theta) = E [ f(x(t+n),z(t),theta) ]
%
% and W is some (globally known) weighting matrix.
%
% This file is called by GMM2 and UNCMIN.
% See also GMM2, GMMSTART, GMMH, and UNCMIN.
%
% Ellen McGrattan, 12-07-92
%
if nargout>1;
[g,dg] = mlgmmgx(theta);
if g~=-99; dJ=2*dg*Wgmm*g; end;
else;
g = mlgmmgx(theta);
end;
J=g'*Wgmm*g;
if g == -99;
J = 1e+20;
dJ = 1e+20*ones(lgmm,1);
return
end;