def setup_class(self): XLISTEXOG2 = 'aget aget2 educyr actlim totchr'.split() endog_name = 'docvis' exog_names = 'private medicaid'.split() + XLISTEXOG2 + ['const'] instrument_names = 'income ssiratio'.split() + XLISTEXOG2 + ['const'] endog = DATA[endog_name] exog = DATA[exog_names] instrument = DATA[instrument_names] asarray = lambda x: np.asarray(x, float) endog, exog, instrument = map(asarray, [endog, exog, instrument]) self.bse_tol = [5e-6, 5e-7] # compare to Stata default options, iterative GMM # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs mod = gmm.NonlinearIVGMM(endog, exog, instrument, moment_exponential_add) res0 = mod.fit(start, maxiter=2, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol': 1e-8}, wargs={'centered': False}, has_optimal_weights=False) self.res1 = res0 from results_gmm_poisson import results_addtwostep as results self.res2 = results
def setup_class(cls): # compare to Stata default options, twostep GMM XLISTEXOG2 = 'aget aget2 educyr actlim totchr'.split() endog_name = 'docvis' exog_names = 'private medicaid'.split() + XLISTEXOG2 + ['const'] instrument_names = 'income medicaid ssiratio'.split() + XLISTEXOG2 + ['const'] endog = DATA[endog_name] exog = DATA[exog_names] instrument = DATA[instrument_names] asarray = lambda x: np.asarray(x, float) endog, exog, instrument = lmap(asarray, [endog, exog, instrument]) # Need to add all data into exog endog_ = np.zeros(len(endog)) exog_ = np.column_stack((endog, exog)) cls.bse_tol = [5e-6, 5e-7] # compare to Stata default options, iterative GMM # with const at end start = OLS(endog, exog).fit().params nobs, k_instr = instrument.shape w0inv = np.dot(instrument.T, instrument) / nobs mod = gmm.NonlinearIVGMM(endog_, exog_, instrument, moment_exponential_mult) res0 = mod.fit(start, maxiter=2, inv_weights=w0inv, optim_method='bfgs', optim_args={'gtol':1e-8, 'disp': 0}, wargs={'centered':False}, has_optimal_weights=False) cls.res1 = res0 from .results_gmm_poisson import results_multtwostep as results cls.res2 = results
def setup_class(cls): # compare to Stata default options, onestep GMM # this uses maxiter=1, one iteration in loop cls.params_tol = [5e-5, 5e-6] cls.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, 'disp': 0}, wargs={'centered':False}, has_optimal_weights=False) cls.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, 'disp': 0}, wargs={'centered':False}, has_optimal_weights=False) cls.res3 = res from .results_gmm_griliches import results_onestep as results cls.res2 = results
# Currently statsmodels has two ways of specifying moment conditions. # The first uses general non-linear functions for the (unconditional) moment condition # The second version uses an instrumental variables approach with additive error structure def moment_consumption1(params, exog): beta, gamma = params r_forw1, c_forw1, c = exog.T # unwrap iterable (ndarray) # moment condition without instrument err = 1 - beta * (1 + r_forw1) * np.power(c_forw1 / c, -gamma) return -err endog1 = np.zeros(exog.shape[0]) mod1 = gmm.NonlinearIVGMM(endog1, exog, instrument, moment_consumption1, k_moms=4) w0inv = np.dot(instrument.T, instrument) / len(endog1) res1 = mod1.fit([1,-1], maxiter=2, inv_weights=w0inv) print(res1.summary(yname='Euler Eq', xname=['discount', 'CRRA'])) # We don't need Nelder-Mead in this case, we can use bfgs default directly # res1_ = mod1.fit([1,-1], maxiter=0, inv_weights=w0inv, opt_method='nm') res1_hac4_2s = mod1.fit([1, -1], maxiter=2, inv_weights=w0inv, weights_method='hac', wargs={'maxlag':4}) print(res1_hac4_2s.summary(yname='Euler Eq', xname=['discount', 'CRRA'])) def moment_consumption2(params, exog): beta, gamma = params