// Simulation code used in // Understanding the Generalized Method of // Moments (GMM): A simple example // http://blog.stata.com/2015/12/03/understanding-the-generalized-method-of-moments-gmm-a-simple-example/ // By David M. Drukker and Enrique Pinzon // version 1.0.1 04Dec2015 clear all set seed 12345 matrix I = I(2) postfile sim d_a d_v d_ml d_gmm d_gmme using efcomp, replace forvalues i = 1/2000 { quietly drop _all quietly set obs 500 quietly generate double y = rchi2(1) quietly mean y local d_a = _b[y] quietly gmm ( (y-{d=`d_a'})^2 - 2*{d}) , instruments( ) /// winitial(unadjusted) onestep conv_maxiter(200) if e(converged)==1 { local d_v = _b[d:_cons] } else { local d_v = . } quietly mlexp (ln(chi2den({d=`d_a'},y))) if e(converged)==1 { local d_ml = _b[d:_cons] } else { local d_ml = . } quietly gmm ( y - {d=`d_a'}) ( (y-{d})^2 - 2*{d}) , instruments( ) /// winitial(I) onestep conv_maxiter(200) if e(converged)==1 { local d_gmm = _b[d:_cons] } else { local d_gmm = . } quietly gmm ( y - {d=`d_a'}) ( (y-{d})^2 - 2*{d}) , instruments( ) /// winitial(unadjusted, independent) conv_maxiter(200) if e(converged)==1 { local d_gmme = _b[d:_cons] } else { local d_gmme = . } post sim (`d_a') (`d_v') (`d_ml') (`d_gmm') (`d_gmme') } postclose sim use efcomp, clear summarize