<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . testing import assert_almost_equal <EOL> import numpy . testing as npt <EOL> import statsmodels . tools . eval_measures as em <EOL> from statsmodels . stats . moment_helpers import cov2corr , se_cov <EOL> from statsmodels . regression . linear_model import OLS <EOL> from statsmodels . sandbox . panel . panel_short import ShortPanelGLS , ShortPanelGLS2 <EOL> from statsmodels . sandbox . panel . random_panel import PanelSample <EOL> import statsmodels . sandbox . panel . correlation_structures as cs <EOL> import statsmodels . stats . sandwich_covariance as sw <EOL> def assert_maxabs ( actual , expected , value ) : <EOL> npt . assert_array_less ( em . maxabs ( actual , expected , None ) , value ) <EOL> def test_short_panel ( ) : <EOL> nobs = <NUM_LIT:100> <EOL> nobs_i = <NUM_LIT:5> <EOL> n_groups = nobs // nobs_i <EOL> k_vars = <NUM_LIT:3> <EOL> dgp = PanelSample ( nobs , k_vars , n_groups , corr_structure = cs . corr_arma , <EOL> corr_args = ( [ <NUM_LIT:1> ] , [ <NUM_LIT:1.> , - <NUM_LIT> ] , ) , seed = <NUM_LIT> , within = False ) <EOL> y = dgp . generate_panel ( ) <EOL> noise = y - dgp . y_true <EOL> dgp_cov_e = np . array ( <EOL> [ [ <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.> ] ] ) <EOL> npt . assert_almost_equal ( dgp . cov , dgp_cov_e , <NUM_LIT> ) <EOL> cov_noise = np . cov ( noise . reshape ( - <NUM_LIT:1> , n_groups , order = '<STR_LIT:F>' ) ) <EOL> corr_noise = cov2corr ( cov_noise ) <EOL> npt . assert_almost_equal ( corr_noise , dgp . cov , <NUM_LIT:1> ) <EOL> mod2 = ShortPanelGLS ( y , dgp . exog , dgp . groups ) <EOL> res2 = mod2 . fit_iterative ( <NUM_LIT:2> ) <EOL> corr_wresid = np . corrcoef ( res2 . wresid . reshape ( - <NUM_LIT:1> , n_groups , order = '<STR_LIT:F>' ) ) <EOL> assert_maxabs ( corr_wresid , np . eye ( <NUM_LIT:5> ) , <NUM_LIT:0.1> ) <EOL> corr_resid = np . corrcoef ( res2 . resid . reshape ( - <NUM_LIT:1> , n_groups , order = '<STR_LIT:F>' ) ) <EOL> assert_maxabs ( corr_resid , dgp . cov , <NUM_LIT:0.1> ) <EOL> assert_almost_equal ( res2 . resid . std ( ) , <NUM_LIT:1> , decimal = <NUM_LIT:0> ) <EOL> y_pred = np . dot ( mod2 . exog , res2 . params ) <EOL> assert_almost_equal ( res2 . fittedvalues , y_pred , <NUM_LIT> ) <EOL> res2_ols = mod2 . _fit_ols ( ) <EOL> npt . assert_ ( mod2 . res_pooled is res2_ols ) <EOL> res2_ols = mod2 . res_pooled <EOL> mod1 = ShortPanelGLS ( y , dgp . exog , dgp . groups ) <EOL> res1 = mod1 . fit_iterative ( <NUM_LIT:1> ) <EOL> assert_almost_equal ( res1 . params , res2_ols . params , decimal = <NUM_LIT> ) <EOL> assert_almost_equal ( res1 . bse , res2_ols . bse , decimal = <NUM_LIT> ) <EOL> res_ols = OLS ( y , dgp . exog ) . fit ( ) <EOL> assert_almost_equal ( res1 . params , res_ols . params , decimal = <NUM_LIT> ) <EOL> assert_almost_equal ( res1 . bse , res_ols . bse , decimal = <NUM_LIT> ) <EOL> mod_old = ShortPanelGLS2 ( y , dgp . exog , dgp . groups ) <EOL> res_old = mod_old . fit ( ) <EOL> assert_almost_equal ( res2 . params , res_old . params , decimal = <NUM_LIT> ) <EOL> assert_almost_equal ( res2 . bse , res_old . bse , decimal = <NUM_LIT> ) <EOL> mod5 = ShortPanelGLS ( y , dgp . exog , dgp . groups ) <EOL> res5 = mod5 . fit_iterative ( <NUM_LIT:5> ) <EOL> cov_clu = sw . cov_cluster ( mod2 . res_pooled , dgp . groups . astype ( int ) ) <EOL> clubse = se_cov ( cov_clu ) <EOL> pnwbse = se_cov ( sw . cov_nw_panel ( mod2 . res_pooled , <NUM_LIT:4> , mod2 . group . groupidx ) ) <EOL> bser = np . vstack ( ( res2 . bse , res5 . bse , clubse , pnwbse ) ) <EOL> bser_mean = np . mean ( bser , axis = <NUM_LIT:0> ) <EOL> npt . assert_array_less ( clubse / bser_mean - <NUM_LIT:1> , <NUM_LIT> ) <EOL> npt . assert_array_less ( pnwbse / bser_mean - <NUM_LIT:1> , <NUM_LIT:0.1> ) <EOL> npt . assert_array_less ( <NUM_LIT> , bser_mean / res_ols . bse - <NUM_LIT:1> ) <EOL> cov_uni = sw . cov_nw_panel ( mod2 . res_pooled , <NUM_LIT:4> , mod2 . group . groupidx , <EOL> weights_func = sw . weights_uniform , <EOL> use_correction = '<STR_LIT:c>' ) <EOL> assert_almost_equal ( cov_uni , cov_clu , decimal = <NUM_LIT> ) <EOL> cov_clu2 = sw . cov_cluster ( mod2 . res_pooled , dgp . groups . astype ( int ) , <EOL> use_correction = False ) <EOL> cov_uni2 = sw . cov_nw_panel ( mod2 . res_pooled , <NUM_LIT:4> , mod2 . group . groupidx , <EOL> weights_func = sw . weights_uniform , <EOL> use_correction = False ) <EOL> assert_almost_equal ( cov_uni2 , cov_clu2 , decimal = <NUM_LIT> ) <EOL> cov_white = sw . cov_white_simple ( mod2 . res_pooled ) <EOL> cov_pnw0 = sw . cov_nw_panel ( mod2 . res_pooled , <NUM_LIT:0> , mod2 . group . groupidx , <EOL> use_correction = '<STR_LIT>' ) <EOL> assert_almost_equal ( cov_pnw0 , cov_white , decimal = <NUM_LIT> ) </s>
