
p7]c           @   sn  d  Z  d d l m Z d d d d d d d	 g Z d d
 l m Z d d l m Z d d l Z	 d d l
 m Z d d l m Z m Z m Z d d l m Z m Z d d l m Z d d l j j Z d d l m Z d d l m Z d d l m Z m Z d d l m  Z  d d l! j" j# Z" d d l$ m% Z% d d l& j' j( Z) d d l* j" j+ Z, d d l- m. Z. d d l/ m0 Z0 y d d l1 Z1 e2 Z3 Wn e4 k
 re5 Z3 n Xd d l6 Z6 e	 j7 e8  j9 Z: d Z; d Z< d Z= d Z> d   Z? d   Z@ d   ZA d e" jB f d      YZC d! eC f d"     YZD d# eD f d$     YZE d% eC f d&     YZF d' eC f d(     YZG d eF f d)     YZH d eF f d*     YZI d eD f d+     YZJ d eD f d,     YZK d eE f d-     YZL d eF f d.     YZM d	 eF f d/     YZN d0 e" jO f d1     YZP d2 eP f d3     YZQ d4 eQ f d5     YZR d6 eR f d7     YZS d8 eP f d9     YZT d: eQ f d;     YZU d< eT eU f d=     YZV d> eT eR f d?     YZW d@ eT eS f dA     YZX dB eP f dC     YZY dD eP f dE     YZZ dF eZ f dG     YZ[ dH eZ f dI     YZ\ dJ eZ f dK     YZ] dL eP f dM     YZ^ dN e^ f dO     YZ_ dP e) j` f dQ     YZa e, jb ea eY  dR e) j` f dS     YZc e, jb ec eQ  dT e) j` f dU     YZd e, jb ed eR  dV e) j` f dW     YZe e, jb ee eS  dX e) j` f dY     YZf e, jb ef eU  dZ e) j` f d[     YZg d\ e) j` f d]     YZh e, jb eh eV  d^ e) j` f d_     YZi e, jb ei eW  d` e) j` f da     YZj e, jb ej eX  db e) j` f dc     YZk e, jb ek eZ  dd e) j` f de     YZl e, jb el e]  df e) j` f dg     YZm e, jb em e^  dh e) j` f di     YZn e, jb en e_  d S(j   s  
Limited dependent variable and qualitative variables.

Includes binary outcomes, count data, (ordered) ordinal data and limited
dependent variables.

General References
--------------------

A.C. Cameron and P.K. Trivedi.  `Regression Analysis of Count Data`.
    Cambridge, 1998

G.S. Madalla. `Limited-Dependent and Qualitative Variables in Econometrics`.
    Cambridge, 1983.

W. Greene. `Econometric Analysis`. Prentice Hall, 5th. edition. 2003.
i(   t   divisiont   Poissont   Logitt   Probitt   MNLogitt   NegativeBinomialt   GeneralizedPoissont   NegativeBinomialP(   t   range(   t   loggammaN(   t   get_dummies(   t   gammalnt   digammat	   polygamma(   t   statst   special(   t   nbinom(   t   data(   t   cache_readonly(   t   PerfectSeparationErrort   SpecificationWarning(   t   approx_fprime_cs(   t   handle_data(   t   fit_l1_slsqp(   t   genpoisson_pt   
s  
    %(one_line_description)s

    Parameters
    ----------
    model : A DiscreteModel instance
    params : array-like
        The parameters of a fitted model.
    hessian : array-like
        The hessian of the fitted model.
    scale : float
        A scale parameter for the covariance matrix.

    Attributes
    ----------
    df_resid : float
        See model definition.
    df_model : float
        See model definition.
    llf : float
        Value of the loglikelihood
    %(extra_attr)ss
      nnz_params : Integer
        The number of nonzero parameters in the model.  Train with
        trim_params == True or else numerical error will distort this.
    trimmed : Boolean array
        trimmed[i] == True if the ith parameter was trimmed from the model.s   
Compute one-step moment estimator for null (constant-only) model

This is a preliminary estimator used as start_params.

Returns
-------
params : ndarray
    parameter estimate based one one-step moment matching

c         C   s   |  j  j d k r6 t j |  d t d t \ } } nL |  j d k ra |  } t |  j d  } n! t j |  d t d t \ } } | | f S(   Nt   St   Ot   dropt	   dictnamesi   i   (   R   R   (   t   dtypet   kindt   toolst   categoricalt   Truet   ndimR   t   shape(   t   endogt   endog_dummiest   ynames(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _numpy_to_dummiesl   s    c         C   s   |  j  d k r` |  j d d k rQ |  j d } t |  j d  d   d f  } qu d } |  } n |  j } t |   } | j j   } | | | f S(   Ni   i   i    t   y(   R#   R$   t   columnsR
   t   iloct   namet   tolist(   R%   t   ynameR&   R'   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _pandas_to_dummiesy   s    "		c         C   s+   |  d k r' t  d j d |     n  d S(   s  
    As of 0.10.0, the supported values for `method` in `fit_regularized`
    are "l1" and "l1_cvxopt_cp".  If an invalid value is passed, raise
    with a helpful error message

    Parameters
    ----------
    method : str

    Raises
    ------
    ValueError
    t   l1t   l1_cvxopt_cpsG   `method` = {method} is not supported, use either "l1" or "l1_cvxopt_cp"t   methodN(   R0   R1   (   t
   ValueErrort   format(   R2   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _validate_l1_method   s    t   DiscreteModelc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d d d d d d d	  Z	 e	 j e
 j j	 j 7_ d d
 d d e d d d d d d e d  Z d   Z d e d  Z d d d d  Z d   Z RS(   s   
    Abstract class for discrete choice models.

    This class does not do anything itself but lays out the methods and
    call signature expected of child classes in addition to those of
    statsmodels.model.LikelihoodModel.
    c         K   s)   t  t |   j | | |  t |  _ d  S(   N(   t   superR6   t   __init__R"   t   raise_on_perfect_prediction(   t   selfR%   t   exogt   kwargs(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8      s    c         C   sI   t  j j |  j  } t | d  |  _ t |  j j d |  |  _ d S(   s   
        Initialize is called by
        statsmodels.model.LikelihoodModel.__init__
        and should contain any preprocessing that needs to be done for a model.
        i   i    N(   t   npt   linalgt   matrix_rankR;   t   floatt   df_modelR$   t   df_resid(   R:   t   rank(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt
   initialize   s    c         C   s
   t   d S(   sD   
        The cumulative distribution function of the model.
        N(   t   NotImplementedError(   R:   t   X(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   cdf   s    c         C   s
   t   d S(   sG   
        The probability density (mass) function of the model.
        N(   RE   (   R:   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   pdf   s    c         G   sm   |  j  } |  j t j |  j | |  j j d    } |  j ri t j | | d  ri d } t |   n  d  S(   Ni   i    s2   Perfect separation detected, results not available(	   R%   RG   R=   t   dotR;   R$   R9   t   allcloseR   (   R:   t   paramst   argsR%   t   fittedvaluest   msg(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _check_perfect_pred   s    	,	t   newtoni#   i   c   	      K   sX   | d k r |  j } n  t t |   j d | d | d | d | d | d | |  } | S(   s   
        Fit the model using maximum likelihood.

        The rest of the docstring is from
        statsmodels.base.model.LikelihoodModel.fit
        t   start_paramsR2   t   maxitert   full_outputt   dispt   callbackN(   t   NoneRO   R7   R6   t   fit(	   R:   RQ   R2   RR   RS   RT   RU   R<   t   mlefit(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW      s    R0   t   defined_by_methodi    t   autog{Gz?g-C6?gQ?c         K   s  t  |  |  j } t j |  } | j   d k s: t  y | | d <Wn  t k
 rj t d |  } n X| d t |  j	 j
 d  | d <| | d <|
 | d <|	 | d <| | d <| | d <| d	 k r | d
 k r d } q | d k r d } q n  i t d
 6} t r4| d k r4d d l m } | | d <n | j   d k rOd } n  | d k rg|  j } n  t t |   j d | d | d | d | d | d | d | d | |  } | S(   sQ  
        Fit the model using a regularized maximum likelihood.
        The regularization method AND the solver used is determined by the
        argument method.

        Parameters
        ----------
        start_params : array-like, optional
            Initial guess of the solution for the loglikelihood maximization.
            The default is an array of zeros.
        method : 'l1' or 'l1_cvxopt_cp'
            See notes for details.
        maxiter : Integer or 'defined_by_method'
            Maximum number of iterations to perform.
            If 'defined_by_method', then use method defaults (see notes).
        full_output : bool
            Set to True to have all available output in the Results object's
            mle_retvals attribute. The output is dependent on the solver.
            See LikelihoodModelResults notes section for more information.
        disp : bool
            Set to True to print convergence messages.
        fargs : tuple
            Extra arguments passed to the likelihood function, i.e.,
            loglike(x,*args)
        callback : callable callback(xk)
            Called after each iteration, as callback(xk), where xk is the
            current parameter vector.
        retall : bool
            Set to True to return list of solutions at each iteration.
            Available in Results object's mle_retvals attribute.
        alpha : non-negative scalar or numpy array (same size as parameters)
            The weight multiplying the l1 penalty term
        trim_mode : 'auto, 'size', or 'off'
            If not 'off', trim (set to zero) parameters that would have been
            zero if the solver reached the theoretical minimum.
            If 'auto', trim params using the Theory above.
            If 'size', trim params if they have very small absolute value
        size_trim_tol : float or 'auto' (default = 'auto')
            For use when trim_mode == 'size'
        auto_trim_tol : float
            For sue when trim_mode == 'auto'.  Use
        qc_tol : float
            Print warning and don't allow auto trim when (ii) (above) is
            violated by this much.
        qc_verbose : Boolean
            If true, print out a full QC report upon failure

        Notes
        -----
        Extra parameters are not penalized if alpha is given as a scalar.
        An example is the shape parameter in NegativeBinomial `nb1` and `nb2`.

        Optional arguments for the solvers (available in Results.mle_settings)::

            'l1'
                acc : float (default 1e-6)
                    Requested accuracy as used by slsqp
            'l1_cvxopt_cp'
                abstol : float
                    absolute accuracy (default: 1e-7).
                reltol : float
                    relative accuracy (default: 1e-6).
                feastol : float
                    tolerance for feasibility conditions (default: 1e-7).
                refinement : int
                    number of iterative refinement steps when solving KKT
                    equations (default: 1).


        Optimization methodology

        With :math:`L` the negative log likelihood, we solve the convex but
        non-smooth problem

        .. math:: \min_\beta L(\beta) + \sum_k\alpha_k |\beta_k|

        via the transformation to the smooth, convex, constrained problem
        in twice as many variables (adding the "added variables" :math:`u_k`)

        .. math:: \min_{\beta,u} L(\beta) + \sum_k\alpha_k u_k,

        subject to

        .. math:: -u_k \leq \beta_k \leq u_k.

        With :math:`\partial_k L` the derivative of :math:`L` in the
        :math:`k^{th}` parameter direction, theory dictates that, at the
        minimum, exactly one of two conditions holds:

        (i) :math:`|\partial_k L| = \alpha_k`  and  :math:`\beta_k \neq 0`
        (ii) :math:`|\partial_k L| \leq \alpha_k`  and  :math:`\beta_k = 0`

        i    t   alphat   alpha_rescaledt	   trim_modet   size_trim_tolt   auto_trim_tolt   qc_tolt
   qc_verboseRY   R0   i  R1   iF   i(   t   fit_l1_cvxopt_cpsE   Attempt to use l1_cvxopt_cp failed since cvxopt could not be importedRQ   R2   RR   RS   RT   RU   t   extra_fit_funcst   cov_params_funcN(   R5   t   cov_params_func_l1R=   t   arrayt   mint   AssertionErrort	   TypeErrort   dictR@   R%   R$   R   t   have_cvxoptt   statsmodels.base.l1_cvxoptRb   t   lowerRV   RO   R7   R6   RW   (   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   Ra   R<   Rd   Rc   Rb   t   messageRX   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   fit_regularized   s@    b
	"




		c         C   s   | j  |  } | d } t j |  d } | j   } | d k r| | | d d  d f | f } t j j |  }	 n t j d  }	 t j t j	 | j
  }
 |	 |
 | d d  d f | f <|
 S(   s  
        Computes cov_params on a reduced parameter space
        corresponding to the nonzero parameters resulting from the
        l1 regularized fit.

        Returns a full cov_params matrix, with entries corresponding
        to zero'd values set to np.nan.
        t   trimmedi    N(   t   hessianR=   t   nonzerot   sumRV   R>   t   invt   zerost   nant   onesR$   (   R:   t   likelihood_modelt   xoptt   retvalst   HRp   t   nz_idxt
   nnz_paramst   H_restrictedt   H_restricted_invt
   cov_params(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRe   s  s    	
  c         C   s
   t   d S(   sQ   
        Predict response variable of a model given exogenous variables.
        N(   RE   (   R:   RK   R;   t   linear(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   predict  s    c         C   s
   t   d S(   sQ   
        This should implement the derivative of the non-linear function
        N(   RE   (   R:   RK   R;   t	   dummy_idxt	   count_idx(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _derivative_exog  s    c   	      C   sn   d d l  m } m } | d k	 r@ | | | | | |  |  } n  | d k	 rj | | | | | |  |  } n  | S(   sK   
        Helper for _derivative_exog to wrap results appropriately
        i   (   t   _get_count_effectst   _get_dummy_effectsN(   t   discrete_marginsR   R   RV   (	   R:   t   margeffRK   R;   R   R   t	   transformR   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _derivative_exog_helper  s    N(   t   __name__t
   __module__t   __doc__R8   RD   RG   RH   RO   RV   RW   t   baset   LikelihoodModelR"   t   FalseRo   Re   R   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR6      s&   										t   BinaryModelc           B   s}   e  Z d    Z d e d  Z d d d d d d d d d d d	 d
  Z e j j e _ d d d  Z	 d d d d d  Z
 RS(   c         K   sh   t  t |   j | | |  t |  j t  rd t j |  j d k |  j d k @ rd t	 d   n  d  S(   Ni    i   s#   endog must be in the unit interval.(
   R7   R   R8   t
   issubclasst	   __class__t   MultinomialModelR=   t   allR%   R3   (   R:   R%   R;   R<   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8     s    &c         C   sK   | d k r |  j } n  | s7 |  j t j | |   St j | |  Sd S(   sV  
        Predict response variable of a model given exogenous variables.

        Parameters
        ----------
        params : array-like
            Fitted parameters of the model.
        exog : array-like
            1d or 2d array of exogenous values.  If not supplied, the
            whole exog attribute of the model is used.
        linear : bool, optional
            If True, returns the linear predictor dot(exog,params).  Else,
            returns the value of the cdf at the linear predictor.

        Returns
        -------
        array
            Fitted values at exog.
        N(   RV   R;   RG   R=   RI   (   R:   RK   R;   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s
    R0   RY   i   i    RZ   g{Gz?g-C6?gQ?c         K   s}   t  |  t t |   j d | d | d | d | d | d | d | d | d	 |	 d
 |
 d | |  } t |  |  } t |  S(   NRQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   (   R5   R7   R   Ro   t   L1BinaryResultst   L1BinaryResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   t   bnryfitt   discretefit(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo     s    
t   dydxc         C   s   | d k r |  j } n  |  j t j | |   d d  d f | } d | k r| | |  j | |  d d  d f } n  | S(   s}  
        For computing marginal effects standard errors.

        This is used only in the case of discrete and count regressors to
        get the variance-covariance of the marginal effects. It returns
        [d F / d params] where F is the predict.

        Transform can be 'dydx' or 'eydx'. Checking is done in margeff
        computations for appropriate transform.
        Nt   ey(   RV   R;   RH   R=   RI   R   (   R:   RK   R;   R   t   dF(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _derivative_predict  s    /)c         C   s   | d k r |  j } n  t j |  j t j | |   d d  d f | d d d  f  } d | k rx | | 9} n  d | k r | |  j | |  d d  d f } n  |  j | | | | | |  S(   s;  
        For computing marginal effects returns dF(XB) / dX where F(.) is
        the predicted probabilities

        transform can be 'dydx', 'dyex', 'eydx', or 'eyex'.

        Not all of these make sense in the presence of discrete regressors,
        but checks are done in the results in get_margeff.
        Nt   exR   (   RV   R;   R=   RI   RH   R   R   (   R:   RK   R;   R   R   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    .)N(   R   R   R8   RV   R   R   Ro   R6   R   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   	R   c           B   s   e  Z d    Z d   Z d e d  Z d d d d d d d  Z e j j	 e _	 d d d d d d d	 d
 d d d d  Z
 e j
 j	 e
 _	 d d d  Z d d d d d  Z RS(   c         K   sL  t  j | d   r- t |  \ } } d } nQ t  j | d   rW t |  \ } } } n' t j |  } t |  \ } } d } t | t	  s t	 t
 t | j d  |   } n  | |  _ t | | | | |  }	 | |	 _ | |	 _ |	 j |  _ xT | D]L }
 |
 d k rq n  y  t |  |
 |	 j j |
   Wq t k
 rCq Xq W|	 S(   NR)   i   t   design_infot   formula(   R   R   (   t
   data_toolst   _is_using_ndarray_typeRV   R(   t   _is_using_pandasR/   R=   t   asarrayt
   isinstanceRj   t   zipR   R$   t   _ynames_mapR   R'   t
   orig_endogR%   t   wendogt   setattrt   __dict__t   popt   KeyError(   R:   R%   R;   t   missingt   hasconstR<   R&   R'   R.   R   t   key(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _handle_data  s.    	%			 c         C   s   t  t |   j   |  j j d  |  _ |  j j d |  _ |  j j d |  _	 |  j
 |  j d 9_
 |  j j d |  j
 |  j d |  _ d S(   s4   
        Preprocesses the data for MNLogit.
        i   i    N(   R7   R   RD   R%   t   argmaxR   R$   t   JR;   t   KRA   RB   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRD   -  s    c         C   s   | d k r |  j } n  | j d k r4 | d } n  t t |   j | | |  } | r t j t j t	 |   | f  } n  | S(   s  
        Predict response variable of a model given exogenous variables.

        Parameters
        ----------
        params : array-like
            2d array of fitted parameters of the model. Should be in the
            order returned from the model.
        exog : array-like
            1d or 2d array of exogenous values.  If not supplied, the
            whole exog attribute of the model is used. If a 1d array is given
            it assumed to be 1 row of exogenous variables. If you only have
            one regressor and would like to do prediction, you must provide
            a 2d array with shape[1] == 1.
        linear : bool, optional
            If True, returns the linear predictor dot(exog,params).  Else,
            returns the value of the cdf at the linear predictor.

        Notes
        -----
        Column 0 is the base case, the rest conform to the rows of params
        shifted up one for the base case.
        i   N(
   RV   R;   R#   R7   R   R   R=   t   column_stackRu   t   len(   R:   RK   R;   R   t   pred(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   9  s    'RP   i#   i   c   	      K   s   | d  k r, t j |  j |  j d  } n t j |  } d   } t j j |  d | d | d | d | d | d | | } | j	 j
 |  j d	 d
 d | _	 t |  |  } t |  S(   Ni   c         S   s   d  S(   N(   RV   (   t   x(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   <lambda>`  t    RQ   R2   RR   RS   RT   RU   it   ordert   F(   RV   R=   Ru   R   R   R   R   R   RW   RK   t   reshapet   MultinomialResultst   MultinomialResultsWrapper(	   R:   RQ   R2   RR   RS   RT   RU   R<   t   mnfit(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW   Z  s     	!R0   RY   i    RZ   g{Gz?g-C6?gQ?c         K   s   | d  k r, t j |  j |  j d  } n t j |  } t j |  d | d | d | d | d | d | d | d	 | d
 |	 d |
 d | | } | j j	 |  j d d d | _ t
 |  |  } t |  S(   Ni   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   iR   R   (   RV   R=   Ru   R   R   R   R6   Ro   RK   R   t   L1MultinomialResultst   L1MultinomialResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo   j  s     !R   c         C   s2  | d k r |  j } n  | j d k rL | j |  j |  j d d d } n  t j t j | |   } d | j	 d  d d  d f } t
 |  j  } t
 |  j  } t j | | d d } t j | | d  }	 | |	 | d }
 | j d d  d d  d f |	 | | | d } | j d	  } t j t j | d  t j |   j t  } | j d d  d d  d f |	 | | d j d
  d d  | f | d d  | f <t j |
 d d  d d d  f | f d d } d | k r.| |  j | |  d d  d d  d f } n  | S(   s  
        For computing marginal effects standard errors.

        This is used only in the case of discrete and count regressors to
        get the variance-covariance of the marginal effects. It returns
        [d F / d params] where F is the predicted probabilities for each
        choice. dFdparams is of shape nobs x (J*K) x (J-1)*K.
        The zero derivatives for the base category are not included.

        Transform can be 'dydx' or 'eydx'. Checking is done in margeff
        computations for appropriate transform.
        i   R   R   Nt   axisi   i    R   (   i   i    i   (   i   i    i   (   RV   R;   R#   R   R   R   R=   t   expRI   Rs   t   intt   repeatt   tilet   Tt	   transposet   kront   eyeRw   t   astypet   boolt   concatenateR   (   R:   RK   R;   R   t   eXBt   sum_eXBR   R   t
   repeat_eXBRF   t   F0t   F1t	   other_idxt   dFdX(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   }  s(    %#62(442c         C   s  t  |  j  } t  |  j  } | d k r6 |  j } n  | j d k rd | j | | d d d } n  t j t j	 |  | f } |  j
 t j | |   }	 t j g  t t  |   D]3 }
 |	 d d  |
 g f | d d  |
 f ^ q  j d  } t j g  t |  D]7 } |	 d d  | g f | d d  | f | ^ q } t j | d
  } d | k rw| | 9} n  d | k r| |  j | |  d d  d d d  f } n  |  j | | | | | |  } | j t |  d	 d d S(   s  
        For computing marginal effects returns dF(XB) / dX where F(.) is
        the predicted probabilities

        transform can be 'dydx', 'dyex', 'eydx', or 'eyex'.

        Not all of these make sense in the presence of discrete regressors,
        but checks are done in the results in get_margeff.

        For Multinomial models the marginal effects are

        P[j] * (params[j] - sum_k P[k]*params[k])

        It is returned unshaped, so that each row contains each of the J
        equations. This makes it easier to take derivatives of this for
        standard errors. If you want average marginal effects you can do
        margeff.reshape(nobs, K, J, order='F).mean(0) and the marginal effects
        for choice J are in column J
        i   R   R   Ni    i   R   R   i(   i   i   i    (   R   R   R   RV   R;   R#   R   R=   t   c_Ru   RG   RI   Rf   R   Rs   R   R   R   R   (   R:   RK   R;   R   R   R   R   R   t
   zeroparamsRG   t   it   itermt   jR   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s(    	U	J2N(   R   R   R   RD   RV   R   R   RW   R6   R   Ro   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   		!	*t
   CountModelc           B   s   e  Z d d d  d  Z d   Z d   Z d d d e d  Z d d d  Z d d d d d  Z	 d d d	 d
 d
 d d  Z
 e j
 j e
 _ d d d d
 d
 d d d d d d d  Z e j j e _ RS(   t   nonec      
   K   s  t  t |   j | | d | d | d | | | d  k	 rR t j |  j  |  _ n  |  j |  j |  j |  j	  | d  k r t
 |  d  n  | d  k r t
 |  d  n  t j |  j	 j t j  } t j |  j	 |  |  _	 t j |  j j t j  } t j |  j |  |  _ d  S(   NR   t   offsett   exposure(   R7   R   R8   RV   R=   t   logR   t   _check_inputsR   R%   t   delattrt   promote_typesR   t   float64R   R;   (   R:   R%   R;   R   R   R   R<   t   dt(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8     s    
c         C   sn   | d  k	 r5 | j d | j d k r5 t d   n  | d  k	 rj | j d | j d k rj t d   n  d  S(   Ni    s&   offset is not the same length as endogs(   exposure is not the same length as endog(   RV   R$   R3   (   R:   R   R   R%   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    &&c         C   sO   t  t |   j   } d | k rK | d d  k	 rK t j | d  | d <n  | S(   NR   (   R7   R   t   _get_init_kwdsRV   R=   R   (   R:   t   kwds(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    c         C   s   | d k r3 | d k r3 t |  d  r3 |  j } n | d k rH d } n  | d k r{ | d k r{ t |  d  r{ |  j } n$ | d k r d } n t j |  } | d k r |  j } n  t j | | | j d   } | | | } | s t j	 |  S| Sd S(   s   
        Predict response variable of a count model given exogenous variables.

        Notes
        -----
        If exposure is specified, then it will be logged by the method.
        The user does not need to log it first.
        R   g        R   i   N(
   RV   t   hasattrR   R   R=   R   R;   RI   R$   R   (   R:   RK   R;   R   R   R   t   fittedt   linpred(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s     '	'	R   c         C   sw   | d k r |  j } n  |  j | |  d d  d f | } d | k rs | |  j | |  d d  d f } n  | S(   s}  
        For computing marginal effects standard errors.

        This is used only in the case of discrete and count regressors to
        get the variance-covariance of the marginal effects. It returns
        [d F / d params] where F is the predict.

        Transform can be 'dydx' or 'eydx'. Checking is done in margeff
        computations for appropriate transform.
        NR   (   RV   R;   R   (   R:   RK   R;   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   %  s    &)c   	      C   s   | d k r |  j } n  t |  d d  } | d k r< | n | |  } |  j | |  d d  d f | d d d  f } d | k r | | 9} n  d | k r | |  j | |  d d  d f } n  |  j | | | | | |  S(   s  
        For computing marginal effects. These are the marginal effects
        d F(XB) / dX
        For the Poisson model F(XB) is the predicted counts rather than
        the probabilities.

        transform can be 'dydx', 'dyex', 'eydx', or 'eyex'.

        Not all of these make sense in the presence of discrete regressors,
        but checks are done in the results in get_margeff.
        t   k_extrai    NR   R   (   RV   R;   t   getattrR   R   (	   R:   RK   R;   R   R   R   R   t   params_exogR   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   8  s    6)RP   i#   i   c   
      K   sU   t  t |   j d | d | d | d | d | d | |  } t |  |  }	 t |	  S(   NRQ   R2   RR   RS   RT   RU   (   R7   R   RW   t   CountResultst   CountResultsWrapper(
   R:   RQ   R2   RR   RS   RT   RU   R<   t   cntfitR   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW   S  s
    R0   RY   i    RZ   g{Gz?g-C6?gQ?c         K   s}   t  |  t t |   j d | d | d | d | d | d | d | d | d	 |	 d
 |
 d | |  } t |  |  } t |  S(   NRQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   (   R5   R7   R   Ro   t   L1CountResultst   L1CountResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo   \  s    
N(   R   R   RV   R8   R   R   R   R   R   R   RW   R6   R   Ro   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    			#	t   OrderedModelc           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   n  s   c           B   s  e  Z d  i e j d 6d e j d 6Z e d    Z d   Z d   Z	 d   Z
 d   Z d	   Z e e _ d d
 d d d d d  Z e j j e _ d d d d d d d d d d d d  Z e j j e _ d d  Z d   Z d   Z d   Z d   Z d   Z RS(   s   
    Poisson model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    RK   s   offset : array_like
        Offset is added to the linear prediction with coefficient equal to 1.
    exposure : array_like
        Log(exposure) is added to the linear prediction with coefficient
        equal to 1.

    t   extra_paramsc         C   s   d d l  m } | j   S(   Ni(   t   families(   t   statsmodels.genmodR   R   (   R:   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   family  s    c         C   s%   |  j  } t j j | t j |   S(   sO  
        Poisson model cumulative distribution function

        Parameters
        ----------
        X : array-like
            `X` is the linear predictor of the model.  See notes.

        Returns
        -------
        The value of the Poisson CDF at each point.

        Notes
        -----
        The CDF is defined as

        .. math:: \exp\left(-\lambda\right)\sum_{i=0}^{y}\frac{\lambda^{i}}{i!}

        where :math:`\lambda` assumes the loglinear model. I.e.,

        .. math:: \ln\lambda_{i}=X\beta

        The parameter `X` is :math:`X\beta` in the above formula.
        (   R%   R   t   poissonRG   R=   R   (   R:   RF   R)   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRG     s    	c         C   s.   |  j  } t j t j j | t j |    S(   s  
        Poisson model probability mass function

        Parameters
        ----------
        X : array-like
            `X` is the linear predictor of the model.  See notes.

        Returns
        -------
        pdf : ndarray
            The value of the Poisson probability mass function, PMF, for each
            point of X.

        Notes
        --------
        The PMF is defined as

        .. math:: \frac{e^{-\lambda_{i}}\lambda_{i}^{y_{i}}}{y_{i}!}

        where :math:`\lambda` assumes the loglinear model. I.e.,

        .. math:: \ln\lambda_{i}=x_{i}\beta

        The parameter `X` is :math:`x_{i}\beta` in the above formula.
        (   R%   R=   R   R   R   t   logpmf(   R:   RF   R)   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRH     s    	c         C   sw   t  |  d d  } t  |  d d  } t j |  j |  | | } |  j } t j t j |  | | t | d   S(   s  
        Loglikelihood of Poisson model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.

        Notes
        --------
        .. math:: \ln L=\sum_{i=1}^{n}\left[-\lambda_{i}+y_{i}x_{i}^{\prime}\beta-\ln y_{i}!\right]
        R   i    R   i   (   R   R=   RI   R;   R%   Rs   R   R   (   R:   RK   R   R   t   XBR%   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   loglike  s
    	c         C   sn   t  |  d d  } t  |  d d  } t j |  j |  | | } |  j } t j |  | | t | d  S(   s   
        Loglikelihood for observations of Poisson model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : array-like
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes

        Notes
        --------
        .. math:: \ln L_{i}=\left[-\lambda_{i}+y_{i}x_{i}^{\prime}\beta-\ln y_{i}!\right]

        for observations :math:`i=1,...,n`

        R   i    R   i   (   R   R=   RI   R;   R%   R   R   (   R:   RK   R   R   R   R%   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt
   loglikeobs  s
    	c         C   sZ   t  |  d d  } t  |  d d  } |  j t j | |  j   } t j |  g } | S(   NR   i    R   (   R   R%   R=   R   t   meanR   (   R:   R   R   t   constRK   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _get_start_params_null  s
     RP   i#   i   c         K   s   | d  k rX |  j j d  k	 rX d t j |  j j d  } |  j   d | |  j j <n  t t	 |   j
 d | d | d | d | d | d	 | |  } d
 | k r | j d i   }	 i | d
 d
 6|	 d 6}
 n i  }
 t |  | |
  } t |  S(   NgMbP?i   i    RQ   R2   RR   RS   RT   RU   t   cov_typet   cov_kwds(   RV   R   t	   const_idxR=   Rw   R;   R$   R   R7   R   RW   t   gett   PoissonResultst   PoissonResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R<   R   R  R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW     s    R0   RY   i    RZ   g{Gz?g-C6?gQ?c         K   s}   t  |  t t |   j d | d | d | d | d | d | d | d | d	 |	 d
 |
 d | |  } t |  |  } t |  S(   NRQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   (   R5   R7   R   Ro   t   L1PoissonResultst   L1PoissonResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo     s    
c      	   K   s  d d l  m } d d l m } | |  j  j |  } | j | j } } | |  | | d | d | \ }	 }
 } |  j d d d d	 d
 d d t	  } | j
 j d t j  | j
 d <| j
 j d t j  | j
 d <| j
 d | j
 d <|	 | j _ |
 | j _ | j d d  } | d k r-|
 | j _ n d | j _ t |  } | j j | 7_ | j j | 8_ | | j _ | | j _ | | j _ | S(   sf  fit the model subject to linear equality constraints

        The constraints are of the form   `R params = q`
        where R is the constraint_matrix and q is the vector of
        constraint_values.

        The estimation creates a new model with transformed design matrix,
        exog, and converts the results back to the original parameterization.

        Parameters
        ----------
        constraints : formula expression or tuple
            If it is a tuple, then the constraint needs to be given by two
            arrays (constraint_matrix, constraint_value), i.e. (R, q).
            Otherwise, the constraints can be given as strings or list of
            strings.
            see t_test for details
        start_params : None or array_like
            starting values for the optimization. `start_params` needs to be
            given in the original parameter space and are internally
            transformed.
        **fit_kwds : keyword arguments
            fit_kwds are used in the optimization of the transformed model.

        Returns
        -------
        results : Results instance

        i(   t
   DesignInfo(   t   fit_constrainedRQ   t   fit_kwdsRR   i    R2   t   nmRT   t   warn_convergencet   fcallt
   iterationst	   convergedR   t	   nonrobustN(   t   patsyR  t   statsmodels.base._constraintsR	  t
   exog_namest   linear_constraintt   coefst	   constantsRW   R   t   mle_retvalsR  R=   Rv   t   _resultsRK   t   cov_params_defaultt   normalized_cov_paramsRV   R   RB   RA   t   constraintst   k_constrt   results_constrained(   R:   R  RQ   R
  R  R	  t   lct   Rt   qRK   t   covt
   res_constrt   resR   R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR	  0  s4    #		c         C   sg   t  |  d d  } t  |  d d  } |  j } t j t j | |  | |  } t j |  j | |  S(   s\  
        Poisson model score (gradient) vector of the log-likelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        score : ndarray, 1-D
            The score vector of the model, i.e. the first derivative of the
            loglikelihood function, evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial\ln L}{\partial\beta}=\sum_{i=1}^{n}\left(y_{i}-\lambda_{i}\right)x_{i}

        where the loglinear model is assumed

        .. math:: \ln\lambda_{i}=x_{i}\beta
        R   i    R   (   R   R;   R=   R   RI   R%   (   R:   RK   R   R   RF   t   L(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   scorev  s
    	#c         C   so   t  |  d d  } t  |  d d  } |  j } t j t j | |  | |  } |  j | d d  d f | S(   sK  
        Poisson model Jacobian of the log-likelihood for each observation

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        score : array-like
            The score vector (nobs, k_vars) of the model evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial\ln L_{i}}{\partial\beta}=\left(y_{i}-\lambda_{i}\right)x_{i}

        for observations :math:`i=1,...,n`

        where the loglinear model is assumed

        .. math:: \ln\lambda_{i}=x_{i}\beta
        R   i    R   N(   R   R;   R=   R   RI   R%   RV   (   R:   RK   R   R   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt	   score_obs  s
    	#c         C   s[   t  |  d d  } t  |  d d  } |  j } t j t j | |  | |  } |  j | S(   s.  
        Poisson model score_factor for each observation

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        score : array-like
            The score factor (nobs, ) of the model evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial\ln L_{i}}{\partial\beta}=\left(y_{i}-\lambda_{i}\right)

        for observations :math:`i=1,...,n`

        where the loglinear model is assumed

        .. math:: \ln\lambda_{i}=x_{i}\beta
        R   i    R   (   R   R;   R=   R   RI   R%   (   R:   RK   R   R   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   score_factor  s
    	#c         C   sh   t  |  d d  } t  |  d d  } |  j } t j t j | |  | |  } t j | | j |  S(   sW  
        Poisson model Hessian matrix of the loglikelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (k_vars, k_vars)
            The Hessian, second derivative of loglikelihood function,
            evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial^{2}\ln L}{\partial\beta\partial\beta^{\prime}}=-\sum_{i=1}^{n}\lambda_{i}x_{i}x_{i}^{\prime}

        where the loglinear model is assumed

        .. math:: \ln\lambda_{i}=x_{i}\beta

        R   i    R   (   R   R;   R=   R   RI   R   (   R:   RK   R   R   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRq     s
    	#c         C   sT   t  |  d d  } t  |  d d  } |  j } t j t j | |  | |  } | S(   sQ  
        Poisson model Hessian factor

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (nobs,)
            The Hessian factor, second derivative of loglikelihood function
            with respect to the linear predictor evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial^{2}\ln L}{\partial\beta\partial\beta^{\prime}}=-\sum_{i=1}^{n}\lambda_{i}

        where the loglinear model is assumed

        .. math:: \ln\lambda_{i}=x_{i}\beta

        R   i    R   (   R   R;   R=   R   RI   (   R:   RK   R   R   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   hessian_factor  s
    	#N(   R   R   R   t   _model_params_doct   _missing_param_docR   t   propertyR   RG   RH   R   R   R   t   _get_start_params_null_docsRV   RW   R6   Ro   R	  R%  R&  R'  Rq   R(  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   s  s.   							F				c           B   s'  e  Z d  i e j d 6d e j d 6Z d d d d d  Z d   Z d   Z	 d	   Z
 d
   Z e e _ d d  Z d d d d d d e d d d d 
 Z e j j e j e _ d d d d d d d d d d d d  Z e j j e _ d   Z d   Z d   Z d   Z d d d d d  Z RS(   s   
    Generalized Poisson model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    RK   s_  
    p: scalar
        P denotes parameterizations for GP regression. p=1 for GP-1 and
        p=2 for GP-2. Default is p=1.
    offset : array_like
        Offset is added to the linear prediction with coefficient equal to 1.
    exposure : array_like
        Log(exposure) is added to the linear prediction with coefficient
        equal to 1.

    R   i   R   c      
   K   sa   t  t |   j | | d | d | d | | | d |  _ |  j j d  d |  _ t |  _ d  S(   NR   R   R   i   R[   (	   R7   R   R8   t   parameterizationR  t   appendR   R   t   _transparams(   R:   R%   R;   t   pR   R   R   R<   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8   (  s    
	c         C   s*   t  t |   j   } |  j d | d <| S(   Ni   R0  (   R7   R   R   R-  (   R:   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   2  s    c         C   s   t  j |  j |   S(   sf  
        Loglikelihood of Generalized Poisson model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.

        Notes
        --------
        .. math:: \ln L=\sum_{i=1}^{n}\left[\mu_{i}+(y_{i}-1)*ln(\mu_{i}+
            \alpha*\mu_{i}^{p-1}*y_{i})-y_{i}*ln(1+\alpha*\mu_{i}^{p-1})-
            ln(y_{i}!)-\frac{\mu_{i}+\alpha*\mu_{i}^{p-1}*y_{i}}{1+\alpha*
            \mu_{i}^{p-1}}\right]

        (   R=   Rs   R   (   R:   RK   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   7  s    c   	      C   s   |  j  r t j | d  } n
 | d } | d  } |  j } |  j } |  j |  } t j | |  } d | | } | | d | } t j |  | d t j |  | t j |  t | d  | | S(   s  
        Loglikelihood for observations of Generalized Poisson model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : ndarray
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes

        Notes
        --------
        .. math:: \ln L=\sum_{i=1}^{n}\left[\mu_{i}+(y_{i}-1)*ln(\mu_{i}+
            \alpha*\mu_{i}^{p-1}*y_{i})-y_{i}*ln(1+\alpha*\mu_{i}^{p-1})-
            ln(y_{i}!)-\frac{\mu_{i}+\alpha*\mu_{i}^{p-1}*y_{i}}{1+\alpha*
            \mu_{i}^{p-1}}\right]

        for observations :math:`i=1,...,n`
        ii   (	   R/  R=   R   R-  R%   R   t   powerR   R   (	   R:   RK   R[   R0  R%   t   mut   mu_pt   a1t   a2(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   P  s    	

		c         C   s   t  |  d d  } t  |  d d  } |  j t j | |  j   } t j |  g } | t j | |  } |  j | } |  j | | d | j d d } | j |  t j	 |  S(   NR   i    R   RB   i   (
   R   R%   R=   R   R   R   t   _estimate_dispersionR$   R.  Rf   (   R:   R   R   R   RK   R2  t   residt   a(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   v  s     #c         C   s\   |  j  } | d  k r% | j d } n  t j |  t j |  d | | j   | } | S(   Ni    i   (   R-  RV   R$   R=   t   abst   sqrtRs   (   R:   R2  R7  RB   R   R8  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR6    s
    	3t   bfgsi#   R  c         K   s,  | r | d k r t  |  _ n" | r7 t j d t  n  t |  _ | d k rAt |  d d  t |  d d  } t j	 |  d k r | d k r d } n  i d d 6t  d	 6t d
 6} | j
 | j d i    t |  j |  j d | } | j |   } | j } |  j | j   | j d | j } t j | t d |   } n  | d k rYd   } n  t t |   j d | d | d | d | d | d | |  } | r| d k rt |  _ t j | j j d  | j j d <n  t |  | j  } t |  } |	 d k r	i  }	 n  | j d | d t  d |
 |	  | S(   sn  
        use_transparams : bool
            This parameter enable internal transformation to impose
            non-negativity. True to enable. Default is False.
            use_transparams=True imposes the no underdispersion (alpha > 0)
            constaint. In case use_transparams=True and method="newton" or
            "ncg" transformation is ignored.
        RP   t   ncgs&   Parameter "use_transparams" is ignoredR   i    R   i   RT   t   skip_hessianR  t   optim_kwds_prelimRB   gc          W   s   |  S(   N(    (   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     R   RQ   RR   R2   RS   RU   iR   t   use_selft   use_t(   RP   R<  N(   RP   R<  (   R"   R/  t   warningst   warnt   RuntimeWarningR   RV   R   R=   t   sizet   updateR  R   R%   R;   RW   RK   R6  R   R7  RB   R.  t   maxR7   R   R   R  t   GeneralizedPoissonResultst    GeneralizedPoissonResultsWrappert   _get_robustcov_results(   R:   RQ   R2   RR   RS   RT   RU   t   use_transparamsR   R  R@  R<   R   R>  t   mod_poit   res_poiR8  RX   t   gpfitt   result(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW     sF    	
	"!	
				&	R0   RY   i    RZ   g{Gz?g-C6?gQ?c         K   s  t  |  t j |  d k rb | d k rb |  j j d |  j } | t j |  } d | d <n  |  j r t j |  d k r | d  n | } t |  _ | d  k rut
 |  d d  t
 |  d d  } t j |  d k r | d k r d  } n  t |  j |  j d | } | j d | d | d | d	 | d
 d d | d | d | d |	 d |
 d | |  j } t j | d  } n  t t |   j d | d | d | d	 | d
 | d | d | d | d |	 d |
 d | |  } t |  |  } t |  S(   Ni   i    iR   R   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   g?(   R5   R=   RD  R;   R$   R   Rw   R   R/  RV   R   R   R%   Ro   RK   R.  R7   R   t   L1GeneralizedPoissonResultst"   L1GeneralizedPoissonResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   t   k_paramst   alpha_pR   RK  R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo     s2    
!.	"!		c         C   sh  |  j  r t j | d  } n
 | d } | d  } |  j } |  j } |  j d  d   d  f } |  j |  d  d   d  f } t j | |  } d | | } | | | | }	 | | | | d }
 |
 | } | | } | | | d |	 d | |	 | d } | | | |
 |	 | d d | | d |	 d | d | } t j	 | t j
 |  f d d S(   Nii   i   R   (   R/  R=   R   R-  R;   R%   RV   R   R1  R   t
   atleast_2d(   R:   RK   R[   R0  R;   R)   R2  R3  R4  R5  t   a3t   a4t   dmudbt   dalphat   dparams(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR&    s(    	

		

*4c         C   sI   t  j |  j |  d d } |  j rA | d | d d k | S| Sd  S(   NR   i    ii   (   R=   Rs   R&  R/  (   R:   RK   R%  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR%    s
    	c         C   s   |  j  r t j | d  } n
 | d } | d  } |  j } |  j } |  j d d  d f } |  j |  d d  d f } t j | |  } d | | } | | | | }	 t j	 t j
 |  |	 | | d |	 d | | d |	 | d  }
 |
 S(   sd  
        Generalized Poisson model derivative of the log-likelihood by p-parameter

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        dldp : float
            dldp is first derivative of the loglikelihood function,
        evaluated at `p-parameter`.
        iNi   i   (   R/  R=   R   R-  R;   R%   RV   R   R1  Rs   R   (   R:   RK   R[   R0  R;   R)   R2  R3  R4  R5  t   dp(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _score_p  s    	

		-c      
   C   s  |  j  r t j | d  } n
 | d } | d  } |  j } |  j } |  j d d  d f } |  j |  d d  d f } t j | |  } d | | } | | | | }	 | | | | d }
 |
 | } | | | d } | | } | j	 d } t j
 | d | d f  } xTt |  D]F} x=t | d  D]+} t j | | d d  | d f | d d  | d f | |
 | | d d |
 d |	 | d d |
 | d | d | | | | |
 | |	 | | d | d | | d |	 | | d d | d |	 d | | d | | | d d | |	 d | | d d | | | f <q9Wq"Wt j | d d } | j | | | <t j d | | | d d |
 | |	 | d | | | d d | |	 d | d | | d | | | d |	 d | | | | |	 | d | d d } | | d d d  f <| | d d  d f <| d d | | d | |	 d	 | d d |	 | d } | j   | d
 <| S(   sl  
        Generalized Poisson model Hessian matrix of the loglikelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (k_vars, k_vars)
            The Hessian, second derivative of loglikelihood function,
            evaluated at `params`
        iNi   i   i   R   i    t   kg       @(   ii(   R/  R=   R   R-  R;   R%   RV   R   R1  R$   t   emptyR   Rs   t   triu_indicesR   (   R:   RK   R[   R0  R;   R)   R2  R3  R4  R5  RT  RU  t   a5RV  t   dimt   hess_arrR   R   t   tri_idxt   dldpdat   dldada(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRq   0  sL    	

		

7,	#R   c   
      C   se  | d	 k r |  j } n  | d	 k r9 t |  d d  } n | d k rW t j |  } n  | d	 k rx t |  d d  } n  t j | | | j d   } | | | } | d k r t j |  S| d k r | S| d k rUt j t j	 d t j
 |  j  d   } |  j | d | d | d | d	 d	  d	 f }	 t j | |	 | d
 |  j d  St d   d	 S(   s   
        Predict response variable of a count model given exogenous variables.

        Notes
        -----
        If exposure is specified, then it will be logged by the method.
        The user does not need to log it first.
        R   i    R   i   R   R   t   probR;   Nis   keyword 'which' not recognized(   RV   R;   R   R=   R   RI   R$   R   RS  t   arangeRF  R%   R   R   t   pmfR-  R3   (
   R:   RK   R;   R   R   t   whichR   R   t   countsR2  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   {  s*    
+N(   R   R   R   R)  R*  R   RV   R8   R   R   R   R   R,  R6  R   RW   R6   Ro   R&  R%  RZ  Rq   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s2   				&			8 			 	K	c           B   s   e  Z d  i e j d 6e j d 6Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d d
 d d d d d  Z e j j e _ RS(   s   
    Binary choice logit model

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    RK   R   c         C   s%   t  j |  } d d t  j |  S(   s  
        The logistic cumulative distribution function

        Parameters
        ----------
        X : array-like
            `X` is the linear predictor of the logit model.  See notes.

        Returns
        -------
        1/(1 + exp(-X))

        Notes
        -----
        In the logit model,

        .. math:: \Lambda\left(x^{\prime}\beta\right)=
                  \text{Prob}\left(Y=1|x\right)=
                  \frac{e^{x^{\prime}\beta}}{1+e^{x^{\prime}\beta}}

        i   (   R=   R   R   (   R:   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRG     s    c         C   s3   t  j |  } t  j |  d t  j |  d S(   s2  
        The logistic probability density function

        Parameters
        ----------
        X : array-like
            `X` is the linear predictor of the logit model.  See notes.

        Returns
        -------
        pdf : ndarray
            The value of the Logit probability mass function, PMF, for each
            point of X. ``np.exp(-x)/(1+np.exp(-X))**2``

        Notes
        -----
        In the logit model,

        .. math:: \lambda\left(x^{\prime}\beta\right)=\frac{e^{-x^{\prime}\beta}}{\left(1+e^{-x^{\prime}\beta}\right)^{2}}
        i   i   (   R=   R   R   (   R:   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRH     s    c         C   sI   d |  j  d } |  j } t j t j |  j | t j | |     S(   s?  
        Log-likelihood of logit model.

        Parameters
        ----------
        params : array-like
            The parameters of the logit model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.

        Notes
        -----
        .. math::

           \ln L=\sum_{i}\ln\Lambda
           \left(q_{i}x_{i}^{\prime}\beta\right)

        Where :math:`q=2y-1`. This simplification comes from the fact that the
        logistic distribution is symmetric.
        i   i   (   R%   R;   R=   Rs   R   RG   RI   (   R:   RK   R   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	c         C   s@   d |  j  d } |  j } t j |  j | t j | |    S(   s  
        Log-likelihood of logit model for each observation.

        Parameters
        ----------
        params : array-like
            The parameters of the logit model.

        Returns
        -------
        loglike : ndarray
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes

        Notes
        -----
        .. math::

           \ln L=\sum_{i}\ln\Lambda
           \left(q_{i}x_{i}^{\prime}\beta\right)

        for observations :math:`i=1,...,n`

        where :math:`q=2y-1`. This simplification comes from the fact that the
        logistic distribution is symmetric.
        i   i   (   R%   R;   R=   R   RG   RI   (   R:   RK   R   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	c         C   sA   |  j  } |  j } |  j t j | |   } t j | | |  S(   s  
        Logit model score (gradient) vector of the log-likelihood

        Parameters
        ----------
        params: array-like
            The parameters of the model

        Returns
        -------
        score : ndarray, 1-D
            The score vector of the model, i.e. the first derivative of the
            loglikelihood function, evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial\ln L}{\partial\beta}=\sum_{i=1}^{n}\left(y_{i}-\Lambda_{i}\right)x_{i}
        (   R%   R;   RG   R=   RI   (   R:   RK   R)   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR%    s    		c         C   sI   |  j  } |  j } |  j t j | |   } | | d d  d f | S(   s  
        Logit model Jacobian of the log-likelihood for each observation

        Parameters
        ----------
        params: array-like
            The parameters of the model

        Returns
        -------
        jac : array-like
            The derivative of the loglikelihood for each observation evaluated
            at `params`.

        Notes
        -----
        .. math:: \frac{\partial\ln L_{i}}{\partial\beta}=\left(y_{i}-\Lambda_{i}\right)x_{i}

        for observations :math:`i=1,...,n`

        N(   R%   R;   RG   R=   RI   RV   (   R:   RK   R)   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR&  7  s    		c         C   sD   |  j  } |  j t j | |   } t j | d | | j |  S(   s  
        Logit model Hessian matrix of the log-likelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (k_vars, k_vars)
            The Hessian, second derivative of loglikelihood function,
            evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial^{2}\ln L}{\partial\beta\partial\beta^{\prime}}=-\sum_{i}\Lambda_{i}\left(1-\Lambda_{i}\right)x_{i}x_{i}^{\prime}
        i   (   R;   RG   R=   RI   R   (   R:   RK   RF   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRq   S  s    	RP   i#   i   c   
      K   sU   t  t |   j d | d | d | d | d | d | |  } t |  |  }	 t |	  S(   NRQ   R2   RR   RS   RT   RU   (   R7   R   RW   t   LogitResultst   BinaryResultsWrapper(
   R:   RQ   R2   RR   RS   RT   RU   R<   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW   j  s
    N(   R   R   R   R)  R*  R   RG   RH   R   R   R%  R&  Rq   RV   RW   R6   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   								c           B   s   e  Z d  i e j d 6e j d 6Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d d
 d d d d d  Z e j j e _ RS(   s   
    Binary choice Probit model

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    RK   R   c         C   s   t  j j |  S(   sn  
        Probit (Normal) cumulative distribution function

        Parameters
        ----------
        X : array-like
            The linear predictor of the model (XB).

        Returns
        -------
        cdf : ndarray
            The cdf evaluated at `X`.

        Notes
        -----
        This function is just an alias for scipy.stats.norm.cdf
        (   R   t   normt   _cdf(   R:   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRG     s    c         C   s   t  j |  } t j j |  S(   s  
        Probit (Normal) probability density function

        Parameters
        ----------
        X : array-like
            The linear predictor of the model (XB).

        Returns
        -------
        pdf : ndarray
            The value of the normal density function for each point of X.

        Notes
        -----
        This function is just an alias for scipy.stats.norm.pdf

        (   R=   R   R   Rk  t   _pdf(   R:   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRH     s    c         C   sX   d |  j  d } |  j } t j t j t j |  j | t j | |   t d    S(   s=  
        Log-likelihood of probit model (i.e., the normal distribution).

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.

        Notes
        -----
        .. math:: \ln L=\sum_{i}\ln\Phi\left(q_{i}x_{i}^{\prime}\beta\right)

        Where :math:`q=2y-1`. This simplification comes from the fact that the
        normal distribution is symmetric.
        i   i   (	   R%   R;   R=   Rs   R   t   clipRG   RI   t	   FLOAT_EPS(   R:   RK   R   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	.c         C   sO   d |  j  d } |  j } t j t j |  j | t j | |   t d   S(   si  
        Log-likelihood of probit model for each observation

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : array-like
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes

        Notes
        -----
        .. math:: \ln L_{i}=\ln\Phi\left(q_{i}x_{i}^{\prime}\beta\right)

        for observations :math:`i=1,...,n`

        where :math:`q=2y-1`. This simplification comes from the fact that the
        normal distribution is symmetric.
        i   i   (   R%   R;   R=   R   Rn  RG   RI   Ro  (   R:   RK   R   RF   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	c         C   s}   |  j  } |  j } t j | |  } d | d } | |  j | |  t j |  j | |  t d t  } t j | |  S(   s  
        Probit model score (gradient) vector

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        score : ndarray, 1-D
            The score vector of the model, i.e. the first derivative of the
            loglikelihood function, evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial\ln L}{\partial\beta}=\sum_{i=1}^{n}\left[\frac{q_{i}\phi\left(q_{i}x_{i}^{\prime}\beta\right)}{\Phi\left(q_{i}x_{i}^{\prime}\beta\right)}\right]x_{i}

        Where :math:`q=2y-1`. This simplification comes from the fact that the
        normal distribution is symmetric.
        i   i   (   R%   R;   R=   RI   RH   Rn  RG   Ro  (   R:   RK   R)   RF   R   R   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR%    s    		;c         C   s   |  j  } |  j } t j | |  } d | d } | |  j | |  t j |  j | |  t d t  } | d d  d f | S(   s  
        Probit model Jacobian for each observation

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        jac : array-like
            The derivative of the loglikelihood for each observation evaluated
            at `params`.

        Notes
        -----
        .. math:: \frac{\partial\ln L_{i}}{\partial\beta}=\left[\frac{q_{i}\phi\left(q_{i}x_{i}^{\prime}\beta\right)}{\Phi\left(q_{i}x_{i}^{\prime}\beta\right)}\right]x_{i}

        for observations :math:`i=1,...,n`

        Where :math:`q=2y-1`. This simplification comes from the fact that the
        normal distribution is symmetric.
        i   i   N(	   R%   R;   R=   RI   RH   Rn  RG   Ro  RV   (   R:   RK   R)   RF   R   R   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR&    s    		;c         C   st   |  j  } t j | |  } d |  j d } | |  j | |  |  j | |  } t j | | | | j |  S(   s  
        Probit model Hessian matrix of the log-likelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (k_vars, k_vars)
            The Hessian, second derivative of loglikelihood function,
            evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial^{2}\ln L}{\partial\beta\partial\beta^{\prime}}=-\lambda_{i}\left(\lambda_{i}+x_{i}^{\prime}\beta\right)x_{i}x_{i}^{\prime}

        where

        .. math:: \lambda_{i}=\frac{q_{i}\phi\left(q_{i}x_{i}^{\prime}\beta\right)}{\Phi\left(q_{i}x_{i}^{\prime}\beta\right)}

        and :math:`q=2y-1`
        i   i   (   R;   R=   RI   R%   RH   RG   R   (   R:   RK   RF   R   R   R$  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRq   '  s
    	(RP   i#   i   c   
      K   sU   t  t |   j d | d | d | d | d | d | |  } t |  |  }	 t |	  S(   NRQ   R2   RR   RS   RT   RU   (   R7   R   RW   t   ProbitResultsRj  (
   R:   RQ   R2   RR   RS   RT   RU   R<   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW   F  s
    N(   R   R   R   R)  R*  R   RG   RH   R   R   R%  R&  Rq   RV   RW   R6   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   t  s   						 		c           B   sd   e  Z d  i e j d 6Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z RS(
   s  
    Multinomial logit model

    Parameters
    ----------
    endog : array-like
        `endog` is an 1-d vector of the endogenous response.  `endog` can
        contain strings, ints, or floats.  Note that if it contains strings,
        every distinct string will be a category.  No stripping of whitespace
        is done.
    exog : array-like
        A nobs x k array where `nobs` is the number of observations and `k`
        is the number of regressors. An intercept is not included by default
        and should be added by the user. See `statsmodels.tools.add_constant`.
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    J : float
        The number of choices for the endogenous variable. Note that this
        is zero-indexed.
    K : float
        The actual number of parameters for the exogenous design.  Includes
        the constant if the design has one.
    names : dict
        A dictionary mapping the column number in `wendog` to the variables
        in `endog`.
    wendog : array
        An n x j array where j is the number of unique categories in `endog`.
        Each column of j is a dummy variable indicating the category of
        each observation. See `names` for a dictionary mapping each column to
        its category.

    Notes
    -----
    See developer notes for further information on `MNLogit` internals.
    R   c         C   s
   t   d S(   s    
        NotImplemented
        N(   RE   (   R:   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRH   z  s    c         C   sN   t  j t  j t |   t  j |  f  } | | j d  d d  d f S(   s  
        Multinomial logit cumulative distribution function.

        Parameters
        ----------
        X : array
            The linear predictor of the model XB.

        Returns
        -------
        cdf : ndarray
            The cdf evaluated at `X`.

        Notes
        -----
        In the multinomial logit model.
        .. math:: \frac{\exp\left(\beta_{j}^{\prime}x_{i}\right)}{\sum_{k=0}^{J}\exp\left(\beta_{k}^{\prime}x_{i}\right)}
        i   N(   R=   R   Rw   R   R   Rs   RV   (   R:   RF   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRG     s    -c         C   s\   | j  |  j d d d } |  j } t j |  j t j |  j |    } t j | |  S(   s  
        Log-likelihood of the multinomial logit model.

        Parameters
        ----------
        params : array-like
            The parameters of the multinomial logit model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.

        Notes
        -----
        .. math::

           \ln L=\sum_{i=1}^{n}\sum_{j=0}^{J}d_{ij}\ln
           \left(\frac{\exp\left(\beta_{j}^{\prime}x_{i}\right)}
           {\sum_{k=0}^{J}
           \exp\left(\beta_{k}^{\prime}x_{i}\right)}\right)

        where :math:`d_{ij}=1` if individual `i` chose alternative `j` and 0
        if not.
        iR   R   (	   R   R   R   R=   R   RG   RI   R;   Rs   (   R:   RK   t   dt   logprob(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	'c         C   sS   | j  |  j d d d } |  j } t j |  j t j |  j |    } | | S(   s  
        Log-likelihood of the multinomial logit model for each observation.

        Parameters
        ----------
        params : array-like
            The parameters of the multinomial logit model.

        Returns
        -------
        loglike : array-like
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes

        Notes
        -----
        .. math::

           \ln L_{i}=\sum_{j=0}^{J}d_{ij}\ln
           \left(\frac{\exp\left(\beta_{j}^{\prime}x_{i}\right)}
           {\sum_{k=0}^{J}
           \exp\left(\beta_{k}^{\prime}x_{i}\right)}\right)

        for observations :math:`i=1,...,n`

        where :math:`d_{ij}=1` if individual `i` chose alternative `j` and 0
        if not.
        iR   R   (   R   R   R   R=   R   RG   RI   R;   (   R:   RK   Rq  Rr  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	'c         C   s   | j  |  j d d d } |  j d d  d d  f |  j t j |  j |   d d  d d  f } t j | j |  j  j   S(   s6  
        Score matrix for multinomial logit model log-likelihood

        Parameters
        ----------
        params : array
            The parameters of the multinomial logit model.

        Returns
        -------
        score : ndarray, (K * (J-1),)
            The 2-d score vector, i.e. the first derivative of the
            loglikelihood function, of the multinomial logit model evaluated at
            `params`.

        Notes
        -----
        .. math:: \frac{\partial\ln L}{\partial\beta_{j}}=\sum_{i}\left(d_{ij}-\frac{\exp\left(\beta_{j}^{\prime}x_{i}\right)}{\sum_{k=0}^{J}\exp\left(\beta_{k}^{\prime}x_{i}\right)}\right)x_{i}

        for :math:`j=1,...,J`

        In the multinomial model the score matrix is K x J-1 but is returned
        as a flattened array to work with the solvers.
        iR   R   Ni   (	   R   R   R   RG   R=   RI   R;   R   t   flatten(   R:   RK   t	   firstterm(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR%    s    .#c         C   s   | j  |  j d d d } |  j t j |  j |   } t j |  j t j |   } |  j d d  d d  f | d d  d d  f } t j | j	 |  j  j
   } | | f S(   s   
        Returns log likelihood and score, efficiently reusing calculations.

        Note that both of these returned quantities will need to be negated
        before being minimized by the maximum likelihood fitting machinery.

        iR   R   Ni   (   R   R   RG   R=   RI   R;   Rs   R   R   R   Rs  (   R:   RK   t   cdf_dot_exog_paramst   loglike_valueRt  t   score_array(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   loglike_and_score  s    9c         C   s   | j  |  j d d d } |  j d d  d d  f |  j t j |  j |   d d  d d  f } | d d  d d  d f |  j d d  d d d  f j  |  j j d d  S(   sZ  
        Jacobian matrix for multinomial logit model log-likelihood

        Parameters
        ----------
        params : array
            The parameters of the multinomial logit model.

        Returns
        -------
        jac : array-like
            The derivative of the loglikelihood for each observation evaluated
            at `params` .

        Notes
        -----
        .. math:: \frac{\partial\ln L_{i}}{\partial\beta_{j}}=\left(d_{ij}-\frac{\exp\left(\beta_{j}^{\prime}x_{i}\right)}{\sum_{k=0}^{J}\exp\left(\beta_{k}^{\prime}x_{i}\right)}\right)x_{i}

        for :math:`j=1,...,J`, for observations :math:`i=1,...,n`

        In the multinomial model the score vector is K x (J-1) but is returned
        as a flattened array. The Jacobian has the observations in rows and
        the flatteded array of derivatives in columns.
        iR   R   Ni   i    (	   R   R   R   RG   R=   RI   R;   RV   R$   (   R:   RK   Rt  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR&  	  s    .#c   
   
   C   s  | j  |  j d d d } |  j } |  j t j | |   } g  } |  j } |  j } xt | d  D] } x t | d  D] } | | k r | j t j | d d  | d f d | d d  | d f d d  d f | j
 |   q | j t j | d d  | d f | d d  | d f d d  d f | j
 |   q Wqh Wt j |  }	 t j |	 j  | d | d | |  d	  j  | d | | d |  }	 |	 S(
   s  
        Multinomial logit Hessian matrix of the log-likelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hess : ndarray, (J*K, J*K)
            The Hessian, second derivative of loglikelihood function with
            respect to the flattened parameters, evaluated at `params`

        Notes
        -----
        .. math:: \frac{\partial^{2}\ln L}{\partial\beta_{j}\partial\beta_{l}}=-\sum_{i=1}^{n}\frac{\exp\left(\beta_{j}^{\prime}x_{i}\right)}{\sum_{k=0}^{J}\exp\left(\beta_{k}^{\prime}x_{i}\right)}\left[\boldsymbol{1}\left(j=l\right)-\frac{\exp\left(\beta_{l}^{\prime}x_{i}\right)}{\sum_{k=0}^{J}\exp\left(\beta_{k}^{\prime}x_{i}\right)}\right]x_{i}x_{l}^{\prime}

        where
        :math:`\boldsymbol{1}\left(j=l\right)` equals 1 if `j` = `l` and 0
        otherwise.

        The actual Hessian matrix has J**2 * K x K elements. Our Hessian
        is reshaped to be square (J*K, J*K) so that the solvers can use it.

        This implementation does not take advantage of the symmetry of
        the Hessian and could probably be refactored for speed.
        iR   R   i   Ni    i   i   (   i    i   i   i   (   R   R   R;   RG   R=   RI   R   R   R.  RV   R   Rf   R   (
   R:   RK   RF   t   prt   partialsR   R   R   R   R{   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRq   %	  s    			^fH(   R   R   R   R*  R   RH   RG   R   R   R%  Rx  R&  Rq   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   O  s   )			 	"			c           B   s8  e  Z d  i e j d 6d e j d 6Z d d% d% d d  Z d   Z d   Z	 d	   Z
 d
 d  Z d   Z d   Z d   Z d   Z d   Z d
 d  Z d   Z d   Z d   Z d   Z d   Z d   Z e e _ d% d  Z d% d d d d d% d d% d% d 	 Z d% d d d d d% d
 d  d! d" d# d$  Z RS(&   s  
    Negative Binomial Model for count data

    %(params)s
    %(extra_params)s

    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.

    References
    ----------
    Greene, W. 2008. "Functional forms for the negtive binomial model
        for count data". Economics Letters. Volume 99, Number 3, pp.585-590.
    Hilbe, J.M. 2011. "Negative binomial regression". Cambridge University
        Press.
    RK   sB  loglike_method : string
        Log-likelihood type. 'nb2','nb1', or 'geometric'.
        Fitted value :math:`\mu`
        Heterogeneity parameter :math:`\alpha`

        - nb2: Variance equal to :math:`\mu + \alpha\mu^2` (most common)
        - nb1: Variance equal to :math:`\mu + \alpha\mu`
        - geometric: Variance equal to :math:`\mu + \mu^2`
    offset : array_like
        Offset is added to the linear prediction with coefficient equal to 1.
    exposure : array_like
        Log(exposure) is added to the linear prediction with coefficient
        equal to 1.

    R   t   nb2R   c      
   K   s   t  t |   j | | d | d | d | | | |  _ |  j   | d
 k ri |  j j d  d |  _ n	 d |  _ |  j j d	  d  S(   NR   R   R   R{  t   nb1R[   i   i    t   loglike_method(   R{  R|  (	   R7   R   R8   R}  t   _initializeR  R.  R   t
   _init_keys(   R:   R%   R;   R}  R   R   R   R<   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8   	  s    
	
	c         C   s   |  j  d k r? |  j |  _ |  j |  _ |  j |  _ t |  _ n |  j  d k r~ |  j	 |  _ |  j
 |  _ |  j |  _ t |  _ nB |  j  d k r |  j |  _ |  j |  _ |  j |  _ n t d   d  S(   NR{  R|  t	   geometrics0   Likelihood type must "nb1", "nb2" or "geometric"(   R}  t   _hessian_nb2Rq   t   _score_nbinR%  t   _ll_nb2R   R"   R/  t   _hessian_nb1t
   _score_nb1t   _ll_nb1t   _hessian_geomt   _score_geomt   _ll_geometricR3   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR~  	  s    c         C   s(   |  j  j   } | d =| d =| d =| S(   NRq   R%  R   (   R   t   copy(   R:   t   odict(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   __getstate__	  s
    c         C   s   |  j  j |  |  j   d  S(   N(   R   RE  R~  (   R:   t   indict(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   __setstate__	  s    i    c         C   s   t  j t  j |   s' t  j |  r0 t } n t } |  j } |  j |  } d | | | } | | | } | | |  | | d  | |  }	 |	 | t  j |  | t  j d |  }
 |
 S(   Ni   (   R=   t   anyt	   iscomplexR	   R   R%   R   R   (   R:   RK   R[   t   Qt   gamma_lnR%   R2  RD  Rd  t   coefft   llf(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _ll_nbin	  s    '		,c         C   sC   |  j  r t j | d  } n
 | d } |  j | d  | d d S(   NiR  i    (   R/  R=   R   R  (   R:   RK   R[   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  	  s    	
c         C   sC   |  j  r t j | d  } n
 | d } |  j | d  | d d S(   NiR  i   (   R/  R=   R   R  (   R:   RK   R[   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  	  s    	
c         C   s   |  j  | d d  S(   Ni   i    (   R  (   R:   RK   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  
  s    c         C   s   t  j |  j |   } | S(   s  
        Loglikelihood for negative binomial model

        Parameters
        ----------
        params : array-like
            The parameters of the model. If `loglike_method` is nb1 or
            nb2, then the ancillary parameter is expected to be the
            last element.

        Returns
        -------
        llf : float
            The loglikelihood value at `params`

        Notes
        -----
        Following notation in Greene (2008), with negative binomial
        heterogeneity parameter :math:`\alpha`:

        .. math::

           \lambda_i &= exp(X\beta) \\
           \theta &= 1 / \alpha \\
           g_i &= \theta \lambda_i^Q \\
           w_i &= g_i/(g_i + \lambda_i) \\
           r_i &= \theta / (\theta+\lambda_i) \\
           ln \mathcal{L}_i &= ln \Gamma(y_i+g_i) - ln \Gamma(1+y_i) + g_iln (r_i) + y_i ln(1-r_i)

        where :math`Q=0` for NB2 and geometric and :math:`Q=1` for NB1.
        For the geometric, :math:`\alpha=0` as well.

        (   R=   Rs   R   (   R:   RK   R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   
  s    "c         C   sd   |  j  } |  j d  d   d  f } |  j |  d  d   d  f } | | | | d } | j d  S(   Ni   i    (   R;   R%   RV   R   Rs   (   R:   RK   R;   R)   R2  RX  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  *
  s
    	c         C   s  |  j  r t j | d  } n
 | d } | d  } |  j } |  j d d  d f } |  j |  d d  d f } d | | | } | | | } | d k r*t | |  t |  }	 | | t j |  |	 }
 | | | t j |  | |	 d | t j |  |	 | d | d j	   } n | d k rt | |  t |  }	 | | | | | | }
 | d } |	 t j |  t j | |  | | | | j	   | } n  |  j  rt j
 |
 j	 d  | | f St j
 |
 j	 d  | f Sd S(   s,   
        Score vector for NB2 model
        iNi   i   i    i(   R/  R=   R   R;   R%   RV   R   R   R   Rs   t   r_(   R:   RK   R  R[   R;   R)   R2  R4  Rd  t   dgpartRX  RW  t   da1(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  1
  s4    	

	A	c         C   s   |  j  | d d S(   NR  i   (   R  (   R:   RK   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  W
  s    c         C   s5  |  j  } |  j d  d   d  f } |  j |  d  d   d  f } | j d } t j | | f  } | d | | d d } x t |  D]} } xt t |  D]f }	 |	 | k r q n  t j | d  d   | d  f | d  d   |	 d  f | d d | | |	 f <q Wq Wt j	 | d d }
 | j
 |
 | |
 <| S(   Ni   i   R   i    R[  (   R;   R%   RV   R   R$   R=   R\  R   Rs   R]  R   (   R:   RK   R;   R)   R2  R_  R`  t	   const_arrR   R   Ra  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  Z
  s    	4"c      	   C   sS  |  j  r t j | d  } n
 | d } | d  } |  j } |  j d d  d f } |  j |  d d  d f } | | } t | |  t |  } d d | } | j d }	 t j	 |	 d |	 d f  }
 | | t j
 |  | } | | } | | } t j d | |  t j d |  } x t |	  D] } x t |	  D] } | | k r]qEn  t j | d d  | d f | d d  | d f | d d  | d f | d d  | d f | d d |
 | | f <qEWq2Wt j |	 d d } |
 j | |
 | <| d } t j | | | | | | | d | d d } | |
 d d d  f <| |
 d d  d f <t j
 |  } | d	 } | d } | d } | | d | d | d	 d | | d
 | | | | | d | | d | | | | | | | | d | | | | | d
 | d | d } | j   |
 d <|
 S(   s'   
        Hessian of NB1 model.
        iNi   R   i    R[  ii   i   i   (   ii(   R/  R=   R   R;   R%   RV   R   R   R$   R\  R   R   R   R   Rs   R]  R   (   R:   RK   R[   R;   R)   R2  R4  R  Rd  R_  R`  RX  RV  t	   xmu_alphat   trigammaR   R   Ra  R  Rb  t	   log_alphat   alpha3t   alpha2t   mu2t   dada(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  n
  sR    	

	


3-#"


oc         C   s  |  j  r t j | d  } n
 | d } d | } | d  } |  j } |  j d d  d f } |  j |  d d  d f } | | | } t | |  t |  } | j d }	 t j	 |	 d |	 d f  }
 | | | | | | d } x t
 |	  D]} } xt t
 |	  D]f } | | k r&qn  t j | d d  | d f | d d  | d f | d d |
 | | f <qWq Wt j |	 d d } |
 j | |
 | <| d } t j | | | | | d | | d d d } | |
 d d d  f <| |
 d d  d f <d | d	 } | | t j |  | | | | } | | | | d t j d | |  t j d |  d | d | | | | | | d j   } | |
 d
 <|
 S(   s'   
        Hessian of NB2 model.
        ii   Ni   R   i    R[  ii(   ii(   R/  R=   R   R;   R%   RV   R   R   R$   R\  R   Rs   R]  R   R   R   R   (   R:   RK   R[   R4  R;   R)   R2  Rd  R  R_  R`  R  R   R   Ra  R  Rb  t   da2RW  R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  
  sB    	


	4"6$7
c         C   s   t  | |  j  } | S(   N(   R   R   (   R:   RK   t   sc(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR&  
  s    c         C   s   t  |  d d  } t  |  d d  } |  j t j | |  j   } t j |  g } | t j | |  } |  j | } |  j | | d | j d d } | j |  t j	 |  S(   NR   i    R   RB   i   (
   R   R%   R=   R   R   R   R6  R$   R.  Rf   (   R:   R   R   R   RK   R2  R7  R8  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   
  s     #c         C   sn   | d  k r | j d } n  |  j d k rN | d | d | j   | } n | d | d j   | } | S(   Ni    R{  i   i   (   RV   R$   R}  Rs   (   R:   R2  R7  RB   R8  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR6  
  s    #R;  i#   i   R  c
         K   s  t  |  _ |  j j d  r3 | d k r3 t |  _ n |  j j d  rQ t  |  _ n  | d  k rgt |  d d  t |  d d  } t j |  d k r | d k r d  } n  i d d 6t d	 6t  d
 6} | j	 |
 j
 d i    t |  j |  j d | } | j |   } | j } |  j j d  r|  j | j   | j d | j } t j | t d |   } qn> |  j t k rt j | d t } t j | d  | d <n  | d  k rd   } n  t t |   j d | d | d | d | d | d | |
  } |  j j d  rgt  |  _ | d k rFt j | j j d  | j j d <n  t |  | j  } t |  } n | } | d  k ri  } n  | j d | d t d |	 |  | S(   Nt   nbRP   R<  R   i    R   i   RT   R=  R  R>  RB   g?R  ic          W   s   |  S(   N(    (   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     R   RQ   RR   R2   RS   RU   R   R?  R@  (   RP   R<  (   RP   R<  (   R   R/  R}  t
   startswithR"   RV   R   R=   RD  RE  R  R   R%   R;   RW   RK   R6  R   R7  RB   R.  RF  Rf   R   R7   R   R   R  t   NegativeBinomialResultst   NegativeBinomialResultsWrapperRI  (   R:   RQ   R2   RR   RS   RT   RU   R   R  R@  R<   R   R>  RK  RL  R8  RX   t   nbinfitRN  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW   
  sR    		"!	
	!			&	R0   RY   RZ   g{Gz?g-C6?gQ?c         K   s  t  |  |  j j d  rt t j |  d k rt | d k rt |  j j d |  j } | t j |  } d | d <n  |  j r t j |  d k r | d  n | } t	 |  _
 | d  k rt |  d d  t |  d d  } t j |  d k r| d k rd  } n  t |  j |  j d | } | j d | d | d	 | d
 | d d d | d | d | d |	 d |
 d | |  j } |  j j d  rt j | d  } qn  t t |   j d | d | d	 | d
 | d | d | d | d | d |	 d |
 d | |  } t |  |  } t |  S(   NR  i   i    iR   R   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   g?(   R5   R}  R  R=   RD  R;   R$   R   Rw   R   R/  RV   R   R   R%   Ro   RK   R.  R7   R   t   L1NegativeBinomialResultst    L1NegativeBinomialResultsWrapper(   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   RQ  RR  R   RK  R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo   8  s6    
'.	"!		N(   R   R   R   R)  R*  R   RV   R8   R~  R  R  R  R  R  R  R   R  R  R  R  R  R  R&  R   R,  R6  RW   Ro   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   	  s:   							%	&			?	1			
		=c           B   s&  e  Z d  i e j d 6d e j d 6Z d d d d d  Z d   Z d   Z	 d	   Z
 d
   Z d   Z d   Z d   Z e e _ d d  Z d d d d d d e d d d d 
 Z e j e j j 7_ d d d d d d d d d d d d  Z e j j e _ d d d d d  Z d   Z RS(    s  
    Generalized Negative Binomial (NB-P) model for count data
    %(params)s
    %(extra_params)s
    Attributes
    ----------
    endog : array
        A reference to the endogenous response variable
    exog : array
        A reference to the exogenous design.
    p : scalar
        P denotes parameterizations for NB-P regression. p=1 for NB-1 and
        p=2 for NB-2. Default is p=1.
    RK   sY  p: scalar
        P denotes parameterizations for NB regression. p=1 for NB-1 and
        p=2 for NB-2. Default is p=2.
    offset : array_like
        Offset is added to the linear prediction with coefficient equal to 1.
    exposure : array_like
        Log(exposure) is added to the linear prediction with coefficient
        equal to 1.
    R   i   R   c      
   K   s]   t  t |   j | | d | d | d | | | |  _ |  j j d  d |  _ t |  _ d  S(   NR   R   R   R[   i   (	   R7   R   R8   R-  R  R.  R   R   R/  (   R:   R%   R;   R0  R   R   R   R<   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8   }  s    
		c         C   s&   t  t |   j   } |  j | d <| S(   NR0  (   R7   R   R   R-  (   R:   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    c         C   s   t  j |  j |   S(   sU  
        Loglikelihood of Generalized Negative Binomial (NB-P) model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : float
            The log-likelihood function of the model evaluated at `params`.
            See notes.
        (   R=   Rs   R   (   R:   RK   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    c   
      C   s   |  j  r t j | d  } n
 | d } | d  } |  j } |  j } |  j |  } | d | } | | } | | } t | |  t | d  t |  | t j |  | t j |  | | t j |  }	 |	 S(   ss  
        Loglikelihood for observations of Generalized Negative Binomial (NB-P) model

        Parameters
        ----------
        params : array-like
            The parameters of the model.

        Returns
        -------
        loglike : ndarray
            The log likelihood for each observation of the model evaluated
            at `params`. See Notes
        ii   i   (   R/  R=   R   R-  R%   R   R   R   (
   R:   RK   R[   R0  R)   R2  R3  R4  R5  R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s    	

		

Gc         C   s,  |  j  r t j | d  } n
 | d } | d  } d |  j } |  j } |  j |  } | | } | | } | | } | | }	 | | | }
 t |	  t |  } | t j | |  d |	 | } |
 | |	 | | | } |  j j	 | | j	 } | | | } t j
 | t j |  j	 f d d S(   s  
        Generalized Negative Binomial (NB-P) model score (gradient) vector of the log-likelihood for each observations.

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        score : ndarray, 1-D
            The score vector of the model, i.e. the first derivative of the
            loglikelihood function, evaluated at `params`
        ii   i   R   (   R/  R=   R   R-  R%   R   R   R   R;   R   R   RS  (   R:   RK   R[   R0  R)   R2  R3  R4  R5  RT  RU  R  t   dgtermRX  RW  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR&    s(    	

	



#c         C   sI   t  j |  j |  d d } |  j rA | d | d d k | S| Sd S(   s  
        Generalized Negative Binomial (NB-P) model score (gradient) vector of the log-likelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        score : ndarray, 1-D
            The score vector of the model, i.e. the first derivative of the
            loglikelihood function, evaluated at `params`
        R   i    ii   N(   R=   Rs   R&  R/  (   R:   RK   R%  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR%    s
    	c         C   s\  |  j  r t j | d  } n
 | d } | d  } d |  j } |  j } |  j } |  j |  } | | } | | } | | }	 | | }
 | | | } | |	 } t j |  } t |
  t |  } t	 d |
  t	 d |  } | j
 d } t j | d | d f  } | d d | d |
 |	 d |
 |	 | d | | | | d d | d | |	 | | | | | d | | | | d | d | d | |
 |	 | | | | | d | } xp t |  D]b } t j |  j d d  d d  f j |  j d d  | f | d d | | d d  f <qW|  j d d  d d  f j | | d | d |
 |	 |	 | | | d | | | |
 | | |	 | | | j d d  | d d d  f <| d | d | d d |
 |	 | | d | | |
 |	 | d } | j   | d <t j | d d d } | j | | | <| S(	   s:  
        Generalized Negative Binomial (NB-P) model hessian maxtrix of the log-likelihood

        Parameters
        ----------
        params : array-like
            The parameters of the model

        Returns
        -------
        hessian : ndarray, 2-D
            The hessian matrix of the model.
        ii   i   NR   i   R[  (   ii(   R/  R=   R   R-  R%   R;   R   R   R   R   R$   Ru   R   Rs   R   R]  (   R:   RK   R[   R0  R)   R;   R2  R3  R4  R5  RT  RU  Rd  t   lprobR  t   pgpartR_  R`  R  R   R  Ra  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRq     s@    	

		




{`Jc   	      C   s   t  |  d d  } t  |  d d  } |  j d } |  j t j | |  j   } t j |  g } | t j | |  } |  j | } |  j | | d | j d d } | j	 |  t j
 |  S(   NR   i    R   i   RB   (   R   R-  R%   R=   R   R   R   R6  R$   R.  Rf   (	   R:   R   R   R   R   RK   R2  R7  R8  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   M  s     #c         C   sR   |  j  d } | d  k r) | j d } n  | d | d | | j   | } | S(   Ni   i    i   (   R-  RV   R$   Rs   (   R:   R2  R7  RB   R   R8  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR6  ]  s
    %R;  i#   i   R  c         K   s,  | r | d k r t  |  _ n" | r7 t j d t  n  t |  _ | d k rAt |  d d  t |  d d  } t j	 |  d k r | d k r d } n  i d d 6t  d	 6t d
 6} | j
 | j d i    t |  j |  j d | } | j |   } | j } |  j | j   | j d | j } t j | t d |   } n  | d k rYd   } n  t t |   j d | d | d | d | d | d | |  } | r| d k rt |  _ t j | j j d  | j j d <n  t |  | j  } t |  } |	 d k r	i  }	 n  | j d | d t  d |
 |	  | S(   sn  
        use_transparams : bool
            This parameter enable internal transformation to impose
            non-negativity. True to enable. Default is False.
            use_transparams=True imposes the no underdispersion (alpha > 0)
            constaint. In case use_transparams=True and method="newton" or
            "ncg" transformation is ignored.
        RP   R<  s&   Parameter "use_transparams" is ignoredR   i    R   i   RT   R=  R  R>  RB   g?c          W   s   |  S(   N(    (   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     R   RQ   RR   R2   RS   RU   iR   R?  R@  (   RP   R<  N(   RP   R<  (   R"   R/  RA  RB  RC  R   RV   R   R=   RD  RE  R  R   R%   R;   RW   RK   R6  R   R7  RB   R.  RF  R7   R   R   R  R  R  RI  (   R:   RQ   R2   RR   RS   RT   RU   RJ  R   R  R@  R<   R   R>  RK  RL  R8  RX   R  RN  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRW   d  sF    	
	"!	
				&	R0   RY   i    RZ   g{Gz?g-C6?gQ?c         K   s  t  |  t j |  d k rb | d k rb |  j j d |  j } | t j |  } d | d <n  |  j r t j |  d k r | d  n | } t |  _ | d  k rut
 |  d d  t
 |  d d  } t j |  d k r | d k r d  } n  t |  j |  j d | } | j d | d | d | d	 | d
 d d | d | d | d |	 d |
 d | |  j } t j | d  } n  t t |   j d | d | d | d	 | d
 | d | d | d | d |	 d |
 d | |  } t |  |  } t |  S(   Ni   i    iR   R   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   g?(   R5   R=   RD  R;   R$   R   Rw   R   R/  RV   R   R   R%   Ro   RK   R.  R7   R   R  R  (   R:   RQ   R2   RR   RS   RT   RU   R[   R]   R_   R^   R`   R<   RQ  RR  R   RK  R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRo     s2    
!.	"!		R   c         C   sz  | d k r |  j } n  | d k r9 t |  d d  } n | d k rW t j |  } n  | d k rx t |  d d  } n  t j | | | j d   } | | | } | d k r t j |  S| d k r | S| d k rft j t j	 d t j
 |  j  d   } |  j | | | |  }	 |  j | |	  \ }
 } t j | |
 d d  d f | d d  d f  St d	 |   d S(
   s|  
        Predict response variable of a model given exogenous variables.

        Parameters
        ----------
        params : array-like
            2d array of fitted parameters of the model. Should be in the
            order returned from the model.
        exog : array-like, optional
            1d or 2d array of exogenous values.  If not supplied, the
            whole exog attribute of the model is used. If a 1d array is given
            it assumed to be 1 row of exogenous variables. If you only have
            one regressor and would like to do prediction, you must provide
            a 2d array with shape[1] == 1.
        linear : bool, optional
            If True, returns the linear predictor dot(exog,params).  Else,
            returns the value of the cdf at the linear predictor.
        offset : array_like, optional
            Offset is added to the linear prediction with coefficient equal to 1.
        exposure : array_like, optional
            Log(exposure) is added to the linear prediction with coefficient
        equal to 1.
        which : 'mean', 'linear', 'prob', optional.
            'mean' returns the exp of linear predictor exp(dot(exog,params)).
            'linear' returns the linear predictor dot(exog,params).
            'prob' return probabilities for counts from 0 to max(endog).
            Default is 'mean'.

        Notes
        -----
        R   i    R   i   R   R   Rd  Ns#   keyword "which" = %s not recognized(   RV   R;   R   R=   R   RI   R$   R   RS  Re  RF  R%   R   t   convert_paramsR   Rf  R3   (   R:   RK   R;   R   R   Rg  R   R   Rh  R2  RD  Rd  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s(    !+3c         C   sA   | d } d |  j  } d | | | } | | | } | | f S(   Nii   g      ?(   R-  (   R:   RK   R2  R[   R0  RD  Rd  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s
    
N(   R   R   R   R)  R*  R   RV   R8   R   R   R   R&  R%  Rq   R   R,  R6  R   RW   R6   Ro   R   R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   c  s2   
				#	,		K			7"	:t   DiscreteResultsc           B   s  e  Z e i d  d 6d d 6Z d d d d  Z d   Z e d    Z e d    Z	 e d	    Z
 d e d
  Z e d    Z e d    Z e d    Z e d    Z e d    Z d   Z d d d e e d  Z d d d d d d  Z d d d d d d  Z RS(   s;   A results class for the discrete dependent variable models.t   one_line_descriptionR   t
   extra_attrR  c         C   s   | |  _  | j |  _ | j |  _ i  |  _ | j j d |  _ |  j j | j  t	 |  d  s | d  k	 rw | |  _ n  | d k r d |  _ i d d d d 6|  _ q | d  k r i  } n  d d	 l m } | |  d | d
 t | n  d  S(   Ni    R   R  s    Standard Errors assume that the s-   covariance matrix of the errors is correctly s
   specified.t   descriptioni(   t   get_robustcov_resultsR?  (   t   modelRA   RB   t   _cacheR;   R$   t   nobsR   RE  R   RV   R@  R   R  t   statsmodels.base.covtypeR  R"   (   R:   R  RX   R   R  R@  R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8     s&    				c         C   sZ   t  |  d d   } | d  k	 rS d | k r7 d  | d <n  d | k rS d  | d <qS n  |  j S(   Nt   mle_settingsRU   Rd   (   R   RV   R   (   R:   R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  /  s    c         C   s   d |  j  |  j S(   sC   
        McFadden's pseudo-R-squared. `1 - (llf / llnull)`
        i   (   R  t   llnull(   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt	   prsquared9  s    c         C   s   d |  j  |  j S(   sM   
        Likelihood ratio chi-squared statistic; `-2*(llnull - llf)`
        i(   R  R  (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   llr@  s    c         C   s   t  j j j |  j |  j  S(   s   
        The chi-squared probability of getting a log-likelihood ratio
        statistic greater than llr.  llr has a chi-squared distribution
        with degrees of freedom `df_model`.
        (   R   t   distributionst   chi2t   sfR  RA   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt
   llr_pvalueG  s    c         K   s   |  j  j d d  |  j  j d d  |  j  j d d  |  j  j d d  t |  d  rd |  ` n  | d k	 r | |  j  d <n  | |  _ | |  _ d S(   s  set fit options for Null (constant-only) model

        This resets the cache for related attributes which is potentially
        fragile. This only sets the option, the null model is estimated
        when llnull is accessed, if llnull is not yet in cache.

        Parameters
        ----------
        llnull : None or float
            If llnull is not None, then the value will be directly assigned to
            the cached attribute "llnull".
        attach_results : bool
            Sets an internal flag whether the results instance of the null
            model should be attached. By default without calling this method,
            thenull model results are not attached and only the loglikelihood
            value llnull is stored.
        kwds : keyword arguments
            `kwds` are directly used as fit keyword arguments for the null
            model, overriding any provided defaults.

        Returns
        -------
        no returns, modifies attributes of this instance

        R  R  R  R  t   res_nullN(   R  R   RV   R   R  t   _attach_nullmodelt   _optim_kwds_null(   R:   R  t   attach_resultsR   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   set_null_optionsP  s    		c   	      C   s  |  j  } | j   j   } x! t | d g   D] } | | =q. W| j | j t j |  j  |  } t |  d i   j   } d | k r | j	 d  } n$ t
 | d  r | j   } n d } t d d d t d d	 d
 d  } | j |  | r	| j d | |  } nW | j d | d d d t d d	 d
 d  } | j d | j d d d t d d	 d
 d  } t |  d t  t k	 r| |  _ n  | j S(   s:   
        Value of the constant-only loglikelihood
        t   _null_drop_keysR  RQ   R   R2   R;  R  RR   i'  RT   i    R  R  N(   R  R   R  R   R   R%   R=   Rw   R  R   R   R   RV   Rj   R   RE  RW   RK   R  R  (	   R:   R  R   R   t   mod_nullt
   optim_kwdst   sp_nullt   opt_kwdsR  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  y  s2    	$	c         C   s*   t  j |  j j |  j |  j j j d   S(   s&   
        Linear predictor XB.
        i   (   R=   RI   R  R;   RK   R$   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRM     s    c         C   s   |  j  j |  j   S(   sj   
        Respnose residuals. The response residuals are defined as
        `endog - fittedvalues`
        (   R  R%   R   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   resid_response  s    c         C   s   d |  j  |  j d S(   s   
        Akaike information criterion.  `-2*(llf - p)` where `p` is the number
        of regressors including the intercept.
        ii   (   R  RA   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   aic  s    c         C   s&   d |  j  t j |  j  |  j d S(   s   
        Bayesian information criterion. `-2*llf + ln(nobs)*p` where `p` is the
        number of regressors including the intercept.
        ii   (   R  R=   R   R  RA   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   bic  s    c         C   s@   | d  k r |  j j } n  | d  k r6 |  j j } n  | | f S(   N(   RV   R  t   endog_names(   R:   R.   t
   yname_list(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _get_endog_name  s
    t   overallR   c         C   s,   d d l  m } | |  | | | | | f  S(   sH  Get marginal effects of the fitted model.

        Parameters
        ----------
        at : str, optional
            Options are:

            - 'overall', The average of the marginal effects at each
              observation.
            - 'mean', The marginal effects at the mean of each regressor.
            - 'median', The marginal effects at the median of each regressor.
            - 'zero', The marginal effects at zero for each regressor.
            - 'all', The marginal effects at each observation. If `at` is all
              only margeff will be available from the returned object.

            Note that if `exog` is specified, then marginal effects for all
            variables not specified by `exog` are calculated using the `at`
            option.
        method : str, optional
            Options are:

            - 'dydx' - dy/dx - No transformation is made and marginal effects
              are returned.  This is the default.
            - 'eyex' - estimate elasticities of variables in `exog` --
              d(lny)/d(lnx)
            - 'dyex' - estimate semielasticity -- dy/d(lnx)
            - 'eydx' - estimate semeilasticity -- d(lny)/dx

            Note that tranformations are done after each observation is
            calculated.  Semi-elasticities for binary variables are computed
            using the midpoint method. 'dyex' and 'eyex' do not make sense
            for discrete variables.
        atexog : array-like, optional
            Optionally, you can provide the exogenous variables over which to
            get the marginal effects.  This should be a dictionary with the key
            as the zero-indexed column number and the value of the dictionary.
            Default is None for all independent variables less the constant.
        dummy : bool, optional
            If False, treats binary variables (if present) as continuous.  This
            is the default.  Else if True, treats binary variables as
            changing from 0 to 1.  Note that any variable that is either 0 or 1
            is treated as binary.  Each binary variable is treated separately
            for now.
        count : bool, optional
            If False, treats count variables (if present) as continuous.  This
            is the default.  Else if True, the marginal effect is the
            change in probabilities when each observation is increased by one.

        Returns
        -------
        DiscreteMargins : marginal effects instance
            Returns an object that holds the marginal effects, standard
            errors, confidence intervals, etc. See
            `statsmodels.discrete.discrete_margins.DiscreteMargins` for more
            information.

        Notes
        -----
        When using after Poisson, returns the expected number of events
        per period, assuming that the model is loglinear.
        i(   t   DiscreteMargins(   t%   statsmodels.discrete.discrete_marginsR  (   R:   t   atR2   t   atexogt   dummyt   countR  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   get_margeff  s    ?g?c   
      C   s  d# d |  j j j g f d d g f d$ d% d d |  j d	 g f g } d& d' d( d d |  j g f d) d d |  j g f d d |  j g f g } t |  d  r | j	 d |  j
 g f  n  | d" k r |  j j j d d } n  d d l m } |   }	 |  j | |  \ } } |	 j |  d | d | d | d | d | |	 j |  d | d | d | d |  j t |  d   r|	 j d! g  n  |	 S(*   s  Summarize the Regression Results

        Parameters
        ----------
        yname : string, optional
            Default is `y`
        xname : list of strings, optional
            Default is `var_##` for ## in p the number of regressors
        title : string, optional
            Title for the top table. If not None, then this replaces the
            default title
        alpha : float
            significance level for the confidence intervals

        Returns
        -------
        smry : Summary instance
            this holds the summary tables and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary : class to hold summary results
        s   Dep. Variable:s   Model:s   Method:t   MLEs   Date:s   Time:s
   converged:s   %sR  s   No. Observations:s   Df Residuals:s	   Df Model:s   Pseudo R-squ.:s   %#6.4gs   Log-Likelihood:s   LL-Null:s   %#8.5gs   LLR p-value:R   s   Covariance Type:t    s   Regression Resultsi(   t   Summaryt   gleftt   grightR.   t   xnamet   titleR[   R@  R  s@   Model has been estimated subject to linear equality constraints.N(   s   Dep. Variable:N(   s   Date:N(   s   Time:N(   s   No. Observations:N(   s   Df Residuals:N(   s	   Df Model:N(   s   Log-Likelihood:N(   RV   R  R   R   R  R  R  R  R   R.  R   t   statsmodels.iolib.summaryR  R  t   add_table_2colst   add_table_paramsR@  t   add_extra_txt(
   R:   R.   R  R  R[   R  t   top_leftt	   top_rightR  t   smry(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   summary  s6    	
s   %.4fc         C   sm   d d l  m } | j   } | j d |  d | d | d | d | d |  t |  d	  ri | j d
  n  | S(   s  Experimental function to summarize regression results

        Parameters
        ----------
        xname : List of strings of length equal to the number of parameters
            Names of the independent variables (optional)
        yname : string
            Name of the dependent variable (optional)
        title : string, optional
            Title for the top table. If not None, then this replaces the
            default title
        alpha : float
            significance level for the confidence intervals
        float_format: string
            print format for floats in parameters summary

        Returns
        -------
        smry : Summary instance
            this holds the summary tables and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary2.Summary : class to hold summary results
        i(   t   summary2t   resultsR[   t   float_formatR  R.   R  R  s@   Model has been estimated subject to linear equality constraints.(   t   statsmodels.iolibR  R  t   add_baseR   t   add_text(   R:   R.   R  R  R[   R  R  R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  T  s    N(   R   R   t   _discrete_results_docsR   RV   R8   R  R   R  R  R  R"   R  R  RM   R  R  R  R  R   R  R  R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s,   	
	).		ADR   c           B   s/   e  Z e i d  d 6d d 6Z e d    Z RS(   s   A results class for count dataR  R   R  c         C   s   |  j  j |  j   S(   s   
        Residuals

        Notes
        -----
        The residuals for Count models are defined as

        .. math:: y - p

        where :math:`p = \exp(X\beta)`. Any exposure and offset variables
        are also handled.
        (   R  R%   R   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR7    s    (   R   R   R  R   R   R7  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   |  s   R  c           B   s\   e  Z e i d  d 6d d 6Z e d    Z e d    Z e d    Z e d    Z RS(   s,   A results class for NegativeBinomial 1 and 2R  R   R  c         C   s   t  j |  j d  S(   s   Natural log of alphai(   R=   R   RK   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   lnalpha  s    c         C   s   |  j  d |  j d S(   s!   Natural log of standardized errori(   t   bseRK   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   lnalpha_std_err  s    c         C   s2   t  |  j d d  } d |  j |  j |  j | S(   NR   i    i(   R   R  R  RA   t
   k_constant(   R:   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    c         C   sB   t  |  j d d  } d |  j t j |  j  |  j |  j | S(   NR   i    i(   R   R  R  R=   R   R  RA   R  (   R:   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    (	   R   R   R  R   R   R  R  R  R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   RG  c           B   s/   e  Z e i d  d 6d d 6Z e d    Z RS(   s'   A results class for Generalized PoissonR  R   R  c         C   s<   t  |  j d d  } |  j   } d |  j d | | d S(   NR-  i    i   ii   (   R   R  R   RK   (   R:   R0  R2  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _dispersion_factor  s    (   R   R   R  R   R   R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRG    s   R   c           B   s)   e  Z e i d  d 6e d 6Z d   Z RS(   s7   A results class for count data fit by l1 regularizationR  R  c         C   s   t  t |   j | |  | j d |  _ |  j j   |  _ t |  j d d  } |  j d | |  _	 t
 |  j j j d |  j  | |  _ d  S(   NRp   R   i    i   (   R7   R   R8   R  Rp   Rs   R}   R   R  RA   R@   R%   R$   RB   (   R:   R  R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8     s    (   R   R   R  t   _l1_results_attrR   R8   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   R  c           B   s/   e  Z d d d d e d   Z e d    Z RS(   c         C   s   | d k	 r t j |  } n. t j t j d t j |  j j  d   } |  j d | d | d | d | d t  d d  d f } t	 j
 j | |  S(	   sm  
        Return predicted probability of each count level for each observation

        Parameters
        ----------
        n : array-like or int
            The counts for which you want the probabilities. If n is None
            then the probabilities for each count from 0 to max(y) are
            given.

        Returns
        -------
        ndarray
            A nobs x n array where len(`n`) columns are indexed by the count
            n. If n is None, then column 0 is the probability that each
            observation is 0, column 1 is the probability that each
            observation is 1, etc.
        i    i   R;   R   R   R   R   N(   RV   R=   RS  Re  RF  R  R%   R   R   R   R   Rf  (   R:   t   nR;   R   R   R   Rh  R2  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   predict_prob  s    .c         C   s'   |  j    } |  j j | t j |  S(   sl  
        Pearson residuals

        Notes
        -----
        Pearson residuals are defined to be

        .. math:: r_j = \frac{(y - M_jp_j)}{\sqrt{M_jp_j(1-p_j)}}

        where :math:`p_j=cdf(X\beta)` and :math:`M_j` is the total number of
        observations sharing the covariate pattern :math:`j`.

        For now :math:`M_j` is always set to 1.
        (   R   R  R%   R=   R:  (   R:   R0  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   resid_pearson  s    N(   R   R   RV   R"   R  R+  R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   R  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   R  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   RO  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRO    s   t   OrderedResultsc           B   s    e  Z e i d  d 6d d 6Z RS(   s*   A results class for ordered discrete data.R  R   R  (   R   R   R  R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  
  s   t   BinaryResultsc           B   s   e  Z e i d  d 6d d 6Z d d  Z d d d d d d  Z e j j e _ e d    Z	 e d	    Z
 e d
    Z RS(   s   A results class for binary dataR  R   R  g      ?c         C   se   |  j  } | j } t j |  j   | k d t } t j d d d g  } t j | | d | d S(   s  
        Prediction table

        Parameters
        ----------
        threshold : scalar
            Number between 0 and 1. Threshold above which a prediction is
            considered 1 and below which a prediction is considered 0.

        Notes
        -----
        pred_table[i,j] refers to the number of times "i" was observed and
        the model predicted "j". Correct predictions are along the diagonal.
        R   i    g      ?i   t   bins(   R  R%   R=   Rf   R   R@   t   histogram2d(   R:   t	   thresholdR  t   actualR   R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt
   pred_table  s
    		!g?c         C   s2  t  t |   j | | | | |  } |  j j |  j  } t j |  j j |  } | d k  j	   }	 |	 t
 |  }
 g  } |	 t
 |  k r d } | d 7} | d 7} | d 7} | d 7} | j |  nN |
 d k rd } | d	 |
 7} | d
 7} | d 7} | d 7} | j |  n  | r.| j |  n  | S(   Ng-C6?s3   Complete Separation: The results show that there iss   complete separation.
s3   In this case the Maximum Likelihood Estimator does s   not exist and the parameters
s   are not identified.g?s/   Possibly complete quasi-separation: A fraction s   %4.2f of observations can be
s4   perfectly predicted. This might indicate that there s0   is complete
quasi-separation. In this case some s"   parameters will not be identified.(   R7   R  R  R  RG   RM   R=   R9  R%   Rs   R   R.  R  (   R:   R.   R  R  R[   R  R  RM   t   absprederrort   predclose_sumt   predclose_fract   etextt   wstr(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  &  s0    	






c         C   s   |  j  j } d } |  j   } d | t j d | t j t j d |    | t j d | t j t j |    } | S(   s  
        Deviance residuals

        Notes
        -----
        Deviance residuals are defined

        .. math:: d_j = \pm\left(2\left[Y_j\ln\left(\frac{Y_j}{M_jp_j}\right) + (M_j - Y_j\ln\left(\frac{M_j-Y_j}{M_j(1-p_j)} \right) \right] \right)^{1/2}

        where

        :math:`p_j = cdf(X\beta)` and :math:`M_j` is the total number of
        observations sharing the covariate pattern :math:`j`.

        For now :math:`M_j` is always set to 1.
        i   i   (   R  R%   R   R=   R:  R9  R   (   R:   R%   t   MR0  R#  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt	   resid_devD  s    3.c         C   sC   |  j  j } d } |  j   } | | | t j | | d |  S(   sl  
        Pearson residuals

        Notes
        -----
        Pearson residuals are defined to be

        .. math:: r_j = \frac{(y - M_jp_j)}{\sqrt{M_jp_j(1-p_j)}}

        where :math:`p_j=cdf(X\beta)` and :math:`M_j` is the total number of
        observations sharing the covariate pattern :math:`j`.

        For now :math:`M_j` is always set to 1.
        i   (   R  R%   R   R=   R:  (   R:   R%   R  R0  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  e  s    c         C   s   |  j  j |  j   S(   s   
        The response residuals

        Notes
        -----
        Response residuals are defined to be

        .. math:: y - p

        where :math:`p=cdf(X\beta)`.
        (   R  R%   R   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    N(   R   R   R  R   R  RV   R  R  R   R  R  R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   !Ri  c           B   s/   e  Z e i d  d 6d d 6Z e d    Z RS(   s   A results class for Logit ModelR  R   R  c         C   s   |  j  j |  j   S(   s  
        Generalized residuals

        Notes
        -----
        The generalized residuals for the Logit model are defined

        .. math:: y - p

        where :math:`p=cdf(X\beta)`. This is the same as the `resid_response`
        for the Logit model.
        (   R  R%   R   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   resid_generalized  s    (   R   R   R  R   R   R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRi    s   Rp  c           B   s/   e  Z e i d  d 6d d 6Z e d    Z RS(   s    A results class for Probit ModelR  R   R  c         C   sb   |  j  } | j } |  j d t  } | j |  } | j |  } | | | d | | d | S(   s   
        Generalized residuals

        Notes
        -----
        The generalized residuals for the Probit model are defined

        .. math:: y\frac{\phi(X\beta)}{\Phi(X\beta)}-(1-y)\frac{\phi(X\beta)}{1-\Phi(X\beta)}
        R   i   (   R  R%   R   R"   RH   RG   (   R:   R  R%   R   RH   RG   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    		(   R   R   R  R   R   R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRp    s   R   c           B   s)   e  Z e i d  d 6e d 6Z d   Z RS(   s9   Results instance for binary data fit by l1 regularizationR  R  c         C   ss   t  t |   j | |  | j d |  _ |  j j   |  _ |  j d |  _ t |  j	 j
 j d |  j  |  _ d  S(   NRp   i   i    (   R7   R   R8   R  Rp   Rs   R}   RA   R@   R  R%   R$   RB   (   R:   R  R   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8     s
    (   R   R   R  R  R   R8   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   R   c           B   s   e  Z e i d  d 6d d 6Z d   Z d   Z e d  Z d   Z e	 d    Z
 e	 d	    Z e	 d
    Z d d d  Z d   Z e	 d    Z d d d  Z RS(   s$   A results class for multinomial dataR  R   R  c         C   s5   t  t |   j | |  | j |  _ | j |  _ d  S(   N(   R7   R   R8   R   R   (   R:   R  RX   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8     s    c         C   s   t  } d } x | D]} } yO | | d d k rM t t | |   | | <n t } t | |  | | <Wq t k
 r t | |  | | <q Xq W| r d d  l } | j | t  n  | S(   Ns   endog contains values are that not int-like. Uses string representation of value. Use integer-valued endog to suppress this warning.i   i    i(   R   t   strR   R"   Ri   RA  RB  R   (   R:   R'   t   issue_warningRN   R   RA  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   _maybe_convert_ynames_int  s    c         C   s   |  j  } | d k r! | j } n  | d k r | j } |  j |  } g  t t | j   D] } | | ^ q[ } g  | D] } d j | | g  ^ qx } | s | d } q | } n  | | f S(   sE   
        If all is False, the first variable name is dropped
        t   =i   N(	   R  RV   R  R   R  R   R   R   t   join(   R:   R.   R  R   R  R'   R   R,   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    		,(	c         C   sp   |  j  j d } t j d g t j d | d |  | g f  } t j |  j  j |  j   j d  d | d S(   s   
        Returns the J x J prediction table.

        Notes
        -----
        pred_table[i,j] refers to the number of times "i" was observed and
        the model predicted "j". Correct predictions are along the diagonal.
        i   i    g      ?R  (	   R  R   R=   R   t   linspaceR  R%   R   R   (   R:   t   juR  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    	1$c         C   s7   t  j t  j |  j     } | j |  j j d d S(   NR   R   (   R=   R:  t   diagR   R   RK   R$   (   R:   R  (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    c         C   s    d |  j  |  j |  j j d S(   Nii   (   R  RA   R  R   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    c         C   s0   d |  j  t j |  j  |  j |  j j d S(   Nii   (   R  R=   R   R  RA   R  R   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    g?c         C   s4   t  t |   j d | d |  } | j d d d  S(   NR[   t   colsi   i    i   (   R7   R  t   conf_intR   (   R:   R[   R  t   confint(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s    	c         C   s   t  d   d  S(   Ns   Use get_margeff instead(   RE   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR      s    c         C   s1   |  j  j j d  |  j   j d  k j t  S(   s^  
        Residuals indicating which observations are misclassified.

        Notes
        -----
        The residuals for the multinomial model are defined as

        .. math:: argmax(y_i) \neq argmax(p_i)

        where :math:`argmax(y_i)` is the index of the category for the
        endogenous variable and :math:`argmax(p_i)` is the index of the
        predicted probabilities for each category. That is, the residual
        is a binary indicator that is 0 if the category with the highest
        predicted probability is the same as that of the observed variable
        and 1 otherwise.
        i   (   R  R   R   R   R   R@   (   R:   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   resid_misclassified#  s    s   %.4fc   
   
   C   sg  d d l  m } | j   } | j | j |    |  j j d } |  j |  } xt |  D]} | j	 |  |  j d d  | f |  j
 d d  | f |  j d d  | f |  j d d  | f | | f d | } |  j j d t |  }	 | j | |	 <| j d d  d d d d d	 d
 d g f } | j | d t d t d | | j d |   q^ W| S(   s:  Experimental function to summarize regression results

        Parameters
        ----------
        alpha : float
            significance level for the confidence intervals
        float_format: string
            print format for floats in parameters summary

        Returns
        -------
        smry : Summary instance
            this holds the summary tables and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary2.Summary : class to hold summary results
        i(   R  i   NR[   s    = i    i   i   i   i   t   indext   headerR  R  (   R  R  R  t   add_dictt   summary_modelRK   R$   R  R   t   summary_paramsR  t   tvaluest   pvaluesR  R  R  R  R+   t   add_dfR   R"   t	   add_title(
   R:   R[   R  R  R  t   eqnR  R   R  t	   level_str(    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  9  s&    	.N(   R   R   R  R   R8   R  R   R  R  R   R  R  R  RV   R  R   R  R  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   				R   c           B   s)   e  Z e i d  d 6e d 6Z d   Z RS(   s=   A results class for multinomial data fit by l1 regularizationR  R  c         C   s}   t  t |   j | |  | j d |  _ |  j j   |  _ |  j |  j j d |  _	 t
 |  j j j d |  j  |  _ d  S(   NRp   i   i    (   R7   R   R8   R  Rp   Rs   R}   R  R   RA   R@   R%   R$   RB   (   R:   R  RX   (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR8   i  s
    (   R   R   R  R  R   R8   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   e  s   t   OrderedResultsWrapperc           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR  w  s   R   c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR   {  s   R  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   RH  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRH    s   R  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   R   c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   R  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   R  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR    s   RP  c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRP    s   Rj  c           B   sB   e  Z i d  d 6d  d 6d  d 6d  d 6Z e j e j j e  Z RS(   t   rowsR  R  R  R  (   R   R   t   _attrst   wrapt   union_dictst   lmt   RegressionResultsWrappert   _wrap_attrs(    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyRj    s   

R   c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   R   c           B   s-   e  Z i d  d 6Z e j e j j e  Z RS(   R  R  (   R   R   R  R  R   R!  R"  R#  (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   R   c           B   s   e  Z RS(    (   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyR     s   (o   R   t
   __future__R    t   __all__t   statsmodels.compat.pythonR   t   statsmodels.compat.scipyR	   t   numpyR=   t   pandasR
   t   scipy.specialR   R   R   t   scipyR   R   t   scipy.statsR   t   statsmodels.tools.toolsR    t   statsmodels.toolsR   R   t   statsmodels.tools.decoratorsR   t   statsmodels.tools.sm_exceptionsR   R   t   statsmodels.tools.numdiffR   t   statsmodels.base.modelR   R  t   statsmodels.base.dataR   t#   statsmodels.regression.linear_modelt
   regressiont   linear_modelR!  t   statsmodels.base.wrappert   wrapperR  t   statsmodels.base.l1_slsqpR   t   statsmodels.distributionsR   t   cvxoptR"   Rk   t   ImportErrorR   RA  t   finfoR@   t   epsRo  t   _discrete_models_docsR  R  R,  R(   R/   R5   R   R6   R   R   R   R   R   R   R   R   R   R   R   t   LikelihoodModelResultsR  R   R  RG  R   R  R  R  RO  R  R  Ri  Rp  R   R   R   R"  R  t   populate_wrapperR   R  RH  R  R   R  R  RP  Rj  R   R   R   (    (    (    sB   lib/python2.7/site-packages/statsmodels/discrete/discrete_model.pyt   <module>   s   

			 c   C   p4				