<s> import scipy . stats <EOL> import numpy as np <EOL> import statsmodels . api as sm <EOL> from numpy . testing import assert_allclose , assert_equal , assert_almost_equal <EOL> from patsy import dmatrices <EOL> from statsmodels . regression . quantile_regression import QuantReg <EOL> from . results_quantile_regression import ( <EOL> biweight_chamberlain , biweight_hsheather , biweight_bofinger , <EOL> cosine_chamberlain , cosine_hsheather , cosine_bofinger , <EOL> gaussian_chamberlain , gaussian_hsheather , gaussian_bofinger , <EOL> epan2_chamberlain , epan2_hsheather , epan2_bofinger , <EOL> parzen_chamberlain , parzen_hsheather , parzen_bofinger , <EOL> epanechnikov_hsheather_q75 , Rquantreg ) <EOL> idx = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class CheckModelResultsMixin ( object ) : <EOL> def test_params ( self ) : <EOL> assert_allclose ( np . ravel ( self . res1 . params . ix [ idx ] ) , <EOL> self . res2 . table [ : , <NUM_LIT:0> ] , rtol = <NUM_LIT> ) <EOL> def test_bse ( self ) : <EOL> assert_equal ( self . res1 . scale , <NUM_LIT:1> ) <EOL> assert_allclose ( np . ravel ( self . res1 . bse . ix [ idx ] ) , <EOL> self . res2 . table [ : , <NUM_LIT:1> ] , rtol = <NUM_LIT> ) <EOL> def test_tvalues ( self ) : <EOL> assert_allclose ( np . ravel ( self . res1 . tvalues . ix [ idx ] ) , <EOL> self . res2 . table [ : , <NUM_LIT:2> ] , rtol = <NUM_LIT> ) <EOL> def test_pvalues ( self ) : <EOL> pvals_stata = scipy . stats . t . sf ( self . res2 . table [ : , <NUM_LIT:2> ] , self . res2 . df_r ) <EOL> assert_allclose ( np . ravel ( self . res1 . pvalues . ix [ idx ] ) , <EOL> pvals_stata , rtol = <NUM_LIT> ) <EOL> pvals_t = scipy . stats . t . sf ( self . res1 . tvalues , self . res2 . df_r ) * <NUM_LIT:2> <EOL> assert_allclose ( np . ravel ( self . res1 . pvalues ) , <EOL> pvals_t , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> def test_conf_int ( self ) : <EOL> assert_allclose ( self . res1 . conf_int ( ) . ix [ idx ] , <EOL> self . res2 . table [ : , - <NUM_LIT:2> : ] , rtol = <NUM_LIT> ) <EOL> def test_nobs ( self ) : <EOL> assert_allclose ( self . res1 . nobs , self . res2 . N , rtol = <NUM_LIT> ) <EOL> def test_df_model ( self ) : <EOL> assert_allclose ( self . res1 . df_model , self . res2 . df_m , rtol = <NUM_LIT> ) <EOL> def test_df_resid ( self ) : <EOL> assert_allclose ( self . res1 . df_resid , self . res2 . df_r , rtol = <NUM_LIT> ) <EOL> def test_prsquared ( self ) : <EOL> assert_allclose ( self . res1 . prsquared , self . res2 . psrsquared , rtol = <NUM_LIT> ) <EOL> def test_sparsity ( self ) : <EOL> assert_allclose ( np . array ( self . res1 . sparsity ) , <EOL> self . res2 . sparsity , rtol = <NUM_LIT> ) <EOL> def test_bandwidth ( self ) : <EOL> assert_allclose ( np . array ( self . res1 . bandwidth ) , <EOL> self . res2 . kbwidth , rtol = <NUM_LIT> ) <EOL> d = { ( '<STR_LIT>' , '<STR_LIT>' ) : biweight_bofinger , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : biweight_chamberlain , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : biweight_hsheather , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : cosine_bofinger , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : cosine_chamberlain , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : cosine_hsheather , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : gaussian_bofinger , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : gaussian_chamberlain , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : gaussian_hsheather , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : parzen_bofinger , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : parzen_chamberlain , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : parzen_hsheather , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : epan2_bofinger , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : epan2_chamberlain , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) : epan2_hsheather <EOL> } <EOL> def setup_fun ( kernel = '<STR_LIT>' , bandwidth = '<STR_LIT>' ) : <EOL> data = sm . datasets . engel . load_pandas ( ) . data <EOL> y , X = dmatrices ( '<STR_LIT>' , data , return_type = '<STR_LIT>' ) <EOL> statsm = QuantReg ( y , X ) . fit ( vcov = '<STR_LIT>' , kernel = kernel , bandwidth = bandwidth ) <EOL> stata = d [ ( kernel , bandwidth ) ] <EOL> return statsm , stata <EOL> def test_fitted_residuals ( ) : <EOL> data = sm . datasets . engel . load_pandas ( ) . data <EOL> y , X = dmatrices ( '<STR_LIT>' , data , return_type = '<STR_LIT>' ) <EOL> res = QuantReg ( y , X ) . fit ( q = <NUM_LIT> ) <EOL> assert_almost_equal ( np . array ( res . fittedvalues ) , Rquantreg . fittedvalues , <NUM_LIT:5> ) <EOL> assert_almost_equal ( np . array ( res . predict ( ) ) , Rquantreg . fittedvalues , <NUM_LIT:5> ) <EOL> assert_almost_equal ( np . array ( res . resid ) , Rquantreg . residuals , <NUM_LIT:5> ) <EOL> class TestEpanechnikovHsheatherQ75 ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> data = sm . datasets . engel . load_pandas ( ) . data <EOL> y , X = dmatrices ( '<STR_LIT>' , data , return_type = '<STR_LIT>' ) <EOL> cls . res1 = QuantReg ( y , X ) . fit ( q = <NUM_LIT> , vcov = '<STR_LIT>' , kernel = '<STR_LIT>' , bandwidth = '<STR_LIT>' ) <EOL> cls . res2 = epanechnikov_hsheather_q75 <EOL> class TestEpanechnikovBofinger ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestEpanechnikovChamberlain ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestEpanechnikovHsheather ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestGaussianBofinger ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestGaussianChamberlain ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestGaussianHsheather ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestBiweightBofinger ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestBiweightChamberlain ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestBiweightHsheather ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestCosineBofinger ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestCosineChamberlain ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestCosineHsheather ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestParzeneBofinger ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestParzeneChamberlain ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestParzeneHsheather ( CheckModelResultsMixin ) : <EOL> @ classmethod <EOL> def setUp ( cls ) : <EOL> cls . res1 , cls . res2 = setup_fun ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_zero_resid ( ) : <EOL> X = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT> ] , [ <NUM_LIT:0> , <NUM_LIT> ] ] , dtype = np . float64 ) <EOL> y = np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , dtype = np . float64 ) <EOL> res = QuantReg ( y , X ) . fit ( <NUM_LIT:0.5> , bandwidth = '<STR_LIT>' ) <EOL> res . summary ( ) <EOL> assert_allclose ( res . params , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT> ] ) , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res . bse , np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res . resid , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT> , - <NUM_LIT> , <EOL> <NUM_LIT> ] ) , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> X = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0.1> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT> ] , [ <NUM_LIT:0> , <NUM_LIT> ] ] , dtype = np . float64 ) <EOL> y = np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , dtype = np . float64 ) <EOL> res = QuantReg ( y , X ) . fit ( <NUM_LIT:0.5> , bandwidth = '<STR_LIT>' ) <EOL> res . summary ( ) <EOL> assert_allclose ( res . params , np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) , <EOL> rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res . bse , np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) , rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> assert_allclose ( res . resid , np . array ( [ - <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , <NUM_LIT> ] ) , rtol = <NUM_LIT> , atol = <NUM_LIT> ) </s>
