o
    þ0Gfm  ã                   @   st   d Z ddlZddlmZ ddlmZ ddlmZ dd„ Z	dd	„ Z
G d
d„ deƒZG dd„ deƒZG d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                 C   s   t  t  | | ¡¡S )N©ÚnpÚmaxÚabs©Zarr1Zarr2© r
   ú<lib/python3.10/site-packages/statsmodels/miscmodels/count.pyÚmaxabs$   s   r   c                 C   s   t  t  ||  d ¡¡S )Né   r   r	   r
   r
   r   Ú	maxabsrel'   s   r   c                   @   s    e Zd ZdZdd„ Zd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                 C   s4   t  | j|¡}| j}t  |¡||  t  t|ƒ¡ 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   r
   r
   r   Únloglikeobs:   s    zPoissonGMLE.nloglikeobsc                 C   s:   t | dƒst‚| j}|j}t t ||¡¡}tj|ddS )zOreturn frozen scipy.stats distribution with mu at estimated prediction
        Úresultr   )Úloc)	ÚhasattrÚ
ValueErrorr   r   r   r   r   r   Zpoisson)r   r   r   r   Zmur
   r
   r   Úpredict_distributionO   s   
z PoissonGMLE.predict_distributionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r
   r
   r
   r   r   ,   s    r   c                       ó*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  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                    sR   |d ur|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   r+   r(   Úkwds©Ú	__class__r
   r   r-   l   s   

ÿzPoissonOffsetGMLE.__init__c                 C   s>   | j t | j|¡ }| j}t |¡||  t t|ƒ¡ }|S r   )r+   r   r   r   r   r   r   r   )r   r   r   r   Únloglikr
   r
   r   r   |   s    zPoissonOffsetGMLE.nloglikeobs©NNr&   ©r    r!   r"   r#   r-   r   Ú__classcell__r
   r
   r0   r   r%   _   s    r%   c                       r$   )	Ú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.

    Nr&   c                    s²   d| _ tƒ j||f|dgdœ|¤Ž |d ur)|jdkr#|d d …d f }| ¡ | _nd| _|d u r9t | jdf¡| _	| j	j
d | _t t | j¡df¡| _|  jd7  _dg| _d S )Nr   Zzi)r(   Zextra_params_namesr'   r   Ústart_params)Zk_extrar,   r-   r)   r*   r+   r   ZonesZnobsr   ÚshapeZnparamsZhstackr7   Z	cloneattrr.   r0   r
   r   r-   ¢   s"   ÿÿ
zPoissonZiGMLE.__init__c                 C   s˜   |dd… }ddt  |d ¡  }| jt  | j|¡ }| j}t  d| ¡ t  |¡ ||  t  t|ƒ¡ }t  |t  ||dk  ¡ ¡ ||dk< |S )r   Néÿÿÿÿr   r   )r   r   r+   r   r   r   r   r   )r   r   ZbetaZgammr   r   r2   r
   r
   r   r   ½   s   0(zPoissonZiGMLE.nloglikeobsr3   r4   r
   r
   r0   r   r6   “   s    r6   )r#   Znumpyr   Zscipyr   Zscipy.specialr   Zstatsmodels.base.modelr   r   r   r   r%   r6   r
   r
   r
   r   Ú<module>   s    34