function f=gmm(beta,X,Y,Z,V) m=zeros(size(Z,2),1); N=size(Y,1); for i=1:N m=m+Z(i,:)'*(Y(i)-X(i,:)*beta)/N; end f=m'*inv(V)*m;