
    'YHhm                         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     U/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/miscmodels/count.pymaxabsr   $   s    66"&&%&&    c                 ^    t        j                  t        j                  || z  dz
              S )N   r   r   s     r   	maxabsrelr   '   s"    66"&&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    " VVDIIv&

vvbzU2X%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     tX& [[F]]FtV,-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$ ,,.DKDK 	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    $ [[266$))V44

&&*b(266)E2B+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     	3g$(6	3-1	3{{a$ ,,.DKDK <1.DIyyq)IIrwwt||'<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   Nr   r   )r   r   r6   r   r   r   r   r   )r   r   betagammr    r   r=   s          r   r!   zPoissonZiGMLE.nloglikeobs   s    " cr{Avbz**+ [[266$))T22

66!D&>/BFF2J.%(:RVVIeDT=UU ffTBFFGE1H4E3E,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   