I'm estimating simultaneous price and demand questions using generalized method of moments in Python. The 'moments' are that the estimates should be unbiased [y-E(y|x)]=0 and that the explanatory variables and instruments are orthogonal [x*[y-E(y|x)]]=0 and [z*[y-E(y|x)]]=0. When I list these moments, should I square the moments related to bias (i.e. [y-E(y|x)]**2), or does the statsmodels GMM code automatically do so? Should I square the orthogonality moments? Sample code below, with the moments not squared.
import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.sandbox.regression.gmm import GMM
rand_array = np.random.rand(150, 7)
yvar=rand_array[:, [0,1]]
zvar=rand_array[:, [2,3,4]]
xvar=rand_array[:, [5,6]]
xvar=sm.add_constant(xvar)
class GMMREM (GMM):
def momcond(self, params):
b0, b1, b2, b3, b4,m0, m1, m2, m3 = params
x = self.exog
z = self.instrument
y= self.endog
error1 = x[:,0]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
error2 = z[:,0]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
error3 = z[:,1]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
error4 = x[:,1]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1]-b4*x[:,1])
error5 = z[:,2]*(y[:,0]-b0-b1*y[:,1]-b2*z[:,0]-b3*z[:,1])
error6 = x[:,0]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
error7 = z[:,2]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
error8= x[:,1]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2]-m3*x[:,1])
error9 = z[:,0]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2])-m3*x[:,1]
error10= z[:,1]*(y[:,1]-m0-m1*y[:,0]-m2*z[:,2])-m3*x[:,1]
return np.column_stack((error1, error2, error3, error4, error5, error6, error7, error8, error9, error10))
model1 = GMMREM(yvar, xvar, zvar, k_moms=10, k_params=9)
b0=[1]*9
res1 = model1.fit(b0, maxiter=100, optim_method='bfgs', wargs=dict(centered=False))
print(res1.summary())
I've tried running it with my actual data and am getting strange results. It's hard to give more detail than that.