
    M/Ph                        g d Z ddl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mZmZmZmZmZmZmZmZmZ ddlmZmZ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	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          Z, G d de,          Z- G d dee-          Z. G d dej/                  Z0 e	j1        e0e-            G d dej/                  Z2 e	j1        e2e.            G d de,          Z3 G d dee3          Z4 G d  d!ej/                  Z5 e	j1        e5e3            G d" d#ej/                  Z6 e	j1        e6e4            G d$ d%e,          Z7 G d& d'ee7          Z8 G d( d)ej/                  Z9 e	j1        e9e7            G d* d+ej/                  Z: e	j1        e:e8           dS ),)ZeroInflatedPoissonZeroInflatedGeneralizedPoissonZeroInflatedNegativeBinomialP    N)DiscreteModel
CountModelPoissonLogitCountResultsL1CountResultsProbit_discrete_results_docs_validate_l1_methodGeneralizedPoissonNegativeBinomialP)	zipoissonzigenpoissonzinegbin)approx_fprimeapprox_hess)cache_readonly)ConvergenceWarning)Appendera  
    exog_infl : array_like or None
        Explanatory variables for the binary inflation model, i.e. for
        mixing probability model. If None, then a constant is used.
    offset : array_like
        Offset is added to the linear prediction with coefficient equal to 1.
    exposure : array_like
        Log(exposure) is added to the linear prediction with coefficient
        equal to 1.
    inflation : {'logit', 'probit'}
        The model for the zero inflation, either Logit (default) or Probit
    c                   B    e Zd Zdej        eej        z   dz  Z	 	 d$ fd	Zd Z	d Z
d	 Z eej        j                  	 	 	 d% fd	            Z eej        j                  	 	 	 	 d& fd	            Zd Zd Zd Zd Zd Zd Z	 	 d'dZd(d Z	 	 d)d!Zd" Zd# Z xZS )*GenericZeroInflatedaH  
    Generic Zero Inflated Model

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : ndarray
        A reference to the endogenous response variable
    exog : ndarray
        A reference to the exogenous design.
    exog_infl : ndarray
        A reference to the zero-inflated exogenous design.
    paramsextra_paramsNlogitnonec                     t                      j        ||f|||d| |@d| _        d| _        t	          j        |j        | j        ft          j                  | _        n || _        |j	        d         | _        d| _        t          |j	                  dk    rd| _        n|j	        d         | _        || _        |dk    rIt          t	          j        | j        j	        d                   | j                  | _        | j        | _        na|dk    rIt%          t	          j        | j        j	        d                   | j                  | _        | j        | _        nt)          d	|z            || _        | j        | _        t          | j                  t          | j                  k    rt)          d
          d | j        j        j        D             }	|	t5          | j                  z   | j        d d <   t	          j        | j        t          j                  | _        | j                            ddg           dg| _        d S )N)offsetexposuremissing   TdtypeFr   r   probitz%inflation == %s, which is not handledz[exog and exog_infl have different number ofobservation. `missing` handling is not supportedc                     g | ]}d |z  S )z
inflate_%s ).0is     `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/discrete/count_model.py
<listcomp>z0GenericZeroInflated.__init__.<locals>.<listcomp>a   s    QQQ1lQ&QQQ    	exog_infl	inflation) super__init__	k_inflate_no_exog_inflnponessizefloat64r/   shapelenk_exoginflr	   zeros
model_infl_hessian_logit_hessian_inflater   _hessian_probit
ValueErrorr0   k_extraexogdataparam_nameslist
exog_namesasarray
_init_keysextend_null_drop_keys)selfendogrD   r/   r!   r0   r"   r#   kwargs
infl_names	__class__s             r,   r2   zGenericZeroInflated.__init__7   sG    	MV;C:A	M 	MEK	M 	M 	M DN!%DWej$.%A+-:7 7 7DNN 'DN&_Q/DN!&Dtz??aDKK*Q-DK	#BHT^-A!-D$E$E$(N4 4DO$($7D!!(""$RXdn.B1.E%F%F$(N4 4DO$($8D!! D() * * * #~ty>>S0000 P Q Q Q RQ0D0PQQQ
'$t*?*??DN"*EEE[9::: +}r.   c                     | j         | j        fS )z;list of exogs, for internal use in post-estimation
        )rD   r/   )rM   s    r,   
_get_exogszGenericZeroInflated._get_exogsh   s     	4>**r.   c                 P    t          j        |                     |                    S )a6  
        Loglikelihood of Generic Zero Inflated model.

        Parameters
        ----------
        params : array_like
            The parameters of the model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.

        Notes
        -----
        .. math:: \ln L=\sum_{y_{i}=0}\ln(w_{i}+(1-w_{i})*P_{main\_model})+
            \sum_{y_{i}>0}(\ln(1-w_{i})+L_{main\_model})
            where P - pdf of main model, L - loglike function of main model.
        )r5   sum
loglikeobsrM   r   s     r,   loglikezGenericZeroInflated.loglikem   s     * vdoof--...r.   c                    |d| j                  }|| j         d         }| j        }| j                            |          }t	          j        |t	          j        t                    j        dt	          j        t                    j        z
            }| j	        
                    |          }t	          j        |dk              d         }t	          j        |          d         }t	          j        |t          j                  }	t	          j        ||         d||         z
  t	          j        ||                   z  z             |	|<   t	          j        d||         z
            ||         z   |	|<   |	S )ar  
        Loglikelihood for observations of Generic Zero Inflated model.

        Parameters
        ----------
        params : array_like
            The parameters of the model.

        Returns
        -------
        loglike : ndarray
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes for definition.

        Notes
        -----
        .. math:: \ln L=\ln(w_{i}+(1-w_{i})*P_{main\_model})+
            \ln(1-w_{i})+L_{main\_model}
            where P - pdf of main model, L - loglike function of main model.

        for observations :math:`i=1,...,n`
        Nr$   r   r%   )r3   rN   r>   predictr5   clipfinfofloateps
model_mainrV   nonzero
zeros_liker8   logexp)
rM   r   params_inflparams_mainywllf_mainzero_idxnonzero_idxllfs
             r,   rV   zGenericZeroInflated.loglikeobs   s.   . _dn_-T^__-JO##K00GArx*A0C,CDD?--k:::a1f%%a(jmmA&mARZ000(8_x'9 : ::!; < <H6!an"4558MMK
r.   bfgs#   r$   	nonrobustc
           
         |Wt          | dd          t          | dd          z   }t          j        |          dk    r|dk    rd }|                                 }|d } t	                      j        d	||||||d|
}|                     | |j                  }|                     |          }|i } |j	        d	|d|	d| |S )
Nr!   r   r"   r$   c                      | S Nr)   )xs    r,   <lambda>z)GenericZeroInflated.fit.<locals>.<lambda>   s    ! r.   )start_paramsmaxiterdispmethodfull_outputcallbackT)cov_typeuse_selfuse_tr)   )
getattrr5   r7   _get_start_paramsr1   fitresult_class_resultsresult_class_wrapper_get_robustcov_results)rM   rt   rw   ru   rx   rv   ry   rz   cov_kwdsr|   rO   r!   mlefitzipfitresultrQ   s                  r,   r   zGenericZeroInflated.fit   s    T8Q//'$
A2N2NNFwv!##!1133L#|H !,&T&#.! !  ! !
 ""499**622H%% 	Nx/35	N 	NDL	N 	N 	Nr.   l1defined_by_methodr   auto{Gz?-C6?Q?c                    t          |           t          j        |          dk    r,|dk    r&| j        | j        z   }|t          j        |          z  }| j        | j        z
  }| j        r+t          j        |          dk    r|d | j        |z
            n|}|t          | dd          t          | dd          z   }t          j        |          dk    r|dk    rd } | j        j	        d||||d||||	|
|d|j
        }t          j        t          j        | j                  |          } t          t          |           j	        d|||||||||	|
|d|}|                     | |          }|                     |          S )Nr$   r   r!   r"   )rt   rw   ru   rx   rv   ry   alpha	trim_modeauto_trim_tolsize_trim_tolqc_tolr)   )r   r5   r7   r;   r3   r6   rC   r}   r_   fit_regularizedr   appendr1   r   result_class_regresult_class_reg_wrapper)rM   rt   rw   ru   rx   rv   ry   r   r   r   r   r   rO   k_paramsextraalpha_pr!   cntfitdiscretefitrQ   s                      r,   r   z#GenericZeroInflated.fit_regularized   s    	F###75>>Q5A::{T^3HBGH---Et~-6:l /"" 04<%/0011). 	T8Q//'$
A2N2NNFwv!##!:4?: F)&''a(-+F	F F ?E	F F GM	 
 9RWT^%<%<lKKL8z4((8 F)&''dXy+F	F F ?E	F F ++D&99,,[999r.   c                 Z   |d| j                  }|| j         d         }| j        }| j                            |          }t	          j        |t	          j        t                    j        dt	          j        t                    j        z
            }| j	        
                    |          }| j	                            |          }|                     |          }t	          j        |dk              d         }	t	          j        |          d         }
| j	                            |          }t	          j        | j        j        d         | j        ft          j                  }t	          j        | j        t          j                  }||	         j        d||	         t	          j        ||	                   z  z
  z  j        ||	ddf<   ||
         ||
ddf<   | j        dk    r| j        |	         j        ||	         z  d||	         z
  z  dt	          j        ||	                   z
  z  t	          j        ||	                   z  j        ||	ddf<   | j        |
         j        ||
         z  j         ||
ddf<   n | j        dk    rt/          || j                  S t	          j        ||f          S )a  
        Generic Zero Inflated model score (gradient) vector of the log-likelihood

        Parameters
        ----------
        params : array_like
            The parameters of the model

        Returns
        -------
        score : ndarray, 1-D
            The score vector of the model, i.e. the first derivative of the
            loglikelihood function, evaluated at `params`
        Nr$   r   r%   r   r'   )r3   rN   r>   rZ   r5   r[   r\   r]   r^   r_   	score_obsrV   r`   r=   rD   r9   r;   r8   ra   r/   Trc   r0   r   hstack)rM   r   rd   re   rf   rg   
score_mainrh   rk   ri   rj   mudldpdldws                 r,   r   zGenericZeroInflated.score_obs   sk    _dn_-T^__-JO##K00GArx*A0C,CDD_..{;;
?--k::oof%%:a1f%%a(jmmA&_$$[11 x+T[9LLL}T^2:>>>&x021X;"&X*?*???ABC 	XaaaZ(5[]>W$$!%!9!;ak!I"#ak/"3"#bfXh-?&@&@"@"B #%&X"7"7"8 :; !!! %)N;$?$A$%kN%345#6DQQQ^x'' 999y$&&&r.   c                 R    |                      |                              d          S )Nr   )r   rU   rW   s     r,   scorezGenericZeroInflated.score  s"    ~~f%%))!,,,r.   c                     d S rq   r)   rW   s     r,   _hessian_mainz!GenericZeroInflated._hessian_main       r.   c                 B   |d | j                  }|| j         d          }| j        }| j                            |          }t	          j        |t	          j        t                    j        dt	          j        t                    j        z
            }| j	        
                    |          }| j	                            |          }|                     |          }t	          j        |dk              d         }	t	          j        |          d         }
t	          j        | j         | j        | j         z   f          }t	          j        |          }t!          | j                   D ]5}t!          |dd          D ]}| j        |	|f         | j        |	|f         z  ||	         d||	         z
  z  dt	          j        ||	                   z
  dd||	         z  z
  z  t	          j        ||	                   z  ||	         ||	         dz  z
  dt	          j        ||	                   z
  dz  z  z
  z  ||	         dz  z  z                                  | j        |
|f         | j        |
|f         z  ||
         z  d||
         z
  z                                  z
  |||f<   !7t!          | j                   D ]q}t!          | j                  D ]Z}||	|f         ||	         z  d||	         z
  z  | j        |	|f         z  ||	         z                                   |||| j         z   f<   [r|S Nr$   r      )r3   rN   r>   rZ   r5   r[   r\   r]   r^   r_   r   rV   r`   r=   r;   rc   ranger/   rU   )rM   r   rd   re   rf   rg   r   rh   rk   ri   rj   hess_arrpmfr+   js                  r,   r?   z"GenericZeroInflated._hessian_logit#  s   _dn_-T^__-JO##K00GArx*A0C,CDD_..{;;
?--k::oof%%:a1f%%a(jmmA&8T^T[4>-IJKKfSkk t~&& 
	B 
	BA1b"%% 	B 	BN8Q;/$.12MMx[A(O4F8H-..:/34q1X;3F9HF3x=))9*-.x[1X;>-I 2333a7-898 9 M1$	%&
 (+suu^KN3dn[RS^6TTkN#&'!K.&8:;>355#AA	B t~&& 	G 	GA4;'' G G4>x{4KhK5 #$q{?54N8Q;/5025h-5@AD3GA..//G
 r.   c                     d S rq   r)   rW   s     r,   rA   z#GenericZeroInflated._hessian_probitJ  r   r.   c                    |                      |          }|                     |          }||t          || j                  S | j        | j        z   }t          j        ||f          }||d| j        ddf<   ||| j        d| j        df<   t          j        | j        | j        z   d          }|j	        |         ||<   |S )a  
        Generic Zero Inflated model Hessian matrix of the loglikelihood

        Parameters
        ----------
        params : array_like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (k_vars, k_vars)
            The Hessian, second derivative of loglikelihood function,
            evaluated at `params`

        Notes
        -----
        Nr$   )k)
r   r@   r   rX   r;   r3   r5   r=   triu_indicesr   )rM   r   hess_arr_mainhess_arr_infldimr   tri_idxs          r,   hessianzGenericZeroInflated.hessianM  s    $ **622--f55 M$9vt|444kDN*8S#J''&3$."#4A01/$+">!DDD$Jw/r.   meanc                 0   d}|	d}| j         }|5|r| j        }na| j        r#t          j        t          |          df          }n6t          j        |          }|j        dk    r| j        dk    r|dddf         }||rt          | dd          }nd}nt          j
        |          }||rt          | dd          }nd}|d| j                 }	|| j        d         }
d| j                            |	|          z
  }t          j        ||
d| j         j        d                            |z   |z   }| j        j         }| j        j        }t          | j        dd          }t          | j        dd          }|| j        _         t          j        |j        d                   | j        _        || j        _        || j        _        | j                            |
          }|| j        _         || j        _        |du r| j        `n|| j        _        |du r| j        `n|| j        _        d|z
  |t          j        |          z  z   }|dk    r|t          j        |          z  S |d	k    rt          j        |          S |d
k    r|S |dk    r|t          j        |          z  d|z
  z  S |dk    r|S |dk    r|S |dk    r.t          j        |          }|                     ||d|z
            S |dk    r|                     ||||||          S t1          d|z            )a9	  
        Predict expected response or other statistic given exogenous variables.

        Parameters
        ----------
        params : array_like
            The parameters of the model.
        exog : ndarray, optional
            Explanatory variables for the main count model.
            If ``exog`` is None, then the data from the model will be used.
        exog_infl : ndarray, optional
            Explanatory variables for the zero-inflation model.
            ``exog_infl`` has to be provided if ``exog`` was provided unless
            ``exog_infl`` in the model is only a constant.
        offset : ndarray, optional
            Offset is added to the linear predictor of the mean function with
            coefficient equal to 1.
            Default is zero if exog is not None, and the model offset if exog
            is None.
        exposure : ndarray, optional
            Log(exposure) is added to the linear predictor with coefficient
            equal to 1. If exposure is specified, then it will be logged by
            the method. The user does not need to log it first.
            Default is one if exog is is not None, and it is the model exposure
            if exog is None.
        which : str (optional)
            Statitistic to predict. Default is 'mean'.

            - 'mean' : the conditional expectation of endog E(y | x). This
              takes inflated zeros into account.
            - 'linear' : the linear predictor of the mean function.
            - 'var' : returns the estimated variance of endog implied by the
              model.
            - 'mean-main' : mean of the main count model
            - 'prob-main' : probability of selecting the main model.
                The probability of zero inflation is ``1 - prob-main``.
            - 'mean-nonzero' : expected value conditional on having observation
              larger than zero, E(y | X, y>0)
            - 'prob-zero' : probability of observing a zero count. P(y=0 | x)
            - 'prob' : probabilities of each count from 0 to max(endog), or
              for y_values if those are provided. This is a multivariate
              return (2-dim when predicting for several observations).

        y_values : array_like
            Values of the random variable endog at which pmf is evaluated.
            Only used if ``which="prob"``
        FNTr$   r"   r   r!   r   	mean-mainlinearzmean-nonzeroz	prob-zero	prob-mainvarprob)y_valueszwhich = %s is not available)rD   r/   r4   r5   r6   r:   rI   ndimr3   r}   rb   r>   rZ   dotr9   r_   rN   r=   r!   r"   rV   rc   _predict_var_predict_probrB   )rM   r   rD   r/   r"   r!   whichr   no_exogrd   re   	prob_mainlin_predtmp_exog	tmp_endog
tmp_offsettmp_exposurerk   	prob_zeror   s                       r,   rZ   zGenericZeroInflated.predictq  sh   b <G9D 8 N		% 8 "TA 7 7I
9--I~""t~':':%aaag.	 "4Q77vh''H>  x33_dn_-T^__-//YGGG	6$,?TY_Q-?,? @AAHLvU
 ?'O)	T_h>>
t
EBB# "A 7 7!'#+ o((55' ) &&%/DO"5  (('3DO$ ]i"&++&==	F??rvh////k!!6(###hOn$$rvh///1y=AAk!!k!!e^^!!B$$VRY???f__%%fdIx&,x & A A A :UBCCCr.   dydxc                     t           zNotImplemented
        NotImplementedError)rM   r   rD   	transforms       r,   _derivative_predictz'GenericZeroInflated._derivative_predict  s
     "!r.   c                     t           r   r   )rM   r   rD   r   	dummy_idx	count_idxs         r,   _derivative_exogz$GenericZeroInflated._derivative_exog  s
     "!r.   c                 $   |d| j                  }|| j         d         }| j                            |          }t          j        |t          j        t                    j        dt          j        t                    j        z
            }| j                            |          }| j        	                    |          }| j        	                    |          }|dddf          |z  }d|dddf         z
  |z  }	t          j
        ||	f          }
|
S )aM  
        Derivative of the expected endog with respect to the parameters.

        Parameters
        ----------
        params : ndarray
            parameter at which score is evaluated

        Returns
        -------
        The value of the derivative of the expected endog with respect
        to the parameter vector.
        Nr$   )r3   r>   rZ   r5   r[   r\   r]   r^   r_   _deriv_mean_dparamscolumn_stack)rM   r   rd   re   rg   r   
score_inflr   	dmat_infl	dmat_maindmats              r,   r   z'GenericZeroInflated._deriv_mean_dparams  s     _dn_-T^__-O##K00GArx*A0C,CDD_$$[11_88EE
_88EE
DkMJ.	111d7^z1		9566r.   c                      t           )a#  derivative of score_obs w.r.t. endog

        Parameters
        ----------
        params : ndarray
            parameter at which score is evaluated

        Returns
        -------
        derivative : ndarray_2d
            The derivative of the score_obs with respect to endog.
        )r   statsmodels.tools.numdiff_approx_fprime_scalarrN   )rM   r   r   fdsendog_originals   ``   @r,   _deriv_score_obs_dendogz+GenericZeroInflated._deriv_score_obs_dendog"  s     "!r.   )NNr   Nr   )	Nrl   rm   r$   r$   Nrn   NN)Nr   r   r$   r$   Nr   r   r   r   r   )NNNNr   N)Nr   )Nr   NN)__name__
__module____qualname__base_model_params_doc_doc_zi_params_missing_param_doc__doc__r2   rS   rX   rV   r   r   r   r   r   r   r   r?   rA   r   rZ   r   r   r   r   __classcell__rQ   s   @r,   r   r   %   s         ,*T-DDF FFG" <@;A/- /- /- /- /- /-b+ + +
/ / /.' ' 'R Xm'((<>,07;     )(8 Xm+3448<IMIM: : : : : 54:B/' /' /'b- - -  % % %N  " " "H CG48FD FD FD FDP" " " "
 =C37" " " "  <             r.   r   c                   v     e Zd Zdej        eej        z   dz  Z	 	 d fd	Zd Z		 ddZ
d	 Zd
 Z	 	 ddZ xZS )r   aH  
    Poisson Zero Inflated Model

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : ndarray
        A reference to the endogenous response variable
    exog : ndarray
        A reference to the exogenous design.
    exog_infl : ndarray
        A reference to the zero-inflated exogenous design.
    r   Nr   r   c           	          t                      j        ||f|||||d| t          | j        | j        ||          | _        t          | _        t          | _	        t          | _        t          | _        t          | _        d S )Nr!   r0   r/   r"   r#   )r!   r"   )r1   r2   r   rN   rD   r_   r   distributionZeroInflatedPoissonResultsr   !ZeroInflatedPoissonResultsWrapperr   L1ZeroInflatedPoissonResultsr   #L1ZeroInflatedPoissonResultsWrapperr   )
rM   rN   rD   r/   r!   r"   r0   r#   rO   rQ   s
            r,   r2   zZeroInflatedPoisson.__init__W  s     	MV<E<E;C:A		M 	M FL		M 	M 	M
 "$*di+35 5 5%6$E! <(K%%%r.   c           	      Z   |d | j                  }|| j         d          }| j        }| j                            |          }t	          j        |t	          j        t                    j        dt	          j        t                    j        z
            }| 	                    |          }t	          j
        |dk              d         }t	          j
        |          d         }| j                            |          }	t	          j        | j        | j        f          }
d||         t	          j        |	|                   dz
  z  z   }t          | j                  D ]}t          |dd          D ]}| j        ||f         | j        ||f         z  |	|         z  ||         dz
  z  d|z  ||         |	|         z  t	          j        |	|                   z  |dz  z  z
  z                                  |	|         | j        ||f         z  | j        ||f         z                                  z
  |
||f<   |
S r   )r3   rN   r>   rZ   r5   r[   r\   r]   r^   r   r`   r_   r=   r;   rc   r   rD   rU   )rM   r   rd   re   rf   rg   r   ri   rj   r   r   coeffr+   r   s                 r,   r   z!ZeroInflatedPoisson._hessian_mainf  s   _dn_-T^__-JO##K00GArx*A0C,CDD

6"":a1f%%a(jmmA&_$$[118T[$+677Qx[BF2h<$8$81$<== t{## 	6 	6A1b"%% 6 6Ihk*TYx{-CCxL!$%hK!O589E	hK"X,.81E1EE1H9  #suu;$)KQRN:S(SIk1n-)./2suu#5A6 r.   c                    |d | j                  }|| j         d          }|At          j        t          j        dt          j        | j                  dz                       }t          |j                  dk     r:d}	t          j        | j        	                    ||                    d d d f         }
n'd}	| j        	                    ||          d d d f         }
t          j
        |
t          j        t                    j        dt          j        t                    j        z
            }
| j        	                    |||          d d d f         }| j                            |||
          }|	r|d         n|S )Nr   r$   r   TF)r!   )r3   r5   
atleast_2darangemaxrN   r:   r9   r>   rZ   r[   r\   r]   r^   r_   r   r   )rM   r   rD   r/   r"   r!   r   rd   re   r   rg   r   r   s                r,   r   z!ZeroInflatedPoisson._predict_prob  sb   _dn_-T^__-}RYq"&2D2DQ2F%G%GHHHy!##I''Y??A AABDJAA I''Y??4HAGArx*A0C,CDD_$$[$ %  11d7$"&&xQ77%1vayy61r.   c                 ,    |}d|z
  |z  d||z  z   z  }|S )  predict values for conditional variance V(endog | exog)

        Parameters
        ----------
        params : array_like
            The model parameters. This is only used to extract extra params
            like dispersion parameter.
        mu : array_like
            Array of mean predictions for main model.
        prob_inlf : array_like
            Array of predicted probabilities of zero-inflation `w`.

        Returns
        -------
        Predicted conditional variance.
        r$   r)   )rM   r   r   	prob_inflrg   var_s         r,   r   z ZeroInflatedPoisson._predict_var  s(    " A|q1r6z*r.   c                 2   t          j                    5  t          j        dt                     | j                            dd          j        }d d d            n# 1 swxY w Y   t          j        t          j	        | j
                  dz  |          }|S )Nignorecategoryr   nmrv   rw   皙?)warningscatch_warningssimplefilterr   r_   r   r   r5   r   r6   r3   rM   rt   s     r,   r~   z%ZeroInflatedPoisson._get_start_params  s    $&& 	K 	K!(5GHHHH?..Ad.CCJL	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K y!8!83!>MM   =AA!$A!c                     |                      |||||d          }|                      |||||d          }|                     |d|z
            }|S )a+  Get frozen instance of distribution based on predicted parameters.

        Parameters
        ----------
        params : array_like
            The parameters of the model.
        exog : ndarray, optional
            Explanatory variables for the main count model.
            If ``exog`` is None, then the data from the model will be used.
        exog_infl : ndarray, optional
            Explanatory variables for the zero-inflation model.
            ``exog_infl`` has to be provided if ``exog`` was provided unless
            ``exog_infl`` in the model is only a constant.
        offset : ndarray, optional
            Offset is added to the linear predictor of the mean function with
            coefficient equal to 1.
            Default is zero if exog is not None, and the model offset if exog
            is None.
        exposure : ndarray, optional
            Log(exposure) is added to the linear predictor  of the mean
            function with coefficient equal to 1. If exposure is specified,
            then it will be logged by the method. The user does not need to
            log it first.
            Default is one if exog is is not None, and it is the model exposure
            if exog is None.

        Returns
        -------
        Instance of frozen scipy distribution subclass.
        r   rD   r/   r"   r!   r   r   r$   )rZ   r   )	rM   r   rD   r/   r"   r!   r   rg   distrs	            r,   get_distributionz$ZeroInflatedPoisson.get_distribution  ss    @ \\&ty#+F+  O OLLdi"*6  N N !!"a!e,,r.   )NNNr   r   rq   NNNN)r   r   r   r   r   r   r   r   r2   r   r   r   r~   r  r   r   s   @r,   r   r   E  s         ,*T-DDF FFG" KO,2L L L L L L  <  $2 2 2 2,  *   =A/3' ' ' ' ' ' ' 'r.   r   c                        e Zd Zdej        edz   ej        z   dz  Z	 	 d fd	Z fd	Z		 dd
Z
d Zd Z eej        j                  	 	 dd            Z xZS )r   a  
    Zero Inflated Generalized Poisson Model

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : ndarray
        A reference to the endogenous response variable
    exog : ndarray
        A reference to the exogenous design.
    exog_infl : ndarray
        A reference to the zero-inflated exogenous design.
    p : scalar
        P denotes parametrizations for ZIGP regression.
    zp : float
        dispersion power parameter for the GeneralizedPoisson model.  p=1 for
        ZIGP-1 and p=2 for ZIGP-2. Default is p=2
    r   Nr   r   r   c	           	      |    t                      j        ||f|||||d|	 t          | j        | j        |||          | _        t          | _        | xj        dz  c_        | xj	        dz  c_	        | j
                            d           t          | _        t          | _        t           | _        t$          | _        d S Nr   )r!   r"   pr$   r   )r1   r2   r   rN   rD   r_   r   r   r;   rC   rH   r   %ZeroInflatedGeneralizedPoissonResultsr   ,ZeroInflatedGeneralizedPoissonResultsWrapperr   'L1ZeroInflatedGeneralizedPoissonResultsr   .L1ZeroInflatedGeneralizedPoissonResultsWrapperr   rM   rN   rD   r/   r!   r"   r0   r  r#   rO   rQ   s             r,   r2   z'ZeroInflatedGeneralizedPoisson.__init__  s     	M9?<E<E;C:A	M 	M
 FL	M 	M 	M -TZH3 3 3(qw'''A$P! G(V%%%r.   c                 l    t                                                      }| j        j        dz   |d<   |S )Nr$   r  r1   _get_init_kwdsr_   parameterizationrM   kwdsrQ   s     r,   r!  z-ZeroInflatedGeneralizedPoisson._get_init_kwds
  s0    ww%%''O4q8S	r.   c                    |d | j                  }|| j         d          }| j        j        dz   }	|At          j        t          j        dt          j        | j                  dz                       }t          |j	                  dk     r:d}
t          j        | j
                            ||                    d d d f         }n'd}
| j
                            ||          d d d f         }t          j        dd          ||dk    <   | j                            ||||          d d d f         }| j                            |||d         |	|          }|
r|d         n|S )	Nr$   r   r   TFg      ?r"   r!   r   )r3   r_   r"  r5   r   r   r   rN   r:   r9   r>   rZ   	nextafterr   r   rM   r   rD   r/   r"   r!   r   rd   re   r  r   rg   r   r   s                 r,   r   z,ZeroInflatedGeneralizedPoisson._predict_prob  sf   _dn_-T^__-O,q0}RYq"&2D2DQ2F%G%GHHHy!##I''Y??A AABDJAA I''Y??4HA\!Q''!r'
_$$[$f % . ../aag7"&&x[_aKK%1vayy61r.   c                 l    |d         }|}| j         j        }d|z
  |z  d|||z  z  z   dz  ||z  z   z  }|S )r  r   r$   r   r_   r"  rM   r   r   r  r   rg   r  r  s           r,   r   z+ZeroInflatedGeneralizedPoisson._predict_var&  sN    " r
O,A|EBEM 1A5B>?r.   c                 0   t          j                    5  t          j        dt                     t	          | j        | j        | j                                      d          j	        }d d d            n# 1 swxY w Y   t          j        |d          }|S )Nr  r  )r/   r   )rv   r  )r  r  r  r   r   rN   rD   r/   r   r   r5   r   r  s     r,   r~   z0ZeroInflatedGeneralizedPoisson._get_start_params=  s    $&& 	= 	=!(5GHHHH.tz49.* * **-#1#++f 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= ys33s   AA66A:=A:c                     | j         j        dz   }|                     |||||d          }|                     |||||d          }|                     ||d         |d|z
            }	|	S )Nr$   r   r  r   r   r_   r"  rZ   r   
rM   r   rD   r/   r"   r!   r  r   rg   r  s
             r,   r  z/ZeroInflatedGeneralizedPoisson.get_distributionE  s     O,q0\\&ty#+F+  O OLLdi"*6  N N !!"fRj!QU;;r.   NNNr   r   r   rq   r  r   r   r   r   r   r   r   r   r2   r!  r   r   r~   r   r   r  r   r   s   @r,   r   r     s         ,* 
!	"# #!#G. KO17W W W W W W&      $2 2 2 2.  .   X!2:;;<@/3
 
 
 <;
 
 
 
 
r.   r   c                        e Zd Zdej        edz   ej        z   dz  Z	 	 d fd	Z fd	Z		 dd
Z
d Zd Z eej        j                  	 	 dd            Z xZS )r   a  
    Zero Inflated Generalized Negative Binomial Model

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : ndarray
        A reference to the endogenous response variable
    exog : ndarray
        A reference to the exogenous design.
    exog_infl : ndarray
        A reference to the zero-inflated exogenous design.
    p : scalar
        P denotes parametrizations for ZINB regression. p=1 for ZINB-1 and
    p=2 for ZINB-2. Default is p=2
    zp : float
        dispersion power parameter for the NegativeBinomialP model.  p=1 for
        ZINB-1 and p=2 for ZINM-2. Default is p=2
    r   Nr   r   r   c	           	      |    t                      j        ||f|||||d|	 t          | j        | j        |||          | _        t          | _        | xj        dz  c_        | xj	        dz  c_	        | j
                            d           t          | _        t          | _        t           | _        t$          | _        d S r  )r1   r2   r   rN   rD   r_   r   r   r;   rC   rH   r   #ZeroInflatedNegativeBinomialResultsr   *ZeroInflatedNegativeBinomialResultsWrapperr   %L1ZeroInflatedNegativeBinomialResultsr   ,L1ZeroInflatedNegativeBinomialResultsWrapperr   r  s             r,   r2   z&ZeroInflatedNegativeBinomialP.__init__l  s     	M9?<E<E;C:A	M 	M
 FL	M 	M 	M ,DJ	H3 3 3$qw'''?$N! E(T%%%r.   c                 f    t                                                      }| j        j        |d<   |S )Nr  r   r#  s     r,   r!  z,ZeroInflatedNegativeBinomialP._get_init_kwds  s+    ww%%''O4S	r.   c                    |d | j                  }|| j         d          }| j        j        }	|/t          j        dt          j        | j                  dz             }t          |j                  dk     r:d}
t          j	        | j
                            ||                    d d d f         }n'd}
| j
                            ||          d d d f         }t          j        |t          j        t                    j        dt          j        t                    j        z
            }| j                            ||||          d d d f         }| j                            |||d         |	|          }|
r|d         n|S )Nr   r$   r   TFr&  r   )r3   r_   r"  r5   r   r   rN   r:   r9   r   r>   rZ   r[   r\   r]   r^   r   r   r(  s                 r,   r   z+ZeroInflatedNegativeBinomialP._predict_prob  sl   _dn_-T^__-O,yBF4:$6$6q$899Hy!##I''Y??A AABDJAA I''Y??4HAGArx*A0C,CDD_$$[$f % . ../aag7"&&x[_aKK%1vayy61r.   c                 l    |d         }|}| j         j        }d|z
  |z  d|||dz
  z  z  z   ||z  z   z  }|S )r  r   r$   r*  r+  s           r,   r   z*ZeroInflatedNegativeBinomialP._predict_var  sN    " r
O,A|q52A;#66R?@r.   c                 ,   t          j                    5  t          j        dt                     | j                            dd          j        }d d d            n# 1 swxY w Y   t          j        t          j	        | j
                  |          }|S )Nr  r  r   r	  r
  )r  r  r  r   r_   r   r   r5   r   r=   r3   r  s     r,   r~   z/ZeroInflatedNegativeBinomialP._get_start_params  s    $&& 	K 	K!(5GHHHH?..Ad.CCJL	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K y$.!9!9<HHr  c                     | j         j        }|                     |||||d          }|                     |||||d          }|                     ||d         |d|z
            }	|	S )Nr   r  r   r   r$   r.  r/  s
             r,   r  z.ZeroInflatedNegativeBinomialP.get_distribution  s     O,\\&ty#+F+  O OLLdi"*6  N N !!"fRj!QU;;r.   r0  rq   r  r1  r   s   @r,   r   r   S  s       " ,* 
!	"# ###G0 KO17U U U U U U&      $2 2 2 2.  .   X!2:;;<@/3   <;    r.   r   c                   $    e Zd Z	 	 	 	 ddZd ZdS )ZeroInflatedResultsNr   FTc                 \    dd l mc m} ||||d}|                    | |||||          }|S )Nr   )r/   r"   r!   r   )rD   r   averageagg_weights	pred_kwds)&statsmodels.base._prediction_inferencer   _prediction_inferenceget_prediction_delta)rM   rD   r/   r"   r!   r   r@  rA  r   r   
row_labelspredrB  ress                 r,   get_predictionz"ZeroInflatedResults.get_prediction  sk    
 	>======== #  	 	 ''4u074?2; ( = = 
r.   c                 $    ddl m}  ||           S )a6  
        Influence and outlier measures

        See notes section for influence measures that do not apply for
        zero inflated models.

        Returns
        -------
        MLEInfluence
            The instance has methods to calculate the main influence and
            outlier measures as attributes.

        See Also
        --------
        statsmodels.stats.outliers_influence.MLEInfluence

        Notes
        -----
        ZeroInflated models have functions that are not differentiable
        with respect to sample endog if endog=0. This means that generalized
        leverage cannot be computed in the usual definition.

        Currently, both the generalized leverage, in `hat_matrix_diag`
        attribute and studetized residuals are not available. In the influence
        plot generalized leverage is replaced by a hat matrix diagonal that
        only takes combined exog into account, computed in the same way as
        for OLS. This is a measure for exog outliers but does not take
        specific features of the model into account.
        r   )MLEInfluence)$statsmodels.stats.outliers_influencerK  )rM   rK  s     r,   get_influencez!ZeroInflatedResults.get_influence  s'    > 	FEEEEE|D!!!r.   )
NNNNr   FNNTN)r   r   r   rI  rM  r)   r.   r,   r>  r>    sC        AE:?2626   ( "  "  "  "  "r.   r>  c                   @    e Zd Zedddz  Zed             Z	 	 d
d	ZdS )r   z)A results class for Zero Inflated Poisson one_line_description
extra_attrc                     |                      d          }d|                                  t          j        |                      d                    z  z
  }d|t          j        |          z  z   S )Nr   r   r$   )rZ   r5   rc   )rM   r   rg   s      r,   _dispersion_factorz-ZeroInflatedPoissonResults._dispersion_factor  s[    \\\))8(D(D!E!EEEAr

N"#r.   overallr   NFc                      t          d          zhGet marginal effects of the fitted model.

        Not yet implemented for Zero Inflated Models
        z&not yet implemented for zero inflationr   rM   atrw   atexogdummycounts         r,   get_margeffz&ZeroInflatedPoissonResults.get_margeff       ""JKKKr.   rV  r   NFFr   r   r   r   r   r   rU  r^  r)   r.   r,   r   r     sl        $G( ( G $ $ ^$
 ?C',L L L L L Lr.   r   c                       e Zd ZdS )r   Nr   r   r   r)   r.   r,   r   r             Dr.   r   c                       e Zd ZdS )r   Nrc  r)   r.   r,   r   r     rd  r.   r   c                       e Zd ZdS )r   Nrc  r)   r.   r,   r   r     rd  r.   r   c                   @    e Zd Zedddz  Zed             Z	 	 d
d	ZdS )r  z5A results class for Zero Inflated Generalized PoissonrO  rP  c                    | j         j        j        }| j        | j         j        d          d         }t          j        |                     d                    }d|                                 |z  z
  }d|||z  z  z   dz  ||z  z   S )Nr   r   rT  r$   r   modelr_   r"  r   r3   r5   rc   rZ   rM   r  r   r   rg   s        r,   rU  z8ZeroInflatedGeneralizedPoissonResults._dispersion_factor*  s    J!2DJ011226VDLLxL0011##URU]"Q&R/0r.   rV  r   NFc                      t          d          rX  r   rY  s         r,   r^  z1ZeroInflatedGeneralizedPoissonResults.get_margeff2  r_  r.   r`  ra  r)   r.   r,   r  r  %  sl        $ W( ( G 1 1 ^1 ?C',L L L L L Lr.   r  c                       e Zd ZdS )r  Nrc  r)   r.   r,   r  r  ;          Dr.   r  c                       e Zd ZdS )r  Nrc  r)   r.   r,   r  r  @  rn  r.   r  c                       e Zd ZdS )r  Nrc  r)   r.   r,   r  r  G  rn  r.   r  c                   @    e Zd Zedddz  Zed             Z	 	 d
d	ZdS )r4  z?A results class for Zero Inflated Generalized Negative BinomialrO  rP  c                    | j         j        j        }| j        | j         j        d          d         }t          j        |                     d                    }d|                                 |z  z
  }d|||dz
  z  z  z   ||z  z   S )Nr   r   rT  r$   ri  rk  s        r,   rU  z6ZeroInflatedNegativeBinomialResults._dispersion_factorS  s    J!2DJ011226VDLLxL0011##EB1I%%B./r.   rV  r   NFc                      t          d          rX  r   rY  s         r,   r^  z/ZeroInflatedNegativeBinomialResults.get_margeff[  r_  r.   r`  ra  r)   r.   r,   r4  r4  N  sl        $ a( ( G 0 0 ^0 ?C$L L L L L Lr.   r4  c                       e Zd ZdS )r6  Nrc  r)   r.   r,   r6  r6  d  rn  r.   r6  c                       e Zd ZdS )r5  Nrc  r)   r.   r,   r5  r5  i  rn  r.   r5  c                       e Zd ZdS )r7  Nrc  r)   r.   r,   r7  r7  p  rn  r.   r7  );__all__r  numpyr5   statsmodels.base.modelr   rj  statsmodels.base.wrapperwrapperwrap#statsmodels.regression.linear_model
regressionlinear_modellm#statsmodels.discrete.discrete_modelr   r   r   r	   r
   r   r   r   r   r   r   statsmodels.distributionsr   r   r   r   r   r   statsmodels.tools.decoratorsr   statsmodels.tools.sm_exceptionsr   statsmodels.compat.pandasr   r   r   r   r   r   r>  r   r   RegressionResultsWrapperr   populate_wrapperr   r  r  r  r  r4  r6  r5  r7  r)   r.   r,   <module>r     s|  , , ,      % % % % % % % % % ' ' ' ' ' ' ' ' ' 0 0 0 0 0 0 0 0 0D D D D D D D D D D D D D D D D D D D D D D D D D D H G G G G G G G G G @ @ @ @ @ @ @ @ 7 7 7 7 7 7 > > > > > > . . . . . .] ] ] ] ]* ] ] ]@W W W W W- W W Wtq q q q q%8 q q qhr r r r r$7 r r rj6" 6" 6" 6" 6", 6" 6" 6"rL L L L L!4 L L L(	 	 	 	 	>3M 	 	 		 	 	 	 	(C 	 	 	  702 2 2	 	 	 	 	"*E 	 	 	  924 4 4L L L L L,? L L L,	 	 	 	 	n-	 	 	
	 	 	 	 	
#	 	 	  B;= = =	 	 	 	 	
#	 	 	  D=? ? ?L L L L L*= L L L,	 	 	 	 	N+	 	 	
	 	 	 	 	
#	 	 	  @9; ; ;	 	 	 	 	
#	 	 	  B;= = = = =r.   