U
    Kvfp                     @   s"  d dl mZ d dlZd dlmZmZmZmZ d dl	Z
d dlZd dlmZ ddlmZ ddlm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ZG dd dZejjG dd deZG dd dZG dd deZG dd dZG dd dZG dd dZ dS )    )PLATFORM_LINUX32N)assert_assert_allcloseassert_array_equalassert_equal   )RandHIE)CheckModelMixinc                   @   sl   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zejjdd ZdS )CheckGenericc                 C   s   t | jj| jjddd d S Nh㈵>atolrtolr   res1paramsres2self r   O/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/discrete/tests/test_count_model.pytest_params   s    zCheckGeneric.test_paramsc                 C   s   t | jj| jjddd d S r   )r   r   Zllfr   r   r   r   r   test_llf   s    zCheckGeneric.test_llfc                 C   s   t | j | jjddd d S )NMbP?r   r   )r   r   Zconf_intr   r   r   r   r   test_conf_int   s    zCheckGeneric.test_conf_intc                 C   s   t | jj| jjddd d S Nr   r   )r   r   bser   r   r   r   r   test_bse   s    zCheckGeneric.test_bsec                 C   s   t | jj| jjddd d S )N{Gz?r   r   r   Zaicr   r   r   r   r   test_aic    s    zCheckGeneric.test_aicc                 C   s   t | jj| jjddd d S )N皙?r   r    r   r   r   r   test_bic#   s    zCheckGeneric.test_bicc                 C   s0   t | jjj}| j|}t| jj|j d S N	npidentityr   r   sizet_testr   ZtvaluesZtvaluer   Zunit_matrixr)   r   r   r   test_t&   s    zCheckGeneric.test_tc                 C   sd   | j j}tt| j j}d|dd < |j|d ddd}t|jdd  | j jdd  ddd	 d S )
Nr   r   F  alphadispmaxiter   皙?r   r   modelr&   oneslenr   Zfit_regularizedr   r   r5   r/   Zres_regr   r   r   test_fit_regularized+   s     z!CheckGeneric.test_fit_regularizedc                 C   sJ   | j j }tt| t| j | j D ]\}}t|| | q.d S r$   )	r   r5   Z_get_init_kwdsr   setkeys	init_keys	init_kwdsitems)r   r=   keyvaluer   r   r   test_init_keys5   s    zCheckGeneric.test_init_keysc                 C   s^   | j j | j jjj}| j jjj}t|jt| j jjdf t	t
|d t	t
|d d S )Nr   r   )r   ZllnullZres_nullr5   exog	exog_inflr   shaper7   r   r&   Zptp)r   Z	exog_nullZexog_infl_nullr   r   r   	test_null;   s    zCheckGeneric.test_nullc                 C   s   | j  }dt|kstd S )NzCovariance Type:)r   summarystrAssertionError)r   Zsummr   r   r   test_summaryF   s    
zCheckGeneric.test_summaryN)__name__
__module____qualname__r   r   r   r   r!   r#   r+   r9   rA   rE   pytestmarkZsmokerI   r   r   r   r   r
      s   
r
   c                   @   s   e Zd Zedd ZdS )TestZeroInflatedModel_logitc                 C   s   t jj }t|j| _t|j|_t j|jd d ddf dd}t j|jd d df dd}t j	|j||ddj
dd	dd
| _d| jj_ddddg| _ddi| _tj}|| _d S )Nr      Fprependr   logitrC   	inflationnewtonr-   methodr1   r0   TrC   exposurerU   offset)smdatasetsrandhieloadr&   asarrayendogrB   add_constantZeroInflatedPoissonfitr   _results_attach_nullmodelr<   r=   r   zero_inflated_poisson_logitr   clsdatarB   rC   r   r   r   r   setup_classM   s$     
  

z'TestZeroInflatedModel_logit.setup_classN)rJ   rK   rL   classmethodrj   r   r   r   r   rO   L   s   rO   c                       s8   e Zd Zedd Zejjedd fddZ	  Z
S )TestZeroInflatedModel_probitc                 C   s   t jj }t|j| _t|j|_t j|jd d ddf dd}t j|jd d df dd}t j	|j||ddj
dd	dd
| _d| jj_ddddg| _ddi| _tj}|| _d S )Nr   rP   FrQ   r   probitrT   rV   r-   rW   TrC   rY   rU   rZ   )r[   r\   r]   r^   r&   r_   r`   rB   ra   rb   rc   r   rd   re   r<   r=   r   Zzero_inflated_poisson_probitr   rg   r   r   r   rj   _   s$     
  

z(TestZeroInflatedModel_probit.setup_classzFails on 32-bit Linux)reasonc                    s   t    d S r$   )superr9   r   	__class__r   r   r9   p   s    z1TestZeroInflatedModel_probit.test_fit_regularized)rJ   rK   rL   rk   rj   rM   rN   Zskipifr   r9   __classcell__r   r   rp   r   rl   ^   s   
rl   c                   @   s    e Zd Zedd Zdd ZdS )TestZeroInflatedModel_offsetc                 C   s   t jj }t|j| _t|j|_t j|jd d ddf dd}t j|jd d df dd}t j	|j|||jd d df dj
dd	dd
| _d| jj_ddddg| _ddi| _tj}|| _d S )Nr   rP   FrQ   r      )rC   rZ   rV   r-   rW   TrC   rY   rU   rZ   rS   )r[   r\   r]   r^   r&   r_   r`   rB   ra   rb   rc   r   rd   re   r<   r=   r   Zzero_inflated_poisson_offsetr   rg   r   r   r   rj   u   s$     
 

z(TestZeroInflatedModel_offset.setup_classc                 C   s  | j j}|j}tj|j|j|jt	|d}|j
| j jdddd}t|j| j jddd | j  }| }t||ddd |j}|j}|j}| j j||| d}	|j||t	|d	}
t|
|	ddd |jd d
d }|jd d
d }|d d
d }| j j|||d}|j||t	|d	}t||ddd t||d d
d ddd t||d d
d ddd | j j||d}|j||d}t||ddd d S )N)rC   rY   rV   r-   Fstart_paramsrX   r1   r0   ư>r   )rB   rC   rZ   )rB   rC   rY   
   r2   rB   rC   )r   r5   rZ   r[   rb   r`   rB   rC   r&   exprc   r   r   predicttolist)r   Zmodel1rZ   Zmodel3Zres3fitted1Zfitted3exZex_inflZ	fitted1_0Z	fitted3_0Z	fitted1_2Z	fitted3_2Z	fitted1_3Z	fitted3_3r   r   r   test_exposure   sP     
  


z*TestZeroInflatedModel_offset.test_exposureN)rJ   rK   rL   rk   rj   r   r   r   r   r   rs   t   s   
rs   c                   @   s    e Zd Zedd Zdd ZdS )TestZeroInflatedModelPandasc                 C   s   t jj }|j| _|| _t j|jjd d ddf dd}t j|jjd d df dd}t	
dddd	d
dg}t j|j||dd}|j|dddd| _d| jj_ddddg| _ddi| _tj}|| _d S )Nr   rP   FrQ   r   g̥ w?gNr(Xg.
g?gA~Wpg`·?rS   rT   rV   r-   ru   TrC   rY   rU   rZ   )r[   r\   r]   Zload_pandasr`   ri   ra   rB   ilocr&   r_   rb   rc   r   rd   re   r<   r=   r   rf   r   )rh   ri   rB   rC   rv   r5   r   r   r   r   rj      s0    "  
  

z'TestZeroInflatedModelPandas.setup_classc                 C   s   ddddddg}t | jjj| t | jjj | t | jjj | tj	| j
jjd d ddf d	d
}tj	| j
jjd d df d	d
}ddddddg}tj| j
j||dd}t |j| d S )NZinflate_lncoinsZinflate_constZidpZlpiZfmdeconstr   rP   TrQ   r   rS   rT   )r   r   r5   Z
exog_namesr   indexr|   r   r[   ra   ri   rB   r   rb   r`   )r   Zparam_namesrB   rC   r5   r   r   r   
test_names   s$     $   z&TestZeroInflatedModelPandas.test_namesN)rJ   rK   rL   rk   rj   r   r   r   r   r   r      s   
r   c                   @   s8   e Zd Zedd Zdd Zdd Zdd Zd	d
 ZdS )TestZeroInflatedPoisson_predictc                 C   s   ddg}t jd d}t |df}d|d |d df< ||}tjjj|d|j	d| _
t| j
|}|jdd	d
d| _|d|g| _d S )Nr         ?    r2   r3   r(   bfgs  FrW   )r&   randomseedr6   dotr[   distributions	zipoissonrvsrD   r`   rb   rc   resparams_truerh   expected_paramsnobsrB   mu_truer5   r   r   r   rj      s    
z+TestZeroInflatedPoisson_predict.setup_classc                 C   s6   dd }|| j  }t| j  | j |dd d S )Nc                 S   sp   d||   }|d|  |    }|d| |  d   7 }|d| |    d 8 }t|}d| t| }|S Nr   r2   meanr&   sqrt)mu	prob_inflr   dispersion_factorvarstdconf_intv_95r   r   r   compute_conf_interval_95   s    
zKTestZeroInflatedPoisson_predict.test_mean.<locals>.compute_conf_interval_95r   r   r   r   r   r{   r   r`   r   r   conf_interval_95r   r   r   	test_mean   s    
 z)TestZeroInflatedPoisson_predict.test_meanc                 C   sJ   dd }| j }||j|jdd|jdd}t| | j ddd d S )Nc                 S   sP   d| }| d|  |   }|d| | d   7 }|d| |   d 8 }|S r   r   r   Z	prob_mainr   r   r   r   r   r   compute_mixture_var   s
    zETestZeroInflatedPoisson_predict.test_var.<locals>.compute_mixture_var	prob-mainwhich	mean-mainr3   r   r   Z_dispersion_factorr{   r   r   r`   r   r   r   r   Z
var_fittedr   r   r   test_var   s    

  z(TestZeroInflatedPoisson_predict.test_varc                 C   sV   | j }|jdd}tjjt|jd d d d f | dj	}t
||ddd d S )Nprobr   r   r3   r   r   )r   r{   r[   r   r   pmfr&   arangerD   Tr   r   r   prpr2r   r   r   test_predict_prob  s    " z1TestZeroInflatedPoisson_predict.test_predict_probc                 C   s   | j }d}|jdd}|j|jjd | dd}t||d | dd | }|j|jjd | d}t||d | dd d S )N   r   r   )rB   r   绽|=r   )rB   )r   r{   r5   rB   r   )r   r   nZpr1Zpr0r}   Zfitted0r   r   r   test_predict_options  s    z4TestZeroInflatedPoisson_predict.test_predict_optionsN)	rJ   rK   rL   rk   rj   r   r   r   r   r   r   r   r   r      s   
r   c                   @   s@   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dS )"TestZeroInflatedGeneralizedPoissonc                 C   s   t jj }t|j| _t|j|_t j|jd d ddf dd}t j|jd d df dd}t j	|j||ddj
dddd	| _d
| jj_dddddg| _ddd| _tj}|| _d S )Nr   rP   FrQ   r   rC   prV   r-   rW   TrC   rY   rU   rZ   r   rS   rU   r   )r[   r\   r]   r^   r&   r_   r`   rB   ra   ZeroInflatedGeneralizedPoissonrc   r   rd   re   r<   r=   r   Z!zero_inflated_generalized_poissonr   rg   r   r   r   rj   *  s$     
   
z.TestZeroInflatedGeneralizedPoisson.setup_classc                 C   s   d S r$   r   r   r   r   r   r   :  s    z+TestZeroInflatedGeneralizedPoisson.test_bsec                 C   s   d S r$   r   r   r   r   r   r   =  s    z0TestZeroInflatedGeneralizedPoisson.test_conf_intc                 C   s   d S r$   r   r   r   r   r   r#   @  s    z+TestZeroInflatedGeneralizedPoisson.test_bicc                 C   s0   t | jjj}| j|}t| jj|j d S r$   r%   r*   r   r   r   r+   C  s    z)TestZeroInflatedGeneralizedPoisson.test_tc           	   
   C   s  | j j}| j jd }|j|ddddd}t|j| jjddd	 t|j| jjdd
d	 t|j	d dk |j|ddddd}t|j| jjddd	 t|j| jjdd
d	 t|j	d dk t
jd}d|i}|jf |dddd ddd|}t|j| jjddd	 t|j| jjdd
d	 d S )Nrv   minimize	trust-ncgr-   Frv   rX   Z
min_methodr1   r0   r   g{Gz?r   g333333?	convergedTdogleg~jth?r   r   basinhoppingr"   )rv   rX   ZniterZstepsizeniter_successr0   interval-C6?)r   r5   mle_settingsrc   r   r   r   r   r   mle_retvalsr&   r   ZRandomState)	r   reset_randomstater5   rv   res_ncgres_dogZrandom_stater   res_bhr   r   r   test_minimizeH  sh                  z0TestZeroInflatedGeneralizedPoisson.test_minimizeN)
rJ   rK   rL   rk   rj   r   r   r#   r+   r   r   r   r   r   r   (  s   
r   c                   @   s0   e Zd Zedd Zdd Zdd Zdd Zd	S )
*TestZeroInflatedGeneralizedPoisson_predictc                 C   s   dddg}t jd d}t |df}d|d |d df< ||d d }tjjj||d dd|j	d| _
tj| j
|dd}|jd	d
dd| _||d dd|g| _d S )Nr   r   r   r   r2   r   r   r   r   FrW   )r&   r   r   r6   r   r[   r   Zzigenpoissonr   rD   r`   r   rc   r   r   r   r   r   r   rj   q  s    
  z6TestZeroInflatedGeneralizedPoisson_predict.setup_classc                 C   s6   dd }|| j  }t| j  | j |dd d S )Nc           	      S   s   |d }d|| |   d ||   }|d|  |    }|d| |  d   7 }|d| |    d 8 }t|}d| t| }|S r   r   	r   r/   r   r   r   r   r   r   r   r   r   r   r     s    
zVTestZeroInflatedGeneralizedPoisson_predict.test_mean.<locals>.compute_conf_interval_95r   r   r   r   r   r   r   r     s    
 z4TestZeroInflatedGeneralizedPoisson_predict.test_meanc                 C   sJ   dd }| j }||j|jdd|jdd}t| | j ddd d S )	Nc                 S   sP   d| }| d|  |   }|d| | d   7 }|d| |   d 8 }|S r   r   r   r   r   r   r     s
    zPTestZeroInflatedGeneralizedPoisson_predict.test_var.<locals>.compute_mixture_varr   r   r   r3   r"   r   r   r   r   r   r   r     s    

  z3TestZeroInflatedGeneralizedPoisson_predict.test_varc                 C   sZ   | j }|jdd}tjjt|jd d d d f | dddj	}t
||ddd d S )	Nr   r   r   r   r2   g{Gz?r3   r   )r   r{   r[   r   zinegbinr   r&   r   rD   r   r   r   r   r   r   r     s    "   z<TestZeroInflatedGeneralizedPoisson_predict.test_predict_probN)rJ   rK   rL   rk   rj   r   r   r   r   r   r   r   r   p  s
   
r   c                   @   s@   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dS )!TestZeroInflatedNegativeBinomialPc                 C   s   t jj }t|j| _t|j|_t j|jd d df dd}t j|jd d df dd}t	ddddd	g}t j
|j||d
dj|ddddd| _d| jj_dddddg| _dd
d| _tj}|| _d S )Nr   FrQ   r   gGz?g(\$gɿg=
ףp=?gq=
ףp?r2   r   nmrw   r   )rv   rX   Zxtolr1   r0   TrC   rY   rU   rZ   r   rS   r   )r[   r\   r]   r^   r&   r_   r`   rB   ra   arrayZeroInflatedNegativeBinomialPrc   r   rd   re   r<   r=   r   Zzero_inflated_negative_binomialr   )rh   ri   rB   rC   spr   r   r   r   rj     s*    
    
z-TestZeroInflatedNegativeBinomialP.setup_classc                 C   s   t | jj| jjddd d S r   r   r   r   r   r   r     s     z-TestZeroInflatedNegativeBinomialP.test_paramsc                 C   s   d S r$   r   r   r   r   r   r     s    z/TestZeroInflatedNegativeBinomialP.test_conf_intc                 C   s   d S r$   r   r   r   r   r   r#     s    z*TestZeroInflatedNegativeBinomialP.test_bicc                 C   sd   | j j}tt| j j}d|dd < |j|d ddd}t|jdd  | j jdd  ddd	 d S )
Nr   r,   r   Fr-   r.   r2   r"   r   r4   r8   r   r   r   r9     s     z6TestZeroInflatedNegativeBinomialP.test_fit_regularizedc                 C   s   | j j}| j jd }|j|ddddd}t|j| jjddd	 t|j| jjdd
d	 t|j	d dk |j|ddddd}t|j| jjddd	 t|j| jjddd	 t|j	d dk |j|ddddd}t|j| jjddd	 t|j| jjddd	 d S )Nrv   r   r   r-   Fr   r   gQ?r   gQ?r   Tr   r   gy&1|?r      )rv   rX   r1   r   r0   r   ga2U0*3?)
r   r5   r   rc   r   r   r   r   r   r   )r   r   r5   rv   r   r   r   r   r   r   r     s\                z/TestZeroInflatedNegativeBinomialP.test_minimizeN)
rJ   rK   rL   rk   rj   r   r   r#   r9   r   r   r   r   r   r     s   
r   c                   @   s8   e Zd Zedd Zdd Zdd Zdd Zd	d
 ZdS ))TestZeroInflatedNegativeBinomialP_predictc                 C   s   dddg}t jd d}t |df}d|d |d df< d}t ||d d }tjjj	||d d||j
d	| _tj| j|dd
}|jdddd| _|| _||d d||g| _d S )Nr   r   r   r   r2   r   g333333?r   r   r   r   FrW   )r&   r   r   r6   rz   r   r[   r   r   r   rD   r`   r   rc   r   r   r   )rh   r   r   rB   r   r   r5   r   r   r   rj     s"    

   z5TestZeroInflatedNegativeBinomialP_predict.setup_classc                 C   sH   dd }|| j  }d| j | j d   }t| j  ||dd d S )Nc           	      S   s   d|| |d    ||   }|d|  |    }|d| |  d   7 }|d| |    d 8 }t|}d| t| }|S r   r   r   r   r   r   r     s    
zUTestZeroInflatedNegativeBinomialP_predict.test_mean.<locals>.compute_conf_interval_95r   r   r   )r   r   r   r   r   r{   )r   r   r   Z	mean_truer   r   r   r     s    
  z3TestZeroInflatedNegativeBinomialP_predict.test_meanc                 C   sH   dd }| j }||j|jdd|jdd}t| | j dd d S )Nc                 S   sP   d| }| d|  |   }|d| | d   7 }|d| |   d 8 }|S r   r   r   r   r   r   r   $  s
    zOTestZeroInflatedNegativeBinomialP_predict.test_var.<locals>.compute_mixture_varr   r   r   皙?r   r   r   r   r   r   r   "  s    

 z2TestZeroInflatedNegativeBinomialP_predict.test_varc                 C   s   | j }|jj}|jdd}tjjt	|j
d d d d f | dd| jj}t||ddd |d}|d}t|tt| }t|| d  dd	d
d t|| d  dd	dd d S )Nr   r   r   r   r2   r"   r   r   r   gMb@?r   )r   r5   r`   r{   r[   r   r   r   r&   r   rD   r   r   r   r   bincountastypeintr7   )r   r   r`   r   r   ZprmZpr2mfreqr   r   r   r   3  s    "   

z;TestZeroInflatedNegativeBinomialP_predict.test_predict_probc              	   C   s  | j }| j}| j jj}| j}t|}t|t	t| }|j
dd}|d}t||dd |j
ddgddggtdgdggdd}	|d|d g }
t|	|
dd |j
ddgddggtdgdggd	d}t||
d d df dd
 |j
ddgddggtdgdggdd}|j
dd}|ddg  t||ddg dd
 t|d| dd |j
ddgddggtdgdggd}||d d df dk  ||d d df dk  f}t||dd
 |j
ddgddggtdgdggdd}t|tt|jdd dd
 ||d d df dk|dk@   ||d d df dk|dk@   f}|j
ddgddggtdgdggdd}t||dd
 |j
ddgddggtdgdggdd}t|t|jdd dd
 d S )Nr   r   r   {Gz?r   r   )rB   rC   r   r   	prob-zeror   r   r   r   ry   r3   r   r   mean-nonzeroZlinear)r   r`   r5   rB   r   r7   r&   r   r   r   r{   r   r   r_   rz   Zcumsumr   )r   r   r`   rB   r   r   r   ZprobsZprobsmZprobs_uniqueZprobs_unique2Zprobs0_uniqueZprobs_main_uniqueZ
probs_mainpredZpred1Zpred_main_uniqueZmean_nzZpred_nonzero_uniqueZpred_lin_uniquer   r   r   test_predict_generic_ziA  sj    

8	"" zATestZeroInflatedNegativeBinomialP_predict.test_predict_generic_ziN)	rJ   rK   rL   rk   rj   r   r   r   r   r   r   r   r   r     s   
r   c                   @   s(   e Zd Zedd Zdd Zdd ZdS )*TestZeroInflatedNegativeBinomialP_predict2c                 C   s   t jj }t|j| _t|j|_|j}tddddddddd	d
dddddddddg}t j	| j||dd}|j
|dddd}|| _d S )Ngmg%ZUg9ig5g
Ag}<$;{gK	(wgHgVіgTq"g} %˿g%{?gUgrDP?g>F/Y˯?gsn?gkv錵?gz?gO.?r2   r   r   i  Fru   )r[   r\   r]   r^   r&   r_   r`   rB   r   r   rc   r   )rh   ri   rB   rv   modr   r   r   r   rj     sH                       z6TestZeroInflatedNegativeBinomialP_predict2.setup_classc                 C   s"   t | j  | j dd d S )Nr   r   )r   r   r{   r   r`   r   r   r   r   r     s    z4TestZeroInflatedNegativeBinomialP_predict2.test_meanc                 C   sD   | j  }d| jjdd  | jjdd  }t||dd d S )Nr   r   r   r   r   r   )r`   r   r   r{   r   )r   Zmean1Zmean2r   r   r   test_zero_nonzero_mean  s
    
zATestZeroInflatedNegativeBinomialP_predict2.test_zero_nonzero_meanN)rJ   rK   rL   rk   rj   r   r   r   r   r   r   r     s   
r   c                   @   s   e Zd Zdd ZdS )TestPandasOffsetc                 C   s   t ddddddgi}t dddddgdddddgd}t dddddg}t dddddg}tj|||d  dd	g}|D ]}tj||d
 ||d  qd S )NFg        g      ?)ICr   r2   )r`   rB   rZ   rS   rm   r   )r`   rB   rY   rU   )pdZ	DataFrameZSeriesr[   ZPoissonrc   rb   )r   r`   rB   rY   rZ   Z
inflationsrU   r   r   r   test_pd_offset_exposure  s    z(TestPandasOffset.test_pd_offset_exposureN)rJ   rK   rL   r   r   r   r   r   r     s   r   )!Zstatsmodels.compat.platformr   Znumpyr&   Znumpy.testingr   r   r   r   Zpandasr   rM   Zstatsmodels.apiapir[   Zresults.results_discreter   Ztest_discreter	   r
   rO   rl   rs   r   r   rN   Zslowr   r   r   r   r   r   r   r   r   r   <module>   s*   9C'JG;P 
