
    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 ddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddl m!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&          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j0                  Z1 e	j2        e1e,            G d' d(ej0                  Z3 e	j2        e3e/            G d) d*e          Z4 G d+ d,ee4          Z5 G d- d.ej0                  Z6 e	j2        e6e4            G d/ d0ej0                  Z7 e	j2        e7e5           dS )1)TruncatedLFPoissonTruncatedLFNegativeBinomialPHurdleCountModel    N)truncatedpoissontruncatednegbin)DiscreteModel
CountModelCountResultsL1CountResultsPoissonNegativeBinomialPGeneralizedPoisson_discrete_results_docs)approx_hess)cache_readonly)ConvergenceWarning)deepcopyc                        e Zd Zdej        dej        z   dz  Z	 	 d fd	Zd Zd	 Z	d
 Z
d Z	 	 	 d fd	Zej        j        e_        	 	 	 	 d fd	Zej        j        e_        d Z	 	 ddZ xZS )TruncatedLFGenerica  
    Generic Truncated model for count data

    .. versionadded:: 0.14.0

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    truncation : int, optional
        Truncation parameter specify truncation point out of the support
        of the distribution. pmf(k) = 0 for k <= truncation
    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.

    paramsextra_paramsr   Nnonec                 T    t                      j        ||f|||d| | j        |k    }| j        |         | _        | j        |         | _        || j        |         | _        || j        |         | _        || _        || _        | j        	                    dg           g | _
        d S )Noffsetexposuremissing
truncation)super__init__endogexogr   r   truncr    
_init_keysextend_null_drop_keys)
selfr#   r$   r    r   r   r   kwargsmask	__class__s
            d/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/discrete/truncated_model.pyr"   zTruncatedLFGeneric.__init__9   s    	 	 	 	 	 	 zJ&IdO	Z%
+d+DK M$/DM
$~...!    c                 P    t          j        |                     |                    S )a`  
        Loglikelihood of Generic Truncated 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
        -----

        npsum
loglikeobsr)   r   s     r-   loglikezTruncatedLFGeneric.loglikeQ        & vdoof--...r.   c                    | j                             |          }| j        dz   }|                     |dt	          j        |                                        d          }t	          j        |t          j                   }|dk    }t          j	        ||<   |dk     }t	          j
        d||         z
            ||<   ||z
  }|S )a  
        Loglikelihood for observations of Generic Truncated model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

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

        Notes
        -----

           	prob-basewhichy_values)
model_mainr3   r%   predictr1   aranger2   	full_likeinfnanlog)r)   r   llf_mainytpmflog_1_m_pmflocllfs           r-   r3   zTruncatedLFGeneric.loglikeobsf   s    & ?--f55Z!^ ll+	"  ? ??Bs2ww 	 l300Ag6CAg6!c#h,//C$
r.   c                 z   | j                             |          }t          j        | j        t          j                  }t          j        |t          j                  }t          | j        dz             D ]}| j                             t          j	        | j                  |z  | j
        t          | dd          t          | dd                    }t          j        |                    |                    }||                    |          j        |z  j        z  }||z  }||j        d|z
  z  j        z   }|S )  
        Generic Truncated 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`
        )dtyper8   r   Nr   )r   r   )r>   	score_obsr1   
zeros_liker#   float64ranger%   r,   	ones_liker$   getattrexpr3   T)	r)   r   
score_mainrG   score_truncimodelpmf_idparamss	            r-   rN   zTruncatedLFGeneric.score_obs   s    _..v66
mDJbj999mJbjAAAtzA~&& 		 		AO--TZ((1,	tXt44 z488	 .  E F5++F3344EEOOF335=@@K5LCCS 9<<r.   c                 R    |                      |                              d          S )rL   r   rN   r2   r4   s     r-   scorezTruncatedLFGeneric.score   $     ~~f%%))!,,,r.   bfgs#   r8   	nonrobustc
           
         |t          | dd          t          | dd          z   }t          j        |          dk    r|dk    rd }| j                            | j        | j        |          }t          j                    5  t          j	        dt                     |                    d          j        }d d d            n# 1 swxY w Y   | j        dz   | j        z   }| j        j        d         |z
  | _         t#                      j        d|||||d	 d
|
}|                     | |j                  }|                     |          }|i } |j        d|d|	d| |S )Nr   r   r   r8   r   ignorecategorydispc                     | S N xs    r-   <lambda>z(TruncatedLFGeneric.fit.<locals>.<lambda>       q r.   start_paramsmethodmaxiterri   full_outputcallbackTcov_typeuse_selfuse_trl   )rS   r1   sizer>   r,   r#   r$   warningscatch_warningssimplefilterr   fitr   df_modelk_extrashapedf_residr!   result_class_resultsresult_class_wrapper_get_robustcov_results)r)   rr   rs   rt   ru   ri   rv   rx   cov_kwdsrz   r*   r   rY   k_paramsmlefitzipfitresultr,   s                    r-   r   zTruncatedLFGeneric.fit   s    T8Q//'$
A2N2NNFwv!##!O--dj$)5; . = =E(** 8 8%h9KLLLL$yyay0078 8 8 8 8 8 8 8 8 8 8 8 8 8 8
 =1$t|3
(+h6 %# [    ""499**622H%% 	Nx/35	N 	NDL	N 	N 	N   7CCCl1defined_by_methodauto{Gz?-C6?Q?c                    t          j        |          dk    r/|dk    r)| j        j        d         }|t          j        |          z  }|}|t          | dd          t          | dd          z   }t          j        |          dk    r|dk    rd }| j                            | j        | j        |          } |j	        d	||||d||||	|
|d|j
        } t          t          |           j	        d	|||||||||	|
|d|}|dv r|                     | |          }nt          d|z            |                     |          S 
Nr8   r   r   r   rd   )rr   rs   rt   ru   ri   rv   alpha	trim_modeauto_trim_tolsize_trim_tolqc_tol)r   l1_cvxopt_cpz+argument method == %s, which is not handledrl   r1   r{   r$   r   onesrS   r>   r,   r#   fit_regularizedr   r!   r	   result_class_reg	TypeErrorresult_class_reg_wrapperr)   rr   rs   rt   ru   ri   rv   r   r   r   r   r   r*   r   alpha_pr   rY   cntfitdiscretefitr,   s                      r-   r   z"TruncatedLFGeneric.fit_regularized       75>>Q5A::yq)HBGH---ET8Q//'$
A2N2NNFwv!##!O--dj$)5; . = =E050 F)&''a(++FF F
 ?EF F
 GM  9z4((8 F)&''dXy+F	F F ?E	F F +++//f==KKAFJL L L ,,[999r.   c                 ,    t          || j                  S )a  
        Generic Truncated 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
        -----
        r   r5   r4   s     r-   hessianzTruncatedLFGeneric.hessian      $ 64<000r.   meanc                    |                      |||          \  }}}t          j        ||d|j        d                            }||z   |z   }|dk    rt          j        |          }	| j        dk    r | j                            |	|          }
|	|
z  S | j        dk    r|	S | j        dk    rt          j        t          j	        d| j        dz                       }| j        
                    ||t          j        |          |d|          }|                    d          }t          j	        | j        dz             |z                      d          }|	|z
  d|z
  z  }|S t          d	          |d
k    r|S |dk    rt          j        |          S |dk    r|t          j        |          }nAt          j        t          j	        dt          j        | j                  dz                       }t          j        |          dddf         }	| j        dk    r"| j                            ||	| j                  }nP| j        dk    r6| j        j        }| j                            ||	|d         || j                  }nt          d          |S |dk    rz|t          j        |          }n/t          j	        dt          j        | j                  dz             }| j        
                    ||t          j        |          |d|          }|S |dk    r2t          j        |          }	t          j        t          j	        d| j        dz                       }| j        
                    ||t          j        |          |d|          }|                    d          }t          j	        | j        dz             |z                      d          }|	|z
  d|z
  z  }t          j	        | j        dz             dz  |z                      d          }| j                            |	|          }|	dz  |z   |z
  d|z
  z  }||dz  z
  }|S t          d|z            )a	  
        Predict response variable 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.
        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)
            - 'mean-main' : mean parameter of truncated count model.
              Note, this is not the mean of the truncated distribution.
            - 'linear' : the linear predictor of the truncated count model.
            - 'var' : returns the estimated variance of endog implied by the
              model.
            - 'prob-trunc' : probability of truncation. This is the probability
              of observing a zero count implied
              by the truncation model.
            - '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).
              The probabilities in the truncated region are zero.
            - 'prob-base' : probabilities for untruncated base distribution.
              The probabilities are for 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"``

        Returns
        -------
        predicted values

        Notes
        -----
        If exposure is specified, then it will be logged by the method.
        The user does not need to log it first.
        r$   r   r   Nr8   r   r   r=   prob)r$   r   r   r;   r<   zunsupported self.truncationlinear	mean-mainzk_extra is not 0 or 1r9   var   z argument which == %s not handled)_get_predict_arraysr1   dotr   rT   r    r>   _prob_nonzero
atleast_2dr@   r?   r2   
ValueErrormaxr#   r   
model_distrG   r%   parameterizationasarray_var)r)   r   r$   r   r   r;   r<   fittedlinpredmu	prob_maincountsprobsprob_tregionmean_tregionr   pmnc2_tregionvmmnc2vs                        r-   r?   zTruncatedLFGeneric.predict*  sV   t "&!9!9 ": " "fh f^djm^4558#f,F??B!## O99"fEE	I~%B&&	1$$ryDOa4G'H'HII//x0@0@!& 0 B B  %yy|| "	$/A*= > > FKKANN\)a,.>? !>???hNk!!6'??"f__#x00ryBF4:4F4Fq4H'I'IJJD)B|q  ++FB
CC""O4++FBr
,-tz; ; !!8999Lk!!#H--1bfTZ&8&8&:;;O++TBF8,<,<Vf , > >E Le^^B]29Q!0C#D#DEEFO++TBF8,<,<Vf , > >E !99Q<<LIdo&9::UBGGJJL%!l*:;DIdo&9::A=!"#&3q66 %%b&11BEBJ-!l2BCDtQwAH2U:< < <r.   )r   NNr   	Nr`   ra   r8   r8   Nrb   NNNr   r   r8   r8   Nr   r   r   r   r   NNNr   N)__name__
__module____qualname__base_model_params_doc_missing_param_doc__doc__r"   r5   r3   rN   r^   r   r   r   r   r?   __classcell__r,   s   @r-   r   r      s^       " + 
!"# ###G6 :>(." " " " " "0/ / /*, , ,\! ! !F- - -" =?,07;# # # # # #J  #+CK -1IMIM	#: #: #: #: #: #:J ,;CO1 1 1( @D'+G< G< G< G< G< G< G< G<r.   r   c                   T     e Zd Zdej        dej        z   dz  Z	 	 d	 fd	Zd Z xZ	S )
r   a  
    Truncated Poisson model for count data

    .. versionadded:: 0.14.0

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    truncation : int, optional
        Truncation parameter specify truncation point out of the support
        of the distribution. pmf(k) = 0 for k <= truncation
    r   r   Nr   r   c           
      @    t                      j        ||f||||d| t          | j        | j        t          | dd           t          | dd                     | _        t          | _        t          | _
        t          | _        t          | _        t          | _        d S )Nr   r   r    r   r   r   )r   r   )r!   r"   r   r#   r$   rS   r>   r   r   TruncatedLFPoissonResultsr    TruncatedLFGenericResultsWrapperr   L1TruncatedLFGenericResultsr   "L1TruncatedLFGenericResultsWrapperr   )	r)   r#   r$   r   r   r    r   r*   r,   s	           r-   r"   zTruncatedLFPoisson.__init__  s    	 !	 	 	 	 	 "$*di+24T+J+J)0x)F)F$ $ $ +5$D! ;(J%%%r.   c                 `    dt          j        |           z
  }||z  }|d|z
  |dz  z  z
  }||fS )  Predict mean and variance of zero-truncated distribution.

        experimental api, will likely be replaced by other methods

        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.

        Returns
        -------
        Predicted conditional variance.
        r8   r   )r1   rT   )r)   r   r   wmvar_s         r-   _predict_mom_trunc0z&TruncatedLFPoisson._predict_mom_trunc0  s?    " _FAEQT>!$wr.   )NNr   r   
r   r   r   r   r   r   r   r"   r   r   r   s   @r-   r   r     s        " + 
!"# ###G6 ;?'-K K K K K K,      r.   r   c                   T     e Zd Zdej        dej        z   dz  Z	 	 d
 fd	Zd	 Z xZ	S )r   a  
    Truncated Generalized Negative Binomial model for count data

    .. versionadded:: 0.14.0

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    truncation : int, optional
        Truncation parameter specify truncation point out of the support
        of the distribution. pmf(k) = 0 for k <= truncation
    r   r   Nr   r   r   c           
          t                      j        ||f||||d| t          | j        | j        t          | dd           t          | dd           |          | _        | j        j        | _        | j        	                    | j        j        | j         d                     t          | _        t          | _        t          | _        t           | _        t$          | _        d S Nr   r   r   )r   r   r   )r!   r"   r   r#   r$   rS   r>   r   
exog_namesr'   r   r    TruncatedNegativeBinomialResultsr   r   r   r   r   r   r   
r)   r#   r$   r   r   r    r   r   r*   r,   s
            r-   r"   z%TruncatedLFNegativeBinomialP.__init__  s    	 !	 	 	 	 	 ,JIT:t444400   .t94<-..IJJJ)<$D! ;(J%%%r.   c                     |d         }| j         j        }d|||dz
  z  z  z   d|z  z  }d|z
  }||z  }|d|||dz
  z  z  z   z  }|dz  |z   |z  }	|	|dz  z
  }
||
fS )r   r=   r8   r   )r>   r   )r)   r   r   r   r   	prob_zeror   r   r   r   r   s              r-   r   z0TruncatedLFNegativeBinomialP._predict_mom_trunc04  s    & r
O,ac**cEk:		MF1urAaCy(()A
aad{$wr.   NNr   r   r   r   r   s   @r-   r   r     s        " + 
!"# ###G6 ;?,2K K K K K K6      r.   r   c                   N     e Zd Zdej        dej        z   dz  Z	 	 d	 fd	Z xZS )
TruncatedLFGeneralizedPoissona  
    Truncated Generalized Poisson model for count data

    .. versionadded:: 0.14.0

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    truncation : int, optional
        Truncation parameter specify truncation point out of the support
        of the distribution. pmf(k) = 0 for k <= truncation
    r   r   Nr   r   r   c           
          t                      j        ||f||||d| t          | j        | j        t          | dd           t          | dd           |          | _        | j        j        | _        | j        	                    | j        j        | j         d                     d | _
        t          | _        t          | _        t          | _        t"          | _        d S r   )r!   r"   r   r#   r$   rS   r>   r   r   r'   r   r   r   r   r   r   r   r   r   r   s
            r-   r"   z&TruncatedLFGeneralizedPoisson.__init__o  s    	 !	 	 	 	 	 -JIT:t444400   .t94<-..IJJJ<$D! ;(J%%%r.   r   	r   r   r   r   r   r   r   r"   r   r   s   @r-   r   r   S  s        " + 
!"# ###G6 ;?,2K K K K K K K K K Kr.   r   c                        e Zd Zdej        dej        z   dz  Z	 	 d fd	Zd Zd Z	d	 Z
d
 Z	 	 	 d fd	Zej        j        e_        	 	 	 	 d fd	Zej        j        e_        d Z xZS )_RCensoredGenerica  
    Generic right Censored model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    r   r   Nr   c                     t          j        |dk              d         | _        t          j        |          d         | _         t	                      j        ||f|||d| d S )Nr   r   )r1   nonzerozero_idxnonzero_idxr!   r"   r)   r#   r$   r   r   r   r*   r,   s          r-   r"   z_RCensoredGeneric.__init__  s~    
5A:..q1:e,,Q/	 	 	 	 	 	 	 	r.   c                 P    t          j        |                     |                    S )a_  
        Loglikelihood of Generic Censored 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
        -----

        r0   r4   s     r-   r5   z_RCensoredGeneric.loglike  r6   r.   c           
          | j                             |          }t          j        || j                 t          j        dt          j        || j                           z
            f          }|S )a  
        Loglikelihood for observations of Generic Censored model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

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

        Notes
        -----

        r8   )r>   r3   r1   concatenater   rD   rT   r   )r)   r   rE   rJ   s       r-   r3   z_RCensoredGeneric.loglikeobs  sd    & ?--f55ndm$VAx(89:::;;= 
 
r.   c           	      b   | j                             |          }| j                             |          }t          j        || j                 || j                 j        t          j        || j                            z  dt          j        || j                           z
  z  j        f          }|S )  
        Generic Censored 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`
        r8   )	r>   rN   r3   r1   r   r   r   rU   rT   )r)   r   rV   rE   r^   s        r-   rN   z_RCensoredGeneric.score_obs  s     _..v66
?--f55t}%()+fXd./0001"&$"23444678	    r.   c                 R    |                      |                              d          S )r   r   r]   r4   s     r-   r^   z_RCensoredGeneric.score  r_   r.   r`   ra   r8   rb   c
           
      n   |t          | dd          t          | dd          z   }t          j        |          dk    r|dk    rd }| j                            | j        | j        |          }t          j                    5  t          j	        dt                     |                    d          j        }d d d            n# 1 swxY w Y    t                      j        d|||||d	 d
|
}|                     | |j                  }|                     |          }|i } |j        d|d|	d| |S )Nr   r   r   r8   rd   re   rf   rh   c                     | S rk   rl   rm   s    r-   ro   z'_RCensoredGeneric.fit.<locals>.<lambda>  rp   r.   rq   Trw   rl   )rS   r1   r{   r>   r,   r#   r$   r|   r}   r~   r   r   r   r!   r   r   r   r   )r)   rr   rs   rt   ru   ri   rv   rx   r   rz   r*   r   rY   r   r   r   r,   s                   r-   r   z_RCensoredGeneric.fit  s    T8Q//'$
A2N2NNFwv!##!O--dj$)5; . = =E(** 8 8%h9KLLLL$yyay0078 8 8 8 8 8 8 8 8 8 8 8 8 8 8  %# [    ""499**622H%% 	Nx/35	N 	NDL	N 	N 	Nr   r   r   r   r   r   r   r   c                    t          j        |          dk    r/|dk    r)| j        j        d         }|t          j        |          z  }|}|t          | dd          t          | dd          z   }t          j        |          dk    r|dk    rd }| j                            | j        | j        |          } |j	        d	||||d||||	|
|d|j
        } t          t          |           j	        d	|||||||||	|
|d|}|dv r|                     | |          }nt          d|z            |                     |          S r   r   r   s                      r-   r   z!_RCensoredGeneric.fit_regularized.  r   r.   c                 ,    t          || j                  S )a  
        Generic Censored 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
        -----
        r   r4   s     r-   r   z_RCensoredGeneric.hessianU  r   r.   NNr   r   r   )r   r   r   r   r   r   r   r"   r5   r3   rN   r^   r   r   r   r   r   r   s   @r-   r   r     s6        + 
!"# ##G, ;?     / / /*  8  6- - -" =?,07;     @  #+CK -1IMIM	#: #: #: #: #: #:J ,;CO1 1 1 1 1 1 1r.   r   c                   N     e Zd Zdej        dej        z   dz  Z	 	 d fd	Z xZS )_RCensoredPoissonz
    Censored Poisson model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    r   r   Nr   c                     t                      j        ||f|||d| t          t          j        | j                  | j                  | _        d | _        t          | _
        t          | _        t          | _        t          | _        d S Nr   )r!   r"   r   r1   rO   r#   r$   r>   r   TruncatedLFGenericResultsr   r   r   r   r   r   r   r   s          r-   r"   z_RCensoredPoisson.__init__  s    	
 	
 	
 	
 	
 	
 ""-
";";TYGG5$D! ;(J%%%r.   r  r   r   s   @r-   r  r  j  s         + 
!"# ##G, ,0(.K K K K K K K K K Kr.   r  c                   N     e Zd Zdej        dej        z   dz  Z	 	 d fd	Z xZS )	_RCensoredGeneralizedPoissona  
    Censored Generalized Poisson model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    r   r   Nr   r   c                     t                      j        ||f|||d| t          t          j        | j                  | j                  | _        d | _        t          | _
        t          | _        t          | _        t          | _        d S r  )r!   r"   r   r1   rO   r#   r$   r>   r   r  r   r   r   r   r   r   r   	r)   r#   r$   r   r   r   r   r*   r,   s	           r-   r"   z%_RCensoredGeneralizedPoisson.__init__  s    4	' &	' 	'%	' 	' 	' -M$*%%ty2 25$D! ;(J%%%r.   Nr   Nr   r   r   s   @r-   r	  r	    s         + 
!"# ##G, 45(.K K K K K K K K K Kr.   r	  c                   N     e Zd Zdej        dej        z   dz  Z	 	 d fd	Z xZS )	_RCensoredNegativeBinomialPa  
    Censored Negative Binomial model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    r   r   Nr   r   c                     t                      j        ||f|||d| t          t          j        | j                  | j        |          | _        d | _        t          | _
        t          | _        t          | _        t          | _        d S )Nr   r   )r!   r"   r   r1   rO   r#   r$   r>   r   r  r   r   r   r   r   r   r   r  s	           r-   r"   z$_RCensoredNegativeBinomialP.__init__  s    	 	 	 	 	 	 ,BM$*,E,E,0I./. . . 5$D! ;(J%%%r.   r  r   r   s   @r-   r  r    s         + 
!"# ##G, 45(.K K K K K K K K K Kr.   r  c                   Z     e Zd Zdej        dej        z   dz  Zeedddf fd	Z	d Z
 xZS )
_RCensoredz
    Censored model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    r   r   Nr   c                     t                      j        ||f|||d|  |t          j        | j                  | j                  | _        || _        | j        j        x| _        }	|	dk    r-| j	        
                    | j        j	        |	 d                     t          | _        t          | _        t          | _        t"          | _        d S )Nr   r   )r!   r"   r1   rO   r#   r$   r>   r   r   r   r'   r  r   r   r   r   r   r   r   )r)   r#   r$   rY   distributionr   r   r   r*   r   r,   s             r-   r"   z_RCensored.__init__  s     		 	 	 	 	 	  %dj 9 949EE&!%!88wQ;;O""4?#=whii#HIII5$D! ;(J%%%r.   c                 <    | j                             ||          }|S )zrProbability that count is not zero

        internal use in Censored model, will be refactored or removed
        )r>   r   )r)   r   r   prob_nzs       r-   r   z_RCensored._prob_nonzero  s     
 ///F;;r.   )r   r   r   r   r   r   r   r   r   r"   r   r   r   s   @r-   r  r    s         + 
!"# ##G, +2.tK K K K K K.      r.   r  c                        e Zd Zdej        dej        z   dz  Z	 	 	 	 d fd	Zd	 Zd
 Z		 	 	 ddZ
ej
        j        e
_        	 	 ddZ xZS )r   a  
    Hurdle model for count data

    .. versionadded:: 0.14.0

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    dist : string
        Log-likelihood type of count model family. 'poisson' or 'negbin'
    zerodist : string
        Log-likelihood type of zero hurdle model family. 'poisson', 'negbin'
    p : scalar
        Define parameterization for count model.
        Used when dist='negbin'.
    pzero : scalar
        Define parameterization parameter zero hurdle model family.
        Used when zerodist='negbin'.
    aJ  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.

    Notes
    -----
    The parameters in the NegativeBinomial zero model are not identified if
    the predicted mean is constant. If there is no or only little variation in
    the predicted mean, then convergence might fail, hessian might not be
    invertible or parameter estimates will have large standard errors.

    References
    ----------
    not yet

    r   Npoissonr   r   c
                    ||d}t          |           t                      j        ||f|||	d|
 d| _        d| _        |                     ||||           t          | _        t          | _	        t          | _        t          | _        d S )Nz+Offset and exposure are not yet implementedr   r   )NotImplementedErrorr!   r"   k_extra1k_extra2_initializeHurdleCountResultsr   HurdleCountResultsWrapperr   L1HurdleCountResultsr   L1HurdleCountResultsWrapperr   )r)   r#   r$   r   distzerodistr   pzeror   r   r*   msgr,   s               r-   r"   zHurdleCountModel.__init__J  s    
 H$8?C%c***	 	 	 	 	 	 xE222.$=! 4(C%%%r.   c                    |dvs|dvrt          d          |dk    r't          | j        | j        t                    | _        n<|dk    r6t          | j        | j        t                    | _        | xj        dz  c_        |dk    r!t          | j        | j                  | _	        d S |dk    r3t          | j        | j        |          | _	        | xj        dz  c_        d S d S )N)r  negbinz,dist and zerodist must be "poisson","negbin"r  )rY   r'  r8   r  )r  r  r#   r$   r   model1r   r  r   model2r   r  )r)   r"  r#  r   r$  s        r-   r  zHurdleCountModel._initializec  s   --- 555% '1 2 2 2 y  $TZ'JJJDKK!!$TZ+<> > >DKMMQMM9,TZCCDKKKX6tz499:< < <DKMMQMMMM r.   c                     t          t          |          | j        z
  | j        z
  dz            | j        z   }| j                            |d|                   | j                            ||d                   z   S )a]  
        Loglikelihood of Generic Hurdle 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
        -----

        r   N)intlenr  r  r(  r5   r)  )r)   r   ks      r-   r5   zHurdleCountModel.loglikew  sx    & Vt},t}<A  M"##F2A2J//##F122J//0 	1r.   r`   ra   r8   rb   c
           
      @   |dk    rt          d           | j        j        d|||||d d|
} | j        j        d|||||d d|
}t	          |          }| |j        _        |j        d         |j        d         g|j        d<   t          j	        |j        j
        |j        j
                  |j        _
        |j        xj        |j        j        z  c_        | xj        t          |j        dd          z  c_        | xj        t          |j        dd          z  c_        | j        | j        z   d	z   | _        d
 | j        j        D             }|| j        j        z   | j        d d <   ddlm} d |j        _        	 |j                                        }|j                                        } |||          |j        _        n)# t           $ r}dt+          |          vr Y d }~nd }~ww xY w|                     | |j        ||          }|                     |          }|S )Nrb   z'robust cov_type currently not supportedc                     | S rk   rl   rm   s    r-   ro   z&HurdleCountModel.fit.<locals>.<lambda>       r.   rq   c                     | S rk   rl   rm   s    r-   ro   z&HurdleCountModel.fit.<locals>.<lambda>  r0  r.   	convergedr   r   r8   c                     g | ]}d |z   S )zm_rl   ).0names     r-   
<listcomp>z(HurdleCountModel.fit.<locals>.<listcomp>  s    CCCD54<CCCr.   )
block_diagzneed covariancerl   )r   r(  r   r)  r   r   rY   mle_retvalsr1   appendr   r   r  rS   r  r   r   scipy.linalgr8  normalized_cov_params
cov_paramsstrr   r   )r)   rr   rs   rt   ru   ri   rv   rx   r   rz   r*   results1results2r   xnames1r8  cov1cov2emodelfits                       r-   r   zHurdleCountModel.fit  s|    {""FGGG"4;? %7#kk  	  #4;? %7#kk  	  (## $+3+?+L+3+?+L+N;'!#8+<+C+3+<+C"E "E 	  H$5$>>  !2IqAAA!2IqAAA59CCDK,BCCC$t{'== 	,+++++04-	$//11D$//11D4>JtT4J4JFO11 	 	 	 A.. /....	
 $$T6?HhOO**844s   :AG 
G)G$$G)r   c                    |                                 }|dnd}|                     |||          \  }}}d}||r| j        }n|}t          t	          |          | j        z
  | j        z
  dz            | j        z   }	|d|	         }
||	d         }t          j        ||d| j        j	        d                            |z   |z   }| j
                            |
|          }| j
        j                            ||
          }d|z
  }t          j        |          }| j        j                            ||          }|dk    r|t          j        |          z  |z  S |d	k    rt          j        |          S |d
k    r|S |dk    rt          j        |          |z  S |dk    r|S |dk    r|S |dk    rd|z
  S |dk    rHt          j        |          }| j                            ||          \  }}||z  |d|z
  z  |dz  z  z   }|S |dk    rL| j                            ||t          j        |          |d|          }||dddf         z  }||dddf<   |S t#          d|z            )a  
        Predict response variable 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)
            - 'mean-main' : mean parameter of truncated count model.
              Note, this is not the mean of the truncated distribution.
            - 'linear' : the linear predictor of the truncated count model.
            - 'var' : returns the estimated variance of endog implied by the
              model.
            - 'prob-main' : probability of selecting the main model which is
              the probability of observing a nonzero count P(y > 0 | x).
            - 'prob-zero' : probability of observing a zero count. P(y=0 | x).
              This is equal to is ``1 - prob-main``
            - 'prob-trunc' : probability of truncation of the truncated count
              model. This is the probability of observing a zero count implied
              by the truncation model.
            - 'mean-nonzero' : expected value conditional on having observation
              larger than zero, E(y | X, y>0)
            - '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"``

        Returns
        -------
        predicted values

        Notes
        -----
        'prob-zero' / 'prob-trunc' is the ratio of probabilities of observing
        a zero count between hurdle model and the truncated count model.
        If this ratio is larger than one, then the hurdle model has an inflated
        number of zeros compared to the count model. If it is smaller than one,
        then the number of zeros is deflated.
        NTFr   r   r8   )r$   r   r   r   zmean-nonzeroz	prob-zeroz	prob-mainz
prob-truncr   r   r:   r   zwhich = %s is not available)lowerr   r$   r+  r,  r  r  r1   r   r   r(  r?   r>   r   rT   r)  r   r   )r)   r   r$   r   r   r;   r<   no_exog	exog_zerok_zerosparams_zeroparams_mainlin_predmu1r   r   mu2prob_ntruncr   mtvtr   
probs_mains                          r-   r?   zHurdleCountModel.predict  s   B ,$$E!%!9!9 ": " "fh 	 ! I		 	s6{{T]2T]BaG  -(XgX&WXX&F4-@dioa.@-@!ABB%& k!!+D!99K*88kJJ	]	fXk,::3LLF??rvh///+==k!!6(###hOn$$6(##k11k!!k!!l""{?"e^^!!B[44["EEFBr>IY$?"a%$GGDKf__,,T26(#3#3V6! - # #J )AAAtG,,J(Jqqq!t:UBCCCr.   )Nr  r  r   r   Nr   r   r   )r   r   r   r   r   r   r   r"   r  r5   r   r   r?   r   r   s   @r-   r   r     s        0 +" 
!#"# #1+#GZ ,0*3(.D D D D D D2  (1 1 10 =?,07;4 4 4 4l  #+CK2648~D ~D ~D ~D ~D ~D ~D ~Dr.   r   c                       e Zd Zedddz  ZdS )r  z%A results class for Generic Truncated one_line_description
extra_attrN)r   r   r   r   r   rl   r.   r-   r  r  H  s(        $ G( ( GGGr.   r  c                   4    e Zd Zedddz  Zed             ZdS )r   z%A results class for Truncated PoissonrU  rV  c                     | j         j        dk    rd}t          |          t          j        |                     d                    }d|t          j        |          dz
  z  z
  S )Nr   0dispersion is only available for zero-truncationr   r;   r8   )rY   r%   r  r1   rT   r?   )r)   r%  r   s      r-   _dispersion_factorz,TruncatedLFPoissonResults._dispersion_factorS  s^    :q  DC%c***VDLLxL0011B"&**q.))*r.   Nr   r   r   r   r   r   r]  rl   r.   r-   r   r   N  sI        $ G( ( G + + ^+ + +r.   r   c                   4    e Zd Zedddz  Zed             ZdS )r   z/A results class for Truncated Negative BinomialrU  rV  c                 (   | j         j        dk    rd}t          |          | j        d         }| j         j        j        }t          j        |                     d                    }d|||dz
  z  z  t          j        ||dz
  z            dz
  z  z
  S )Nr   r[  r=   r   r\  r8   )	rY   r%   r  r   r>   r   r1   rT   r?   )r)   r%  r   r   r   s        r-   r]  z3TruncatedNegativeBinomialResults._dispersion_factord  s    :q  DC%c***BJ!2VDLLxL0011EB1I%QqS	):):Q)>??@r.   Nr^  rl   r.   r-   r   r   ^  sN        $=( ( G
 	A 	A ^	A 	A 	Ar.   r   c                       e Zd ZdS )r   Nr   r   r   rl   r.   r-   r   r   q          Dr.   r   c                       e Zd ZdS )r   Nrb  rl   r.   r-   r   r   u  rc  r.   r   c                       e Zd ZdS )r   Nrb  rl   r.   r-   r   r   }  rc  r.   r   c                   ^     e Zd Zedddz  Z	 d	 fd	Zed             Zed             Z xZ	S )
r  z A results class for Hurdle modelrU  rV  rb   Nc                     t                                          |||||           || _        || _        | j        j        j        d         t          | j                  z
  | _	        d S )N)rx   r   rz   r   )
r!   r"   results_zeroresults_countrY   r#   r   r,  r   r   )	r)   rY   r   rh  ri  rx   r   rz   r,   s	           r-   r"   zHurdleCountResults.__init__  sl     	 	 	 	 )*
(.q1C4D4DDr.   c                 H    | j         j        j        | j        j        j        z   S rk   )rh  r   llnullri  r)   s    r-   rk  zHurdleCountResults.llnull  s$    !*1"+23 	4r.   c                 T    t          j        | j        j        | j        j                  S rk   )r1   r:  rh  bseri  rl  s    r-   rn  zHurdleCountResults.bse  s     y*.0B0FGGGr.   )rb   NN)
r   r   r   r   r   r"   r   rk  rn  r   r   s   @r-   r  r    s        $ B( ( G
 =AE E E E E E 4 4 ^4 H H ^H H H H Hr.   r  c                       e Zd ZdS )r   Nrb  rl   r.   r-   r   r     rc  r.   r   c                       e Zd ZdS )r  Nrb  rl   r.   r-   r  r    rc  r.   r  c                       e Zd ZdS )r!  Nrb  rl   r.   r-   r!  r!    rc  r.   r!  )8__all__r|   numpyr1   statsmodels.base.modelr   rY   statsmodels.base.wrapperwrapperwrap#statsmodels.regression.linear_model
regressionlinear_modellm"statsmodels.distributions.discreter   r   #statsmodels.discrete.discrete_modelr   r	   r
   r   r   r   r   r   statsmodels.tools.numdiffr   statsmodels.tools.decoratorsr   statsmodels.tools.sm_exceptionsr   copyr   r   r   r   r   r   r  r	  r  r  r   r  r   r   r   RegressionResultsWrapperr   populate_wrapperr   r  r   r  r!  rl   r.   r-   <module>r     s          % % % % % % % % % ' ' ' ' ' ' ' ' ' 0 0 0 0 0 0 0 0 0       	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 2 1 1 1 1 1 7 7 7 7 7 7 > > > > > >      T< T< T< T< T< T< T< T<nF F F F F+ F F FRS S S S S#5 S S Sl5K 5K 5K 5K 5K$6 5K 5K 5Kp\1 \1 \1 \1 \1
 \1 \1 \1~&K &K &K &K &K) &K &K &KR#K #K #K #K #K#4 #K #K #KL)K )K )K )K )K"3 )K )K )KX4 4 4 4 4" 4 4 4niD iD iD iD iDz iD iD iDX	       + + + + + 9 + + + A A A A A'@ A A A&	 	 	 	 	.2K 	 	 		 	 	 	 	r'B 	 	 	  6/1 1 1	 	 	 	 	)D 	 	 	  813 3 3H H H H H H H H:	 	 	 	 	>+= 	 	 		 	 	 	 	 ; 	 	 	  /(* * *	 	 	 	 	""= 	 	 	  1*, , , , ,r.   