
    >hD                         S r SSKrSSKJr  SSKJr  SSKJr  S r	S r
 " S S	\5      r " S
 S\5      r " S S\5      rg)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                 Z    [         R                  " [         R                  " X-
  5      5      $ )Nnpmaxabsarr1arr2s     oC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/miscmodels/count.pymaxabsr   $   s    66"&&%&&    c                 `    [         R                  " [         R                  " X-  S-
  5      5      $ )N   r   r   s     r   	maxabsrelr   '   s     66"&&q)**r   c                   $    \ rS rSrSrS rS rSrg)PoissonGMLE,   aJ  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                     [         R                  " U R                  U5      nU R                  n[         R                  " U5      X2-  -
  [         R
                  " [        U5      5      -   $   
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   nloglikeobsPoissonGMLE.nloglikeobs:   sI    " VVDIIv&

vvbzUX%y/?(@@@r   c                     [        U S5      (       d  [        eU R                  nUR                  n[        R
                  " [        R                  " X5      5      n[        R                  " USS9$ )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_distribution PoissonGMLE.predict_distributionO   sS     tX&& [[F]]Ft,-B==++r    N)__name__
__module____qualname____firstlineno____doc__r"   r+   __static_attributes__r-   r   r   r   r   ,   s    
A*,r   r   c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )PoissonOffsetGMLE_   aY  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                    > Ub/  UR                   S:X  a	  US S 2S 4   nUR                  5       U l        OSU l        [        TU ]  " X4SU0UD6  g )Nr           missing)ndimraveloffsetsuper__init__r   r   r   r<   r9   kwds	__class__s         r   r>   PoissonOffsetGMLE.__init__l   sT    {{a$ ,,.DKDK 	g 		r   c                     U R                   [        R                  " U R                  U5      -   nU R                  n[        R
                  " U5      X2-  -
  [        R                  " [        U5      5      -   nU$ r   )r<   r   r   r   r   r   r   r   )r   r    r!   r   nlogliks        r   r"   PoissonOffsetGMLE.nloglikeobs|   sV    $ [[266$))V44

&&*(266)E2B+CCr   )r<   NNnone	r.   r/   r0   r1   r2   r>   r"   r3   __classcell__rA   s   @r   r5   r5   _   s    
	  r   r5   c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )PoissonZiGMLE   a  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                   > SU l         [        TU ]  " X4US/S.UD6  Ub/  UR                  S:X  a	  US S 2S 4   nUR	                  5       U l        OSU l        Uc'  [        R                  " U R                  S45      U l	        U R                  R                  S   U l        [        R                  " [        R                  " U R                  5      S45      U l        U =R                  S-  sl        S/U l        g )Nr   zi)r9   extra_params_namesr8   r   start_params)k_extrar=   r>   r:   r;   r<   r   onesnobsr   shapenparamshstackrQ   	cloneattrr?   s         r   r>   PoissonZiGMLE.__init__   s     	3g$(6	3-1	3{{a$ ,,.DKDK <1.DIyyq)IIrwwt||'<a&@A()r   c                    USS nSS[         R                  " US   5      -   -  nU R                  [         R                  " U R                  U5      -   nU R
                  n[         R                  " SU-
  5      * [         R                  " U5      -   XT-  -
  [         R                  " [        U5      5      -   n[         R                  " U[         R                  " XeS:H     * 5      -   5      * XeS:H  '   U$ )r   Nr   r   )r   r   r<   r   r   r   r   r   )r   r    betagammr!   r   rD   s          r   r"   PoissonZiGMLE.nloglikeobs   s    " cr{Avbz**+ [[266$))T22

66!D&>/BFF2J.%(:RVVIeDT=UU ffTBFFG1H4E3E,F%FGGqr   )rX   r   rR   rV   r<   rQ   rF   rH   rJ   s   @r   rL   rL      s    *6 r   rL   )r2   numpyr   scipyr   scipy.specialr   statsmodels.base.modelr   r   r   r   r5   rL   r-   r   r   <module>rc      sM   :   # 9'+
/,( /,f2. 2hD* Dr   