
    M/Phm                         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dS )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                 T    t          j        t          j        | |z
                      S )Nnpmaxabsarr1arr2s     \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/miscmodels/count.pymaxabsr   $   s     6"&%%&&&    c                 Z    t          j        t          j        || z  dz
                      S )N   r   r   s     r   	maxabsrelr   '   s$    6"&q))***r   c                       e Zd ZdZd Zd ZdS )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:   sL    " VDIv&&
vbzzU2X%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   s\     tX&& 		, [F]FtV,,--B=++++r   N)__name__
__module____qualname____doc__r!   r)    r   r   r   r   ,   s?        
 
A A 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.

    Nnonec                     |1|j         dk    r|d d d f         }|                                | _        nd| _         t                      j        ||fd|i| d S )Nr           missing)ndimraveloffsetsuper__init__r   r   r   r7   r4   kwds	__class__s         r   r9   zPoissonOffsetGMLE.__init__l   s|    {a$ ,,..DKKDK 	 	g 		 	 	 	 	r   c                     | j         t          j        | j        |          z   }| j        }t          j        |          ||z  z
  t          j        t          |                    z   }|S r   )r7   r   r   r   r   r   r   r   )r   r   r    r   nlogliks        r   r!   zPoissonOffsetGMLE.nloglikeobs|   sW    $ [26$)V444
&**b(26)E2B2B+C+CCr   NNr1   r*   r+   r,   r-   r9   r!   __classcell__r<   s   @r   r0   r0   _   sV        
 
	 	 	 	 	 	       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.

    Nr1   c                    d| _          t                      j        ||f|dgd| |1|j        dk    r|d d d f         }|                                | _        nd| _        | t          j        | j        df          | _	        | j	        j
        d         | _        t          j        t          j        | j                  df          | _        | xj        dz  c_        dg| _        d S )Nr   zi)r4   extra_params_namesr3   r   start_params)k_extrar8   r9   r5   r6   r7   r   onesnobsr   shapenparamshstackrH   	cloneattrr:   s         r   r9   zPoissonZiGMLE.__init__   s     	3g$(6	3 	3-1	3 	3 	3{a$ ,,..DKKDK <1..DIyq)Irwt|'<'<a&@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   r7   r   r   r   r   r   )r   r   betagammr    r   r>   s          r   r!   zPoissonZiGMLE.nloglikeobs   s    " crc{Avbz***+ [26$)T222
6!D&>>/BF2JJ.%(:RVIeDTDT=U=UU fTBFGE1H4E3E,F,F%FGGGqr   r?   r@   rB   s   @r   rD   rD      sV         * * * * * *6      r   rD   )r-   numpyr   scipyr   scipy.specialr   statsmodels.base.modelr   r   r   r   r0   rD   r.   r   r   <module>rX      s   :           # # # # # # 9 9 9 9 9 9' ' '+ + +
/, /, /, /, /,( /, /, /,f2 2 2 2 2. 2 2 2hD D D D D* D D D D Dr   