U
    Ãmœd¦I  ã                   @   sà   d Z ddlZddlm  mZ ddlm  mZ	 ddl
m  mZ ddlmZmZ ddlZddlZddlZG dd„ dejƒZG dd„ deƒZG dd	„ d	eƒZG d
d„ dejƒZG dd„ deƒZG dd„ de	jƒZe ee¡ dS )zA
Conditional logistic, Poisson, and multinomial logit regression
é    N)ÚMultinomialResultsÚMultinomialResultsWrapperc                	       sP   e Zd Zd‡ fdd„	Zdd„ Zd‡ fdd„	Zddd„Zed‡ fdd„	ƒZ‡  Z	S )Ú_ConditionalModelÚnonec                    sÀ  d|krt dƒ‚|d }|j|jkr0d}t |ƒ‚|jd |jkrLd}t |ƒ‚tt| ƒj||fd|i|—Ž | jjd k	r‚d}t |ƒ‚| j}|jd | _	i }t
|ƒD ]&\}}	|	|kr¸g ||	< ||	  |¡ q t |¡t |¡ }}| d	¡}
g | _g | _g | _|
d k	�rt |
¡}
g | _g | _g | _d| _ddg}| ¡ D ]¾\}	}|| j}t |¡dk�r€|d  d7  < |d  t|ƒ7  < �q6|  jt|ƒ7  _| j |¡ |
d k	�r¸| j |
| ¡ | j t|ƒ¡ | j ||d d …f ¡ | j t |¡¡ �q6|d dk�rd
t|ƒ }t |¡ |
d k	�rZg | _t
| jƒD ]$\}}| j t | j| |¡¡ �q4t| jƒ| _ g | _!g | _"t#| j ƒD ]>}	| j! t | j|	 | j|	 ¡¡ | j" t | j|	 ¡¡ �q|d S )NÚgroupsú'groups' is a required argumentz4'endog' and 'groups' should have the same dimensionsr   zBThe leading dimension of 'exog' should equal the length of 'endog'ÚmissingzDConditional models should not have an intercept in the design matrixé   ÚoffsetzIDropped %d groups and %d observations for having no within-group variance)$Ú
ValueErrorÚsizeÚshapeÚsuperr   Ú__init__ÚdataZ	const_idxÚexogÚk_paramsÚ	enumerateÚappendÚnpZasarrayÚgetÚ
_endog_grpÚ	_exog_grpÚ
_groupsizeÚ_offset_grpÚ_offsetÚ_sumyÚnobsÚitemsZflatZstdÚlenÚsumÚtupleÚwarningsÚwarnÚ_endofsÚdotÚ	_n_groupsÚ_xyÚ_n1Úrange)ÚselfÚendogr   r   Úkwargsr   ÚmsgZrow_ixÚiÚgr
   ZdropsZixÚyÚkÚofs©Ú	__class__© ú`/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/discrete/conditional_models.pyr      s†    
 ÿÿÿ




ÿ

 z_ConditionalModel.__init__c                 C   s&   ddl m} ||| jƒ}t |¡}|S )Nr   )Úapprox_fprime)Zstatsmodels.tools.numdiffr7   Úscorer   Z
atleast_2d)r*   Úparamsr7   Zhessr5   r5   r6   Úhessianb   s    
z_ConditionalModel.hessianNÚBFGSéd   TFr5   c
                    s~   t t| ƒj||||||	d�}t| |j| ¡ dƒ}||_| j|_| j|_	dt
| jƒ dt| jƒ dt | j¡ g|_t|ƒ}|S )N©Ústart_paramsÚmethodÚmaxiterÚfull_outputÚdispÚskip_hessianr	   z%dz%.1f)r   r   ÚfitÚConditionalResultsr9   Z
cov_paramsr?   r   r&   Ún_groupsÚminr   Úmaxr   ZmeanÚ_group_statsÚConditionalResultsWrapper)r*   r>   r?   r@   rA   rB   ÚfargsÚcallbackÚretallrC   r,   ÚrsltZcrsltr3   r5   r6   rD   i   s$    
úýz_ConditionalModel.fitÚelastic_netç        c                 K   sN   ddl m} |dkrtdƒ‚dddddœ}| |¡ || f||||d	œ|—ŽS )
aÂ  
        Return a regularized fit to a linear regression model.

        Parameters
        ----------
        method : {'elastic_net'}
            Only the `elastic_net` approach is currently implemented.
        alpha : scalar or array_like
            The penalty weight.  If a scalar, the same penalty weight
            applies to all variables in the model.  If a vector, it
            must have the same length as `params`, and contains a
            penalty weight for each coefficient.
        start_params : array_like
            Starting values for `params`.
        refit : bool
            If True, the model is refit using only the variables that
            have non-zero coefficients in the regularized fit.  The
            refitted model is not regularized.
        **kwargs
            Additional keyword argument that are used when fitting the model.

        Returns
        -------
        Results
            A results instance.
        r   )Úfit_elasticnetrO   z.method for fit_regularized must be elastic_neté2   r	   g»½×Ùß|Û=)r@   ZL1_wtZ	cnvrg_tolZzero_tol)r?   Úalphar>   Úrefit)Zstatsmodels.base.elastic_netrQ   r   Úupdate)r*   r?   rS   r>   rT   r,   rQ   Údefaultsr5   r5   r6   Úfit_regularized‰   s    !ÿ
ýüz!_ConditionalModel.fit_regularizedc           	         s‚   z|d }|d= W n t k
r.   tdƒ‚Y nX t|tƒrB|| }d| dd¡kr\t d¡ tt| ƒj	|f|ž||dœ|—Ž}|S )Nr   r   z0+ú Ú z2Conditional models should not include an intercept)r   r   )
ÚKeyErrorr   Ú
isinstanceÚstrÚreplacer"   r#   r   r   Úfrom_formula)	ÚclsZformular   ZsubsetZ	drop_colsÚargsr,   r   Úmodelr3   r5   r6   r^   º   s(    	



ÿÿ ÿÿz_ConditionalModel.from_formula)r   )	Nr;   r<   TFr5   NFF)rO   rP   NF)NN)
Ú__name__Ú
__module__Ú__qualname__r   r:   rD   rW   Úclassmethodr^   Ú__classcell__r5   r5   r3   r6   r      s*   P         ÷!    ü
1  ür   c                       sV   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zdd
d„Zddd„Zdd„ Z	dd„ Z
‡  ZS )ÚConditionalLogita°  
    Fit a conditional logistic regression model to grouped data.

    Every group is implicitly given an intercept, but the model is fit using
    a conditional likelihood in which the intercepts are not present.  Thus,
    intercept estimates are not given, but the other parameter estimates can
    be interpreted as being adjusted for any group-level confounders.

    Parameters
    ----------
    endog : array_like
        The response variable, must contain only 0 and 1.
    exog : array_like
        The array of covariates.  Do not include an intercept
        in this array.
    groups : array_like
        Codes defining the groups. This is a required keyword parameter.
    r   c                    sX   t t| ƒj||fd|i|—Ž t t | j¡tjd k¡rFd}t|ƒ‚| j	j
d | _d S )Nr   )r   r	   zendog must be coded as 0, 1r	   )r   rg   r   r   ÚanyÚuniquer+   Zr_r   r   r   ÚK)r*   r+   r   r   r,   r-   r3   r5   r6   r   é   s    
 ÿÿÿzConditionalLogit.__init__c                 C   s,   d}t t| jƒƒD ]}||  ||¡7 }q|S ©Nr   )r)   r   r   Úloglike_grp)r*   r9   Úllr/   r5   r5   r6   Úloglikeõ   s    zConditionalLogit.loglikec                 C   s(   d}t | jƒD ]}||  ||¡7 }q|S rk   )r)   r&   Ú	score_grp)r*   r9   r8   r/   r5   r5   r6   r8   ý   s    zConditionalLogit.scoreNc                    sR   |d krd}t  t  | j| |¡| ¡‰ i ‰‡ ‡‡fdd„‰ˆ| j| | j| ƒS )Nr   c                    sx   | |k rdS |dkrdS zˆ| |f W S  t k
r:   Y nX ˆ| d |ƒˆ| d |d ƒˆ | d    }|ˆ| |f< |S )Nr   r	   )rZ   )Útr1   Úv©ÚexbÚfÚmemor5   r6   rt     s    ,z"ConditionalLogit._denom.<locals>.f)r   Úexpr%   r   r   r(   ©r*   Úgrpr9   r2   r5   rr   r6   Ú_denom  s    zConditionalLogit._denomc                    sZ   |d krd}ˆj | ‰ t t ˆ |¡| ¡‰i ‰‡ ‡‡‡‡fdd„‰ˆˆj| ˆj| ƒS )Nr   c           
         sÎ   | |k rdt  ˆj¡fS |dkr$dS zˆ| |f W S  tk
rF   Y nX ˆ| d  }ˆ| d |ƒ\}}ˆ| d |d ƒ\}}|| ˆ | d d d …f  }|||  || ||   }}	||	fˆ| |f< ||	fS )Nr   )r	   r   r	   )r   Úzerosr   rZ   )
rp   r1   ÚhÚaÚbÚcÚeÚdÚurq   ©Úexrs   ru   Úsr*   r5   r6   r„   .  s    z'ConditionalLogit._denom_grad.<locals>.s)r   r   rv   r%   r   r(   rw   r5   r‚   r6   Ú_denom_grad"  s    
zConditionalLogit._denom_gradc                 C   s\   d }t | dƒr| j| }t | j| |¡}|d k	r@|| j| 7 }|t |  |||¡¡8 }|S )Nr
   )Úhasattrr   r   r%   r'   r$   Úlogry   )r*   rx   r9   r2   Zllgr5   r5   r6   rl   F  s    

zConditionalLogit.loglike_grpc                 C   s<   d}t | dƒr| j| }|  |||¡\}}| j| ||  S )Nr   r
   )r†   r   r…   r'   )r*   rx   r9   r2   r€   r{   r5   r5   r6   ro   U  s
    

zConditionalLogit.score_grp)r   )N)N)rb   rc   rd   Ú__doc__r   rn   r8   ry   r…   rl   ro   rf   r5   r5   r3   r6   rg   Õ   s   

$rg   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚConditionalPoissonaU  
    Fit a conditional Poisson regression model to grouped data.

    Every group is implicitly given an intercept, but the model is fit using
    a conditional likelihood in which the intercepts are not present.  Thus,
    intercept estimates are not given, but the other parameter estimates can
    be interpreted as being adjusted for any group-level confounders.

    Parameters
    ----------
    endog : array_like
        The response variable
    exog : array_like
        The covariates
    groups : array_like
        Codes defining the groups. This is a required keyword parameter.
    c           	      C   sš   d }t | dƒr| j}d}tt| jƒƒD ]n}t | j| |¡}|d k	rP||| 7 }t |¡}| j| }|t ||¡7 }| 	¡ }|| j
| t |¡ 8 }q&|S ©Nr
   rP   )r†   r   r)   r   r   r   r%   r   rv   r    r   r‡   )	r*   r9   r2   rm   r.   Úxbrs   r0   r„   r5   r5   r6   rn   r  s    


zConditionalPoisson.loglikec           
      C   s¤   d }t | dƒr| j}d}tt| jƒƒD ]x}| j| }t ||¡}|d k	rT||| 7 }t |¡}| 	¡ }| j| }	|t |	|¡7 }|| j
| t ||¡ | 8 }q&|S rŠ   )r†   r   r)   r   r   r   r   r%   rv   r    r   )
r*   r9   r2   r8   r.   Úxr‹   rs   r„   r0   r5   r5   r6   r8   ‡  s    



 zConditionalPoisson.scoreN)rb   rc   rd   rˆ   rn   r8   r5   r5   r5   r6   r‰   _  s   r‰   c                       s&   e Zd Z‡ fdd„Zddd„Z‡  ZS )rE   c                    s   t t| ƒj||||d� d S )N)Únormalized_cov_paramsÚscale)r   rE   r   )r*   ra   r9   r�   rŽ   r3   r5   r6   r   Ÿ  s    
üzConditionalResults.__init__Nçš™™™™™©?c           	      C   s    dddd| j gfddg}dd| jgfd	| jd
 gfd| jd gfd| jd gfg}|dkr^d}d
dlm} |ƒ }|j| |||||d� |j| |||| jd� |S )a<  
        Summarize the fitted model.

        Parameters
        ----------
        yname : str, optional
            Default is `y`
        xname : list[str], optional
            Names for the exogenous variables, default is "var_xx".
            Must match the number of parameters in the model
        title : str, optional
            Title for the top table. If not None, then this replaces the
            default title
        alpha : float
            Significance level for the confidence intervals

        Returns
        -------
        smry : Summary instance
            This holds the summary tables and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary : class to hold summary
            results
        )zDep. Variable:N)zModel:N)zLog-Likelihood:NzMethod:)zDate:N)zTime:N)zNo. Observations:NzNo. groups:zMin group size:r   zMax group size:r	   zMean group size:é   Nz*Conditional Logit Model Regression Results)ÚSummary)ZgleftZgrightÚynameÚxnameÚtitle)r’   r“   rS   Úuse_t)r?   rF   rI   Zstatsmodels.iolib.summaryr‘   Zadd_table_2colsZadd_table_paramsr•   )	r*   r’   r“   r”   rS   Útop_leftÚ	top_rightr‘   Zsmryr5   r5   r6   Úsummary§  sB    
ú

ûú    ÿzConditionalResults.summary)NNNr�   )rb   rc   rd   r   r˜   rf   r5   r5   r3   r6   rE   ž  s   rE   c                	       s<   e Zd ZdZd‡ fdd„	Zddd„Zdd„ Zdd„ Z‡  ZS )ÚConditionalMNLogita‡  
    Fit a conditional multinomial logit model to grouped data.

    Parameters
    ----------
    endog : array_like
        The dependent variable, must be integer-valued, coded
        0, 1, ..., c-1, where c is the number of response
        categories.
    exog : array_like
        The independent variables.
    groups : array_like
        Codes defining the groups. This is a required keyword parameter.

    Notes
    -----
    Equivalent to femlogit in Stata.

    References
    ----------
    Gary Chamberlain (1980).  Analysis of covariance with qualitative
    data. The Review of Economic Studies.  Vol. 47, No. 1, pp. 225-238.
    r   c                    s  t t| ƒj||fd|i|—Ž | j t¡| _| j ¡ d | _| jd | jj	d  | _
| j| j
 | _dd„ t| jƒD ƒ| _| j| _| jj	d | _| j ¡ dk r¨d}t|ƒ‚t t¡‰ t| jƒD ]\}}ˆ |  |¡ q¼tˆ  ¡ ƒ| _| j ¡  ‡ fdd„| jD ƒ| _d S )	Nr   r	   c                 S   s   i | ]}|t |ƒ“qS r5   )r\   )Ú.0Újr5   r5   r6   Ú
<dictcomp>  s      z/ConditionalMNLogit.__init__.<locals>.<dictcomp>r   z%endog may not contain negative valuesc                    s   g | ]}ˆ | ‘qS r5   r5   )rš   r1   ©Zgrxr5   r6   Ú
<listcomp>  s     z/ConditionalMNLogit.__init__.<locals>.<listcomp>)r   r™   r   r+   ZastypeÚintrH   Úk_catr   r   Zdf_modelr   Zdf_residr)   Z_ynames_mapÚJrj   rG   r   ÚcollectionsÚdefaultdictÚlistr   r   r   ÚkeysZ_group_labelsÚsortÚ_grp_ix)r*   r+   r   r   r,   r-   r1   rq   r3   r�   r6   r      s0    
 ÿÿÿ

zConditionalMNLogit.__init__Nr;   r<   TFr5   c
              	   K   s„   |d kr0| j jd }| jd }tjj|| d�}tjj| ||||||	d�}|j	 
| j jd df¡|_	t| |ƒ}|jtjd� t|ƒS )Nr	   )r   r=   éÿÿÿÿ)Zllnull)r   r   r    r   ÚrandomÚnormalÚbaseÚLikelihoodModelrD   r9   Úreshaper   Zset_null_optionsÚnanr   )r*   r>   r?   r@   rA   rB   rK   rL   rM   rC   r,   Úqr~   rN   r5   r5   r6   rD     s"    
ù	
zConditionalMNLogit.fitc                 C   sÜ   | j jd }| jd }| ||f¡}tjt |df¡|fdd�}t | j |¡}d}| jD ]~}||d d …f }tj	|jd t
d�}	| j| }
d}t |
¡D ]}|t ||	|f  ¡ ¡7 }q˜|||	|
f  ¡ t |¡ 7 }qX|S )Nr	   ©ZaxisrP   r   ©Zdtype)r   r   r    r­   r   Úconcatenaterz   r%   r§   ÚarangerŸ   r+   Ú	itertoolsÚpermutationsrv   r    r‡   )r*   r9   r¯   r~   ÚpmatÚlprrm   ÚiirŒ   Újjr0   ÚdenomÚpr5   r5   r6   rn   ?  s    


 zConditionalMNLogit.loglikec                 C   s„  | j jd }| jd }| ||f¡}tjt |df¡|fdd�}t | j |¡}t ||f¡}| jD �]}||d d …f }tj	|jd t
d�}	| j| }
d}t ||f¡}t |
¡D ]n}t ||	|f  ¡ ¡}||7 }t|ƒD ]B\}}|dkrÜ|d d …|d f  || j || d d …f  7  < qÜq²t|
ƒD ]B\}}|dk�r*|d d …|d f  | j || d d …f 7  < �q*||| 8 }qb| ¡ S )Nr	   r°   r   r±   rP   )r   r   r    r­   r   r²   rz   r%   r§   r³   rŸ   r+   r´   rµ   rv   r    r   Úflatten)r*   r9   r¯   r~   r¶   r·   Zgradr¸   rŒ   r¹   r0   rº   Zdenomgr»   rq   r.   Úrr5   r5   r6   r8   U  s.    

6
2zConditionalMNLogit.score)r   )	Nr;   r<   TFr5   NFF)	rb   rc   rd   rˆ   r   rD   rn   r8   rf   r5   r5   r3   r6   r™   ç  s            ÷
%r™   c                   @   s   e Zd ZdS )rJ   N)rb   rc   rd   r5   r5   r5   r6   rJ   v  s   rJ   )rˆ   Únumpyr   Zstatsmodels.base.modelr«   ra   Z#statsmodels.regression.linear_modelZ
regressionZlinear_modelZlmZstatsmodels.base.wrapperÚwrapperÚwrapZ#statsmodels.discrete.discrete_modelr   r   r¢   r"   r´   r¬   r   rg   r‰   ZLikelihoodModelResultsrE   r™   ZRegressionResultsWrapperrJ   Zpopulate_wrapperr5   r5   r5   r6   Ú<module>   s$    F ?I 