<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from numpy . testing import assert_allclose , assert_raises <EOL> from statsmodels . base . _constraints import ( TransformRestriction , <EOL> transform_params_constraint , fit_constrained ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> R = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> k_constr , k_vars = R . shape <EOL> m = np . eye ( k_vars ) - R . T . dot ( np . linalg . pinv ( R ) . T ) <EOL> evals , evecs = np . linalg . eigh ( m ) <EOL> L = evecs [ : , : k_constr ] <EOL> T = evecs [ : , k_constr : ] <EOL> print T . T . dot ( np . eye ( k_vars ) ) <EOL> tr = np . column_stack ( ( T , R . T ) ) <EOL> q = [ <NUM_LIT:2> , <NUM_LIT:0> ] <EOL> tr0 = TransformRestriction ( R , q ) <EOL> p_reduced = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> assert_allclose ( tr0 . reduce ( tr0 . expand ( p_reduced ) ) , p_reduced , rtol = <NUM_LIT> ) <EOL> p = tr0 . expand ( p_reduced ) <EOL> assert_allclose ( R . dot ( p ) , q , rtol = <NUM_LIT> ) <EOL> R = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ] ] ) <EOL> q = np . zeros ( R . shape [ <NUM_LIT:0> ] ) <EOL> tr1 = TransformRestriction ( R , q ) <EOL> p = tr1 . expand ( [ <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> Ri = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:2> , <NUM_LIT:0> ] ] ) <EOL> tri = TransformRestriction ( Ri , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> p = tri . expand ( [ <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> print ( p [ [ <NUM_LIT:2> , <NUM_LIT:3> ] ] ) <EOL> Ri2 = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , - <NUM_LIT:2> , <NUM_LIT:0> ] ] ) <EOL> q = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> assert_raises ( ValueError , TransformRestriction , Ri2 , q ) <EOL> np . random . seed ( <NUM_LIT:1> ) <EOL> x = np . random . randn ( <NUM_LIT:10> , <NUM_LIT:5> ) <EOL> xr = tr1 . reduce ( x ) <EOL> x2 = tr1 . expand ( xr ) <EOL> from patsy import DesignInfo <EOL> names = '<STR_LIT>' . split ( ) <EOL> LC = DesignInfo ( names ) . linear_constraint ( '<STR_LIT>' ) <EOL> LC = DesignInfo ( names ) . linear_constraint ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> r_matrix , q_matrix = LC . coefs , LC . constants <EOL> np . random . seed ( <NUM_LIT> ) <EOL> nobs = <NUM_LIT:20> <EOL> x = <NUM_LIT:1> + np . random . randn ( nobs , <NUM_LIT:4> ) <EOL> exog = np . column_stack ( ( np . ones ( nobs ) , x ) ) <EOL> endog = exog . sum ( <NUM_LIT:1> ) + np . random . randn ( nobs ) <EOL> from statsmodels . regression . linear_model import OLS <EOL> res2 = OLS ( endog , exog ) . fit ( ) <EOL> transf = TransformRestriction ( [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ] , res2 . params [ - <NUM_LIT:2> : ] . sum ( ) ) <EOL> exog_st = transf . reduce ( exog ) <EOL> res1 = OLS ( endog , exog_st ) . fit ( ) <EOL> res1 = OLS ( endog - exog . dot ( transf . constant . squeeze ( ) ) , exog_st ) . fit ( ) <EOL> params = transf . expand ( res1 . params ) . squeeze ( ) <EOL> assert_allclose ( params , res2 . params , rtol = <NUM_LIT> ) <EOL> print ( res2 . params ) <EOL> print ( params ) <EOL> print ( res1 . params ) <EOL> res3_ols = OLS ( endog - exog [ : , - <NUM_LIT:1> ] , exog [ : , : - <NUM_LIT:2> ] ) . fit ( ) <EOL> transf3 = TransformRestriction ( [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> exog3_st = transf3 . reduce ( exog ) <EOL> res3 = OLS ( endog , exog3_st ) . fit ( ) <EOL> res3 = OLS ( endog - exog . dot ( transf3 . constant . squeeze ( ) ) , exog3_st ) . fit ( ) <EOL> params = transf3 . expand ( res3 . params ) . squeeze ( ) <EOL> assert_allclose ( params [ : - <NUM_LIT:2> ] , res3_ols . params , rtol = <NUM_LIT> ) <EOL> print ( res3 . params ) <EOL> print ( params ) <EOL> print ( res3_ols . params ) <EOL> print ( res3_ols . bse ) <EOL> cov_params3 = transf3 . transf_mat . dot ( res3 . cov_params ( ) ) . dot ( transf3 . transf_mat . T ) <EOL> bse3 = np . sqrt ( np . diag ( cov_params3 ) ) <EOL> print ( bse3 ) <EOL> tp = transform_params_constraint ( res2 . params , res2 . normalized_cov_params , <EOL> transf3 . R , transf3 . q ) <EOL> tp = transform_params_constraint ( res2 . params , res2 . cov_params ( ) , transf3 . R , transf3 . q ) <EOL> from statsmodels . discrete . discrete_model import Poisson <EOL> import statsmodels . api as sm <EOL> rand_data = sm . datasets . randhie . load ( ) <EOL> rand_exog = rand_data . exog . view ( float ) . reshape ( len ( rand_data . exog ) , - <NUM_LIT:1> ) <EOL> rand_exog = sm . add_constant ( rand_exog , prepend = False ) <EOL> poisson_mod0 = sm . Poisson ( rand_data . endog , rand_exog ) <EOL> poisson_res0 = poisson_mod0 . fit ( method = "<STR_LIT>" ) <EOL> R = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:10> ) ) <EOL> R [ <NUM_LIT:0> , - <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> R [ <NUM_LIT:1> , - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> transfp = TransformRestriction ( R , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> poisson_mod = sm . Poisson ( rand_data . endog , rand_exog [ : , : - <NUM_LIT:2> ] ) <EOL> poisson_res = poisson_mod . fit ( method = "<STR_LIT>" , offset = rand_exog . dot ( transfp . constant . squeeze ( ) ) ) <EOL> exogp_st = transfp . reduce ( rand_exog ) <EOL> poisson_modr = sm . Poisson ( rand_data . endog , exogp_st ) <EOL> poisson_resr = poisson_modr . fit ( method = "<STR_LIT>" ) <EOL> paramsp = transfp . expand ( poisson_resr . params ) . squeeze ( ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( paramsp ) <EOL> print ( poisson_res . params ) <EOL> poisson_mod0 = sm . Poisson ( rand_data . endog , rand_exog ) <EOL> poisson_res0 = poisson_mod0 . fit ( method = "<STR_LIT>" ) <EOL> tp = transform_params_constraint ( poisson_res0 . params , poisson_res0 . cov_params ( ) , transfp . R , transfp . q ) <EOL> cov_params3 = transf3 . transf_mat . dot ( res3 . cov_params ( ) ) . dot ( transf3 . transf_mat . T ) <EOL> bse3 = np . sqrt ( np . diag ( cov_params3 ) ) <EOL> poisson_mod = sm . Poisson ( rand_data . endog , rand_exog [ : , : - <NUM_LIT:2> ] , offset = rand_exog [ : , - <NUM_LIT:1> ] ) <EOL> poisson_res = poisson_mod . fit ( method = "<STR_LIT>" ) <EOL> exogp_st = transfp . reduce ( rand_exog ) <EOL> poisson_modr = sm . Poisson ( rand_data . endog , exogp_st , offset = rand_exog . dot ( transfp . constant . squeeze ( ) ) ) <EOL> poisson_resr = poisson_modr . fit ( method = "<STR_LIT>" ) <EOL> paramsp = transfp . expand ( poisson_resr . params ) . squeeze ( ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( paramsp ) <EOL> print ( poisson_resr . params ) <EOL> tp = transform_params_constraint ( poisson_res0 . params , poisson_res0 . cov_params ( ) , transfp . R , transfp . q ) <EOL> cov_paramsp = transfp . transf_mat . dot ( poisson_resr . cov_params ( ) ) . dot ( transfp . transf_mat . T ) <EOL> bsep = np . sqrt ( np . diag ( cov_paramsp ) ) <EOL> print ( bsep ) <EOL> p , cov , res_r = fit_constrained ( poisson_mod0 , transfp . R , transfp . q ) <EOL> se = np . sqrt ( np . diag ( cov ) ) <EOL> print ( p ) <EOL> print ( se ) </s>
