ó
áp7]c           @  sÀ   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l	 m
 Z
 d „  Z d „  Z d	 e f d
 „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d S(   s´  
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, doesn't 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


iÿÿÿÿ(   t   print_functionN(   t   stats(   t	   factorial(   t   GenericLikelihoodModelc         C  s   t  j t  j |  | ƒ ƒ S(   N(   t   npt   maxt   abs(   t   arr1t   arr2(    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   maxabs&   s    c         C  s   t  j t  j | |  d ƒ ƒ S(   Ni   (   R   R   R   (   R   R   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt	   maxabsrel)   s    t   NonlinearDeltaCovc           B  s>   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z d „  Z RS(   sR  Asymptotic covariance by Deltamethod

    the function is designed for 2d array, with rows equal to
    the number of equations and columns equal to the number
    of parameters. 1d params work by chance ?

    fun: R^{m*k) -> R^{m}  where m is number of equations and k is
    the number of parameters.

    equations follow Greene

    c         C  s   | |  _  | |  _ | |  _ d  S(   N(   t   funt   paramst
   cov_params(   t   selfR   R   R   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   __init__9   s    		c         K  sK   | d  k r |  j } n  | j d d ƒ d d l m } | | |  j |  S(   Nt   epsilong-Cëâ6?iÿÿÿÿ(   t   approx_fprime(   t   NoneR   t
   setdefaultt   statsmodels.tools.numdiffR   R   (   R   R   t   kwdsR   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   grad>   s
    c         C  s4   |  j  ƒ  } t j t j | |  j ƒ | j ƒ } | S(   N(   R   R   t   dotR   t   T(   R   t   gt   covar(    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   covE   s    $c         C  s   |  j  |  j ƒ S(   N(   R   R   (   R   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   expectedJ   s    c         C  sw   |  j  ƒ  } |  j ƒ  } t j | ƒ } | | } t j t j | j t j j | ƒ ƒ | ƒ } | t j	 j
 | | ƒ f S(   N(   R   R   R   t   sizeR   R   t   linalgt   invR   t   chi2t   sf(   R   t   valuet   mt   vt   dft   difft   lmstat(    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   waldN   s    
-N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R)   (    (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR   ,   s   			t   PoissonGMLEc           B  s    e  Z d  Z d „  Z d „  Z RS(   sf  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  sF   t  j |  j | ƒ } |  j } t  j | ƒ | | t  j t | ƒ ƒ S(   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   R   t   exogt   endogt   expt   logR   (   R   R   t   XBR/   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   nloglikeobsg   s    	c         C  s\   t  |  d ƒ s t ‚ n@ |  j } | j } t j t j | | ƒ ƒ } t j | d d ƒSd S(   sO   return frozen scipy.stats distribution with mu at estimated prediction
        t   resultt   loci    N(	   t   hasattrt
   ValueErrorR4   R   R   R0   R   R   t   poisson(   R   R.   R4   R   t   mu(    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   predict_distribution|   s    			(   R*   R+   R,   R3   R:   (    (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR-   Y   s   	t   PoissonOffsetGMLEc           B  s)   e  Z d  Z d d d d „ Z d „  Z RS(   su  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.

    t   nonec         K  su   | d  k	 rF | j d k r4 | d  d  … d  f } n  | j ƒ  |  _ n	 d |  _ t t |  ƒ j | | d | | d  S(   Ni   g        t   missing(   R   t   ndimt   ravelt   offsett   superR;   R   (   R   R/   R.   R@   R=   R   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR   ™   s    	c         C  sS   |  j  t j |  j | ƒ } |  j } t j | ƒ | | t j t | ƒ ƒ } | S(   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@   R   R   R.   R/   R0   R1   R   (   R   R   R2   R/   t   nloglik(    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR3   ©   s    	*N(   R*   R+   R,   R   R   R3   (    (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR;   Œ   s   t   PoissonZiGMLEc           B  s)   e  Z d  Z d d d d „ Z d „  Z RS(   sÊ  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.

    R<   c         K  sò   t  t |  ƒ j | | d | | | d  k	 rh | j d k rV | d  d  … d  f } n  | j ƒ  |  _ n	 d |  _ | d  k r› t j |  j	 d f ƒ |  _
 n  |  j
 j d |  _ t j t j |  j ƒ d f ƒ |  _ d g |  _ |  j j d ƒ d  S(   NR=   i   g        i    t   start_paramst   zi(   RA   RC   R   R   R>   R?   R@   R   t   onest   nobsR.   t   shapet   nparamst   hstackRD   t	   cloneattrt
   exog_namest   append(   R   R/   R.   R@   R=   R   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR   Ï   s    	$c         C  s¼   | d  } d d t  j | d ƒ } |  j t  j |  j | ƒ } |  j } t  j d | ƒ t  j | ƒ | | t  j t | ƒ ƒ } t  j | t  j | | d k ƒ ƒ | | d k <| S(   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]
        iÿÿÿÿi   i    (   R   R0   R@   R   R.   R/   R1   R   (   R   R   t   betat   gammR2   R/   RB   (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyR3   ç   s    
	<2N(   R*   R+   R,   R   R   R3   (    (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyRC   À   s   (   R,   t
   __future__R    t   numpyR   t   scipyR   t   statsmodels.compat.scipyR   t   statsmodels.base.modelR   R	   R
   t   objectR   R-   R;   RC   (    (    (    s;   lib/python2.7/site-packages/statsmodels/miscmodels/count.pyt   <module>   s   		-34