I am working on implementing a linear GMM estimator in Julia. As a first step, I compare 2 ways of getting parameter estimates: numerically minimizing the loss function
function LossFn(θ, y, X, Z)
W = diagm(0 => ones(size(Z, 2)))
g = [(y[i] - X[i, :]'*θ)*Z[i] for i=1:size(Z, 2)]
loss = g'*W*g
return loss
end
gmm = optimize(θ1 -> LossFn(θ1, y, x, z), ones(6))
and using analytical solution:
inv(X'*Z*W*Z'*X)*X'*Z*W*Z'*y
I was surprised to find that the results are different.
Yea is Z just a vector here or is it a matrix? Because you call size(Z,2) which indicates that it does have a second dimension but you are indexing it with only Z[i] which suggests it does not.
Z is a N-by-L matrix of instrument, in which each row is an observation and each column is an exogenous variable.
function LossFn(θ, y, X, Z)
W = diagm(0 => ones(size(Z, 2)))
g = [(y[i] - X[i, :]'*θ)*Z[i, :] for i=1:size(Z, 1)]
gbar = mean(g, dims = 1)[1]
loss = gbar'*W*gbar
return loss
end
I think this should get it right. Here g[i] is the value of moment funtion for observation i: g_i = (y_i - x_i'\theta)z_i
and gbar gives sample moment \bar{g} = \frac{1}{N}\sum_{i}^N g_i.