U
    mdaY                     @   s  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 Z e!e j"e _"e!e j#e _#ee j#dde _#dZ$dZ%e
j&ee%ddZ'e'(e)Z'e*e'd e'd< G dd dZ+G dd de+Z,G dd de+Z-G dd de+Z.G dd de+Z/G dd de+Z0G d d! d!e+Z1G d"d# d#e+Z2G d$d% d%e+Z3G d&d' d'e+Z4G d(d) d)e+Z5G d*d+ d+e5Z6G d,d- d-e5Z7G d.d/ d/e5Z8G d0d1 d1e5Z9G d2d3 d3e5Z:d4d5 Z;dS )6zM
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pyearsZ	logpyearsc                   @   s$   e Zd Zdd Zdd Zdd ZdS )CheckPoissonConstrainedMixinc                 C   sz   | j }| j}t|d |j| j dd tt|d }|dkt|j	| j @ }t||  |j	| j |  dd d S )Nr   ư>Zrtolr   )
res1res2r   paramsidxnpsqrtZdiagisnanbse)selfr   r   Zbse1mask r$   d/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/discrete/tests/test_constrained.py
test_basic9   s    z'CheckPoissonConstrainedMixin.test_basicc           
      C   s  t | drt | jds| jn| jj}| j}t|j|j| j dd |jdkt	|j| j @ }t|j|  |j| j |  dd |j
| jdf }t|jt	|@ }t|j|  ||  dd |j
| jdf }|jdkt	|@ }t|j|  ||  dd |j
| jd	f }|j
| jd
f }t||f}t| t	|  |t	|  dd t|j|jdd t|j|j |j|j d }	t|j|	 n
td d S )Nres1m_resultsr   r   r         g-C6
?      r   not available yet)hasattrr'   r(   r   r   r   r   r!   r   r    Zparams_tableisinftvaluespvaluesZcolumn_stackZconf_intZllfllr   Zdf_modelZdf_mNZdf_residpytestskip)
r"   r   r   r#   r0   r1   Zci_lowZci_uppciZdf_rr$   r$   r%   test_basic_methodB   s6    "z.CheckPoissonConstrainedMixin.test_basic_methodc                 C   sd   t | drV| j}| j}t |dr4t|j|jdd q`tr`dd l}d|j }|| n
t	
d d S )Nr'   ll_0r   r   r   z&test: ll_0 not available, llnull=%6.4Fr-   )r.   r'   r   r   Zllnullr8   DEBUGwarningswarnr4   r5   )r"   r   r   r:   messager$   r$   r%   
test_otherl   s    

z'CheckPoissonConstrainedMixin.test_otherN)__name__
__module____qualname__r&   r7   r=   r$   r$   r$   r%   r   7   s   	*r   c                   @   s8   e Zd Zedd Zejjdd Zejjdd Z	dS )TestPoissonConstrained1ac                 C   s   t j| _dddddddg| _d}tj|td	}t|j}t	
|}t	|j |d< d
}t|j|}t||j|j|dddd| _|j||ddd| _d S )N   r*   r+   r,      r   r   'deaths ~ logpyears + smokes + C(agecat)dataC(agecat)[T.4] = C(agecat)[T.5]bfgsmethoddispstart_paramsfit_kwdsrM   rJ   rK   )resultsresults_noexposure_constraintr   r   r	   from_formularF   len
exog_namesr   zeroslogendogmeanpatsy
DesignInfolinear_constraintr   coefs	constantsr   r'   clsformulamodZk_varsrM   constrlcr$   r$   r%   setup_class   s"    

 z$TestPoissonConstrained1a.setup_classc                 C   s   | j  }td|jk d S )Nlinear equality constraints)r'   summaryr   	extra_txtr"   summr$   r$   r%   test_summary   s    
z%TestPoissonConstrained1a.test_summaryc                 C   s    | j  }td|jd k d S )Nre   r   )r'   summary2r   rg   rh   r$   r$   r%   test_summary2   s    
z&TestPoissonConstrained1a.test_summary2N)
r>   r?   r@   classmethodrd   r4   marksmokerj   rl   r$   r$   r$   r%   rA      s   

rA   c                   @   s   e Zd Zedd ZdS )TestPoissonConstrained1bc                 C   s   t j| _ddddddg| _d}tj|ttd jd	}d
}t	|j
|}t||j|jdddd| _|| _|j|ddd| _d S )NrC   r)   r*   r+   r,   r   deaths ~ smokes + C(agecat)r   rF   ZexposurerG   newtonrI   rN   )rP   results_exposure_constraintr   r   r	   rR   rF   valuesrY   rZ   rT   r[   r   r\   r]   r   constraintsr'   r_   r`   ra   rb   rc   r$   r$   r%   rd      s"    z$TestPoissonConstrained1b.setup_classNr>   r?   r@   rm   rd   r$   r$   r$   r%   rp      s   rp   c                   @   s   e Zd Zedd ZdS )TestPoissonConstrained1cc                 C   s   t j| _ddddddg| _d}tj|tttd j	d	}d
}t
|j|}t||j|jdddd| _|| _|j|ddd| _d S )NrC   r)   r*   r+   r,   r   rq   r   rF   offsetrG   rs   rI   rt   )rP   ru   r   r   r	   rR   rF   r   rV   rv   rY   rZ   rT   r[   r   r\   r]   r   rw   r'   rx   r$   r$   r%   rd      s    z$TestPoissonConstrained1c.setup_classNry   r$   r$   r$   r%   rz      s   rz   c                   @   s   e Zd Zedd ZdS )TestPoissonNoConstrainedc                 C   sb   t j| _ddddddg| _d}tj|tttd j	d	}|j
dd
j}|j| f| _|| _d S )NrC   r)   r*   r+   r,   r   rq   r   r{   )rK   )rP   Zresults_exposure_noconstraintr   r   r	   rR   rF   r   rV   rv   fitr(   r   Z
cov_paramsr   r'   )r_   r`   ra   r   r$   r$   r%   rd      s    z$TestPoissonNoConstrained.setup_classNry   r$   r$   r$   r%   r}      s   r}   c                   @   s   e Zd Zedd ZdS )TestPoissonConstrained2ac                 C   s   t j| _dddddddg| _d}tj|td	}t|j}t	
|}t	|j |d< d
}t|j|}t||j|j|dddd| _|j||ddd| _d S )NrB   r*   r+   r,   rC   r   r   rD   rE   %C(agecat)[T.5] - C(agecat)[T.4] = 0.5rH   rI   rL   rO   )rP   Zresults_noexposure_constraint2r   r   r	   rR   rF   rS   rT   r   rU   rV   rW   rX   rY   rZ   r[   r   r\   r]   r   r'   r^   r$   r$   r%   rd      s"    

 z$TestPoissonConstrained2a.setup_classNry   r$   r$   r$   r%   r      s   r   c                   @   s   e Zd Zedd ZdS )TestPoissonConstrained2bc                 C   s   t j| _ddddddg| _d}tj|ttd jd	}d
}t	|j
|}t||j|jdddd| _|| _|j|dd| jd d| _d S )NrC   r)   r*   r+   r,   r   rq   r   rr   r   rs   rI   rt   rH   rJ   rK   rM   )rP   results_exposure_constraint2r   r   r	   rR   rF   rv   rY   rZ   rT   r[   r   r\   r]   r   rw   r'   rx   r$   r$   r%   rd     s"    
z$TestPoissonConstrained2b.setup_classNry   r$   r$   r$   r%   r     s   r   c                   @   s   e Zd Zedd ZdS )TestPoissonConstrained2cc                 C   s   t j| _ddddddg| _d}tj|tttd j	d	}d
}t
|j|}t||j|jdddd| _|| _|j|dd| jd d| _d S )NrC   r)   r*   r+   r,   r   rq   r   r{   r   rs   rI   rt   rH   r   )rP   r   r   r   r	   rR   rF   r   rV   rv   rY   rZ   rT   r[   r   r\   r]   r   rw   r'   rx   r$   r$   r%   rd   +  s"     z$TestPoissonConstrained2c.setup_classNry   r$   r$   r$   r%   r   )  s   r   c                   @   s   e Zd Zedd ZdS )TestGLMPoissonConstrained1ac                 C   s   ddl m} tj| _dddddddg| _d	}tj|tt	
 d
}d}t|j|}|||j|jddid| _|| _|j|dd| _d S )Nr   r   rB   r*   r+   r,   rC   r   rD   )rF   familyrG   atol绽|=rt   r   )statsmodels.base._constraintsr   rP   rQ   r   r   r   rR   rF   r   r	   rY   rZ   rT   r[   r\   r]   r   rw   r'   )r_   r   r`   ra   rb   rc   r$   r$   r%   rd   E  s    z'TestGLMPoissonConstrained1a.setup_classNry   r$   r$   r$   r%   r   C  s   r   c                   @   s    e Zd Zedd Zdd ZdS )TestGLMPoissonConstrained1bc                 C   s   ddl m} ddlm} ddlm} tj| _ddddd	dg| _	d
}|j
|t| ttd jd}d}t|j|}|||j|jddid| _|| _|j|ddj| _d S )Nr   r   r   r   rC   r)   r*   r+   r,   rq   r   )rF   r   r|   rG   r   r   rt   r   )r   r   statsmodels.genmodr   +statsmodels.genmod.generalized_linear_modelr   rP   ru   r   r   rR   rF   r	   r   rV   rv   rY   rZ   rT   r[   r\   r]   r   rw   r(   r'   )r_   r   r   r   r`   ra   rb   rc   r$   r$   r%   rd   \  s"    z'TestGLMPoissonConstrained1b.setup_classc                 C   s   | j }| j}d}tj|ttd jd}d}|j|| j jdddd}t|j|jd	d
 t|j	|j	dd
 |
 }t||
 dd
 t|j|dd
 t|j|dd
 t|j
dd|j
dddd
 d S )Nrq   r   rr   rG   rs   Fr   )rM   rJ   Zwarn_convergencerK   g-q=r   gdy=r   Zlinear)which)r'   r   r	   rR   rF   rv   r   r   r   r!   predictmufittedvalues)r"   r   r   r`   ra   rb   	predictedr$   r$   r%   test_compare_glm_poissons  s,     
z4TestGLMPoissonConstrained1b.test_compare_glm_poissonN)r>   r?   r@   rm   rd   r   r$   r$   r$   r%   r   Z  s   
r   c                   @   s   e Zd Zdd ZdS )CheckGLMConstrainedMixinc              	   C   sr   | j }| j}t|j|jd dd dd l}| $ |dt t|j	|j	dd W 5 Q R X t|j
|j
dd d S )Nr+   r   r   r   ignore)r   r'   r   ZaicZinfocritr:   catch_warningssimplefilterFutureWarningZbicZdeviance)r"   r   r   r:   r$   r$   r%   test_glm  s    
z!CheckGLMConstrainedMixin.test_glmN)r>   r?   r@   r   r$   r$   r$   r%   r     s   r   c                   @   s   e Zd Zedd ZdS )TestGLMLogitConstrained1c                 C   sV   t d | _tj| _ttjtjt	
 d}d}||| _| jj\}}t|||| _d S )Nr   x1 = 2.8)slicer   reslogitresults_constraint1r   r   spector_datarW   exogr   Binomialr   r'   rw   r   r_   mod1rb   Rqr$   r$   r%   rd     s    

z$TestGLMLogitConstrained1.setup_classNry   r$   r$   r$   r%   r     s   r   c                   @   s.   e Zd Zedd Zejjdddd ZdS )TestLogitConstrained1c                 C   sd   t d | _tj| _ttjtj}d}|j	|dd| _
| j
jj| j
jj }}t	|||ddid| _d S )Nr   rH   )rJ   rJ   rt   )r   r   r   r   r   r
   r   rW   r   r   r'   rw   r\   r]   r   r   r$   r$   r%   rd     s    
z!TestLogitConstrained1.setup_class	not a GLMreasonc                 C   s   d S Nr$   r"   r$   r$   r%   r     s    zTestLogitConstrained1.test_glmN	r>   r?   r@   rm   rd   r4   rn   r5   r   r$   r$   r$   r%   r     s   
r   c                   @   sH   e Zd Zedd Zdd Zejjdd Z	ejjdd Z
d	d
 ZdS )TestGLMLogitConstrained2c                 C   sv   t d | _tj| _ttjtjt	
 d}d}|j|dd| _| jjj| jjj }}t|||ddid| _||f| _d S )Nr   x1 - x3 = 0r   r   r   rt   )r   r   r   Zresults_constraint2r   r   r   rW   r   r   r   r   r'   rw   r\   r]   r   constraints_rqr   r$   r$   r%   rd     s    

z$TestGLMLogitConstrained2.setup_classc                 C   sH   | j }| j}| }t||jdd t|j|dd t|j|dd d S )NgHz>r   r   r   )r   r'   r   r   Z
predict_mur   r   )r"   r   r   r   r$   r$   r%   test_predict  s    z%TestGLMLogitConstrained2.test_predictc                 C   s4   | j  }td|jk t| j j}|dks0td S )Nre   zx1 - x3 = 0.0)r'   rf   r   rg   strrw   AssertionError)r"   ri   Z	lc_stringr$   r$   r%   rj     s    
z%TestGLMLogitConstrained2.test_summaryc              	   C   sH   dd l }|  |dt | j }W 5 Q R X td|jd k d S )Nr   r   re   )r:   r   r   r   r'   rk   r   rg   )r"   r:   ri   r$   r$   r%   rl     s
    
z&TestGLMLogitConstrained2.test_summary2c                 C   sJ   | j }ddlm} || jj| j}t|j|jdd t|j|jdd d S )Nr   )fit_constrained_wrapr   r   )r   r   r   r'   modelr   r   r   )r"   r   r   Zres_wrapr$   r$   r%   test_fit_constrained_wrap	  s
    z2TestGLMLogitConstrained2.test_fit_constrained_wrapN)r>   r?   r@   rm   rd   r   r4   rn   ro   rj   rl   r   r$   r$   r$   r%   r     s   



r   c                   @   s   e Zd Zedd ZdS )TestGLMLogitConstrained2HCc                 C   s   t d | _tj| _ttjtjt	
 d}d}ddi}d}|j|||dd| _| jj\}}t|||d||dd	| _||f| _d S )
Nr   HC0scaling_factorB!?r   r   )cov_typecov_kwdsr   )r   r   r   rt   )r   r   r   results_constraint2_robustr   r   r   rW   r   r   r   r   r'   rw   r   r   r_   r   r   r   rb   r   r   r$   r$   r%   rd     s$    

 
z&TestGLMLogitConstrained2HC.setup_classNry   r$   r$   r$   r%   r     s   r   c                   @   s.   e Zd Zedd Zejjdddd ZdS )TestLogitConstrained2HCc                 C   s   t d | _tj| _ttjtj}d}ddi}d}|j	|||dd| _
| j
jj| j
jj }}t	|||d||dd| _||f| _d S )	Nr   r   r   r   r   )r   r   tol)r   r   r   rt   )r   r   r   r   r   r
   r   rW   r   r   r'   rw   r\   r]   r   r   r   r$   r$   r%   rd   0  s     
 
z#TestLogitConstrained2HC.setup_classr   r   c                 C   s   d S r   r$   r   r$   r$   r%   r   I  s    z TestLogitConstrained2HC.test_glmNr   r$   r$   r$   r%   r   .  s   
r   c            
      C   s   d} t j| ttd jd}|  d}dd l}||j|}|j	|j
 }}|j||ddid d	}t j|td
}|  ||jd}	|j|	j	|	j
ddid d S )Nz*deaths ~ C(agecat) + C(smokes) : C(agecat)r   rr   z:C(smokes)[T.1]:C(agecat)[3] = C(smokes)[T.1]:C(agec`at)[4]r   rJ   rH   rt   rD   rE   rG   rs   )r	   rR   rF   rv   r~   rY   rZ   rT   r[   r\   r]   r   )
Zformula2ra   rw   rY   rc   r   r   Z	formula1aZmod1aZlc_1ar$   r$   r%   junkN  s&    r   )<__doc__ior   numpyr   Znumpy.testingr   r   r   ZpandaspdrY   r4   Zstatsmodelsr   r   r   Z#statsmodels.discrete.discrete_modelr	   r
   r   r   r   r   Zstatsmodels.tools.toolsr   rP   r   r   r   Zspectorloadr   ZasarrayrW   r   r9   ssZread_csvrF   ZastypeintrV   r   rA   rp   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   sN   

H.!7; 