<s> """<STR_LIT>""" <EOL> from statsmodels . compat . python import StringIO <EOL> import numpy as np <EOL> from numpy . testing import assert_allclose , assert_equal , assert_ <EOL> from nose import SkipTest <EOL> import pandas as pd <EOL> import patsy <EOL> from statsmodels . discrete . discrete_model import Poisson <EOL> from statsmodels . discrete . discrete_model import Logit <EOL> from statsmodels . genmod . generalized_linear_model import GLM <EOL> from statsmodels . genmod import families <EOL> from statsmodels . base . _constraints import fit_constrained <EOL> from statsmodels . tools . tools import add_constant <EOL> from statsmodels import datasets <EOL> spector_data = datasets . spector . load ( ) <EOL> spector_data . exog = add_constant ( spector_data . exog , prepend = False ) <EOL> from . results import results_poisson_constrained as results <EOL> from . results import results_glm_logit_constrained as reslogit <EOL> DEBUG = False <EOL> ss = '''<STR_LIT>''' <EOL> data = pd . read_csv ( StringIO ( ss ) , delimiter = '<STR_LIT:\t>' ) <EOL> data [ '<STR_LIT>' ] = np . log ( data [ '<STR_LIT>' ] ) <EOL> class CheckPoissonConstrainedMixin ( object ) : <EOL> def test_basic ( self ) : <EOL> res1 = self . res1 <EOL> res2 = self . res2 <EOL> assert_allclose ( res1 [ <NUM_LIT:0> ] , res2 . params [ self . idx ] , rtol = <NUM_LIT> ) <EOL> bse1 = np . sqrt ( np . diag ( res1 [ <NUM_LIT:1> ] ) ) <EOL> mask = ( bse1 == <NUM_LIT:0> ) & np . isnan ( res2 . bse [ self . idx ] ) <EOL> assert_allclose ( bse1 [ ~ mask ] , res2 . bse [ self . idx ] [ ~ mask ] , rtol = <NUM_LIT> ) <EOL> def test_basic_method ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> res1 = ( self . res1m if not hasattr ( self . res1m , '<STR_LIT>' ) <EOL> else self . res1m . _results ) <EOL> res2 = self . res2 <EOL> assert_allclose ( res1 . params , res2 . params [ self . idx ] , rtol = <NUM_LIT> ) <EOL> mask = ( res1 . bse == <NUM_LIT:0> ) & np . isnan ( res2 . bse [ self . idx ] ) <EOL> assert_allclose ( res1 . bse [ ~ mask ] , res2 . bse [ self . idx ] [ ~ mask ] , rtol = <NUM_LIT> ) <EOL> tvalues = res2 . params_table [ self . idx , <NUM_LIT:2> ] <EOL> mask = np . isinf ( res1 . tvalues ) & np . isnan ( tvalues ) <EOL> assert_allclose ( res1 . tvalues [ ~ mask ] , tvalues [ ~ mask ] , rtol = <NUM_LIT> ) <EOL> pvalues = res2 . params_table [ self . idx , <NUM_LIT:3> ] <EOL> mask = ( res1 . pvalues == <NUM_LIT:0> ) & np . isnan ( pvalues ) <EOL> assert_allclose ( res1 . pvalues [ ~ mask ] , pvalues [ ~ mask ] , rtol = <NUM_LIT> ) <EOL> ci_low = res2 . params_table [ self . idx , <NUM_LIT:4> ] <EOL> ci_upp = res2 . params_table [ self . idx , <NUM_LIT:5> ] <EOL> ci = np . column_stack ( ( ci_low , ci_upp ) ) <EOL> assert_allclose ( res1 . conf_int ( ) [ ~ np . isnan ( ci ) ] , ci [ ~ np . isnan ( ci ) ] , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . llf , res2 . ll , rtol = <NUM_LIT> ) <EOL> assert_equal ( res1 . df_model , res2 . df_m ) <EOL> df_r = res2 . N - res2 . df_m - <NUM_LIT:1> <EOL> assert_equal ( res1 . df_resid , df_r ) <EOL> else : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def test_other ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> res1 = self . res1m <EOL> res2 = self . res2 <EOL> if hasattr ( res2 , '<STR_LIT>' ) : <EOL> assert_allclose ( res1 . llnull , res2 . ll_0 , rtol = <NUM_LIT> ) <EOL> else : <EOL> if DEBUG : <EOL> import warnings <EOL> message = ( '<STR_LIT>' <EOL> % res1 . llnull ) <EOL> warnings . warn ( message ) <EOL> else : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> class TestPoissonConstrained1a ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_noexposure_constraint <EOL> cls . idx = [ <NUM_LIT:7> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data ) <EOL> k_vars = len ( mod . exog_names ) <EOL> start_params = np . zeros ( k_vars ) <EOL> start_params [ <NUM_LIT:0> ] = np . log ( mod . endog . mean ( ) ) <EOL> p = np . array ( [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants , <EOL> start_params = start_params , <EOL> fit_kwds = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> cls . res1m = mod . fit_constrained ( constr , start_params = start_params , <EOL> method = '<STR_LIT>' , disp = <NUM_LIT:0> ) <EOL> def test_smoke ( self ) : <EOL> summ = self . res1m . summary ( ) <EOL> assert_ ( '<STR_LIT>' in summ . extra_txt ) <EOL> summ = self . res1m . summary2 ( ) <EOL> assert_ ( '<STR_LIT>' in summ . extra_txt [ <NUM_LIT:0> ] ) <EOL> class TestPoissonConstrained1b ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_exposure_constraint <EOL> cls . idx = [ <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data , <EOL> exposure = data [ '<STR_LIT>' ] . values ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants , <EOL> fit_kwds = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> cls . constraints = lc <EOL> cls . res1m = mod . fit_constrained ( constr , method = '<STR_LIT>' , <EOL> disp = <NUM_LIT:0> ) <EOL> class TestPoissonConstrained1c ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_exposure_constraint <EOL> cls . idx = [ <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data , <EOL> offset = np . log ( data [ '<STR_LIT>' ] . values ) ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants , <EOL> fit_kwds = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> cls . constraints = lc <EOL> cls . res1m = mod . fit_constrained ( constr , method = '<STR_LIT>' , disp = <NUM_LIT:0> ) <EOL> class TestPoissonNoConstrained ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_exposure_noconstraint <EOL> cls . idx = [ <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data , <EOL> offset = np . log ( data [ '<STR_LIT>' ] . values ) ) <EOL> res1 = mod . fit ( disp = <NUM_LIT:0> ) . _results <EOL> cls . res1 = ( res1 . params , res1 . cov_params ( ) ) <EOL> cls . res1m = res1 <EOL> class TestPoissonConstrained2a ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_noexposure_constraint2 <EOL> cls . idx = [ <NUM_LIT:7> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data ) <EOL> k_vars = len ( mod . exog_names ) <EOL> start_params = np . zeros ( k_vars ) <EOL> start_params [ <NUM_LIT:0> ] = np . log ( mod . endog . mean ( ) ) <EOL> p = np . array ( [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants , <EOL> start_params = start_params , <EOL> fit_kwds = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> cls . res1m = mod . fit_constrained ( constr , start_params = start_params , <EOL> method = '<STR_LIT>' , disp = <NUM_LIT:0> ) <EOL> class TestPoissonConstrained2b ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_exposure_constraint2 <EOL> cls . idx = [ <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data , <EOL> exposure = data [ '<STR_LIT>' ] . values ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants , <EOL> fit_kwds = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> cls . constraints = lc <EOL> cls . res1m = mod . fit_constrained ( constr , method = '<STR_LIT>' , disp = <NUM_LIT:0> , <EOL> start_params = cls . res1 [ <NUM_LIT:0> ] ) <EOL> class TestPoissonConstrained2c ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . res2 = results . results_exposure_constraint2 <EOL> cls . idx = [ <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data , <EOL> offset = np . log ( data [ '<STR_LIT>' ] . values ) ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants , <EOL> fit_kwds = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> cls . constraints = lc <EOL> cls . res1m = mod . fit_constrained ( constr , <EOL> method = '<STR_LIT>' , disp = <NUM_LIT:0> , <EOL> start_params = cls . res1 [ <NUM_LIT:0> ] ) <EOL> class TestGLMPoissonConstrained1a ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> from statsmodels . base . _constraints import fit_constrained <EOL> cls . res2 = results . results_noexposure_constraint <EOL> cls . idx = [ <NUM_LIT:7> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> formula = '<STR_LIT>' <EOL> mod = GLM . from_formula ( formula , data = data , <EOL> family = families . Poisson ( ) ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants ) <EOL> cls . constraints = lc <EOL> cls . res1m = mod . fit_constrained ( constr ) <EOL> class TestGLMPoissonConstrained1b ( CheckPoissonConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> from statsmodels . genmod . generalized_linear_model import GLM <EOL> from statsmodels . genmod import families <EOL> from statsmodels . base . _constraints import fit_constrained <EOL> cls . res2 = results . results_exposure_constraint <EOL> cls . idx = [ <NUM_LIT:6> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:0> ] <EOL> formula = '<STR_LIT>' <EOL> mod = GLM . from_formula ( formula , data = data , <EOL> family = families . Poisson ( ) , <EOL> offset = np . log ( data [ '<STR_LIT>' ] . values ) ) <EOL> constr = '<STR_LIT>' <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constr ) <EOL> cls . res1 = fit_constrained ( mod , lc . coefs , lc . constants ) <EOL> cls . constraints = lc <EOL> cls . res1m = mod . fit_constrained ( constr ) . _results <EOL> def test_compare_glm_poisson ( self ) : <EOL> res1 = self . res1m <EOL> res2 = self . res2 <EOL> formula = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula , data = data , <EOL> exposure = data [ '<STR_LIT>' ] . values ) <EOL> constr = '<STR_LIT>' <EOL> res2 = mod . fit_constrained ( constr , start_params = self . res1m . params , <EOL> method = '<STR_LIT>' , warn_convergence = False , <EOL> disp = <NUM_LIT:0> ) <EOL> assert_allclose ( res1 . params , res2 . params , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . bse , res2 . bse , rtol = <NUM_LIT> ) <EOL> predicted = res1 . predict ( ) <EOL> assert_allclose ( predicted , res2 . predict ( ) , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . mu , predicted , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . fittedvalues , predicted , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res2 . predict ( linear = True ) , res2 . predict ( linear = True ) , <EOL> rtol = <NUM_LIT> ) <EOL> class CheckGLMConstrainedMixin ( CheckPoissonConstrainedMixin ) : <EOL> def test_glm ( self ) : <EOL> res2 = self . res2 <EOL> res1 = self . res1m <EOL> assert_allclose ( res1 . aic , res2 . infocrit [ <NUM_LIT:4> ] , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . bic , res2 . bic , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . deviance , res2 . deviance , rtol = <NUM_LIT> ) <EOL> class TestGLMLogitConstrained1 ( CheckGLMConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . idx = slice ( None ) <EOL> cls . res2 = reslogit . results_constraint1 <EOL> mod1 = GLM ( spector_data . endog , spector_data . exog , <EOL> family = families . Binomial ( ) ) <EOL> constr = '<STR_LIT>' <EOL> cls . res1m = mod1 . fit_constrained ( constr ) <EOL> R , q = cls . res1m . constraints . coefs , cls . res1m . constraints . constants <EOL> cls . res1 = fit_constrained ( mod1 , R , q ) <EOL> class TestGLMLogitConstrained2 ( CheckGLMConstrainedMixin ) : <EOL> @ classmethod <EOL> def setup_class ( cls ) : <EOL> cls . idx = slice ( None ) <EOL> cls . res2 = reslogit . results_constraint2 <EOL> mod1 = GLM ( spector_data . endog , spector_data . exog , <EOL> family = families . Binomial ( ) ) <EOL> constr = '<STR_LIT>' <EOL> cls . res1m = mod1 . fit_constrained ( constr ) <EOL> R , q = cls . res1m . constraints . coefs , cls . res1m . constraints . constants <EOL> cls . res1 = fit_constrained ( mod1 , R , q ) <EOL> cls . constraints_rq = ( R , q ) <EOL> def test_predict ( self ) : <EOL> res2 = self . res2 <EOL> res1 = self . res1m <EOL> predicted = res1 . predict ( ) <EOL> assert_allclose ( predicted , res2 . predict_mu , atol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . mu , predicted , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res1 . fittedvalues , predicted , rtol = <NUM_LIT> ) <EOL> def test_smoke ( self ) : <EOL> summ = self . res1m . summary ( ) <EOL> assert_ ( '<STR_LIT>' in summ . extra_txt ) <EOL> summ = self . res1m . summary2 ( ) <EOL> assert_ ( '<STR_LIT>' in summ . extra_txt [ <NUM_LIT:0> ] ) <EOL> def test_fit_constrained_wrap ( self ) : <EOL> res2 = self . res2 <EOL> from statsmodels . base . _constraints import fit_constrained_wrap <EOL> res_wrap = fit_constrained_wrap ( self . res1m . model , self . constraints_rq ) <EOL> assert_allclose ( res_wrap . params , res2 . params , rtol = <NUM_LIT> ) <EOL> assert_allclose ( res_wrap . params , res2 . params , rtol = <NUM_LIT> ) <EOL> def junk ( ) : <EOL> formula1 = '<STR_LIT>' <EOL> formula2 = '<STR_LIT>' <EOL> mod = Poisson . from_formula ( formula2 , data = data , exposure = data [ '<STR_LIT>' ] . values ) <EOL> res0 = mod . fit ( ) <EOL> constraints = '<STR_LIT>' <EOL> import patsy <EOL> lc = patsy . DesignInfo ( mod . exog_names ) . linear_constraint ( constraints ) <EOL> R , q = lc . coefs , lc . constants <EOL> resc = mod . fit_constrained ( R , q , fit_kwds = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> formula1a = '<STR_LIT>' <EOL> mod1a = Poisson . from_formula ( formula1a , data = data ) <EOL> print ( mod1a . exog . shape ) <EOL> res1a = mod1a . fit ( ) <EOL> lc_1a = patsy . DesignInfo ( mod1a . exog_names ) . linear_constraint ( '<STR_LIT>' ) <EOL> resc1a = mod1a . fit_constrained ( lc_1a . coefs , lc_1a . constants , fit_kwds = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> print ( resc1a [ <NUM_LIT:0> ] ) <EOL> print ( resc1a [ <NUM_LIT:1> ] ) </s>
