<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from statsmodels . compat . python import lrange , lmap <EOL> import numpy as np <EOL> from numpy . testing import assert_allclose , assert_almost_equal , assert_equal <EOL> from statsmodels import iolib <EOL> from statsmodels . tools . tools import add_constant <EOL> from statsmodels . regression . linear_model import OLS <EOL> import statsmodels . sandbox . regression . gmm as gmm <EOL> def get_griliches76_data ( ) : <EOL> import os <EOL> curdir = os . path . split ( __file__ ) [ <NUM_LIT:0> ] <EOL> path = os . path . join ( curdir , '<STR_LIT>' ) <EOL> griliches76_data = iolib . genfromdta ( path , missing_flt = np . NaN , pandas = True ) <EOL> years = griliches76_data [ '<STR_LIT>' ] . unique ( ) <EOL> N = griliches76_data . shape [ <NUM_LIT:0> ] <EOL> for yr in years : <EOL> griliches76_data [ '<STR_LIT>' % yr ] = np . zeros ( N ) <EOL> for i in range ( N ) : <EOL> if griliches76_data . ix [ i , '<STR_LIT>' ] == yr : <EOL> griliches76_data . ix [ i , '<STR_LIT>' % yr ] = <NUM_LIT:1> <EOL> else : <EOL> pass <EOL> griliches76_data [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> X = add_constant ( griliches76_data [ [ '<STR_LIT:s>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ] , <EOL> prepend = True ) <EOL> Z = add_constant ( griliches76_data [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] ) <EOL> Y = griliches76_data [ '<STR_LIT>' ] <EOL> return Y , X , Z <EOL> yg_df , xg_df , zg_df = get_griliches76_data ( ) <EOL> endog = np . asarray ( yg_df , dtype = float ) <EOL> exog , instrument = lmap ( np . asarray , [ xg_df , zg_df ] ) <EOL> assert exog . dtype == np . float64 <EOL> assert instrument . dtype == np . float64 <EOL> varnames = np . array ( [ "<STR_LIT>" , "<STR_LIT:s>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> params = np . array ( [ <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> bse = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> tvalues = np . array ( [ <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> pvalues = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> ] ) <EOL> def test_iv2sls_r ( ) : <EOL> mod = gmm . IV2SLS ( endog , exog , instrument ) <EOL> res = mod . fit ( ) <EOL> n , k = exog . shape <EOL> assert_allclose ( res . params , params , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res . bse , bse , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> def test_ivgmm0_r ( ) : <EOL> n , k = exog . shape <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> w0 = np . linalg . inv ( w0inv ) <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( np . ones ( exog . shape [ <NUM_LIT:1> ] , float ) , maxiter = <NUM_LIT:0> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , <EOL> optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> assert_allclose ( res . params , params , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res . bse , bse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> score = res . model . score ( res . params , w0 ) <EOL> assert_allclose ( score , np . zeros ( score . shape ) , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> def test_ivgmm1_stata ( ) : <EOL> params_stata = np . array ( <EOL> [ <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> bse_stata = np . array ( <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> n , k = exog . shape <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> w0 = np . linalg . inv ( w0inv ) <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> idx = lrange ( len ( params ) ) <EOL> idx = idx [ <NUM_LIT:1> : ] + idx [ : <NUM_LIT:1> ] <EOL> exog_st = exog [ : , idx ] <EOL> class TestGMMOLS ( object ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> exog = exog_st <EOL> res_ols = OLS ( endog , exog ) . fit ( ) <EOL> nobs , k_instr = exog . shape <EOL> w0inv = np . dot ( exog . T , exog ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , exog ) <EOL> res = mod . fit ( np . ones ( exog . shape [ <NUM_LIT:1> ] , float ) , maxiter = <NUM_LIT:0> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . res1 = res <EOL> self . res2 = res_ols <EOL> def test_basic ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> n = res1 . model . exog . shape [ <NUM_LIT:0> ] <EOL> dffac = <NUM_LIT:1> <EOL> assert_allclose ( res1 . bse * dffac , res2 . HC0_se , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . bse * dffac , res2 . HC0_se , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> def test_other ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> class CheckGMM ( object ) : <EOL> params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> def test_basic ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> rtol , atol = self . params_tol <EOL> assert_allclose ( res1 . params , res2 . params , rtol = rtol , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT:0> , atol = atol ) <EOL> n = res1 . model . exog . shape [ <NUM_LIT:0> ] <EOL> dffac = <NUM_LIT:1> <EOL> rtol , atol = self . bse_tol <EOL> assert_allclose ( res1 . bse * dffac , res2 . bse , rtol = rtol , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . bse * dffac , res2 . bse , rtol = <NUM_LIT:0> , atol = atol ) <EOL> def _est_other ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> assert_allclose ( res1 . q , res2 . Q , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . jval , res2 . J , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> def test_hypothesis ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> restriction = np . eye ( len ( res1 . params ) ) <EOL> res_t = res1 . t_test ( restriction ) <EOL> assert_allclose ( res_t . tvalue , res1 . tvalues , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res_t . pvalue , res1 . pvalues , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> rtol , atol = self . bse_tol <EOL> assert_allclose ( res_t . tvalue , res2 . tvalues , rtol = rtol * <NUM_LIT:10> , atol = atol ) <EOL> assert_allclose ( res_t . pvalue , res2 . pvalues , rtol = rtol * <NUM_LIT:10> , atol = atol ) <EOL> res_f = res1 . f_test ( restriction [ : - <NUM_LIT:1> ] ) <EOL> res_wald = res1 . wald_test ( restriction [ : - <NUM_LIT:1> ] ) <EOL> class TestGMMSt1 ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res10 = mod . fit ( start , maxiter = <NUM_LIT:10> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } ) <EOL> self . res1 = res10 <EOL> from . results_gmm_griliches_iter import results <EOL> self . res2 = results <EOL> class TestGMMStTwostep ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res10 = mod . fit ( start , maxiter = <NUM_LIT:2> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } ) <EOL> self . res1 = res10 <EOL> from . results_gmm_griliches import results_twostep as results <EOL> self . res2 = results <EOL> class TestGMMStTwostepNO ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res10 = mod . fit ( start , maxiter = <NUM_LIT:2> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res1 = res10 <EOL> from . results_gmm_griliches import results_twostep as results <EOL> self . res2 = results <EOL> class TestGMMStOnestep ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:0> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , <EOL> optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . res1 = res <EOL> from . results_gmm_griliches import results_onestep as results <EOL> self . res2 = results <EOL> def test_bse_other ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> bse = np . sqrt ( np . diag ( ( res1 . _cov_params ( has_optimal_weights = False ) ) ) ) <EOL> q = self . res1 . model . gmmobjective ( self . res1 . params , np . linalg . inv ( self . res1 . weights ) ) <EOL> class TestGMMStOnestepNO ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:0> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res1 = res <EOL> from . results_gmm_griliches import results_onestep as results <EOL> self . res2 = results <EOL> class TestGMMStOneiter ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . res1 = res <EOL> from . results_gmm_griliches import results_onestep as results <EOL> self . res2 = results <EOL> def test_bse_other ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> moms = res1 . model . momcond ( res1 . params ) <EOL> w = res1 . model . calc_weightmatrix ( moms ) <EOL> bse = np . sqrt ( np . diag ( ( res1 . _cov_params ( has_optimal_weights = False , <EOL> weights = res1 . weights ) ) ) ) <EOL> bse = np . sqrt ( np . diag ( ( res1 . _cov_params ( has_optimal_weights = False , <EOL> ) ) ) ) <EOL> nobs = instrument . shape [ <NUM_LIT:0> ] <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> q = self . res1 . model . gmmobjective ( self . res1 . params , w ) <EOL> class TestGMMStOneiterNO ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res1 = res <EOL> from . results_gmm_griliches import results_onestep as results <EOL> self . res2 = results <EOL> class TestGMMStOneiterNO_Linear ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . LinearIVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res1 = res <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res3 = res <EOL> from . results_gmm_griliches import results_onestep as results <EOL> self . res2 = results <EOL> class TestGMMStOneiterNO_Nonlinear ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> def func ( params , exog ) : <EOL> return np . dot ( exog , params ) <EOL> mod = gmm . NonlinearIVGMM ( endog , exog , instrument , func ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res1 = res <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> wargs = { '<STR_LIT>' : False } , has_optimal_weights = False ) <EOL> self . res3 = res <EOL> from . results_gmm_griliches import results_onestep as results <EOL> self . res2 = results <EOL> def test_score ( self ) : <EOL> params = self . res1 . params * <NUM_LIT> <EOL> weights = self . res1 . weights <EOL> sc1 = self . res1 . model . score ( params , weights ) <EOL> sc2 = super ( self . res1 . model . __class__ , self . res1 . model ) . score ( params , <EOL> weights ) <EOL> assert_allclose ( sc1 , sc2 , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( sc1 , sc2 , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> sc1 = self . res1 . model . score ( self . res1 . params , weights ) <EOL> assert_allclose ( sc1 , np . zeros ( len ( params ) ) , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> class TestGMMStOneiterOLS_Linear ( CheckGMM ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . params_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> self . bse_tol = [ <NUM_LIT> , <NUM_LIT> ] <EOL> exog = exog_st <EOL> res_ols = OLS ( endog , exog ) . fit ( ) <EOL> start = np . ones ( len ( res_ols . params ) ) <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( exog . T , exog ) / nobs <EOL> mod = gmm . LinearIVGMM ( endog , exog , exog ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:0> , inv_weights = w0inv , <EOL> optim_args = { '<STR_LIT>' : <NUM_LIT:0> } , <EOL> weights_method = '<STR_LIT>' , <EOL> wargs = { '<STR_LIT>' : False , '<STR_LIT>' : '<STR_LIT>' } , <EOL> has_optimal_weights = True ) <EOL> res . use_t = True <EOL> res . df_resid = res . nobs - len ( res . params ) <EOL> self . res1 = res <EOL> self . res2 = res_ols <EOL> class TestGMMSt2 ( object ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> w0inv = np . dot ( instrument . T , instrument ) / nobs <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:2> , inv_weights = w0inv , <EOL> wargs = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : False } , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . res1 = res <EOL> from . results_ivreg2_griliches import results_gmm2s_robust as results <EOL> self . res2 = results <EOL> mod = gmm . IVGMM ( endog , exog , instrument ) <EOL> res = mod . fit ( start , maxiter = <NUM_LIT:1> , inv_weights = w0inv , <EOL> wargs = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : False } , <EOL> optim_method = '<STR_LIT>' , optim_args = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . res3 = res <EOL> def test_basic ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> n = res1 . model . exog . shape [ <NUM_LIT:0> ] <EOL> dffact = np . sqrt ( <NUM_LIT> / <NUM_LIT> ) <EOL> assert_allclose ( res1 . bse * dffact , res2 . bse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . bse * dffact , res2 . bse , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> bse = np . sqrt ( np . diag ( ( res1 . _cov_params ( has_optimal_weights = True , <EOL> weights = res1 . weights ) ) ) ) <EOL> assert_allclose ( res1 . bse , res2 . bse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> bse = np . sqrt ( np . diag ( ( res1 . _cov_params ( has_optimal_weights = True , <EOL> weights = res1 . weights , <EOL> use_weights = True ) ) ) ) <EOL> assert_allclose ( res1 . bse , res2 . bse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( self . res3 . bse , res2 . bse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( self . res3 . bse , res2 . bse , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> class CheckIV2SLS ( object ) : <EOL> def test_basic ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> n = res1 . model . exog . shape [ <NUM_LIT:0> ] <EOL> assert_allclose ( res1 . bse , res2 . bse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . bse , res2 . bse , rtol = <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . tvalues , res2 . tvalues , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> def test_other ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> assert_allclose ( res1 . rsquared , res2 . r2 , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . rsquared_adj , res2 . r2_a , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . fvalue , res2 . F , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . f_pvalue , res2 . Fp , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( np . sqrt ( res1 . mse_resid ) , res2 . rmse , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . ssr , res2 . rss , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . uncentered_tss , res2 . yy , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . centered_tss , res2 . yyc , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . ess , res2 . mss , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_equal ( res1 . df_model , res2 . df_m ) <EOL> assert_equal ( res1 . df_resid , res2 . df_r ) <EOL> def test_hypothesis ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> restriction = np . eye ( len ( res1 . params ) ) <EOL> res_t = res1 . t_test ( restriction ) <EOL> assert_allclose ( res_t . tvalue , res1 . tvalues , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res_t . pvalue , res1 . pvalues , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> res_f = res1 . f_test ( restriction [ : - <NUM_LIT:1> ] ) <EOL> assert_allclose ( res_f . fvalue , res1 . fvalue , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res_f . pvalue , res1 . f_pvalue , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res_f . fvalue , res2 . F , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( res_f . pvalue , res2 . Fp , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> def test_hausman ( self ) : <EOL> res1 , res2 = self . res1 , self . res2 <EOL> hausm = res1 . spec_hausman ( ) <EOL> assert_allclose ( hausm [ <NUM_LIT:0> ] , res2 . hausman [ '<STR_LIT>' ] , rtol = <NUM_LIT> , atol = <NUM_LIT:0> ) <EOL> assert_allclose ( hausm [ <NUM_LIT:1> ] , res2 . hausman [ '<STR_LIT>' ] , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> def test_smoke ( self ) : <EOL> res1 = self . res1 <EOL> res1 . summary ( ) <EOL> class TestIV2SLSSt1 ( CheckIV2SLS ) : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> exog = exog_st <EOL> start = OLS ( endog , exog ) . fit ( ) . params <EOL> nobs , k_instr = instrument . shape <EOL> mod = gmm . IV2SLS ( endog , exog , instrument ) <EOL> res = mod . fit ( ) <EOL> self . res1 = res <EOL> from . results_ivreg2_griliches import results_small as results <EOL> self . res2 = results </s>
