def setup_class(cls): # compare to Stata default options, onestep GMM # this uses maxiter=1, one iteration in loop cls.params_tol = [1e-5, 1e-6] cls.bse_tol = [5e-6, 5e-7] exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=1, inv_weights=w0inv, optim_method='bfgs', optim_args={ 'gtol': 1e-6, 'disp': 0 }, wargs={'centered': False}, has_optimal_weights=False) cls.res1 = res from .results_gmm_griliches import results_onestep as results cls.res2 = results
def test_ivgmm0_r(): n, k = exog.shape nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(np.ones(exog.shape[1], float), maxiter=0, inv_weights=w0inv, optim_method='bfgs', optim_args={ 'gtol': 1e-8, 'disp': 0 }) assert_allclose(res.params, params, rtol=1e-4, atol=1e-4) # TODO : res.bse and bse are not the same, rtol=0.09 is large in this case #res.bse is still robust?, bse is not a sandwich ? assert_allclose(res.bse, bse, rtol=0.09, atol=0) score = res.model.score(res.params, w0) assert_allclose(score, np.zeros(score.shape), rtol=0, atol=5e-6) # atol=1e-8) ??
def test_ivgmm1_stata(): # copied constant to the beginning params_stata = np.array([ 4.0335099, 0.17242531, -0.00909883, 0.04928949, 0.04221709, -0.10179345, 0.12611095, -0.05961711, 0.04867956, 0.15281763, 0.17443605, 0.09166597, 0.09323976 ]) # robust bse with gmm onestep bse_stata = np.array([ 0.33503289, 0.02073947, 0.00488624, 0.0080498, 0.00946363, 0.03371053, 0.03081138, 0.05171372, 0.04981322, 0.0479285, 0.06112515, 0.0554618, 0.06084901 ]) n, k = exog.shape nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs w0 = np.linalg.inv(w0inv) start = OLS(endog, exog).fit().params mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=1, inv_weights=w0inv, optim_method='bfgs', optim_args={ 'gtol': 1e-6, 'disp': 0 })
def setup_class(self): # compare to Stata default options, twostep GMM self.params_tol = [5e-5, 5e-6] self.bse_tol = [1e-6, 5e-5] exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res10 = mod.fit(start, maxiter=2, inv_weights=w0inv, optim_method='bfgs', optim_args={ 'gtol': 1e-6, 'disp': 0 }, wargs={'centered': False}, has_optimal_weights=False) self.res1 = res10 from .results_gmm_griliches import results_twostep as results self.res2 = results
def setup_class(cls): # compare to Stata default options, iterative GMM exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=2, inv_weights=w0inv, wargs={ 'ddof': 0, 'centered': False }, optim_method='bfgs', optim_args={ 'gtol': 1e-6, 'disp': 0 }) cls.res1 = res from .results_ivreg2_griliches import results_gmm2s_robust as results cls.res2 = results # TODO: remove after testing, compare bse from 1 iteration # see test_basic mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=1, inv_weights=w0inv, wargs={ 'ddof': 0, 'centered': False }, optim_method='bfgs', optim_args={ 'gtol': 1e-6, 'disp': 0 }) cls.res3 = res
def setup_class(cls): exog = exog_st # with const at end res_ols = OLS(endog, exog).fit() # use exog as instrument nobs, k_instr = exog.shape w0inv = np.dot(exog.T, exog) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, exog) res = mod.fit(np.ones(exog.shape[1], float), maxiter=0, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol':1e-6, 'disp': 0}) cls.res1 = res cls.res2 = res_ols
def setup_class(cls): #cls.bse_tol = [5e-7, 5e-7] # compare to Stata default options, iterative GMM exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res10 = mod.fit(start, maxiter=10, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol':1e-6, 'disp': 0}, wargs={'centered':False}) cls.res1 = res10 from .results_gmm_griliches_iter import results cls.res2 = results
def setup_class(cls): # compare to Stata default options, onestep GMM cls.params_tol = [5e-4, 5e-5] cls.bse_tol = [7e-3, 5e-4] exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=0, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol':1e-6, 'disp': 0}) cls.res1 = res from .results_gmm_griliches import results_onestep as results cls.res2 = results
def setup_class(self): # compare to Stata default options, onestep GMM # this uses maxiter=1, one iteration in loop self.params_tol = [5e-4, 5e-5] self.bse_tol = [7e-3, 5e-4] exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=1, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol': 1e-6}) self.res1 = res from results_gmm_griliches import results_onestep as results self.res2 = results
def setup_class(self): # compare to Stata default options, onestep GMM # this uses maxiter=1, one iteration in loop self.params_tol = [5e-5, 5e-6] self.bse_tol = [5e-6, 1e-1] exog = exog_st # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs #w0 = np.linalg.inv(w0inv) def func(params, exog): return np.dot(exog, params) mod = gmm.NonlinearIVGMM(endog, exog, instrument, func) res = mod.fit(start, maxiter=1, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol': 1e-8}, wargs={'centered': False}, has_optimal_weights=False) self.res1 = res mod = gmm.IVGMM(endog, exog, instrument) res = mod.fit(start, maxiter=1, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol': 1e-6}, wargs={'centered': False}, has_optimal_weights=False) self.res3 = res from results_gmm_griliches import results_onestep as results self.res2 = results
return endog, exog, instrument if exampledata == 'ols': endog, exog, _ = sample_ols(X) instrument = exog elif exampledata == 'iv': endog, exog, instrument = sample_iv(X) elif exampledata == 'ivfake': endog, exog, instrument = sample_ivfake(X) #using GMM and IV2SLS classes #---------------------------- mod = gmm.IVGMM(endog, exog, instrument, nmoms=instrument.shape[1]) res = mod.fit() modgmmols = gmm.IVGMM(endog, exog, exog, nmoms=exog.shape[1]) resgmmols = modgmmols.fit() #the next is the same as IV2SLS, (Z'Z)^{-1} as weighting matrix modgmmiv = gmm.IVGMM(endog, exog, instrument, nmoms=instrument.shape[1]) #same as mod resgmmiv = modgmmiv.fitgmm(np.ones(exog.shape[1], float), weights=np.linalg.inv(np.dot(instrument.T, instrument))) modls = gmm.IV2SLS(endog, exog, instrument) resls = modls.fit() modols = OLS(endog, exog) resols = modols.fit() print('\nIV case') print('params') print('IV2SLS', resls.params)