
    !gI                     T   d Z ddlZddlmc mZ ddlmc mZ	 ddl
mc mZ ddlmZmZ ddlZddlZddlZ G d dej&                        Z G d de      Z G d d	e      Z G d
 dej.                        Z G d de      Z G d de	j4                        Z ej8                  ee       y)zA
Conditional logistic, Poisson, and multinomial logit regression
    N)MultinomialResultsMultinomialResultsWrapperc                   n     e Zd Zd fd	Zd Z	 	 	 	 	 	 	 	 	 d fd	Z	 	 	 	 ddZe	 	 d	 fd	       Z xZ	S )
_ConditionalModelc                    d|vrt        d      |d   }|j                  |j                  k7  rd}t        |      |j                  d   |j                  k7  rd}t        |      t        |   ||fd|i| | j
                  j                  d}t        |      | j                  }|j                  d   | _        i }t        |      D ]"  \  }}	|	|vrg ||	<   ||	   j                  |       $ t        j                  |      t        j                  |      }}|j                  d	      }
g | _        g | _        g | _        |
t        j                  |
      }
g | _        g | _        g | _        d| _        ddg}|j+                         D ]  \  }	}||   j,                  }t        j.                  |      dk(  r$|dxx   dz  cc<   |dxx   t1        |      z  cc<   R| xj(                  t1        |      z  c_        | j                  j                  |       |
| j"                  j                  |
|          | j                   j                  t1        |             | j                  j                  ||d d f          | j&                  j                  t        j2                  |             ! |d   dkD  r#d
t5        |      z  }t7        j8                  |       |
`g | _        t        | j"                        D ]A  \  }}| j:                  j                  t        j<                  | j                  |   |             C t1        | j                        | _        g | _         g | _!        tE        | j>                        D ]  }	| j@                  j                  t        j<                  | j                  |	   | j                  |	                | jB                  j                  t        j2                  | j                  |	                 y )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super__init__data	const_idxexogk_params	enumerateappendnpasarrayget
_endog_grp	_exog_grp
_groupsize_offset_grp_offset_sumynobsitemsflatstdlensumtuplewarningswarn_endofsdot	_n_groups_xy_n1range)selfendogr   r
   kwargsr   msgrow_ixigr   dropsixykofs	__class__s                   d/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/discrete/conditional_models.pyr   z_ConditionalModel.__init__   sh   6!>??!;;%**$HCS/!::a=EJJ&VCS/!4	4!(	4,2	4 99*"CS/!yy

1 f% 	 DAqq	1IQ	  jj'D)9tH%ZZ'F!D
	A\\^ 	)EArb	AvvayA~aAaCF"IIQIOO""1%!  ''r
3OO""3q6*NN!!$r1u+.JJbffQi(	) 8a<.16u>CMM# DL#D$4$45 E3##BFF4??1+=s$CDE T__- t~~& 	8AHHOOBFF4??1#5t~~a7HIJHHOOBFF4??1#567	8    c                 b    ddl m}  ||| j                        }t        j                  |      }|S )Nr   )approx_fprime)statsmodels.tools.numdiffr@   scorer   
atleast_2d)r0   paramsr@   hesss       r=   hessianz_ConditionalModel.hessianb   s(    ;VTZZ0}}T"r>   c
                    t         |   ||||||	      }t        | |j                  |j	                         d      }||_        | j                  |_        | j                  |_        dt        | j                        z  dt        | j                        z  dt        j                  | j                        z  g|_        t        |      }|S )Nstart_paramsmethodmaxiterfull_outputdispskip_hessianr   z%dz%.1f)r   fitConditionalResultsrD   
cov_paramsrJ   r!   r,   n_groupsminr   maxr   mean_group_statsConditionalResultsWrapper)r0   rI   rJ   rK   rL   rM   fargscallbackretallrN   r2   rsltcrsltr<   s                r=   rO   z_ConditionalModel.fiti   s     w{%#%  ' #4doo6GKYY
3t''3t''RWWT__--

 )/r>   c                 z    ddl m} |dk7  rt        d      ddddd}|j                  |        || 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_elasticnetelastic_netz.method for fit_regularized must be elastic_net2   r   g|=)rK   L1_wt	cnvrg_tolzero_tol)rJ   alpharI   refit)statsmodels.base.elastic_netr^   r   update)r0   rJ   rd   rI   re   r2   r^   defaultss           r=   fit_regularizedz!_ConditionalModel.fit_regularized   sc    B 	@]"MNN!AE %'d *6$)+7$)* !)	* 	*r>   c                     	 |d   }|d= t        |t              r||   }d|j	                  dd      vrt        j                  d       t        	|    |g|||d|}|S # t         $ r t        d      w xY w)Nr   r	   z0+  z2Conditional models should not include an intercept)r   r   )	KeyErrorr   
isinstancestrreplacer(   r)   r   from_formula)
clsformular   subset	drop_colsargsr2   r   modelr<   s
            r=   rq   z_ConditionalModel.from_formula   s    	@H%Fx  fc"&\FwsB//MMNO$@04@v@8>@   	@>??	@s   A! !A6none	NBFGSd   TF NFF)r_           NF)NN)
__name__
__module____qualname__r   rF   rO   ri   classmethodrq   __classcell__r<   s   @r=   r   r      sb    N8` B  - %)#	.*b  !#	 r>   r   c                   L     e Zd ZdZd	 fd	Zd Zd Zd
dZd
dZd Z	d Z
 x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.
    c                    t        |   ||fd|i| t        j                  t        j                  | j
                        t        j                  d   k7        rd}t        |      | j                  j                  d   | _
        y )Nr
   )r   r   zendog must be coded as 0, 1r   )r   r   r   anyuniquer1   r_r   r   r   K)r0   r1   r   r
   r2   r3   r<   s         r=   r   zConditionalLogit.__init__   sp    4	4!(	4,2	4 66"))DJJ'255;67/CS/!#r>   c                 z    d}t        t        | j                              D ]  }|| j                  ||      z  } |S Nr   )r/   r%   r   loglike_grp)r0   rD   llr6   s       r=   loglikezConditionalLogit.loglike   sB    s4??+, 	.A$""1f--B	. 	r>   c                 h    d}t        | j                        D ]  }|| j                  ||      z  } |S r   )r/   r,   	score_grp)r0   rD   rB   r6   s       r=   rB   zConditionalLogit.score   s;    t~~& 	/AT^^Av..E	/ r>   c                     |d}t        j                  t        j                  | j                  |   |      |z         i fd | j                  |   | j
                  |         S )Nr   c                     | |k  ry|dk(  ry	 | |f   S # t         $ r Y nw xY w | dz
  |       | dz
  |dz
        | dz
     z  z   }|| |f<   |S )Nr   r   )rm   )tr:   vexbfmemos      r=   r   z"ConditionalLogit._denom.<locals>.f  s    1uAvQF|#  !a%aAq1uoAE
::AD!QLHs    	"")r   expr+   r   r   r.   )r0   grprD   r;   r   r   r   s       @@@r=   _denomzConditionalLogit._denom  s`    ;CffRVVDNN3/83>? 	  %txx}55r>   c                      |d} j                   |   t        j                  t        j                  |      |z         i  fd  j                  |    j
                  |         S )Nr   c                 @   | |k  r!dt        j                  j                        fS |dk(  ry	 | |f   S # t        $ r Y nw xY w| dz
     } | dz
  |      \  }} | dz
  |dz
        \  }}||z  
| dz
  d d f   z  }|||z  z   ||z   ||z  z   }	}||	f| |f<   ||	fS )Nr   )r   r   r   )r   zerosr   rm   )r   r:   habcedur   exr   r   sr0   s             r=   r   z'ConditionalLogit._denom_grad.<locals>.s.  s    1u"((4==111AvQF|#  AE
AQUA;DAqQUAE?DAqA1q5!8$Aq1u9a!ea!emqAq6D!QLa4Ks   6 	AA)r   r   r   r+   r   r.   )r0   r   rD   r;   r   r   r   r   s   `   @@@@r=   _denom_gradzConditionalLogit._denom_grad"  sj    ;C^^C ffRVVB'#-. 	 	, %txx}55r>   c                     d }t        | d      r| j                  |   }t        j                  | j                  |   |      }||| j
                  |   z  }|t        j                  | j                  |||            z  }|S )Nr   )hasattrr   r   r+   r-   r*   logr   )r0   r   rD   r;   llgs        r=   r   zConditionalLogit.loglike_grpF  sv    4"""3'CffTXXc]F+?4<<$$Crvvdkk#vs344
r>   c                     d}t        | d      r| j                  |   }| j                  |||      \  }}| j                  |   ||z  z
  S )Nr   r   )r   r   r   r-   )r0   r   rD   r;   r   r   s         r=   r   zConditionalLogit.score_grpU  sP    4"""3'CVS11xx}q1u$$r>   rx   )N)r   r   r   __doc__r   r   rB   r   r   r   r   r   r   s   @r=   r   r      s,    &	$6:"6H%r>   r   c                       e Zd ZdZd Zd Zy)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                    d }t        | d      r| j                  }d}t        t        | j                              D ]  }t        j                  | j                  |   |      }||||   z  }t        j                  |      }| j                  |   }|t        j                  ||      z  }|j                         }|| j                  |   t        j                  |      z  z  } |S Nr   r~   )r   r   r/   r%   r   r   r+   r   r   r&   r    r   )	r0   rD   r;   r   r5   xbr   r9   r   s	            r=   r   zConditionalPoisson.logliker  s    4"""Cs4??+, 		,Aq)62Bc!f&&*C"A"&&B-B	A$**Q-"&&)++B		, 	r>   c                    d }t        | d      r| j                  }d}t        t        | j                              D ]  }| j
                  |   }t        j                  ||      }||||   z  }t        j                  |      }|j                         }| j                  |   }	|t        j                  |	|      z  }|| j                  |   t        j                  ||      z  |z  z  } |S r   )r   r   r/   r%   r   r   r   r+   r   r&   r    )
r0   rD   r;   rB   r5   xr   r   r   r9   s
             r=   rB   zConditionalPoisson.score  s    4"""Cs4??+, 
	8Aq!A6"Bc!f&&*C	A"ARVVAq\!ETZZ]RVVC^3a77E
	8 r>   N)r   r   r   r   r   rB   r}   r>   r=   r   r   _  s    $*r>   r   c                   &     e Zd Z fdZddZ xZS )rP   c                 ,    t         |   ||||       y )N)normalized_cov_paramsscale)r   r   )r0   rw   rD   r   r   r<   s        r=   r   zConditionalResults.__init__  s!    "7	 	 	r>   c                 H   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}d
dlm}  |       }|j                  | |||||       |j                  | |||| j                         |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:   z*Conditional Logit Model Regression Results)Summary)gleftgrightynamexnametitle)r   r   rd   use_t)rJ   rR   rV   statsmodels.iolib.summaryr   add_table_2colsadd_table_paramsr   )	r0   r   r   r   rd   top_left	top_rightr   smrys	            r=   summaryzConditionalResults.summary  s    < %%&
 (T]]O,!2!21!5 67!2!21!5 67$"3"3A"6!78
	 =@E 	6y 	 	 	U%tzz 	 	K r>   )NNNg?)r   r   r   r   r   r   r   s   @r=   rP   rP     s    >r>   rP   c                   J     e Zd ZdZd fd	Z	 	 	 	 	 	 	 	 	 ddZd Zd Z x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.
    c                    t        
|   ||fd|i| | j                  j                  t              | _        | j                  j                         dz   | _        | j                  dz
  | j                  j                  d   z  | _	        | j                  | j                  z
  | _        t        | j                        D ci c]  }|t        |       c}| _        | j                  | _        | j                  j                  d   | _        | j                  j#                         dk  rd}t%        |      t'        j(                  t*              }t-        | j.                        D ]  \  }}	||	   j1                  |        t+        |j3                               | _        | j4                  j7                          | j4                  D cg c]  }||   	 c}| _        y c c}w c c}w )Nr
   r   r   z%endog may not contain negative values)r   r   r1   astypeintrT   k_catr   r   df_modelr!   df_residr/   ro   _ynames_mapJr   rS   r   collectionsdefaultdictlistr   r   r   keys_group_labelssort_grp_ix)r0   r1   r   r
   r2   jr3   grxr:   r   r<   s             r=   r   zConditionalMNLogit.__init__   sv   4	4!(	4,2	4 ZZ&&s+
ZZ^^%)
a499??1+==		DMM1/4TZZ/@A!As1vIA#::>>a9CS/!%%d+dkk* 	DAqFMM!	!#((*-!(,(:(:;1A; B <s   >GGc
           	         |K| j                   j                  d   }| j                  dz
  }t        j                  j                  ||z        }t        j                  j                  | ||||||	      }|j                  j                  | j                   j                  d   df      |_	        t        | |      }|j                  t        j                         t        |      S )Nr   )r   rH   )llnull)r   r   r   r   randomnormalbaseLikelihoodModelrO   rD   reshaper   set_null_optionsnanr   )r0   rI   rJ   rK   rL   rM   rX   rY   rZ   rN   r2   qr   r[   s                 r=   rO   zConditionalMNLogit.fit  s     		"A

QA99++Q+7L ##''%#% ( ' kk))499??1+=r*BC!$-
 	RVV,(..r>   c                    | j                   j                  d   }| j                  dz
  }|j                  ||f      }t	        j
                  t	        j                  |df      |fd      }t	        j                  | j                   |      }d}| j                  D ]  }||d d f   }t	        j                  |j                  d   t              }	| j                  |   }
d}t        j                  |
      D ]-  }|t	        j                  ||	|f   j                               z  }/ |||	|
f   j                         t	        j                   |      z
  z  } |S )Nr   axisr~   r   dtype)r   r   r   r   r   concatenater   r+   r   aranger   r1   	itertoolspermutationsr   r&   r   )r0   rD   r   r   pmatlprr   iir   jjr9   denomps                r=   r   zConditionalMNLogit.loglike?  s!   IIOOAJJN~~q!f%~~rxxA/6Q?ffTYY%,, 	3BBE
A1771:S1B

2AE++A. 22q'
 0112!RG*.."RVVE]22B	3 	r>   c                    | j                   j                  d   }| j                  dz
  }|j                  ||f      }t	        j
                  t	        j                  |df      |fd      }t	        j                  | j                   |      }t	        j                  ||f      }| j                  D ]0  }||d d f   }t	        j                  |j                  d   t              }	| j                  |   }
d}t	        j                  ||f      }t        j                  |
      D ]s  }t	        j                  ||	|f   j                               }||z  }t!        |      D ]6  \  }}|dk7  s|d d |dz
  fxx   || j                   ||   d d f   z  z  cc<   8 u t!        |
      D ]3  \  }}|dk7  s|d d |dz
  fxx   | j                   ||   d d f   z  cc<   5 |||z  z  }3 |j#                         S )Nr   r   r   r   r~   )r   r   r   r   r   r   r   r+   r   r   r   r1   r   r   r   r&   r   flatten)r0   rD   r   r   r   r   gradr   r   r   r9   r   denomgr   r   r5   rs                    r=   rB   zConditionalMNLogit.scoreU  s   IIOOAJJN~~q!f%~~rxxA/6Q?ffTYY%xxA,, 	#BBE
A1771:S1B

2AEXXq!f%F++A. DFF1b!W:>>+,
%aL DDAqAvq!a%x(A		"Q%(0C,CC(DD "! :16AENdii1q&99N: FUN"D#	#& ||~r>   rx   rz   )	r   r   r   r   r   rO   r   rB   r   r   s   @r=   r   r     s:    0<6 #/J,r>   r   c                       e Zd Zy)rW   N)r   r   r   r}   r>   r=   rW   rW   v  s    r>   rW   )r   numpyr   statsmodels.base.modelr   rw   #statsmodels.regression.linear_model
regressionlinear_modellmstatsmodels.base.wrapperwrapperwrap#statsmodels.discrete.discrete_modelr   r   r   r(   r   r   r   r   r   LikelihoodModelResultsrP   r   RegressionResultsWrapperrW   populate_wrapperr}   r>   r=   <module>r     s     % % 0 0 ' '!   B,, BJG%( G%T<* <~G44 GRK* K^	 ; ; 	   /1C Dr>   