# Comparing GMM results

**URL:** <https://discourse.julialang.org/t/comparing-gmm-results/47322>\
**Category:** General Usage\
**Created:** [September 26, 2020, 4:08pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322 "2020-09-26T16:08:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 26, 2020, 4:08pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/1 "2020-09-26T16:08:55Z")

</div>

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

```julia
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:

```julia
inv(X'*Z*W*Z'*X)*X'*Z*W*Z'*y

```

I was surprised to find that the results are different.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 26, 2020, 4:18pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/2 "2020-09-26T16:18:20Z")

</div>

Different how? Like obviously or slightly different? If they compare as equivalent with `isapprox` then they are sufficiently the same for this purpose.

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 26, 2020, 4:42pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/3 "2020-09-26T16:42:10Z")

</div>

Obiviously different, making me wonder where my code is wrong lol

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 26, 2020, 4:47pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/4 "2020-09-26T16:47:37Z")

</div>

I think the error is `size(Z, 2)`.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 26, 2020, 4:49pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/5 "2020-09-26T16:49:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 26, 2020, 5:03pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/6 "2020-09-26T17:03:57Z")

</div>

Z is a N-by-L matrix of instrument, in which each row is an observation and each column is an exogenous variable.

```julia
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.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 26, 2020, 5:10pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/7 "2020-09-26T17:10:18Z")

</div>

Likely that you want `.*` when constructing `g`

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 26, 2020, 5:15pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/8 "2020-09-26T17:15:07Z")

</div>

y is N-by-1 and X is N-by-K. The model is overidentified so we have K\<L. Therefore

```julia
(y[i] - X[i, :]'*θ)*Z[i, :]

```

is a scaler muptiplying a 1-by-L vector.

I think this should be right, but the results are still quite different. Don’t know where I made another stupid mistake again.

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 26, 2020, 9:09pm UTC](https://discourse.julialang.org/t/comparing-gmm-results/47322/9 "2020-09-26T21:09:36Z")

</div>

Still haven’t figured out why the previous function was wrong, but found that the following matrix-based function works correctly:

```julia
function LossFn(θ, y, X, Z)
    W = diagm(0 => ones(size(Z, 2)))
    g = z'*(y - x*θ)
    loss = g'*W*g
    return loss
end

```
