Julia Manual GMM

question

#1

Hey
I am having a bit of trouble understanding how to manually do GMM and in particular how to do GMM in Julia.
Here is the Stata code + data:

use http://www.stata-press.com/data/r13/docvisits
gmm (docvis - exp({xb:private chronic female income}+{b0})), instruments(private chronic female age black hispanic) twostep

As I understand it, I need to first find the residuals and then find estimates for theta but am not sure on how to do either. I am supposed to first assume i.i.d errors and then assume heteroskedasticity but no autocorrelation in the errors.

Is it something of the form:
inv(n)Z’(y-Xtheta))'W(inv(n)Z’(y-Xtheta))

Where W is the weighting matrix for the heteroskedastic case and W=I for i.i.d
Any help would be appreciated,
Thanks

EDIT: Here is what I have so far, not sure how correct it is:

using Optim
using CSV

data=CSV.read(“data Ass_2.csv”, datarow=2)

docvis=Array(data[:docvis])
y=docvis
#x and z variables
private=Array(data[:private])
chronic=Array(data[:chronic])
female=Array(data[:female])
income=Array(data[:income])
age=Array(data[:age])
black=Array(data[:black])
hispanic=Array(data[:hispanic])
β0=ones(4412)
#collection of instruments
Z=[private chronic female age black hispanic]

#collection of regressors

X=[β0 private chronic female income]

“”"
L>K so X=Z inv(Z’Z)Z’X
“”"
X̂=(Z*inv(Z’*Z)*Z’*X)
W=eye(6)
θ=(inv(X̂’*X̂)*X̂’y)
#The solution for Optimal β
“”"
β=inv(X’ZW
Z’*X)X’ZWZ’y
eX=exp(X)
βe=inv(eX’ZW
Z’*eX)eX’ZWZ’y
“”"
u=y-X̂
θ
σ= u.^2

f(σ)=y-X̂*θ
optimize(f, [0, 0, 0, 0, 0])


#2

You can include code on Discourse using code blocks,

using Optim
using CSV

data=CSV.read(“data Ass_2.csv”, datarow=2)

docvis=Array(data[:docvis])
y=docvis
#x and z variables
private=Array(data[:private])
chronic=Array(data[:chronic])
female=Array(data[:female])
income=Array(data[:income])
age=Array(data[:age])
black=Array(data[:black])
hispanic=Array(data[:hispanic])
β0=ones(4412)
#collection of instruments
Z=[private chronic female age black hispanic]

#collection of regressors

X=[β0 private chronic female income]

"""
 L>K so X=Z inv(Z’Z)Z’X
"""
X̂=(Z*inv(Z’*Z)*Z’*X)
W=eye(6)
θ=(inv(X̂’*X̂)*X̂’y)
#The solution for Optimal β
"""
β=inv(X’ZWZ’*X)X’ZWZ’y
eX=exp(X)
βe=inv(eX’ZWZ’*eX)eX’ZWZ’y
"""
u=y-X̂θ
σ= u.^2

f(σ)=y-X̂*θ
optimize(f, [0, 0, 0, 0, 0])

#3

You define a function f(σ)=y-X̂*θ that does not depend on the input variable σ. So your function is constant. I think you may benefit from a better understanding of how to define methods and functions in Julia, see the resources at https://julialang.org/learning/

Optim works well to minimise least squares problems, but you can also consider using LsqFit.jl


#4

I have a function which will do this, pretty easily, at https://github.com/mcreel/Econometrics.jl/blob/master/src/GMM/gmmresults.jl

It gives results as follows:

julia> gmmresults();
************************************************************
GMM example, two step
GMM Estimation Results    BFGS convergence: Normal
Observations: 100
Hansen-Sargan statistic: 1.84211
Hansen-Sargan p-value: 0.1747

                estimate     st. err      t-stat     p-value
           1    -0.13380     0.10507    -1.27341     0.20588
           2     1.01924     0.15808     6.44768     0.00000

************************************************************
************************************************************
GMM example, CUE
GMM Estimation Results    BFGS convergence: Normal
Observations: 100
Hansen-Sargan statistic: 1.56835
Hansen-Sargan p-value: 0.21045

                estimate     st. err      t-stat     p-value
           1    -0.10846     0.10572    -1.02592     0.30745
           2     0.98518     0.14669     6.71591     0.00000

************************************************************

julia> 

For your problem, you would just need to define the appropriate moments function. An example which uses IV to estimate a linear model similar to yours is here: https://github.com/mcreel/Econometrics/blob/master/Examples/GMM/MeasurementErrorIV.jl

By the way, the gmmresults() function is general purpose, for possibly nonlinear in the parameters moment conditions. As such, it’s using Optim.jl, which is not necessary in the case of linear in parameters moment conditions. But, it works.


#5

From the Stata Code I provided earlier, am I understanding correctly that the moment conditions would be something like:

[y-theta[1]exp(X[1]) y-theta[2]exp(X[2]) … y-theta[5]exp(X[5])]

Where there are 5 moment conditions?


#6

Just found out that we are required to use

Optim
Optimize

Any thoughts on this?

β0=ones(n)
#collection of instruments
Z=[private chronic female age black hispanic]

X=[β0 private chronic female income]
W1=eye(6)

Q(θ)=(inv(n)(Z)(y-Xθ’))'W1(inv(n)(Z)(y-Xθ’))

optimize(Q)