
    !gIY                        d Z ddlmZ ddlZddlmZmZmZ ddl	Z
ddlZddlZddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ej<                  j?                         Z  ejB                  e jD                        e _"         ejB                  e jF                        e _#         ee jF                  d      e _#        dZ$dZ% e
jL                   ee%      d      Z'e'jQ                  e)      Z' ejT                  e'd         e'd<    G d d      Z+ G d de+      Z, G d de+      Z- G d de+      Z. G d de+      Z/ G d de+      Z0 G d  d!e+      Z1 G d" d#e+      Z2 G d$ d%e+      Z3 G d& d'e+      Z4 G d( d)e+      Z5 G d* d+e5      Z6 G d, d-e5      Z7 G d. d/e5      Z8 G d0 d1e5      Z9 G d2 d3e5      Z:d4 Z;y)5zM
Created on Fri May 30 16:22:29 2014

Author: Josef Perktold
License: BSD-3

    )StringION)assert_assert_allcloseassert_equal)datasetsfit_constrained)PoissonLogitfamiliesGLM)add_constant   )results_glm_logit_constrainedresults_poisson_constrainedF)prependzagecat	smokes	deaths	pyears
1	1	32	52407
2	1	104	43248
3	1	206	28612
4	1	186	12663
5	1	102	5317
1	0	2	18790
2	0	12	10673
3	0	28	5710
4	0	28	2585
5	0	31	1462	)	delimiterpyears	logpyearsc                       e Zd Zd Zd Zd Zy)CheckPoissonConstrainedMixinc                    | j                   }| j                  }t        |d   |j                  | j                     d       t        j                  t        j                  |d               }|dk(  t        j                  |j                  | j                           z  }t        ||    |j                  | j                     |    d       y )Nr   ư>rtolr   )
res1res2r   paramsidxnpsqrtdiagisnanbse)selfr   r    bse1masks        h/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/discrete/tests/test_constrained.py
test_basicz'CheckPoissonConstrainedMixin.test_basic8   s    yyyyQTXX!6TBwwrwwtAw'(	RXXdhhtxx&899dUTXXdhh%7%>TJ    c                    t        | d      rt        | j                  d      s| j                  n| j                  j                  }| j                  }t	        |j
                  |j
                  | j                     d       |j                  dk(  t        j                  |j                  | j                           z  }t	        |j                  |    |j                  | j                     |    d       |j                  | j                  df   }t        j                  |j                        t        j                  |      z  }t	        |j                  |    ||    d       |j                  | j                  df   }|j                  dk(  t        j                  |      z  }t	        |j                  |    ||    d       |j                  | j                  d	f   }|j                  | j                  d
f   }t        j                  ||f      }t	        |j                         t        j                  |          |t        j                  |          d       t	        |j                   |j"                  d       t%        |j&                  |j(                         |j*                  |j(                  z
  dz
  }	t%        |j,                  |	       y t/        j0                  d       y )Nres1m_resultsr   r   r         g-C6
?      r   not available yet)hasattrr/   r0   r    r   r!   r"   r'   r#   r&   params_tableisinftvaluespvaluescolumn_stackconf_intllfllr   df_modeldf_mNdf_residpytestskip)
r(   r   r    r*   r9   r:   ci_lowci_uppcidf_rs
             r+   test_basic_methodz.CheckPoissonConstrainedMixin.test_basic_methodA   s-   4!&-djj*&EDJJ,, 99DDKKTXX)>TJ HHMRXXdhhtxx.@%AADDHHdUOTXXdhh-?-F!%' ''!4G 88DLL)BHHW,==DDLL$/$dK''!4G LLA%')::DDLL$/$dK&&txx{3F&&txx{3F&&!12B DMMORXXb\M:B}<M!%' DHHdggD9		266DII%)D-KK+,r-   c                 ,   t        | d      rs| j                  }| j                  }t        |d      r#t        |j                  |j
                  d       y t        r%dd l}d|j                  z  }|j                  |       y y t        j                  d       y )Nr/   ll_0r   r   r   z&test: ll_0 not available, llnull=%6.4Fr5   )r6   r/   r    r   llnullrK   DEBUGwarningswarnrC   rD   )r(   r   r    rN   messages        r+   
test_otherz'CheckPoissonConstrainedMixin.test_otherk   ss    4!::D99DtV$TYYTB#G!% -GMM'*	  KK+,r-   N)__name__
__module____qualname__r,   rI   rQ    r-   r+   r   r   6   s    K(-T-r-   r   c                       e Zd Zed        Zej                  j                  d        Zej                  j                  d        Z	y)TestPoissonConstrained1ac           	         t         j                  | _        g d| _        d}t	        j
                  |t              }t        |j                        }t        j                  |      }t        j                  |j                  j                               |d<   d}t        j                  |j                        j!                  |      }t#        ||j$                  |j&                  |ddd      | _        |j#                  ||dd	      | _        y )
N   r2   r3   r4      r   r   'deaths ~ logpyears + smokes + C(agecat)datar   C(agecat)[T.4] = C(agecat)[T.5]bfgsmethoddispstart_paramsfit_kwdsre   rb   rc   )resultsresults_noexposure_constraintr    r"   r
   from_formular^   len
exog_namesr#   zeroslogendogmeanpatsy
DesignInfolinear_constraintr	   coefs	constantsr   r/   clsformulamodk_varsre   constrlcs          r+   setup_classz$TestPoissonConstrained1a.setup_class   s     88' <""76S^^$xx'&&!12Q
 3cnn-??G"3",,0<7=q,IK ''\/5A ( ?	r-   c                 f    | j                   j                         }t        d|j                  v        y )Nlinear equality constraints)r/   summaryr   	extra_txtr(   summs     r+   test_summaryz%TestPoissonConstrained1a.test_summary   s(     zz!!#-?@r-   c                 l    | j                   j                         }t        d|j                  d   v        y )Nr   r   )r/   summary2r   r   r   s     r+   test_summary2z&TestPoissonConstrained1a.test_summary2   s-     zz""$-1BBCr-   N)
rR   rS   rT   classmethodr}   rC   marksmoker   r   rU   r-   r+   rW   rW   ~   sT    ? ?8 [[A A [[D Dr-   rW   c                       e Zd Zed        Zy)TestPoissonConstrained1bc                    t         j                  | _        g d| _        d}t	        j
                  |t        t        d   j                        }d}t        j                  |j                        j                  |      }t        ||j                  |j                  ddd	      | _        || _        |j                  |dd      | _        y )
Nr[   r1   r2   r3   r4   r   deaths ~ smokes + C(agecat)r   r^   exposurer_   newtonr   ra   rf   )rh   results_exposure_constraintr    r"   r
   rj   r^   valuesrq   rr   rl   rs   r	   rt   ru   r   constraintsr/   rw   rx   ry   r{   r|   s        r+   r}   z$TestPoissonConstrained1b.setup_class   s     66$ 0""7,0N,A,AC2cnn-??G"3",,7?56-89  ''x-. ( 0	r-   NrR   rS   rT   r   r}   rU   r-   r+   r   r      s    0 0r-   r   c                       e Zd Zed        Zy)TestPoissonConstrained1cc                    t         j                  | _        g d| _        d}t	        j
                  |t        t        j                  t        d   j                              }d}t        j                  |j                        j                  |      }t        ||j                  |j                   ddd	      | _        || _        |j                  |dd      | _        y )
Nr   r   r   r^   offsetr_   r   r   ra   r   )rh   r   r    r"   r
   rj   r^   r#   rn   r   rq   rr   rl   rs   r	   rt   ru   r   r   r/   r   s        r+   r}   z$TestPoissonConstrained1c.setup_class   s     66$ 0""7*,&&h1F1F*GI2cnn-??G"3",,7?56-89  ''xa'H	r-   Nr   rU   r-   r+   r   r      s    I Ir-   r   c                       e Zd Zed        Zy)TestPoissonNoConstrainedc                 L   t         j                  | _        g d| _        d}t	        j
                  |t        t        j                  t        d   j                              }|j                  d      j                  }|j                  |j                         f| _        || _        y )Nr   r   r   r   r   )rc   )rh   results_exposure_noconstraintr    r"   r
   rj   r^   r#   rn   r   fitr0   r!   
cov_paramsr   r/   )rw   rx   ry   r   s       r+   r}   z$TestPoissonNoConstrained.setup_class   s}     88$ 0""7*,&&h1F1F*GIwwAw''KK!23	r-   Nr   rU   r-   r+   r   r      s     r-   r   c                       e Zd Zed        Zy)TestPoissonConstrained2ac           	         t         j                  | _        g d| _        d}t	        j
                  |t              }t        |j                        }t        j                  |      }t        j                  |j                  j                               |d<   d}t        j                  |j                        j!                  |      }t#        ||j$                  |j&                  |ddd      | _        |j#                  ||dd	      | _        y )
NrY   r\   r]   r   %C(agecat)[T.5] - C(agecat)[T.4] = 0.5r`   ra   rd   rg   )rh   results_noexposure_constraint2r    r"   r
   rj   r^   rk   rl   r#   rm   rn   ro   rp   rq   rr   rs   r	   rt   ru   r   r/   rv   s          r+   r}   z$TestPoissonConstrained2a.setup_class   s     99' <""76 S^^$xx'&&!12Q
 9cnn-??G"3",,0<7=q,IK ''\/5A ( ?	r-   Nr   rU   r-   r+   r   r      s    ? ?r-   r   c                       e Zd Zed        Zy)TestPoissonConstrained2bc                    t         j                  | _        g d| _        d}t	        j
                  |t        t        d   j                        }d}t        j                  |j                        j                  |      }t        ||j                  |j                  ddd	      | _        || _        |j                  |d
d| j                  d         | _        y )Nr   r   r   r   r   r   r   ra   r   r`   rb   rc   re   )rh   results_exposure_constraint2r    r"   r
   rj   r^   r   rq   rr   rl   rs   r	   rt   ru   r   r   r/   r   s        r+   r}   z$TestPoissonConstrained2b.setup_class  s     77$ 0""7,0N,A,AC8cnn-??G"3",,7?56-89  ''vA58XXa[ ( B	r-   Nr   rU   r-   r+   r   r     s    B Br-   r   c                       e Zd Zed        Zy)TestPoissonConstrained2cc                    t         j                  | _        g d| _        d}t	        j
                  |t        t        j                  t        d   j                              }d}t        j                  |j                        j                  |      }t        ||j                  |j                   ddd	      | _        || _        |j                  |d
d| j"                  d         | _        y )Nr   r   r   r   r   r   r   ra   r   r`   r   )rh   r   r    r"   r
   rj   r^   r#   rn   r   rq   rr   rl   rs   r	   rt   ru   r   r   r/   r   s        r+   r}   z$TestPoissonConstrained2c.setup_class*  s     77$ 0""7*,&&h1F1F*GI 9cnn-??G"3",,7?,KM ''/5A58XXa[ ( B	r-   Nr   rU   r-   r+   r   r   (  s    B Br-   r   c                       e Zd Zed        Zy)TestGLMPoissonConstrained1ac                    ddl m} t        j                  | _        g d| _        d}t        j                  |t        t        j                               }d}t        j                  |j                        j                  |      } |||j                  |j                   ddi	      | _        || _        |j                  |d
      | _        y )Nr   r   rY   r\   )r^   familyr_   atol绽|=r   r   )statsmodels.base._constraintsr	   rh   ri   r    r"   r   rj   r^   r   r
   rq   rr   rl   rs   rt   ru   r   r   r/   )rw   r	   rx   ry   r{   r|   s         r+   r}   z'TestGLMPoissonConstrained1a.setup_classD  s    A88' <wT&.&6&6&8: 3cnn-??G"3",,-3UO=''U';	r-   Nr   rU   r-   r+   r   r   B  s    < <r-   r   c                   "    e Zd Zed        Zd Zy)TestGLMPoissonConstrained1bc           	         ddl m} ddlm} ddlm} t        j                  | _        g d| _	        d} |j                  |t         |j                         t        j                  t        d   j                              }d	}t!        j"                  |j$                        j'                  |      } |||j(                  |j*                  d
di      | _        || _        |j                  |d      j0                  | _        y )Nr   r   r   r   r   r   r   )r^   r   r   r_   r   r   r   r   )r   r	   statsmodels.genmodr   +statsmodels.genmod.generalized_linear_modelr   rh   r   r    r"   rj   r^   r
   r#   rn   r   rq   rr   rl   rs   rt   ru   r   r   r0   r/   )rw   r	   r   r   rx   ry   r{   r|   s           r+   r}   z'TestGLMPoissonConstrained1b.setup_class[  s    A/C66$ 0cwT&6h&6&6&8&(ffT(^-B-B&CE 3cnn-??G"3",,-3UO=''U';DD	r-   c                    | j                   }| j                  }d}t        j                  |t        t        d   j
                        }d}|j                  || j                   j                  ddd      }t        |j                  |j                  d	
       t        |j                  |j                  d
       |j                         }t        ||j                         d
       t        |j                  |d
       t        |j                  |d
       t        |j                  d      |j                  d      d
       y )Nr   r   r   r_   r   Fr   )re   rb   warn_convergencerc   g-q=r   gdy=r   linear)which)r/   r    r
   rj   r^   r   r	   r!   r   r'   predictmufittedvalues)r(   r   r    rx   ry   r{   	predicteds          r+   test_compare_glm_poissonz4TestGLMPoissonConstrained1b.test_compare_glm_poissonr  s    zzyy/""7,0N,A,AC 3""6

8I8I*2U() # + 	T[[u=$((7 LLN		4<<>>7))95A8484"	$r-   N)rR   rS   rT   r   r}   r   rU   r-   r+   r   r   Y  s    E E,$r-   r   c                       e Zd Zd Zy)CheckGLMConstrainedMixinc                    | j                   }| j                  }t        |j                  |j                  d   d       dd l}|j                         5  |j                  dt               t        |j                  |j                  d       d d d        t        |j                  |j                  d       y # 1 sw Y   ,xY w)Nr3   r   r   r   ignore)r    r/   r   aicinfocritrN   catch_warningssimplefilterFutureWarningbicdeviance)r(   r    r   rN   s       r+   test_glmz!CheckGLMConstrainedMixin.test_glm  s    yyzz 	$--"2?$$& 	<!!(M:DHHdhhU;	< 	t}}5A	< 	<s   9B66B?N)rR   rS   rT   r   rU   r-   r+   r   r     s    Br-   r   c                       e Zd Zed        Zy)TestGLMLogitConstrained1c                 L   t        d       | _        t        j                  | _        t        t        j                  t        j                  t        j                               }d}|j                  |      | _        | j                  j                  \  }}t        |||      | _        y )Nr   x1 = 2.8)slicer"   reslogitresults_constraint1r    r   spector_dataro   exogr   Binomialr	   r/   r   r   rw   mod1r{   Rqs        r+   r}   z$TestGLMLogitConstrained1.setup_class  s~    + //<%%|'8'8"++-/ ((0	yy$$1"4A.r-   Nr   rU   r-   r+   r   r     s    / /r-   r   c                   `    e Zd Zed        Zej                  j                  d      d        Zy)TestLogitConstrained1c                 ~   t        d       | _        t        j                  | _        t        t        j                  t        j                        }d}|j                  |d      | _
        | j                  j                  j                  | j                  j                  j                  }}t        |||ddi      | _        y )Nr   r`   )rb   rb   r   )r   r"   r   r   r    r   r   ro   r   r	   r/   r   rt   ru   r   r   s        r+   r}   z!TestLogitConstrained1.setup_class  s    +
 //\''):):;(((?	yy$$**CII,A,A,K,K1"4A68JKr-   	not a GLMreasonc                      y NrU   r(   s    r+   r   zTestLogitConstrained1.test_glm      r-   N	rR   rS   rT   r   r}   rC   r   rD   r   rU   r-   r+   r   r     s<    L L" [[[) *r-   r   c                       e Zd Zed        Zd Zej                  j                  d        Z	ej                  j                  d        Z
d Zy)TestGLMLogitConstrained2c                    t        d       | _        t        j                  | _        t        t        j                  t        j                  t        j                               }d}|j                  |d      | _        | j                  j                  j                  | j                  j                  j                  }}t        |||ddi      | _        ||f| _        y )Nr   x1 - x3 = 0r   r   r   r   )r   r"   r   results_constraint2r    r   r   ro   r   r   r   r	   r/   r   rt   ru   r   constraints_rqr   s        r+   r}   z$TestGLMLogitConstrained2.setup_class  s    +//<%%|'8'8"++-/ ((e(<	 yy$$**CII,A,A,K,K1"4AHVr-   c                     | j                   }| j                  }|j                         }t        ||j                  d       t        |j
                  |d       t        |j                  |d       y )NgHz>r   r   r   )r    r/   r   r   
predict_mur   r   )r(   r    r   r   s       r+   test_predictz%TestGLMLogitConstrained2.test_predict  sQ    yyzzLLN		4??>7))95Ar-   c                     | j                   j                         }t        d|j                  v        t	        | j                   j
                        }|dk(  sJ y )Nr   zx1 - x3 = 0.0)r/   r   r   r   strr   )r(   r   	lc_strings      r+   r   z%TestGLMLogitConstrained2.test_summary  sJ     zz!!#-?@

../	O+++r-   c                     dd l }|j                         5  |j                  dt               | j                  j                         }d d d        t        dj                  d   v        y # 1 sw Y   $xY w)Nr   r   r   )rN   r   r   r   r/   r   r   r   )r(   rN   r   s      r+   r   z&TestGLMLogitConstrained2.test_summary2  sc     	$$& 	)!!(M:::&&(D	)
 	-1BBC	) 	)s   1A))A2c                     | j                   }ddlm}  || j                  j                  | j
                        }t        |j                  |j                  d       t        |j                  |j                  d       y )Nr   )fit_constrained_wrapr   r   )r    r   r   r/   modelr   r   r!   )r(   r    r   res_wraps       r+   test_fit_constrained_wrapz2TestGLMLogitConstrained2.test_fit_constrained_wrap  sR    yyF'

(8(8$:M:MN4@4@r-   N)rR   rS   rT   r   r}   r   rC   r   r   r   r   r   rU   r-   r+   r   r     s^    $ $B [[, , [[	D 	DAr-   r   c                       e Zd Zed        Zy)TestGLMLogitConstrained2HCc           	      ~   t        d       | _        t        j                  | _        t        t        j                  t        j                  t        j                               }d}ddi}d}|j                  |||d      | _        | j                  j                  \  }}t        |||d||d	      | _        ||f| _        y )
Nr   HC0scaling_factorB!?r   r   )cov_typecov_kwdsr   )r   r  r  r   )r   r"   r   results_constraint2_robustr    r   r   ro   r   r   r   r	   r/   r   r   r   rw   r   r  r  r{   r   r   s          r+   r}   z&TestGLMLogitConstrained2HC.setup_class  s    +66<%%|'8'8"++-/ $e,(((2: ) H	 yy$$1"4AEMEM9O P  Vr-   Nr   rU   r-   r+   r   r     s    $ $r-   r   c                   `    e Zd Zed        Zej                  j                  d      d        Zy)TestLogitConstrained2HCc           	         t        d       | _        t        j                  | _        t        t        j                  t        j                        }d}ddi}d}|j                  |||d      | _
        | j                  j                  j                  | j                  j                  j                  }}t        |||d||d      | _        ||f| _        y )	Nr  r  r  r   r   )r  r  tol)r  r  r  r   )r   r"   r   r  r    r   r   ro   r   r	   r/   r   rt   ru   r   r   r  s          r+   r}   z#TestLogitConstrained2HC.setup_class/  s    +66\''):):;
 $e,(((2: ) +	 yy$$**CII,A,A,K,K1"4AEMEM9O P  Vr-   r   r   c                      y r   rU   r   s    r+   r   z TestLogitConstrained2HC.test_glmH  r   r-   Nr   rU   r-   r+   r
  r
  -  s:    $ $0 [[[) *r-   r
  c                  D   d} t        j                  | t        t        d   j                        }|j	                          d}dd l} |j                  |j                        j                  |      }|j                  |j                  }}|j                  ||ddi       d	}t        j                  |t        
      }|j	                           |j                  |j                        j                  d      }	|j                  |	j                  |	j                  ddi       y )Nz*deaths ~ C(agecat) + C(smokes) : C(agecat)r   r   z:C(smokes)[T.1]:C(agecat)[3] = C(smokes)[T.1]:C(agec`at)[4]r   rb   r`   r   r\   r]   r_   r   )r
   rj   r^   r   r   rq   rr   rl   rs   rt   ru   r	   )
formula2ry   r   rq   r|   r   r   	formula1amod1alc_1as
             r+   junkr  M  s     <H


xd(,X(=(=?C GGINK			#..	)	;	;K	HB88R\\qA1&'9: :I  6E	IIKEU--.@@)+E	%++u$,h#7  9r-   )<__doc__ior   numpyr#   numpy.testingr   r   r   pandaspdrq   rC   statsmodelsr   r   r	   #statsmodels.discrete.discrete_modelr
   r   r   r   r   r   statsmodels.tools.toolsr   rh   r   r   r   spectorloadr   asarrayro   r   rM   ssread_csvr^   astypeintrn   r   rW   r   r   r   r   r   r   r   r   r   r   r   r   r   r
  r  rU   r-   r+   <module>r$     s     @ @      9 > ' ; 0
 $$&RZZ 2 23 BJJ|001  !2!2EB  	 r{{8B<40{{3BFF4>*[ E- E-P+D; +D\0; 00I; I0; $?; ?BB; B2B; B4<"> <.4$"> 4$nB; B4/7 /(4 28A7 8Av$!9 $66 @9r-   