lecture4 - massachusetts institute of...

36
14 170: Programming for 14.170: Programming for Economists 1/12/2009-1/16/2009 1/12/2009-1/16/2009 Melissa Dell Matt Notowidigdo Paul Schrimpf

Upload: others

Post on 07-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

14 170: Programming for14.170: Programming for Economists

1/12/2009-1/16/20091/12/2009-1/16/2009

Melissa DellMatt Notowidigdo

Paul Schrimpf

Page 2: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Lecture 4, Introduction to Mata in Stata

Page 3: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Mata in SataM t i t i i l th t i b ilt• Mata is a matrix programming language that is now built into Stata. The syntax is a cross between Matlab and Stata.

• Mata is not (yet) seamlessly integrated into Stata; for more complicated projects it still might be better to export to Matlab and write Matlab codeexport to Matlab and write Matlab code

• Examples of when to use Mata (rather than Stata or Matlab): )– Add robust standard errors to existing Stata estimator that does

not currently support it– Simple GMM estimatorSimple GMM estimator– Simple ML estimator (or any estimator) that would be easier to

implement using matrix notation

Page 4: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

But first back to Stata MLBut first ... back to Stata ML

Page 5: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

set obs 10000set seed 14170local lambda = 0.25local sigma_1 = 1local sigma 2 = 2

Normal Mixture Slocal sigma_2 = 2

local mu_1 = 1local mu_2 = 0.5gen type = (uniform() < `lambda')gen v = (`mu_1' + `sigma_1'*invnorm(uniform())) if type == 1

l (` 2' + ` i 2'*i ( if ())) if t 0

in Stata MLreplace v = ( mu_2' + sigma_2'*invnorm(uniform())) if type == 0

program define mixture_d0args todo b lnftempvar lnf_jtempname lambda sigma_1 sigma_2 mu_1 mu_2scalar `mu_1' = `b'[1,1]scalar `mu_2' = `b'[1,2]scalar `sigma_1' = exp(`b'[1,3])scalar `sigma 2' = exp(`b'[1,4])g _ p( [ , ])scalar `lambda' = normal(`b'[1,5])gen double `lnf_j' = ///`lambda' * (1/`sigma_1') * normalden(($ML_y1 - `mu_1')/`sigma_1') + ///(1-`lambda') * (1/`sigma_2') * normalden(($ML_y1 - `mu_2')/`sigma_2')

mlsum `lnf' = log(`lnf j')mlsum lnf log( lnf_j )endgen mu_1 = 1ml model d0 mixture_d0 (v = mu_1, noconstant) ///

/mu_2 /ln_sigma_1 /ln_sigma_2 /inv_lambdaml ma imi eml maximizenlcom exp([ln_sigma_1]_b[_cons])nlcom exp([ln_sigma_2]_b[_cons])nlcom normal([inv_lambda]_b[_cons]) )/)(()/1)(1(

)/)(()/1()(

222

111

σμφσλσμφσλ

−−+−=

yyyf

Page 6: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Normal Mixture inMixture in Stata ML

Page 7: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Normal Mixture inMixture in Stata ML

Page 8: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM in Stata MLGMM in Stata ML• In principle, Stata ML can be used to implement p p , p

any estimator based on maximization of an objective function.Th S ML i l NLLS• Thus we can use Stata ML to implement NLLS or GMM estimators– BENEFIT: Simple to code; can re-use well-knownBENEFIT: Simple to code; can re-use well-known

Stata syntax and helper functions• Particularly useful for panel data estimators (egen, bysort,

etc )etc.)– COST: Mata is better if moment conditions are basd

on matrix algebra

Page 9: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM-OLSGMM OLS

′==Ε=

GMM WggXg

)()(minarg0]'[)(

βββεβ

β

−=

∑N

iii

Xg

Xy

ˆ1)(ˆ

ˆ

εβ

βεβ

=

= ∑=i

ii

IW

XN

g1

ˆ

)( εβ

⎟⎠

⎞⎜⎝

⎛ ′′⎟⎠

⎞⎜⎝

⎛ ′= ∑∑==

N

iii

N

iiiGMM X

NX

N 11

ˆ1ˆ1minargˆ εεββ ⎠⎝⎠⎝ == ii NN 11

Page 10: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM-OLS = OLS

( ) ( )XyXXyX ′′′ )()(1minargˆ βββ ( ) ( )

( ) ( )XXyXXXyX

XyXXyXNOLSGMM

′−′′′−′=

−−=−

2

2

))1minarg

)()(minarg

ββ

ββββ

( ) ( )

( ) ( ) XXXXyXXXXXyX

yyN

′−′′−′+′−′′−′=

2

)()(0

))g

ββ

βββ

yXXX

XXyX

OLSGMM ′′=

′−′=−

−1)(ˆ

0

β

β

Page 11: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM in Stata MLprogram drop _allprogram define mygmmargs todo b lnfargs todo b lnftempvar xb e summleval `xb' = `b', eq(1)gen `e' = $ML y1 - `xb'gen e $ML_y1 xbmatrix vecaccum Xe = `e' $xlistmatrix m = Xe' / _Nmatrix obj = m' * mjmlsum `lnf' = -1 * obj[1,1] if _n == 1end

clearset obs 100set seed 14170

1 i ( if ())gen x1 = invnorm(uniform())gen y = 1 + x1 + invnorm(uniform())global xlist = "x1"reg y x1reg y x1ml model d0 mygmm (y = x1)ml maximize

Page 12: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM inGMM in Stata ML

Page 13: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM-OLS d d'

)(∂

∂=′

gGββ

standard errors( ) ( ) 111]'[

−− ′Ψ′′

=Ψ∂

GGGGGGV

mmEβ

( ) ( ) 11

ˆ

1

−=

′Ψ′′=

Xy

GGGGGGN

V

iii

GMM

βε

1)(1)(ˆ

=

−′= ∑ XyXN

g ii

N

ii ββ

2

)(ˆ

′′′′

′−=

′∂∂

NXXg

ββ

2

2

ˆˆ

][]))([(′

′=′′′=Ψ

NXX

XXEXXE

εσ

εεε

12 )(ˆˆ −′= XXVN

GMM εσ

Page 14: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Mata in Sata• How to learn more about Mata? Type the following into

Stata:– help [M-0] intro– help [M-4] intro

• help [M-4] manipulation• help [M-4] matrix• help [M-4] scalar• help [M-4] statistical• help [M 4] string• help [M-4] string• help [M-4] io• help [M-4] stata• help [M-4] programmingp [ ] p g g

Page 15: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset obs 200set seed 1234set more off

OLS in Mataset more offgen x = invnorm(uniform()) gen y = 1 + 2 * x + 0.1*invnorm(uniform())

** enter Mata enter Matamata

x = st_data(., ("x")) cons = J(rows(x) 1 1)cons J(rows(x), 1, 1)X = (x, cons) y = st_data(., ("y")) Xbeta hat = (invsym(X'*X))*(X'*y)beta_hat = (invsym(X X)) (X y)e_hat = y - X * beta_hats2 = (1 / (rows(X) - cols(X))) * (e_hat' * e_hat)

V ols = s2 * invsym(X'*X)V_ols = s2 invsym(X X)se_ols = sqrt(diagonal(V_ols)) beta_hatse_ols

/** leave mata **/endregress y x

Page 16: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

OLS in Mata

Page 17: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset obs 200set seed 1234t ff

“robust” OLS in Mataset more offgen x = invnorm(uniform()) gen y = 1 + 2 * x + x * x * invnorm(uniform())

tmatax_vars = st_data(., ("x")) cons = J(rows(x_vars), 1, 1) X = (x_vars , cons)

t d t ( (" "))y = st_data(., ("y"))Xbeta_hat = (invsym(X'*X))*(X'*y) e_hat = y - X * beta_hat

d i h id ( l ( ) l ( ) 0)sandwich_mid = J(cols(X), cols(X), 0)n = rows(X) for (i=1; i<=n; i++) {sandwich_mid =sandwich_mid+(e_hat[i,1]*X[i,.])'*(e_hat[i,1]*X[i,.])

}}V_robust = (n/(n-cols(X)))*invsym(X'*X)*sandwich_mid*invsym(X'*X) se_robust = sqrt(diagonal(V_robust)) beta_hat

bse_robustendreg y x, robust

1

1

1 )()ˆ()ˆ()( −

=

− ∗′∗⎟⎠

⎞⎜⎝

⎛∗∗′∗∗∗′∗

−= ∑ XXxxXX

KNNV

N

iiiiirobust εε

Page 18: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

“robust” OLS in Mata

Page 19: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Fixed Effects OLS (LSDV)Fixed Effects OLS (LSDV)Xy += εβ

)( 1

PIMiiiiIP

wTNw

TITTNw

−=

′′⊗=

×

))(())(( 1 MXMXMXM

MXMyM www

wTNw

′′

+=−

×

β

εβ

)()(

))(())((1

1

yMMXXMMX

yMXMXMXM

wwww

wwwwFE

′′′′=

′′=−

β

)()(

)()(1

1

yMXXMX

yMMXXMMX wwww

′′=

′′=−

)()( yMXXMX ww=

Page 20: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset obs 100local N = 10gen id = 1+floor(( n - 1)/10)

OLS FE in Matag ((_ ) )bys id: gen fe = 5*invnorm(uniform())by id: replace fe = fe[1]gen x = invnorm(uniform())gen y = 1.2 * x + fe + invnorm(uniform())g y

mataX = st_data(., ("x"))y = st data(., ("y"))y _ , yI_N = I(`N')I_NT = I(rows(X))i_T = J(`N',1,1)P w = I N # (i T*invsym(i T'*i T)*i T')_ _ _ y _ _ _M_w = I_NT - P_wbeta = invsym(X'*M_w*X)*(X'*M_w*y)e_hat = M_w*y - (M_w*X)*betas2 = (1 / (rows(X) - cols(X) - `N')) * (e hat' * e hat) _ _V = s2 * invsym(X'*M_w*X)se = sqrt(diagonal(V))betaseendreg y xareg y x, absorb(id)

Page 21: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

OLS FE in Mata

Page 22: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset seed 14170set obs 50set more off

BootstrappingWith M tlocal B = 10000

set matsize `B'matrix betas = J(`B', 1, 0)

gen x invnormal(uniform())

With Mata(BROKEN!)gen x = invnormal(uniform())

gen y = x + invnormal(uniform())

forvalues b = 1/`B' {preserve

(BROKEN!)pbsamplematax = st_data(., ("x"))cons = J(rows(x), 1, 1)

t d t ( (" "))y = st_data(., ("y"))X = (x, cons)beta_hat = invsym(cross(X,X)) * cross(X,y)st_matrix("b", beta_hat)endmatrix betas[`b',1] = b[1,1]restore

}regress y xd lldrop _allsvmat betassumm

Page 23: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

BootstrappingWith M tWith Mata

(BROKEN!)(BROKEN!)

Page 24: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset seed 14170set obs 50set more off

10000

BootstrappingWith M tlocal B = 10000

set matsize `B'matrix betas = J(`B', 1, 0)gen x = invnormal(uniform())gen y = x + invnormal(uniform())

With Mata(GOOD!)

forvalues b = 1/`B' {preservebsamplequietly do helper.do

(GOOD!)quietly do helper.domatrix betas[`b',1] = b[1,1]restore

}regress y xdrop alldrop _allsvmat betassumm

(h l d fil )(helper.do file)matax = st_data(., ("x"))cons = J(rows(x), 1, 1)y = st_data(., ("y"))_X = (x, cons)beta_hat = invsym(cross(X,X)) * cross(X,y)st_matrix("b", beta_hat)end

Page 25: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

BootstrappingWith M tWith Mata(GOOD!)(GOOD!)

Page 26: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM-OLS reviewGMM OLS review

′==Ε=

GMM WggXg

)()(minarg0]'[)(

βββεβ

β

−=

∑N

iii

Xg

Xy

ˆ1)(ˆ

ˆ

εβ

βεβ

=

= ∑=i

ii

IW

XN

g1

ˆ

)( εβ

⎟⎠

⎞⎜⎝

⎛ ′′⎟⎠

⎞⎜⎝

⎛ ′= ∑∑==

N

iii

N

iiiGMM X

NX

N 11

ˆ1ˆ1minargˆ εεββ ⎠⎝⎠⎝ == ii NN 11

Page 27: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset obs 100set seed 14170gen x = invnorm(uniform())

GMM in Matag ( ())gen y = 1 + 2 * x + invnorm(uniform())

matamata clearx_vars = st_data(., ("x")) cons = J(rows(x_vars), 1, 1) X = (x_vars , cons) y = st data(., ("y")) y _ ( , ( y ))Xdata = (y, X) void ols_gmm0(todo,betas,data,Xe,S,H){y = data[1...,1] p = optimize(S)yX = data[1...,2..3]e = y - X * (betas') Xe = (X'*e/rows(X))'*(X'*e/rows(X)) }

p p ( )gmm_V = ///(1/(rows(X)-cols(X))) * ///(y-X*p')'*(y-X*p') * ///invsym(X' * X)

S = optimize_init() optimize_init_evaluator(S, &ols_gmm0()) optimize_init_evaluatortype(S, "v0") optimize init which(S, "min")

y ( )gmm_se = sqrt(diagonal(gmm_V))

Pgmm sep _ _

optimize_init_params(S, J(1,2,3)) optimize_init_argument(S, 1, data)

g _end

reg y x

Page 28: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t
Page 29: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

GMM-IV overview(iid )(iid errors)

=′Ε Z 0][ ε−==Ε

∑N

iii XyZ

1

ˆ0][βε

ε

⎞⎛ ′

′= ∑−

=iii

ZZ

ZN

g

11

ˆ

ˆ1)(ˆ εβ

′=

⎟⎠⎞

⎜⎝⎛=

GMM WggNZZW

)()(minarg

ˆ

βββ

⎟⎞

⎜⎛ ′⎟

⎠⎞

⎜⎝⎛ ′′⎟⎞

⎜⎛ ′= ∑∑

− N

ii

N

iiGMM

GMM

ZZZZ

gg

1

ˆ1ˆ1minargˆ

)()(g

εεβ

ββββ

⎟⎠

⎜⎝

⎟⎠

⎜⎝

⎟⎠

⎜⎝

∑∑== i

iii

iiGMM NNN 11gβ

β

Page 30: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

clearset obs 100set seed 14170gen spunk = invnorm(uniform())

GMM in Mata (IV) g p ( ())gen z1 = invnorm(uniform()) gen z2 = invnorm(uniform()) gen z3 = invnorm(uniform()) gen x = ///ginvnorm(uniform()) + ///10*spunk + ///z1 + z2 + z3gen ability = ///

void oiv_gmm0(todo,betas,data,mWm,S,H){

y = data[1...,1]Z = data[1...,2..4]g y

invnorm(uniform())+10*spunkgen y = ///2*x+ability + ///.1*invnorm(uniform())

X = data[1...,5]e = y - X * (betas') m = (1/rows(Z)) :* (Z'*e) mWm = (m'*(invsym(Z'Z)*rows(Z))*m)

matamata clearx vars = st data(., ("x"))

}

S = optimize_init() optimize_init_evaluator(S,&oiv_gmm0()) _ _

Z = st_data(., ("z1","z2","z3")) cons = J(rows(x_vars), 1, 1) X = (x_vars) y = st data(., ("y"))

optimize_init_evaluatortype(S, "v0")optimize_init_which(S, "min") optimize_init_params(S, J(1,1,5)) optimize_init_argument(S, 1, data) y _ y

Xdata = (y, Z, X)

p = optimize(S)pendivreg y (x = z1 z2 z3), nocons

Page 31: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t
Page 32: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

1GMM-IV = 2SLS( ) ( ) ( )

( ) XZZZZX

XyZZZXyZNGMM

)('')'(1i

)(''')(1minargˆ

1

1

− −−′=

ββ

ββββ

( )

XyPXy

XyZZZZXyN

Z )()'(1minarg

)('')'(minarg

−−=

−−=

ββ

βββ

XPXXPXyXPXy

XyPXyN

ZZ

Z

)'()()'()()'(0

)()(minarg

−−+−−=β

ββ

βββ

XyPXPXXy

XPXy

z

Z

))')('(()''()'(

)'(

=−=−=

βββ

XPXyPXXPXyPX

XyPX

zz

z

'')'''(

)))(((

=−=−=

ββ

β

yPXXPX

XPXyPX

ZZGMM

zz

')'(ˆ 1−=

=

β

β

Page 33: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Normal Mixture using “Method of Moment-Generating

Functions”Functions

)/)(()/1)(1()/)(()/1()(

222

111

−−+−=σμφσλ

σμφσλy

yyf

0))1((1ˆ

][)(

2/2/

2/

22

22

21

21

22

==

++

+

∑ σμσμ

σμ

λλ ttttN

ty

tttx eeEtM

i 0))1(( 2/2/

1

2211 =−+−= ++

=∑ σμσμ λλ tttt

i

tyGMM eee

Nm i

Page 34: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

matamata cleardata = st_data(., ("v"))

i i 0

Normal Mi t GMMvoid oiv_gmm0(todo,betas,data,mWm,S,H) {

N = rows(data)ones = J(1, N, 1)ts = (0.1, 0.2, 0.3, 0.4, 0.5)m = 0

Mixture GMM in Matalambda = normal(betas[1,1])

sigma_1 = exp(betas[1,2])sigma_2 = exp(betas[1,3])for (i = 1; i<=5; i++) {t = ts[1,i]

in Matat ts[1,i]mT=ones*exp(t*data)/NmT=mT-lambda*exp(t*betas[1,4]+t^2*(sigma_1^2)/2)mT=mT-(1-lambda)*exp(t*betas[1,5]+t^2*(sigma_2^2)/2)m = (m, mT)}}mWm = m * m'}S = optimize_init()optimize_init_evaluator(S,&oiv_gmm0())

i i i i l (S " 0")optimize_init_evaluatortype(S, "v0")optimize_init_which(S, "min")init = (-0.2,0,0.7,1,0.5)optimize_init_params(S, init)optimize_init_argument(S, 1, data)_ _p = optimize(S)p = (normal(p[1,1]), exp(p[1,2]), exp(p[1,3]), p[1,4], p[1,5])pend

Page 35: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

Normal Mixture inMixture in Stata ML

Page 36: lecture4 - Massachusetts Institute of Technologyweb.mit.edu/econ-gea/14.170/lectures/lecture4.pdf · Lecture 4, Introduction to Mata in Stata. Mata in Sata • M t i t i i l th t

ExercisesExercises

(A) Non-linear GMM-IV using Mata (EASY)(A) Non linear GMM IV using Mata (EASY)(B) Bootstrap standard errors of Non-linear

GMM IV estimator (MEDIUM)GMM-IV estimator (MEDIUM)(C)Test that the bootstrapped standard

i t t i M terrors are consistent using a Monte Carlo simulation (HARD)