Ë
    £�DjD  ã                   ó‚   — d Z ddlZddlmZ ddlmZ ddlmZ d„ Z	d„ Z
 G d„ d	e«      Z G d
„ de«      Z G d„ de«      Zy)aµ  
Created on Mon Jul 26 08:34:59 2010

Author: josef-pktd

changes:
added offset and zero-inflated version of Poisson
 - kind of ok, need better test cases,
 - a nan in ZIP bse, need to check hessian calculations
 - found error in ZIP loglike
 - all tests pass with

Issues
------
* If true model is not zero-inflated then numerical Hessian for ZIP has zeros
  for the inflation probability and is not invertible.
  -> hessian inverts and bse look ok if row and column are dropped, pinv also works
* GenericMLE: still get somewhere (where?)
   "CacheWriteWarning: The attribute 'bse' cannot be overwritten"
* bfgs is too fragile, does not come back
* `nm` is slow but seems to work
* need good start_params and their use in genericmle needs to be checked for
  consistency, set as attribute or method (called as attribute)
* numerical hessian needs better scaling

* check taking parts out of the loop, e.g. factorial(endog) could be precalculated


é    N)Ústats)Ú	factorial)ÚGenericLikelihoodModelc                 óX   — t        j                  t        j                  | |z
  «      «      S )N©ÚnpÚmaxÚabs©Úarr1Úarr2s     ú`C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/miscmodels/count.pyÚmaxabsr   $   s   € Ü�6‰6”"—&‘&˜ ™Ó%Ó&Ð&ó    c                 ó^   — t        j                  t        j                  || z  dz
  «      «      S )Né   r   r   s     r   Ú	maxabsrelr   '   s"   € Ü�6‰6”"—&‘&˜ ™ q™Ó)Ó*Ð*r   c                   ó   — e Zd ZdZd„ Zd„ Zy)ÚPoissonGMLEaf  Maximum Likelihood Estimation of Poisson Model

    This is an example for generic MLE which has the same
    statistical model as discretemod.Poisson.

    Except for defining the negative log-likelihood method, all
    methods and results are generic. Gradients and Hessian
    and all resulting statistics are based on numerical
    differentiation.

    c                 óÎ   — t        j                  | j                  |«      }| j                  }t        j                  |«      ||z  z
  t        j
                  t        |«      «      z   S ©á|  
        Loglikelihood of Poisson model

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

        Returns
        -------
        The log likelihood of the model evaluated at `params`

        Notes
        -----
        .. math:: \ln L=\sum_{i=1}^{n}\left[-\lambda_{i}+y_{i}x_{i}^{\prime}\beta-\ln y_{i}!\right]
        )r   ÚdotÚexogÚendogÚexpÚlogr   )ÚselfÚparamsÚXBr   s       r   ÚnloglikeobszPoissonGMLE.nloglikeobs:   sK   € ô" �V‰V�D—I‘I˜vÓ&ˆØ—
‘
ˆÜ�v‰v�b‹z˜U 2™XÑ%¬¯©¬y¸Ó/?Ó(@Ñ@Ð@r   c                 óÖ   — t        | d«      st        ‚| j                  }|j                  }t	        j
                  t	        j                  ||«      «      }t        j                  |d¬«      S )zOreturn frozen scipy.stats distribution with mu at estimated prediction
        Úresultr   )Úloc)	ÚhasattrÚ
ValueErrorr#   r   r   r   r   r   Úpoisson)r   r   r#   r   Úmus        r   Úpredict_distributionz PoissonGMLE.predict_distributionO   sR   € ô �t˜XÔ&ô Ðà—[‘[ˆFØ—]‘]ˆFÜ—‘œŸ™˜t VÓ,Ó-ˆBÜ—=‘= ¨Ô+Ð+r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r)   © r   r   r   r   ,   s   „ ñ
òAó*,r   r   c                   ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )ÚPoissonOffsetGMLEau  Maximum Likelihood Estimation of Poisson Model

    This is an example for generic MLE which has the same
    statistical model as discretemod.Poisson but adds offset

    Except for defining the negative log-likelihood method, all
    methods and results are generic. Gradients and Hessian
    and all resulting statistics are based on numerical
    differentiation.

    c                 óš   •— |�.|j                   dk(  r	|d d …d f   }|j                  «       | _        nd| _        t        ‰| �  ||fd|i|¤Ž y )Nr   ç        Úmissing)ÚndimÚravelÚoffsetÚsuperÚ__init__©r   r   r   r6   r3   ÚkwdsÚ	__class__s         €r   r8   zPoissonOffsetGMLE.__init__l   sV   ø€ àÐØ�{‰{˜aÒØ¢ $ ™�Ø Ÿ,™,›.ˆD�KàˆDŒKÜ‰Ñ˜ ñ 	¨gð 	Øó	r   c                 óì   — | j                   t        j                  | j                  |«      z   }| j                  }t        j
                  |«      ||z  z
  t        j                  t        |«      «      z   }|S r   )r6   r   r   r   r   r   r   r   )r   r   r    r   Únlogliks        r   r!   zPoissonOffsetGMLE.nloglikeobs|   sX   € ð$ �[‰[œ2Ÿ6™6 $§)¡)¨VÓ4Ñ4ˆØ—
‘
ˆÜ—&‘&˜“*  b¡Ñ(¬2¯6©6´)¸EÓ2BÓ+CÑCˆØˆr   ©NNÚnone©r*   r+   r,   r-   r8   r!   Ú__classcell__©r;   s   @r   r0   r0   _   s   ø„ ñ
õ	ö r   r0   c                   ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )ÚPoissonZiGMLEaÊ  Maximum Likelihood Estimation of Poisson Model

    This is an example for generic MLE which has the same statistical model
    as discretemod.Poisson but adds offset and zero-inflation.

    Except for defining the negative log-likelihood method, all
    methods and results are generic. Gradients and Hessian
    and all resulting statistics are based on numerical
    differentiation.

    There are numerical problems if there is no zero-inflation.

    c                 óä  •— d| _         t        ‰| �  ||f|dgdœ|¤Ž |�.|j                  dk(  r	|d d …d f   }|j	                  «       | _        nd| _        |€&t        j                  | j                  df«      | _	        | j                  j                  d   | _        t        j                  t        j                  | j                  «      df«      | _        | xj                  dz  c_        dg| _        y )Nr   Úzi)r3   Úextra_params_namesr2   r   Ústart_params)Úk_extrar7   r8   r4   r5   r6   r   ÚonesÚnobsr   ÚshapeÚnparamsÚhstackrH   Ú	cloneattrr9   s         €r   r8   zPoissonZiGMLE.__init__¢   sÐ   ø€ àˆŒÜ‰Ñ˜ ð 	3¨gØ$( 6ñ	3Ø-1ò	3àÐØ�{‰{˜aÒØ¢ $ ™�Ø Ÿ,™,›.ˆD�KàˆDŒKð ˆ<ÜŸ™ §¡¨1 Ó.ˆDŒIØ—y‘y—‘ qÑ)ˆŒäŸI™I¤r§w¡w¨t¯|©|Ó'<¸aÐ&@ÓAˆÔà�Š˜Ñ�Ø(Ð)ˆ�r   c                 óØ  — |dd }ddt        j                  |d   «      z   z  }| j                  t        j                  | j                  |«      z   }| j
                  }t        j                  d|z
  «       t        j                  |«      z   ||z  z
  t        j                  t        |«      «      z   }t        j                  |t        j                  ||dk(      «      z   «       ||dk(  <   |S )r   Néÿÿÿÿr   r   )r   r   r6   r   r   r   r   r   )r   r   ÚbetaÚgammr    r   r=   s          r   r!   zPoissonZiGMLE.nloglikeobs½   sÈ   € ð" �c�rˆ{ˆØ�AœŸ™˜v b™zÓ*Ñ*Ñ+ˆð �[‰[œ2Ÿ6™6 $§)¡)¨TÓ2Ñ2ˆØ—
‘
ˆÜ—6‘6˜!˜D™&“>�/¤B§F¡F¨2£JÑ.°%¸±(Ñ:¼R¿V¹VÄIÈeÓDTÓ=UÑUˆÜ Ÿf™f T¬B¯F©F°G¸EÀ1¹HÑ4EÐ3EÓ,FÑ%FÓGÐGˆ��q‘Ñàˆr   r>   r@   rB   s   @r   rD   rD   “   s   ø„ ñõ*ö6r   rD   )r-   Únumpyr   Úscipyr   Úscipy.specialr   Ústatsmodels.base.modelr   r   r   r   r0   rD   r.   r   r   ú<module>rX      sM   ðñó: Ý Ý #Ý 9ò'ò+ô
/,Ð(ô /,ôf2Ð.ô 2ôhDÐ*õ Dr   