U
    Kvf                     @   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
   @/tmp/pip-unpacked-wheel-2v6byqio/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n,| j}|j}tt||}tj|ddS dS )zOreturn frozen scipy.stats distribution with mu at estimated prediction
        resultr   )locN)	hasattr
ValueErrorr   r   r   r   r   r   Zpoisson)r   r   r   r   mur
   r
   r   predict_distributionP   s    
z PoissonGMLE.predict_distributionN)__name__
__module____qualname____doc__r   r    r
   r
   r
   r   r   -   s   r   c                       s*   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                    sV   |d k	r.|j dkr"|d d d f }| | _nd| _tt| j||fd|i| d S )Nr           missing)ndimraveloffsetsuperr%   __init__r   r   r   r+   r(   kwds	__class__r
   r   r-   m   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                       s*   e Zd ZdZd fdd	Zdd Z  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.

    Nr&   c                    s   d| _ tt| j||f|dgd| |d k	rV|jdkrJ|d d d f }| | _nd| _|d krvt| 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,   r5   r-   r)   r*   r+   r   ZonesZnobsr   shapeZnparamsZhstackr6   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   Nr   r   )r   r   r+   r   r   r   r   r   )r   r   betaZgammr   r   r2   r
   r
   r   r      s    0(zPoissonZiGMLE.nloglikeobs)NNr&   r3   r
   r
   r0   r   r5      s   r5   )r$   Znumpyr   Zscipyr   Zscipy.specialr   Zstatsmodels.base.modelr   r   r   r   r%   r5   r
   r
   r
   r   <module>   s   34