Mata Programming II
Christopher F Baum
Boston College and DIW Berlin
NCER, Queensland University of Technology, August 2015
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 1 / 73
Mata Programming II Christopher F Baum Boston College and DIW - - PowerPoint PPT Presentation
Mata Programming II Christopher F Baum Boston College and DIW Berlin NCER, Queensland University of Technology, August 2015 Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 1 / 73 Mata-based likelihood function evaluators
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 1 / 73
Mata-based likelihood function evaluators
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 2 / 73
Mata-based likelihood function evaluators
. type mynormal_lf.ado *! mynormal_lf v1.0.0 CFBaum 08feb2014 program mynormal_lf version 13.1 args lnfj mu sigma quietly replace `lnfj´ = ln( normalden( $ML_y1, `mu´, `sigma´ ) ) end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 3 / 73
Mata-based likelihood function evaluators
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 4 / 73
Mata-based likelihood function evaluators
. version 13 . mata: mata (type end to exit) : : void mynormal_lf( transmorphic scalar ML, > real rowvector b, > real colvector lnfj) > { > real vector depvar, xb > real scalar sigma > depvar = moptimize_util_depvar(ML, 1) > xb = moptimize_util_xb(ML, b, 1) > sigma = moptimize_util_xb(ML, b, 2) > > lnfj = ln( normalden( depvar, xb, sigma) ) > } : end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 5 / 73
Mata-based likelihood function evaluators
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 6 / 73
Mata-based likelihood function evaluators
. sysuse auto,clear (1978 Automobile Data) . ml model lf mynormal_lf() (mpg = weight displacement) /sigma . ml maximize, nolog initial: log likelihood =
(could not be evaluated) feasible: log likelihood = -10383.274 rescale: log likelihood = -292.89564 rescale eq: log likelihood = -238.45986 Number of obs = 74 Wald chi2(2) = 139.21 Log likelihood =
Prob > chi2 = 0.0000 mpg Coef.
z P>|z| [95% Conf. Interval] eq1 weight
.0011424
0.000
displacement .0052808 .0096674 0.55 0.585
.0242286 _cons 40.08452 1.978738 20.26 0.000 36.20627 43.96278 sigma _cons 3.385282 .2782684 12.17 0.000 2.839886 3.930678
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 7 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 8 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 9 / 73
Creating arrays of temporary objects with pointers
forvalues i = 1/‘lags’ { tempname phi‘i’ matrix ‘phi‘i” = exp }
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 10 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 11 / 73
Creating arrays of temporary objects with pointers
: X = (1, 2 \3, 4) : p = &X
: Z = *p
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 12 / 73
Creating arrays of temporary objects with pointers
: r = (*p)[ 2, .]
: p 0x2823b10c
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 13 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 14 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 15 / 73
Creating arrays of temporary objects with pointers
: pointer(real matrix) rowvector ap : ap = J(1, nlag, NULL)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 16 / 73
Creating arrays of temporary objects with pointers
: for(i = 1; i <= nlag; i++) { : ap[i] = &(matrix expression) : }
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 17 / 73
Creating arrays of temporary objects with pointers
: pointer(real vector) matrix p2
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 18 / 73
Creating arrays of temporary objects with pointers
: mata: : void pointer2(real scalar bar1, real scalar bar2) : { : pointer(pointer(real matrix) rowvector) rowvector pp : pp = J(1, bar2, NULL) : }
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 19 / 73
Creating arrays of temporary objects with pointers
: for(j = 1; j <= bar2; j++) { : pp[j] = &(J(1, bar1, NULL)) : for(i = 1; i <= bar1; i++) { : (*pp[j])[i] = &(J(i , j, i*j + j ^2) ) : } : }
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 20 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 21 / 73
Creating arrays of temporary objects with pointers
: for(k = 1; k <= bar2; k++) { : for(l = 1; l <= bar1; l++) { : printf("j=%5.2f, i=%5.2f\n", k, l) : *(*pp[k])[l] : } : } : end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 22 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 23 / 73
Creating arrays of temporary objects with pointers
. mata: pointer2(2, 3) j= 1, i= 1 2 j= 1, i= 2 1 1 3 2 3 j= 2, i= 1 1 2 1 6 6 j= 2, i= 2 [symmetric] 1 2 1 8 2 8 8
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 24 / 73
Creating arrays of temporary objects with pointers
j= 3, i= 1 1 2 3 1 12 12 12 j= 3, i= 2 1 2 3 1 15 15 15 2 15 15 15
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 25 / 73
Creating arrays of temporary objects with pointers
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 26 / 73
Structures
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 27 / 73
Structures
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 28 / 73
Structures
. type myvecstr.mata mata: mata clear mata: mata set matastrict on version 13.1 mata: struct mypoint { real vector coords } struct myvecstr { struct mypoint scalar pt real scalar length, angle string scalar color } end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 29 / 73
Structures
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 30 / 73
Structures
. type makevec.mata version 13.1 mata: function makevec(real scalar xcoord, real scalar ycoord, real scalar len, real scalar ang, string scalar color) { struct myvecstr scalar v v.pt.coords = (xcoord, ycoord) v.length = len v.angle = ang v.color = color myvecsub(v) } end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 31 / 73
Structures
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 32 / 73
Structures
. type myvecsub.mata version 13.1 mata: function myvecsub(struct myvecstr scalar e) { real scalar dist_from_origin, xx, yy, ll, ang string scalar clr xx = e.pt.coords[1] yy = e.pt.coords[2] dist_from_origin = sqrt(xx^2 + yy^2) printf("\n The %s vector begins %7.2f units from the origin at (%f, %f)", /// e.color, dist_from_origin, xx, yy) printf("\n It is %7.2f units long, at an angle of %5.2f degrees\n", e.length, > e.angle) } end
. mata: mata (type end to exit) : makevec(42, 30, 12, 45, "red") The red vector begins 51.61 units from the origin at (42, 30) It is 12.00 units long, at an angle of 45.00 degrees : end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 33 / 73
Structures
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 34 / 73
Structures
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 35 / 73
Associative arrays in Mata functions
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 36 / 73
Associative arrays in Mata functions
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 37 / 73
Associative arrays in Mata functions
: void function loadhash(string scalar filename) > { > external A > string colvector wpelts > A = asarray_create() > wpelts = J(7,1,"") > fh = fopen(filename, "r") > while ((line=fget(fh))!=J(0,0,"")) { > if(substr(line,1,6) != "Handle") { > if(substr(line,1,5) == "Title") { > i = 1 > wpelts[i] = substr(line,7,.) > } > if(substr(line,1, 11) == "Author-Name") { > ++i > wpelts[i] = substr(line, 13,.) > } > if(substr(line,1, 13 ) == "Creation-Date") wpelts[6] = substr(line, 15,.) > if(substr(line,1, 13 ) == "Revision-Date") wpelts[7] = substr(line, 15,.) > if(substr(line,1,6) == "Number") { > wpnum = substr(line,9,3) > asarray(A, wpnum, wpelts) > wpelts = J(7,1,"") > } > } > } > fclose(fh) > printf("\n%4.0f elements loaded in hash\n", asarray_elements(A)) > }
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 38 / 73
Associative arrays in Mata functions
: : void function gethash(string scalar key) > { > external A > asarray(A,key) > } : end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 39 / 73
Associative arrays in Mata functions . mata: loadhash("bcwp.rdf") 19 elements loaded in hash . mata: gethash("874") 1 Is the Intrinsic Value of Macroeconomic News Announcements Related to Their Asset Price Impact? 2 Thomas Gilbert 3 Chiara Scotti 4 Georg H. Strasser 5 Clara Vega 6 20150226 7 . mata: gethash("865") 1 The Self-Medication Hypothesis: Evidence from Terrorism and Cigarette Accessibility 2 Michael Pesko 3 Christopher F Baum 4 5 6 20141112 7 20141212 . mata: gethash("870") 1 Decomposing Random Mechanisms 2 Marek Pycia 3 M. Utku Unver 4 5 6 20150115 7 . mata: gethash("862") 1 The surprisingly low importance of income uncertainty for precaution 2 Scott Fulford 3 4 5 6 20140925 7 Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 40 / 73
A GMM-CUE estimator using Mata’s optimize() functions
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 41 / 73
A GMM-CUE estimator using Mata’s optimize() functions
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 42 / 73
A GMM-CUE estimator using Mata’s optimize() functions
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 43 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.ado
. type mygmm2s.ado *! mygmm2s 1.0.2 MES/CFB 31dec2014 program mygmm2s, eclass version 13.1 /* Our standard syntax: mygmm2s y, endog(varlist1) inexog(varlist2) exexog(varlist3) [robust] where varlist1 contains endogenous regressors varlist2 contains exogenous regressors (included instruments) varlist3 contains excluded instruments Without robust, efficient GMM is IV. With robust, efficient GMM is 2-step efficient GMM, robust to arbitrary heteroskedasticity. To accommodate time-series operators in the options, add the "ts" */ syntax varname(ts) [if] [in] [, endog(varlist ts) inexog(varlist ts) // > / exexog(varlist ts) robust ] local depvar `varlist´ /* marksample handles the variables in `varlist´ automatically, but not the variables listed in the options `endog´, `inexog´ and so on. -markout- sets `touse´ to 0 for any observations where the variables listed are missing. */ marksample touse markout `touse´ `endog´ `inexog´ `exexog´ // These are the local macros that our Stata program will use tempname b V omega
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 44 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.ado
// Call the Mata routine. All results will be waiting for us in "r()" macros af > terwards. mata: m_mygmm2s("`depvar´", "`endog´", "`inexog´", /// "`exexog´", "`touse´", "`robust´") // Move the basic results from r() macros into Stata matrices. mat `b´ = r(beta) mat `V´ = r(V) mat `omega´ = r(omega) // Prepare row/col names. // Our convention is that regressors are [endog included exog] // and instruments are [excluded exog included exog] // Constant is added by default and is the last column. local vnames `endog´ `inexog´ _cons matrix rownames `V´ = `vnames´ matrix colnames `V´ = `vnames´ matrix colnames `b´ = `vnames´ local vnames2 `exexog´ `inexog´ _cons matrix rownames `omega´ = `vnames2´ matrix colnames `omega´ = `vnames2´ // We need the number of observations before we post our results. local N = r(N) ereturn post `b´ `V´, depname(`depvar´) obs(`N´) esample(`touse´)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 45 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.ado
// Store remaining estimation results as e() macros accessible to the user. ereturn matrix omega `omega´ ereturn local depvar = "`depvar´" ereturn scalar N = r(N) ereturn scalar j = r(j) ereturn scalar L = r(L) ereturn scalar K = r(K) if "`robust´" != "" { ereturn local vcetype "Robust" } display _newline "Two-step GMM results" _col(60) "Number of obs = " e(N > ) ereturn display display "Sargan-Hansen J statistic: " %7.3f e(j) display "Chi-sq(" %3.0f e(L)-e(K) " ) P-val = " /// %5.4f chiprob(e(L)-e(K), e(j)) _newline end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 46 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.ado
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 47 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.mata
. type m_mygmm2s.mata mata:mata clear version 13.1 mata: mata set matastrict on mata: // m_mygmm2s 1.0.0 MES/CFB 31dec2014 void m_mygmm2s(string scalar yname, string scalar endognames, string scalar inexognames, string scalar exexognames, string scalar touse, string scalar robust) { real matrix Y, X1, X2, Z1, X, Z, QZZ, QZX, W, omega, V real vector cons, beta_iv, beta_gmm, e, gbar real scalar K, L, N, j // Use st_tsrevar in case any variables use Stata´s time-series operators. st_view(Y, ., st_tsrevar(tokens(yname)), touse) st_view(X1, ., st_tsrevar(tokens(endognames)), touse) st_view(X2, ., st_tsrevar(tokens(inexognames)), touse) st_view(Z1, ., st_tsrevar(tokens(exexognames)), touse)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 48 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.mata
// Our convention is that regressors are [endog included exog] // and instruments are [excluded exog included exog] // Constant is added by default and is the last column. cons = J(rows(X2), 1, 1) X2 = X2, cons X = X1, X2 Z = Z1, X2 K = cols(X) L = cols(Z) N = rows(Y) QZZ = 1/N * quadcross(Z, Z) QZX = 1/N * quadcross(Z, X) // First step of 2-step feasible efficient GMM: IV (2SLS). Weighting matrix // is inv of Z´Z (or QZZ). W = invsym(QZZ) beta_iv = (invsym(X´Z * W * Z´X) * X´Z * W * Z´Y) // By convention, Stata parameter vectors are row vectors beta_iv = beta_iv´
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 49 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.mata
// Use first-step residuals to calculate optimal weighting matrix for 2-step FE > GMM
// Second step of 2-step feasible efficient GMM: IV (2SLS). Weighting matrix // is inv of Z´Z (or QZZ). W = invsym(omega) beta_gmm = (invsym(X´Z * W * Z´X) * X´Z * W * Z´Y) // By convention, Stata parameter vectors are row vectors beta_gmm = beta_gmm´ // Sargan-Hansen J statistic: first we calculate the second-step residuals e = Y - X * beta_gmm´ // Calculate gbar = 1/N * Z´*e gbar = 1/N * quadcross(Z, e) j = N * gbar´ * W * gbar // Sandwich var-cov matrix (no finite-sample correction) // Reduces to classical var-cov matrix if Omega is not robust form. // But the GMM estimator is "root-N consistent", and technically we do // inference on sqrt(N)*beta. By convention we work with beta, so we adjust // the var-cov matrix instead: V = 1/N * invsym(QZX´ * W * QZX)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 50 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmm2s.mata
// Easiest way of returning results to Stata: as r-class macros. st_matrix("r(beta)", beta_gmm) st_matrix("r(V)", V) st_matrix("r(omega)", omega) st_numscalar("r(j)", j) st_numscalar("r(N)", N) st_numscalar("r(L)", L) st_numscalar("r(K)", K) } end mata: mata mosave m_mygmm2s(), dir(PERSONAL) replace
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 51 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_myomega.mata
. type m_myomega.mata mata: mata clear version 13.1 mata: mata set matastrict on mata: // m_myomega 1.0.0 MES/CFB 31dec2014 real matrix m_myomega(real rowvector beta, real colvector Y, real matrix X, real matrix Z, string scalar robust) { real matrix QZZ, omega real vector e, e2 real scalar N, sigma2 // Calculate residuals from the coefficient estimates N = rows(Z) e = Y - X * beta´
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 52 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_myomega.mata
if (robust=="") { // Compute classical, non-robust covariance matrix QZZ = 1/N * quadcross(Z, Z) sigma2 = 1/N * quadcross(e, e)
} else { // Compute heteroskedasticity-consistent covariance matrix e2 = e:^2
} _makesymmetric(omega) return (omega) } end mata: mata mosave m_myomega(), dir(PERSONAL) replace
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 53 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_myomega.mata
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 54 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmmcue.ado
. type mygmmcue.ado *! mygmmcue 1.0.2 MES/CFB 31dec2014 program mygmmcue, eclass version 13.1 syntax varname(ts) [if] [in] [ , endog(varlist ts) /// inexog(varlist ts) exexog(varlist ts) robust ] local depvar `varlist´ marksample touse markout `touse´ `endog´ `inexog´ `exexog´ tempname b V omega mata: m_mygmmcue("`depvar´", "`endog´", "`inexog´", /// "`exexog´", "`touse´", "`robust´") mat `b´ = r(beta) mat `V´ = r(V) mat `omega´=r(omega) // Prepare row/col names // Our convention is that regressors are [endog included exog] // and instruments are [excluded exog included exog] local vnames `endog´ `inexog´ _cons matrix rownames `V´ = `vnames´ matrix colnames `V´ = `vnames´ matrix colnames `b´ = `vnames´ local vnames2 `exexog´ `inexog´ _cons matrix rownames `omega´ = `vnames2´ matrix colnames `omega´ = `vnames2´ local N = r(N)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 55 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmmcue.ado
ereturn post `b´ `V´, depname(`depvar´) obs(`N´) esample(`touse´) ereturn matrix omega `omega´ ereturn local depvar = "`depvar´" ereturn scalar N = r(N) ereturn scalar j = r(j) ereturn scalar L = r(L) ereturn scalar K = r(K) if "`robust´" != "" ereturn local vcetype "Robust" display _newline "GMM-CUE estimates" _col(60) "Number of obs = " e(N) ereturn display display "Sargan-Hansen J statistic: " %7.3f e(j) display "Chi-sq(" %3.0f e(L)-e(K) " ) P-val = " /// %5.4f chiprob(e(L)-e(K), e(j)) _newline end
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 56 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmmcue.ado
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 57 / 73
A GMM-CUE estimator using Mata’s optimize() functions mygmmcue.ado
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 58 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_mygmmcue.mata
. type m_mygmmcue.mata mata: mata clear version 13.1 mata: mata set matastrict on mata: // m_mygmmcue 1.0.0 MES/CFB 31dec2014 void m_mygmmcue(string scalar yname, string scalar endognames, string scalar inexognames, string scalar exexognames, string scalar touse, string scalar robust) { real matrix X1, X2, Z1, QZZ, QZX, W, V real vector cons, beta_iv, beta_cue real scalar K, L, N, S, j // In order for the optimization objective function to find various variables // and data they have to be set as externals. This means subroutines can // find them without having to have them passed to the subroutines as arguments > . // robustflag is the robust argument recreated as an external Mata scalar. external Y, X, Z, e, omega, robustflag robustflag = robust st_view(Y, ., st_tsrevar(tokens(yname)), touse) st_view(X1, ., st_tsrevar(tokens(endognames)), touse) st_view(X2, ., st_tsrevar(tokens(inexognames)), touse) st_view(Z1, ., st_tsrevar(tokens(exexognames)), touse)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 59 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_mygmmcue.mata
// Our convention is that regressors are [endog included exog] // and instruments are [excluded exog included exog] // The constant is added by default and is the last column. cons = J(rows(X2), 1, 1) X2 = X2, cons X = X1, X2 Z = Z1, X2 K = cols(X) L = cols(Z) N = rows(Y) QZZ = 1/N * quadcross(Z, Z) QZX = 1/N * quadcross(Z, X) // First step of CUE GMM: IV (2SLS). Use beta_iv as the initial values for // the numerical optimization. W = invsym(QZZ) beta_iv = invsym(X´Z * W *Z´X) * X´Z * W * Z´Y // Stata convention is that parameter vectors are row vectors, and optimizers // require this, so must conform to this in what follows. beta_iv = beta_iv´ // What follows is how to set out an optimization in Stata. First, initialize // the optimization structure in the variable S. Then tell Mata where the // objective function is, that it´s a minimization, that it´s a "d0" type of // objective function (no analytical derivatives or Hessians), and that the // initial values for the parameter vector are in beta_iv. Finally, optimize.
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 60 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_mygmmcue.mata
S = optimize_init()
beta_cue = optimize(S) // The last omega is the CUE omega, and the last evaluation of the GMM // objective function is J. W = invsym(omega) j = optimize_result_value(S) V = 1/N * invsym(QZX´ * W * QZX) st_matrix("r(beta)", beta_cue) st_matrix("r(V)", V) st_matrix("r(omega)", omega) st_numscalar("r(j)", j) st_numscalar("r(N)", N) st_numscalar("r(L)", L) st_numscalar("r(K)", K) } end mata: mata mosave m_mygmmcue(), dir(PERSONAL) replace
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 61 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_mygmmcue.mata
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 62 / 73
A GMM-CUE estimator using Mata’s optimize() functions m_mycuecrit.mata
. type m_mycuecrit.mata mata: mata clear version 13.1 mata: mata set matastrict on mata: // GMM-CUE evaluator function. // Handles only d0-type optimization; todo, g and H are just ignored. // beta is the parameter set over which we optimize, and // j is the objective function to minimize. // m_mycuecrit 1.0.0 MES/CFB 31dec2014 void m_mycuecrit(todo, beta, j, g, H) { external Y, X, Z, e, omega, robustflag real matrix W real vector gbar real scalar N
W = invsym(omega) N = rows(Z) e = Y - X * beta´ // Calculate gbar=Z´*e/N gbar = 1/N * quadcross(Z,e) j = N * gbar´ * W * gbar } end mata: mata mosave m_mycuecrit(), dir(PERSONAL) replace
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 63 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 64 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
. which mygmm2s ./mygmm2s.ado *! mygmm2s 1.0.2 MES/CFB 31dec2014 . set more off . *mata: mata clear . *program drop _all . // use http://fmwww.bc.edu/ec-p/data/hayashi/griliches76.dta, clear . use griliches76, clear (Wages of Very Young Men, Zvi Griliches, J.Pol.Ec. 1976) . quietly ivreg2 lw s expr tenure rns smsa (iq=med kww age mrt), gmm2s . storedresults save ivhomo e() . mygmm2s lw, endog(iq) inexog(s expr tenure rns smsa) /// > exexog(med kww age mrt) Two-step GMM results Number of obs = 758 lw Coef.
z P>|z| [95% Conf. Interval] iq
.0052169
0.027
s .1373477 .0169631 8.10 0.000 .1041007 .1705947 expr .0338041 .007268 4.65 0.000 .019559 .0480492 tenure .040564 .0088553 4.58 0.000 .023208 .0579201 rns
.0353037
0.001
smsa .149983 .0314254 4.77 0.000 .0883903 .2115757 _cons 4.837875 .3448209 14.03 0.000 4.162038 5.513711 Sargan-Hansen J statistic: 61.137 Chi-sq( 3 ) P-val = 0.0000
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 65 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
. storedresults compare ivhomo e(), include(macros: depvar scalar: N j matrix: > b V) /// > tol(1e-7) verbose comparing macro e(depvar) comparing scalar e(N) comparing scalar e(j) comparing matrix e(b) comparing matrix e(V) . . quietly ivreg2 lw s expr tenure rns smsa (iq=med kww age mrt), gmm2s robust . storedresults save ivrobust e() . quietly mygmm2s lw, endog(iq) inexog(s expr tenure rns smsa) /// > exexog(med kww age mrt) robust . storedresults compare ivrobust e(), include(macros: depvar scalar: N j matrix > : b V) /// > tol(1e-7) verbose comparing macro e(depvar) comparing scalar e(N) comparing scalar e(j) comparing matrix e(b) comparing matrix e(V)
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 66 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 67 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
. cscript mygmmcue adofile mygmmcue BEGIN mygmmcue
./mygmmcue.ado *! mygmmcue 1.0.2 MES/CFB 31dec2014 . set more off . *mata: mata clear . *program drop _all . set rmsg on r; t=0.00 16:58:43 . use griliches76, clear (Wages of Very Young Men, Zvi Griliches, J.Pol.Ec. 1976) r; t=0.00 16:58:43 . quietly ivreg2 lw s expr tenure rns smsa (iq=med kww age mrt), cue r; t=0.87 16:58:44 . storedresults save ivreg2cue e() r; t=0.00 16:58:44 . mygmmcue lw, endog(iq) inexog(s expr tenure rns smsa) /// > exexog(med kww age mrt) Iteration 0: f(p) = 61.136598 Iteration 1: f(p) = 32.923655 Iteration 2: f(p) = 32.834123 Iteration 3: f(p) = 32.831617 Iteration 4: f(p) = 32.831615
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 68 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
GMM-CUE estimates Number of obs = 758 lw Coef.
z P>|z| [95% Conf. Interval] iq
.0132447
0.000
s .3296912 .0430661 7.66 0.000 .2452831 .4140992 expr .0098901 .0184522 0.54 0.592
.0460558 tenure .0679956 .022482 3.02 0.002 .0239317 .1120594 rns
.0896297
0.001
smsa .2071595 .0797834 2.60 0.009 .050787 .363532 _cons 8.907022 .8754368 10.17 0.000 7.191198 10.62285 Sargan-Hansen J statistic: 32.832 Chi-sq( 3 ) P-val = 0.0000 r; t=0.32 16:58:44 . storedresults compare ivreg2cue e(), include(macros: depvar scalar: N j matri > x: b V) /// > tol(1e-4) verbose comparing macro e(depvar) comparing scalar e(N) comparing scalar e(j) comparing matrix e(b) comparing matrix e(V) r; t=0.13 16:58:44
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 69 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
. . quietly ivreg2 lw s expr tenure rns smsa (iq=med kww age mrt), cue robust r; t=1.70 16:58:46 . storedresults save ivreg2cue e() r; t=0.01 16:58:46 . mygmmcue lw, endog(iq) inexog(s expr tenure rns smsa) /// > exexog(med kww age mrt) robust Iteration 0: f(p) = 52.768916 Iteration 1: f(p) = 28.946534 (not concave) Iteration 2: f(p) = 27.459026 Iteration 3: f(p) = 26.995682 (not concave) Iteration 4: f(p) = 26.920672 Iteration 5: f(p) = 26.564586 Iteration 6: f(p) = 26.42493 Iteration 7: f(p) = 26.420641 Iteration 8: f(p) = 26.420637 Iteration 9: f(p) = 26.420637
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 70 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
GMM-CUE estimates Number of obs = 758 Robust lw Coef.
z P>|z| [95% Conf. Interval] iq
.0147825
0.000
s .3348492 .0469882 7.13 0.000 .242754 .4269443 expr .0197632 .0199592 0.99 0.322
.0588825 tenure .0857848 .0242331 3.54 0.000 .0382888 .1332807 rns
.091536
0.000
smsa .255257 .0837255 3.05 0.002 .0911579 .419356 _cons 8.943699 .9742256 9.18 0.000 7.034251 10.85315 Sargan-Hansen J statistic: 26.421 Chi-sq( 3 ) P-val = 0.0000 r; t=0.84 16:58:47 . storedresults compare ivreg2cue e(), include(macros: depvar scalar: N j matri > x: b V) /// > tol(1e-4) verbose comparing macro e(depvar) comparing scalar e(N) comparing scalar e(j) comparing matrix e(b) comparing matrix e(V) r; t=0.00 16:58:47
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 71 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 72 / 73
A GMM-CUE estimator using Mata’s optimize() functions certification
Christopher F Baum (BC / DIW) Mata Programming II NCER/QUT, 2015 73 / 73