
    M/Ph                       d Z ddlmZ ddlZddlZddlmZ ddlm	c m
Z	 ddlm	c mZ ddlmZ ddlmZ ddlm	c mZ ddlmZmZmZ ddlmc mZ ddlmc mZ  ddl!m"Z"m#Z#m$Z$ dd	l%m&Z& dd
l'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z. ddgZ/d Z0 G d d          Z1 e1            Z2e2j3        Z4 G d de	j5                  Z6 e&ej7        j                   Z8e89                    d            G d de	j:                  Z; G d de j<                  Z= ej>        e=e;           e?dk    rkddl@mAZA  eAjB                    ZC e6eCjD        eCjE                  F                                ZGeGH                    d          ZIeGH                    d          ZJdS dS )a  
Generalized linear models currently supports estimation using the one-parameter
exponential families

References
----------
Gill, Jeff. 2000. Generalized Linear Models: A Unified Approach.
    SAGE QASS Series.

Green, PJ. 1984.  "Iteratively reweighted least squares for maximum
    likelihood estimation, and some robust and resistant alternatives."
    Journal of the Royal Statistical Society, Series B, 46, 149-192.

Hardin, J.W. and Hilbe, J.M. 2007.  "Generalized Linear Models and
    Extensions."  2nd ed.  Stata Press, College Station, TX.

McCullagh, P. and Nelder, J.A.  1989.  "Generalized Linear Models." 2nd ed.
    Chapman & Hall, Boca Rotan.
    )AppenderN)LinAlgError)_prediction_inference)PredictionResultsMean)_plot_added_variable_doc_plot_ceres_residuals_doc_plot_partial_residuals_doc)cache_readonlycached_datacached_value)	Docstring)DomainWarningHessianInversionWarningPerfectSeparationWarning)
float_like   )familiesGLMr   c                 P    t          j        | |         | |dz            ||          S )Nr   )atolrtol)npallclose)	criterion	iterationr   r   s       k/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/genmod/generalized_linear_model.py_check_convergencer   =   s2    ;y+Yy1}-E t- - - -    c                   .    e Zd ZdZed             Zd ZdS )_ModuleVariableNc                     | j         S N)_valueselfs    r   use_bic_llfz_ModuleVariable.use_bic_llfF   s
    {r   c                 \    |dvrt          d          |t          |          n|| _        d S )N)TFNzMust be True, False or None)
ValueErrorboolr#   r%   vals     r   set_use_bic_llfz_ModuleVariable.set_use_bic_llfJ   s6    ))):;;;#&?d3iiir   )__name__
__module____qualname__r#   propertyr&   r,    r   r   r    r    C   sA        F  X< < < < <r   r    c                   t    e Zd Zd                    ej                  ZdZ	 	 	 d6 fd	Zd Z	d Z
 fd	Zd7dZd8dZd8dZd8dZd8dZd9dZd:dZd8dZ	 	 	 d;dZd Z	 	 d<dZd Zd8dZ	 	 d=dZd Zd Zd Zd>d!Z	 	 d?d#Z	 	 d@d$Zd% Z 	 	 	 dAd,Z!	 	 	 	 dB fd.	Z"	 	 	 dCd/Z#	 	 	 dDd3Z$d4 Z%d8d5Z& xZ'S )Er   a'  
    Generalized Linear Models

    GLM inherits from statsmodels.base.model.LikelihoodModel

    Parameters
    ----------
    endog : array_like
        1d array of endogenous response variable.  This array can be 1d or 2d.
        Binomial family models accept a 2d array with two columns. If
        supplied, each observation is expected to be [success, failure].
    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 (models specified using a formula
        include an intercept by default). See `statsmodels.tools.add_constant`.
    family : family class instance
        The default is Gaussian.  To specify the binomial distribution
        family = sm.family.Binomial()
        Each family can take a link instance as an argument.  See
        statsmodels.family.family for more information.
    offset : array_like or None
        An offset to be included in the model.  If provided, must be
        an array whose length is the number of rows in exog.
    exposure : array_like or None
        Log(exposure) will be added to the linear prediction in the model.
        Exposure is only valid if the log link is used. If provided, it must be
        an array with the same length as endog.
    freq_weights : array_like
        1d array of frequency weights. The default is None. If None is selected
        or a blank value, then the algorithm will replace with an array of 1's
        with length equal to the endog.
        WARNING: Using weights is not verified yet for all possible options
        and results, see Notes.
    var_weights : array_like
        1d array of variance (analytic) weights. The default is None. If None
        is selected or a blank value, then the algorithm will replace with an
        array of 1's with length equal to the endog.
        WARNING: Using weights is not verified yet for all possible options
        and results, see Notes.
    {extra_params}

    Attributes
    ----------
    df_model : float
        Model degrees of freedom is equal to p - 1, where p is the number
        of regressors.  Note that the intercept is not reported as a
        degree of freedom.
    df_resid : float
        Residual degrees of freedom is equal to the number of observation n
        minus the number of regressors p.
    endog : ndarray
        See Notes.  Note that `endog` is a reference to the data so that if
        data is already an array and it is changed, then `endog` changes
        as well.
    exposure : array_like
        Include ln(exposure) in model with coefficient constrained to 1. Can
        only be used if the link is the logarithm function.
    exog : ndarray
        See Notes.  Note that `exog` is a reference to the data so that if
        data is already an array and it is changed, then `exog` changes
        as well.
    freq_weights : ndarray
        See Notes. Note that `freq_weights` is a reference to the data so that
        if data is already an array and it is changed, then `freq_weights`
        changes as well.
    var_weights : ndarray
        See Notes. Note that `var_weights` is a reference to the data so that
        if data is already an array and it is changed, then `var_weights`
        changes as well.
    iteration : int
        The number of iterations that fit has run.  Initialized at 0.
    family : family class instance
        The distribution family of the model. Can be any family in
        statsmodels.families.  Default is Gaussian.
    mu : ndarray
        The mean response of the transformed variable.  `mu` is the value of
        the inverse of the link function at lin_pred, where lin_pred is the
        linear predicted value of the WLS fit of the transformed variable.
        `mu` is only available after fit is called.  See
        statsmodels.families.family.fitted of the distribution family for more
        information.
    n_trials : ndarray
        See Notes. Note that `n_trials` is a reference to the data so that if
        data is already an array and it is changed, then `n_trials` changes
        as well. `n_trials` is the number of binomial trials and only available
        with that distribution. See statsmodels.families.Binomial for more
        information.
    normalized_cov_params : ndarray
        The p x p normalized covariance of the design / exogenous data.
        This is approximately equal to (X.T X)^(-1)
    offset : array_like
        Include offset in model with coefficient constrained to 1.
    scale : float
        The estimate of the scale / dispersion of the model fit.  Only
        available after fit is called.  See GLM.fit and GLM.estimate_scale
        for more information.
    scaletype : str
        The scaling used for fitting the model.  This is only available after
        fit is called.  The default is None.  See GLM.fit for more information.
    weights : ndarray
        The value of the weights after the last iteration of fit.  Only
        available after fit is called.  See statsmodels.families.family for
        the specific distribution weighting functions.

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> data = sm.datasets.scotland.load()
    >>> data.exog = sm.add_constant(data.exog)

    Instantiate a gamma family model with the default link function.

    >>> gamma_model = sm.GLM(data.endog, data.exog,
    ...                      family=sm.families.Gamma())

    >>> gamma_results = gamma_model.fit()
    >>> gamma_results.params
    array([-0.01776527,  0.00004962,  0.00203442, -0.00007181,  0.00011185,
           -0.00000015, -0.00051868, -0.00000243])
    >>> gamma_results.scale
    0.0035842831734919055
    >>> gamma_results.deviance
    0.087388516416999198
    >>> gamma_results.pearson_chi2
    0.086022796163805704
    >>> gamma_results.llf
    -83.017202161073527

    See Also
    --------
    statsmodels.genmod.families.family.Family
    :ref:`families`
    :ref:`links`

    Notes
    -----
    Note: PerfectSeparationError exception has been converted to a
    PerfectSeparationWarning and perfect separation or perfect prediction will
    not raise an exception by default. (changed in version 0.14)

    Only the following combinations make sense for family and link:

     ============= ===== === ===== ====== ======= === ==== ====== ====== ====
     Family        ident log logit probit cloglog pow opow nbinom loglog logc
     ============= ===== === ===== ====== ======= === ==== ====== ====== ====
     Gaussian      x     x   x     x      x       x   x     x      x
     inv Gaussian  x     x                        x
     binomial      x     x   x     x      x       x   x           x      x
     Poisson       x     x                        x
     neg binomial  x     x                        x        x
     gamma         x     x                        x
     Tweedie       x     x                        x
     ============= ===== === ===== ====== ======= === ==== ====== ====== ====

    Not all of these link functions are currently available.

    Endog and exog are references so that if the data they refer to are already
    arrays and these arrays are changed, endog and exog will change.

    statsmodels supports two separate definitions of weights: frequency weights
    and variance weights.

    Frequency weights produce the same results as repeating observations by the
    frequencies (if those are integers). Frequency weights will keep the number
    of observations consistent, but the degrees of freedom will change to
    reflect the new weights.

    Variance weights (referred to in other packages as analytic weights) are
    used when ``endog`` represents an an average or mean. This relies on the
    assumption that that the inverse variance scales proportionally to the
    weight--an observation that is deemed more credible should have less
    variance and therefore have more weight. For the ``Poisson`` family--which
    assumes that occurrences scale proportionally with time--a natural practice
    would be to use the amount of time as the variance weight and set ``endog``
    to be a rate (occurrences per period of time). Similarly, using a
    compound Poisson family, namely ``Tweedie``, makes a similar assumption
    about the rate (or frequency) of occurrences having variance proportional to
    time.

    Both frequency and variance weights are verified for all basic results with
    nonrobust or heteroscedasticity robust ``cov_type``. Other robust
    covariance types have not yet been verified, and at least the small sample
    correction is currently not based on the correct total frequency count.

    Currently, all residuals are not weighted by frequency, although they may
    incorporate ``n_trials`` for ``Binomial`` and ``var_weights``

    +---------------+----------------------------------+
    | Residual Type | Applicable weights               |
    +===============+==================================+
    | Anscombe      | ``var_weights``                  |
    +---------------+----------------------------------+
    | Deviance      | ``var_weights``                  |
    +---------------+----------------------------------+
    | Pearson       | ``var_weights`` and ``n_trials`` |
    +---------------+----------------------------------+
    | Reponse       | ``n_trials``                     |
    +---------------+----------------------------------+
    | Working       | ``n_trials``                     |
    +---------------+----------------------------------+

    WARNING: Loglikelihood and deviance are not valid in models where
    scale is equal to 1 (i.e., ``Binomial``, ``NegativeBinomial``, and
    ``Poisson``). If variance weights are specified, then results such as
    ``loglike`` and ``deviance`` are based on a quasi-likelihood
    interpretation. The loglikelihood is not correctly specified in this case,
    and statistics based on it, such AIC or likelihood ratio tests, are not
    appropriate.
    )extra_params   Nnonec	           	      j   t          |           t          u r|                     |	dg           |qt          |j        t          |j                            sJt          j        dt          |j                  j	         dt          |          j	         dt                     |t          j        |          }|t          j        |          }|t          j        |          }|t          j        |          }|| _        || _         t!                      j        ||f|||||d|	 |                     || j        | j        | j        | j        | j                   |t-          | d           |t-          | d           | j        j        d         | _        | j                            g d	           | j                            d
           |                                  d|	v r|	d         | _        d}
t?          | d          r| j        }
t?          | d          r
|
| j        z   }
|
| _         d | _!        d S )Nn_trialszThe z2 link function does not respect the domain of the z family.)missingoffsetexposurefreq_weightsvar_weightsr9   r:   r   )weightsmur;   r<   iweights_offset_exposurer7   family        )"typer   _check_kwargs
isinstancelinktuple
safe_linkswarningswarnr-   r   r   logasarrayr;   r<   super__init___check_inputsr9   r:   endogdelattrshapenobs
_data_attrextend
_init_keysappend_setup_binomialr7   hasattrr@   	scaletype)r%   rP   exogrA   r9   r:   r;   r<   r8   kwargsoffset_exposure	__class__s              r   rN   zGLM.__init__*  s    ::v
|444
6;389J3K3K)M )M M >$v{"3"3"< > >"6ll3> > > () ) )
 vh''HZ''F#:l33L"*[11K(& 	Eg)/(/;.9	E 	E >D	E 	E 	E 	64;tz,d.>	@ 	@ 	@>D(###D*%%%J$Q'	 	  ,  ,  , 	- 	- 	- 	x(((":.DM 4"" 	*"kO4$$ 	>-=O /r   c                    t           j                            | j                  dz
  | _        | j        ]| j        j        d         | j        j        d         k    r7| j                                        | _	        | j	        | j        z
  dz
  | _
        dS | j        j        d         | _	        | j        j        d         | j        z
  dz
  | _
        dS )z8
        Initialize a generalized linear model.
        r   Nr   )r   linalgmatrix_rankr[   df_modelr;   rR   rP   sumwnobsdf_residr$   s    r   
initializezGLM.initializej  s     	--di881<)#A&$**:1*===*..00DJ J6:DMMM+DJ IOA.>BDMMMr   c                 v   |t          j                    }|| _        |ct          | j        j        t           j        j                  st          d          |j        d         |j        d         k    rt          d          |+|j        d         |j        d         k    rt          d          |R|j        d         |j        d         k    rt          d          t          |j                  dk    rt          d          | j
        d u| _        | j
        $t          j        |j        d                   | _
        t          j        | j
                  dk    r7| j
        dk    r,| j
        t          j        |j        d                   z  | _
        |R|j        d         |j        d         k    rt          d	          t          |j                  dk    rt          d
          |d u| _        |$t          j        |j        d                   | _        t          j        | j
        | j        z            | _        d S )N4exposure can only be used with the log link functionr   z(exposure is not the same length as endogz&offset is not the same length as endogz)freq weights not the same length as endogr   z$freq weights has too many dimensionsr1   z(var weights not the same length as endogz#var weights has too many dimensions)r   GaussianrA   rE   rF   linksLogr(   rR   lenr;   _has_freq_weightsr   ones_has_var_weightsr<   rL   r?   )r%   rA   r9   r:   rP   r;   r<   s          r   rO   zGLM._check_inputsx  s    >&((Fdk.0BCC M  "1 2 2 2"ek!n44 !KLLL|A%+a.00 !IJJJ#!!$A66 !LMMM<%&&** !GHHH #'"34"?$ "A 7 7D 8D%&&",,1BQ1F1F!%!2!#Q!8!8"9D " #u{1~55 !KLLL;$%%)) !FGGG "-D!8!wu{1~66D
4#4t7G#GHHr   c                     t                                                      }d|v r%|d         t          j        |d                   |d<   |S )Nr:   )rM   _get_init_kwdsr   exp)r%   kwdsr^   s     r   rq   zGLM._get_init_kwds  sM     ww%%''$z"2">!vd:&677Dr         ?c                 |    t          |d          }| j                            | j        || j        | j        |          S )M
        Evaluate the log-likelihood for a generalized linear model.
        scale)r   rA   loglikerP   r<   r;   )r%   r>   rw   s      r   
loglike_muzGLM.loglike_mu  s@     5'**{""4:r43C#'#4e= = 	=r   c                 4   t          |dd          }t          j        | j        |          | j        z   }| j        j                            |          }||                     |          }| j        	                    | j
        || j        | j        |          }|S )rv   rw   Toptional)r   r   dotr[   r@   rA   rF   inverseestimate_scalerx   rP   r<   r;   )r%   paramsrw   lin_predexpvalllfs         r   rx   zGLM.loglike  s     5'D9996$)V,,t/DD!))(33=''//Ek!!$*fd6F"&"3U< <
r   c                 |    t          |dd          }|                     ||          }|dddf         | j        z  S )ag  score first derivative of the loglikelihood for each observation.

        Parameters
        ----------
        params : ndarray
            Parameter at which score is evaluated.
        scale : None or float
            If scale is None, then the default scale will be calculated.
            Default scale is defined by `self.scaletype` and set in fit.
            If scale is not None, then it is used as a fixed scale.

        Returns
        -------
        score_obs : ndarray, 2d
            The first derivative of the loglikelihood function evaluated at
            params for each observation.
        rw   Tr{   rw   N)r   score_factorr[   r%   r   rw   r   s       r   	score_obszGLM.score_obs  sJ    $ 5'D999((u(==AAAtG$ty00r   c                     t          |dd          }|                     ||          }t          j        || j                  S )aQ  score, first derivative of the loglikelihood function

        Parameters
        ----------
        params : ndarray
            Parameter at which score is evaluated.
        scale : None or float
            If scale is None, then the default scale will be calculated.
            Default scale is defined by `self.scaletype` and set in fit.
            If scale is not None, then it is used as a fixed scale.

        Returns
        -------
        score : ndarray_1d
            The first derivative of the loglikelihood function calculated as
            the sum of `score_obs`
        rw   Tr{   r   )r   r   r   r}   r[   r   s       r   scorez	GLM.score  sD    $ 5'D999((u(==vlDI...r   c                 J   t          |dd          }|                     |          }||                     |          }| j        |z
  | j        j                            |          z  }|| j                            |          z  }|| j        | j	        z  z  }|dk    s||z  }|S )a  weights for score for each observation

        This can be considered as score residuals.

        Parameters
        ----------
        params : ndarray
            parameter at which score is evaluated
        scale : None or float
            If scale is None, then the default scale will be calculated.
            Default scale is defined by `self.scaletype` and set in fit.
            If scale is not None, then it is used as a fixed scale.

        Returns
        -------
        score_factor : ndarray_1d
            A 1d weight vector used in the calculation of the score_obs.
            The score_obs are obtained by `score_factor[:, None] * exog`
        rw   Tr{   Nr   )
r   predictr   rP   rA   rF   derivvariancer?   r7   r%   r   rw   r>   r   s        r   r   zGLM.score_factor  s    ( 5'D999\\&!!=''++E
R4;+;+A+A"+E+EE,,R00055zzE!Lr   Tc                    |                      |          }||                     |          }d| j        j                            |          dz  | j                            |          z  z  }|| j        | j        z  z  }|s|dk    s||z  }|S |                     |d          }|j	        dk    s|j	        dk    rt          d          | j                            |          | j        j                            |          z  }|| j        j                            |          | j        j                            |          z  z  }||z  }|| j        | j        z  z  }|d|z   z  }|j	        dk    rt          d          |dk    s||z  }|S )a  Weights for calculating Hessian

        Parameters
        ----------
        params : ndarray
            parameter at which Hessian is evaluated
        scale : None or float
            If scale is None, then the default scale will be calculated.
            Default scale is defined by `self.scaletype` and set in fit.
            If scale is not None, then it is used as a fixed scale.
        observed : bool
            If True, then the observed Hessian is returned. If false then the
            expected information matrix is returned.

        Returns
        -------
        hessian_factor : ndarray, 1d
            A 1d weight vector used in the calculation of the Hessian.
            The hessian is obtained by `(exog.T * hessian_factor).dot(exog)`
        Nr   r4   rt   r   zsomething wrong)r   r   rA   rF   r   r   r?   r7   r   ndimRuntimeErrorderiv2)	r%   r   rw   observedr>   
eim_factorr   tmp
oim_factors	            r   hessian_factorzGLM.hessian_factor  s   . \\&!!=''++E$+*0044a7+..r223 4
dmdm33
 	A::e#
 ((r(::?Q,"3a"7"70111k""2&&)9)@)@)D)DDt{#))"--0@0F0Fr0J0JJJS t}t},,1s7+
8a<<0111zz%Jr   c                 x   |t          | dd          dk    rd}nd}t          |dd          }t          | dt          j        | j        t
          	                    }|                     |||
          }t          j        | j        j        ||j                   |j        	                    | j                   S )a  Hessian, second derivative of loglikelihood function

        Parameters
        ----------
        params : ndarray
            parameter at which Hessian is evaluated
        scale : None or float
            If scale is None, then the default scale will be calculated.
            Default scale is defined by `self.scaletype` and set in fit.
            If scale is not None, then it is used as a fixed scale.
        observed : bool
            If True, then the observed Hessian is returned (default).
            If False, then the expected information matrix is returned.

        Returns
        -------
        hessian : ndarray
            Hessian, i.e. observed information, or expected information matrix.
        N_optim_hessianeimFTrw   r{   _tmp_like_exogdtyperw   r   )out)
getattrr   r   
empty_liker[   floatr   multiplyTr}   )r%   r   rw   r   r   factors         r   hessianzGLM.hessianM  s    ( t-t44== 5'D999d,bmDIU.S.S.STT$$V58$LL
DIKSU3333		$)$$$$r   c                 V    t          |dd          }|                     ||d          S )z,
        Fisher information matrix.
        rw   Tr{   Fr   )r   r   )r%   r   rw   s      r   informationzGLM.informationm  s0     5'D999||F%%|@@@r   dydxc                    || j         }||t          d          |                     ||d||          }t          | dd          }	|	dk    r|n
|d|	          }
| j        j                            |          dddf         |
z  }d|v r||z  }d|v r.| j        j                            |          }||dddf         z  }|                     ||||||          S )	zS
        Derivative of mean, expected endog with respect to the parameters
        N!offset and exposure not supportedlinear)whichr9   r:   k_extrar   exey)	r[   NotImplementedErrorr   r   rA   rF   inverse_derivr~   _derivative_exog_helper)r%   r   r[   	transform	dummy_idx	count_idxr9   r:   r   r   params_exogmargeffmeans                r   _derivative_exogzGLM._derivative_exogt  s     <9DH$8%&IJJJ<<H'-   B B $	1-- '1ff&7(2C;#11(;;AAAtGD9tOG9;#++H55DtAAAdF|#G++GVT,5y)M M 	Mr   c                 ^    ddl m}m} | |||||| |          }| |||||| |          }|S )zK
        Helper for _derivative_exog to wrap results appropriately
        r   )_get_count_effects_get_dummy_effects)%statsmodels.discrete.discrete_marginsr   r   )	r%   r   r   r[   r   r   r   r   r   s	            r   r   zGLM._derivative_exog_helper  s}    
	 	 	 	 	 	 	 	
  (($	9)-v7 7G (($	9)-v7 7G r   c                 P   || j         }||t          | dd          t          d          |                     ||d          }| j        j                            |          }||dddf         z  }d|v r.| j        j                            |          }	||	dddf         z  }|S )a(  
        Derivative of the expected endog with respect to the parameters.

        Parameters
        ----------
        params : ndarray
            parameter at which score is evaluated
        exog : ndarray or None
            Explanatory variables at which derivative are computed.
            If None, then the estimation exog is used.
        offset, exposure : None
            Not yet implemented.

        Returns
        -------
        The value of the derivative of the expected endog with respect
        to the parameter vector.
        Nr9   r   r   )r[   r   r   )r[   r   r   r   rA   rF   r   r~   )
r%   r   r[   r   r9   r:   r   idldmatr   s
             r   _derivative_predictzGLM._derivative_predict  s    , <9DH$8h--9%&IJJJ<<T<BBk,,X66c!!!T'l"9;#++H55DDDM!Dr   c                     |                      |d          }| j        j                            |          }| j        |dddf         z  }|S )aM  
        Derivative of the expected endog with respect to the parameters.

        Parameters
        ----------
        params : ndarray
            parameter at which score is evaluated

        Returns
        -------
        The value of the derivative of the expected endog with respect
        to the parameter vector.
        r   r   N)r   rA   rF   r   r[   )r%   r   r   r   r   s        r   _deriv_mean_dparamszGLM._deriv_mean_dparams  sN     <<h<77k,,X66y3qqq$w<'r   c                 ^   t          |dd          }|                     |          }||                     |          }d| j        j                            |          z  }|| j                            |          z  }|| j        | j        z  z  }|dk    s||z  }|dddf         | j	        z  S )a  derivative of score_obs w.r.t. endog

        Parameters
        ----------
        params : ndarray
            parameter at which score is evaluated
        scale : None or float
            If scale is None, then the default scale will be calculated.
            Default scale is defined by `self.scaletype` and set in fit.
            If scale is not None, then it is used as a fixed scale.

        Returns
        -------
        derivative : ndarray_2d
            The derivative of the score_obs with respect to endog. This
            can is given by `score_factor0[:, None] * exog` where
            `score_factor0` is the score_factor without the residual.
        rw   Tr{   Nr   )
r   r   r   rA   rF   r   r   r?   r7   r[   r   s        r   _deriv_score_obs_dendogzGLM._deriv_score_obs_dendog  s    & 5'D999\\&!!=''++E4;+11"555,,R00055zzE!LAAAtG$ty00r   c                    |>|t          d          |                     |          }|                     ||          }n|d}t          j        | j        |f          }||j        d         | j        j        d         z
  z  }|                     |          }|dddf         |z                      d          }| 	                    ||          }	t          j
        |j        |	z  |           }ddlm}
 |
                    t          j                            ||dddf                              }|
j                            ||          }|||fS )aW  score test for restrictions or for omitted variables

        The covariance matrix for the score is based on the Hessian, i.e.
        observed information matrix or optionally on the expected information
        matrix..

        Parameters
        ----------
        params_constrained : array_like
            estimated parameter of the restricted model. This can be the
            parameter estimate for the current when testing for omitted
            variables.
        k_constraints : int or None
            Number of constraints that were used in the estimation of params
            restricted relative to the number of exog in the model.
            This must be provided if no exog_extra are given. If exog_extra is
            not None, then k_constraints is assumed to be zero if it is None.
        exog_extra : None or array_like
            Explanatory variables that are jointly tested for inclusion in the
            model, i.e. omitted variables.
        observed : bool
            If True, then the observed Hessian is used in calculating the
            covariance matrix of the score. If false then the expected
            information matrix is used.

        Returns
        -------
        chi2_stat : float
            chisquare statistic for the score test
        p-value : float
            P-value of the score test based on the chisquare distribution.
        df : int
            Degrees of freedom used in the p-value calculation. This is equal
            to the number of constraints.

        Notes
        -----
        not yet verified for case with scale not equal to 1.
        Nz:if exog_extra is None, then k_constraintsneeds to be givenr   r   r   )stats)r(   r   r   r   column_stackr[   rR   r   rc   r   r}   r   scipyr   r`   solvechi2sf)r%   params_constrainedk_constraints
exog_extrar   r   r   r   r   r   r   chi2statpvals                r   
score_testzGLM.score_test  sq   T $  "5 6 6 6 JJ122Ell#5lIIGG $ !$)Z!899BRXa[49?1+===M,,-?@@L!!!!T'*R/44Q77E!001C:B 1 D DNvbd^3R888G IIbiooguQQQW~FFGGGz}}X}55},,r   c           	          |d                              |j                   |d                              | j                            | j        || j        | j        | j                             |S )zG
        Helper method to update history during iterative fit.
        r   deviance)rW   r   rA   r   rP   r<   r;   rw   )r%   
tmp_resultr>   historys       r   _update_historyzGLM._update_historyD  st     	  !2333
""4;#7#7
B8<8H8<8I8<
$D $D 	E 	E 	E r   c                     | j         sMt          | j        t          j        t          j        t          j        f          rdS |                     |          S t          | j         t                    rt          j
        | j                   S t          | j         t                    r| j                                         dk    r|                     |          S | j                                         dk    r5| j                            | j        || j        | j        d          | j        z  S t%          d| j         dt'          | j                   d          t%          d| j         dt'          | j                   d          )a2  
        Estimate the dispersion/scale.

        Type of scale can be chose in the fit method.

        Parameters
        ----------
        mu : ndarray
            mu is the mean response estimate

        Returns
        -------
        Estimate of scale

        Notes
        -----
        The default scale for Binomial, Poisson and Negative Binomial
        families is 1.  The default for the other families is Pearson's
        Chi-Square estimate.

        See Also
        --------
        statsmodels.genmod.generalized_linear_model.GLM.fit
        rt   x2devzScale z with type z not understood)rZ   rE   rA   r   BinomialPoissonNegativeBinomial_estimate_x2_scaler   r   arraystrlowerr   rP   r<   r;   re   r(   rC   )r%   r>   s     r   r   zGLM.estimate_scaleO  s   2 ~ 	3$+(98;K(0(A(C D D 3r..r222dne,, 	,8DN+++dnc** 	E~##%%--..r222%%''500,,TZT=M-1->D D( ) !j"&...$t~2F2F2F2F"H I I I *"nnnd4>.B.B.B.BD E E Er   c                     t          j        | j        |z
  d          | j        z  }t          j        || j                            |          z            | j        z  S Nr4   )r   powerrP   r?   rc   rA   r   re   )r%   r>   resids      r   r   zGLM._estimate_x2_scale  sL    b!,,t}<vedk22266677$-GGr   brentq)\(?      @c                 h     |dk    rddl m}  fd} |||||          }nt          d          |S )a3  
        Tweedie specific function to estimate scale and the variance parameter.
        The variance parameter is also referred to as p, xi, or shape.

        Parameters
        ----------
        mu : array_like
            Fitted mean response variable
        method : str, defaults to 'brentq'
            Scipy optimizer used to solve the Pearson equation. Only brentq
            currently supported.
        low : float, optional
            Low end of the bracketing interval [a,b] to be used in the search
            for the power. Defaults to 1.01.
        high : float, optional
            High end of the bracketing interval [a,b] to be used in the search
            for the power. Defaults to 5.

        Returns
        -------
        power : float
            The estimated shape or power.
        r   r   )r   c                 2   j         j        |z
  dz  z  || z  z                                  j        z  }t	          j        j         j        |z
  dz  ||| z  z  z  dz
  z  t	          j        |          z            j                                        z  S )Nr4   r   )r?   rP   rc   re   r   rK   r;   )r   r>   rw   r%   s      r   psi_pz)GLM.estimate_tweedie_power.<locals>.psi_p  s    -4:?q*@@;(),?t}bQ0F %u 5179:1;  <!vbzz * + +-1->-B-B-D-DE Fr   )argsz!Only brentq can currently be used)scipy.optimizer   r   )r%   r>   methodlowhighr   r   r   s   `       r   estimate_tweedie_powerzGLM.estimate_tweedie_power  sp    0 X------F F F F F F5#t2777EE%&IJJJr   r   c                    |"d}t          j        |t                     |du rd}||t          | d          r| j        }n|d}|8t          | j        j        t          j	        j
                  st          d          ||t          | d          r| j        }n+|d}n&t          j        t          j        |                    }|| j        }t          j        ||          |z   |z   }|d	k    r| j                            |          S |dk    r|S |d
k    r6| j                            |          }	| j                            |	          }
|
S t          d| d          )a  
        Return predicted values for a design matrix

        Parameters
        ----------
        params : array_like
            Parameters / coefficients of a GLM.
        exog : array_like, optional
            Design / exogenous data. Is exog is None, model exog is used.
        exposure : array_like, optional
            Exposure time values, only can be used with the log link
            function.  See notes for details.
        offset : array_like, optional
            Offset values.  See notes for details.
        which : 'mean', 'linear', 'var'(optional)
            Statitistic to predict. Default is 'mean'.

            - 'mean' returns the conditional expectation of endog E(y | x),
              i.e. inverse of the model's link function of linear predictor.
            - 'linear' returns the linear predictor of the mean function.
            - 'var_unscaled' variance of endog implied by the likelihood model.
              This does not include scale or var_weights.

        linear : bool
            The ``linear` keyword is deprecated and will be removed,
            use ``which`` keyword instead.
            If True, returns the linear predicted values.  If False or None,
            then the statistic specified by ``which`` will be returned.


        Returns
        -------
        An array of fitted values

        Notes
        -----
        Any `exposure` and `offset` provided here take precedence over
        the `exposure` and `offset` used in the model fit.  If `exog`
        is passed as an argument here, then any `exposure` and
        `offset` values in the fit will be ignored.

        Exposure values must be strictly positive.
        Nz0linear keyword is deprecated, use which="linear"Tr   r9   rB   rh   r:   r   var_unscaledzThe which value "z" is not recognized)rI   rJ   FutureWarningrY   r9   rE   rA   rF   r   rj   rk   r(   r:   r   rK   rL   r[   r}   fittedr   )r%   r   r[   r:   r9   r   r   msglinpredr   var_s              r   r   zGLM.predict  s   Z DCM#}---~~  >dlwtX/F/Fl[FF^F
4;3C3;>3E)G )G ( ) ) ) z1J1J}HHHHvbj2233H<9D&v&&/(:F??;%%g...hNn$$;%%g..D;''--DKKKKKLLLr   c                 v   t          |dd          }t          | j        t          j        t          j        t          j        f          rd}|                     ||||d          }i }	t          j	        |dk              r$t          | j        t          j                  r||	d<    | j        j
        ||fd	|i|	}
|
S )
a   
        Return a instance of the predictive distribution.

        Parameters
        ----------
        params : array_like
            The model parameters.
        scale : scalar
            The scale parameter.
        exog : array_like
            The predictor variable matrix.
        offset : array_like or None
            Offset variable for predicted mean.
        exposure : array_like or None
            Log(exposure) will be added to the linear prediction.
        var_weights : array_like
            1d array of variance (analytic) weights. The default is None.
        n_trials : int
            Number of trials for the binomial distribution. The default is 1
            which corresponds to a Bernoulli random variable.

        Returns
        -------
        gen
            Instance of a scipy frozen distribution based on estimated
            parameters.
            Use the ``rvs`` method to generate random values.

        Notes
        -----
        Due to the behavior of ``scipy.stats.distributions objects``, the
        returned random number generator must be called with ``gen.rvs(n)``
        where ``n`` is the number of observations in the data set used
        to fit the model.  If any other value is used for ``n``, misleading
        results will be produced.
        rw   Tr{   rt   r   r   r   r7   r<   )r   rE   rA   r   r   r   r   r   r   anyget_distribution)r%   r   rw   r[   r:   r9   r<   r7   r>   rs   distrs              r   r   zGLM.get_distribution  s    L 5'D999dkH$5x7G$,$=$? @ @ 	E\\&$&\GGF8q=!! 	(4;(9::	(  (D,,R N N9DNHLN Nr   c                 L   t          j        | j        j        d                   | _        t          | j        t          j                  r[| j        	                    | j        | j
                  }|d         | _        |d         | _        | j                            d           d S d S )Nr   r   r7   )r   rn   rP   rR   r7   rE   rA   r   r   rf   r;   rV   rW   )r%   r   s     r   rX   zGLM._setup_binomial9  s     
 0 344dk8#455 	/+((T5FGGCQDJFDMO"":.....		/ 	/r   d   IRLS:0yE>	nonrobustF   c                 b   t          |t                    r(|                                }|dvrt          d          n=|;	 t	          |          }n*# t
          $ r} t          |          d          d}~ww xY w|| _        |                                dk    r/|                                dk    rd} | j        d|||||||d|S |	                    d	          | _
        | j
        |d	= t          j        | j        t          
          | _         | j        d||||||	|
||||d|}| `
| `|S )a  
        Fits a generalized linear model for a given family.

        Parameters
        ----------
        start_params : array_like, optional
            Initial guess of the solution for the loglikelihood maximization.
            The default is family-specific and is given by the
            ``family.starting_mu(endog)``. If start_params is given then the
            initial mean will be calculated as ``np.dot(exog, start_params)``.
        maxiter : int, optional
            Default is 100.
        method : str
            Default is 'IRLS' for iteratively reweighted least squares.
            Otherwise gradient optimization is used.
        tol : float
            Convergence tolerance.  Default is 1e-8.
        scale : str or float, optional
            `scale` can be 'X2', 'dev', or a float
            The default value is None, which uses `X2` for Gamma, Gaussian,
            and Inverse Gaussian.
            `X2` is Pearson's chi-square divided by `df_resid`.
            The default is 1 for the Binomial and Poisson families.
            `dev` is the deviance divided by df_resid
        cov_type : str
            The type of parameter estimate covariance matrix to compute.
        cov_kwds : dict-like
            Extra arguments for calculating the covariance of the parameter
            estimates.
        use_t : bool
            If True, the Student t-distribution is used for inference.
        full_output : bool, optional
            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.
            Not used if methhod is IRLS.
        disp : bool, optional
            Set to True to print convergence messages.  Not used if method is
            IRLS.
        max_start_irls : int
            The number of IRLS iterations used to obtain starting
            values for gradient optimization.  Only relevant if
            `method` is set to something other than 'IRLS'.
        atol : float, optional
            (available with IRLS fits) The absolute tolerance criterion that
            must be satisfied. Defaults to ``tol``. Convergence is attained
            when: :math:`rtol * prior + atol > abs(current - prior)`
        rtol : float, optional
            (available with IRLS fits) The relative tolerance criterion that
            must be satisfied. Defaults to 0 which means ``rtol`` is not used.
            Convergence is attained when:
            :math:`rtol * prior + atol > abs(current - prior)`
        tol_criterion : str, optional
            (available with IRLS fits) Defaults to ``'deviance'``. Can
            optionally be ``'params'``.
        wls_method : str, optional
            (available with IRLS fits) options are 'lstsq', 'pinv' and 'qr'
            specifies which linear algebra function to use for the irls
            optimization. Default is `lstsq` which uses the same underlying
            svd based approach as 'pinv', but is faster during iterations.
            'lstsq' and 'pinv' regularize the estimate in singular and
            near-singular cases by truncating small singular values based
            on `rcond` of the respective numpy.linalg function. 'qr' is
            only valid for cases that are not singular nor near-singular.
        optim_hessian : {'eim', 'oim'}, optional
            (available with scipy optimizer fits) When 'oim'--the default--the
            observed Hessian is used in fitting. 'eim' is the expected Hessian.
            This may provide more stable fits, but adds assumption that the
            Hessian is correctly specified.

        Notes
        -----
        If method is 'IRLS', then an additional keyword 'attach_wls' is
        available. This is currently for internal use only and might change
        in future versions. If attach_wls' is true, then the final WLS
        instance of the IRLS iteration is attached to the results instance
        as `results_wls` attribute.
        )r   r   z-scale must be either X2 or dev when a string.Nz/scale must be a float if given and no a string.irlsr   r  start_paramsmaxitertolrw   cov_typecov_kwdsuse_toptim_hessianr   )r
  r   r  r  rw   full_outputdispr  r  r  max_start_irlsr1   )rE   r   r   r(   r   	ExceptionrC   rZ   	_fit_irlsgetr   r   r   r[   r   _fit_gradient)r%   r
  r  r   r  rw   r  r  r  r  r  r  r\   excfit_s                  r   fitzGLM.fitD  s   b eS!! 	KKMMEM)) C   * e   d3iiE   <<>>V##~~5((&!4> L|W&)+35L LDJL L L #)**_"="=D".?+"$-	"G"G"GD%4% 0<-3.5*-U2=+/(/7u5C0 0 )/0 0D ##Ks   A 
A8A33A8newtonc                 ^   | j         }d| _         |dk    r| | j        d|||ddddd|}|j        }~ t                      j        d|||||d|}|| _         |                     |j                  }|                     |          }|j        d}n
|j        |z  }|                                dk    rd}d}nd	}	 t          j
                            |                     |j        |
                     |z  }n,# t          $ r t          j        dt                      d}Y nw xY wt#          | dt$                    }t#          | dt&                    } || |j        ||||	|
          }ddi}|r%|j        |_        d|j        v r|j        d         |d<   ||_        ||_         ||          S )z}
        Fits a generalized linear model for a given family iteratively
        using the scipy gradient optimizers.
        rt   r   Nr  r	  )r
  r  r  r   r  r   FTr   z8Inverting hessian failed, no bse or cov_params available_results_class_results_class_wrapperr  r  r  r   
iterationsr1   )rZ   r  r   rM   r  r   r   normalized_cov_paramsr   r   r`   invr   r   rI   rJ   r   r   
GLMResultsGLMResultsWrappermle_retvalsr   fit_history)r%   r
  r   r  r  r  r  rw   r  r  r  r  r\   rZ   	irls_rsltrsltr>   cov_poimresults_classresults_class_wrapperglm_resultsr   r^   s                          r   r  zGLM._fit_gradient  s>    N	Q\%9& 1L/=+.b;04D1 1 *0	1 1I
 %+Luww{ H,3+1H H@FH H
 #\\$+&&##B''%-EE.6E>>u$$C"HHC	IMM4<<c<#J#J"JKKeSEE 	 	 	M &'>@ @ @EEE	
  &6
CC '.FHY Z Z#mD$+$)$)-5*/	1 1 1 " 	F&*&6K#t///'+'7'E$#")$$[111s   >=C< <&D%$D%c           	         |                     dd          }	|                    d          }
|                    dd          }|                    dd          }|                    dd	          }|
|n|
}
| j        }| j        }|^t	          j        | j        j        d                   }| j                            | j                  }| j        	                    |          }n7t	          j
        ||          | j        z   }| j                            |          }|                     |          | _        | j                            | j        || j        | j        | j                  }t	          j        |          rt'          d          t)          t          j        |gt          j        |g          }d}||         }|dk    rL| j                            |          }|                     |          | _        t-          j        | |d
          }d}t1          |          D ]z}| j        | j        z  | j                            |          z  | _        || j        j                            |          | j        |z
  z  z   | j        z
  }t=          j        ||| j        dd          }|                     |          }t	          j
        | j        |j!                  }|| j        z  }| j                            |          }| "                    |||          }|                     |          | _        |#                                j$        dk    r5t	          j%        ||z
  d          rd}tM          j'        |tP                     tS          ||dz   |
|          }|r n||| _*        |dk    r;|d	k    rdn|}t-          j+        ||| j                  }|                     |          }tY          | |j!        |j-        | j        |||          }d|_.        i |_/        ||j/        d<   |j.        |j/        d<   |dk    r|	du r||_0        |dz   |d<   ||_1        ||_2        tg          |          S )z
        Fits a generalized linear model for a given family using
        iteratively reweighted least squares (IRLS).
        
attach_wlsFr   r   rB   tol_criterionr   
wls_methodlstsqNr   zsThe first guess on the deviance function returned a nan.  This could be a boundary  problem and should be reported.)r   r   r   T)check_endogcheck_weights)r   zJPerfect separation or prediction detected, parameter may not be identified)categorypinvr  r  	optimizerr   )4popr  rP   r[   r   zerosrR   rA   starting_mur   r}   r@   r   r   rw   r   r<   r;   isnanr(   dictinflmRegressionResultsranger?   r7   r=   rF   r   	reg_tools_MinimalWLSr  r   r   squeezer   r   rI   rJ   r   r   r>   WLSr#  r!  r   mle_settingsresults_wlsr&  	convergedr$  )r%   r
  r  r  rw   r  r  r  r\   r/  r   r   r0  r1  rP   wlsexogr>   r   r   r   rG  r   wls_resultsr   wlsendogwls_modr   wls_method2	wls_modelr-  s                                 r   r  zGLM._fit_irls  sY    ZZe44
zz&!!zz&"%%

?J??ZZg66
lss
)8DIOA$677L((44B{**2..HHvg|44t7LLH##H--B((,,
k""4:r43C#'#4djB B8C== 	A @ A A A rv|4}MMM	M*	 a<<##H--B,,R00DJ.t\4HHKIw 	 	I MDM9 K//334DL 4;#3#9#9"#=#=B#OO/0H+Hg,0Ld:>@ @ @G "++Z+88Kvdi);<<H--H##H--B**;GDDG,,R00DJ}}#q((R[eQ-G-G(9c,DEEEE*9i!mT+/1 1I Q;;$.'$9$9&&zKx$,??I#--{-;;K {'9!,!B!%*2X',	. . . $#% 1; .0;0B -aKKjD00&1K#(1}") ) ---r   elastic_netrB   bfgsc                 d   |                     dd          dk    r|                     |||          S ddlm} |dk    rt	          d          ddddd	}|                    |           |                     d
i           }	|                     di           }
|                     di           }d|	d<   d|
d<   d|d<   |	|d
<   |
|d<   ||d<    || f||||d|}|                     |j                  | _        | 	                    | j                  | _
        |j        st          j        d           |S )a!  
        Return a regularized fit to a linear regression model.

        Parameters
        ----------
        method : {'elastic_net'}
            Only the `elastic_net` approach is currently implemented.
        alpha : scalar or array_like
            The penalty weight.  If a scalar, the same penalty weight
            applies to all variables in the model.  If a vector, it
            must have the same length as `params`, and contains a
            penalty weight for each coefficient.
        start_params : array_like
            Starting values for `params`.
        refit : bool
            If True, the model is refit using only the variables that
            have non-zero coefficients in the regularized fit.  The
            refitted model is not regularized.
        opt_method : string
            The method used for numerical optimization.
        **kwargs
            Additional keyword arguments used when fitting the model.

        Returns
        -------
        GLMResults
            An array or a GLMResults object, same type returned by `fit`.

        Notes
        -----
        The penalty is the ``elastic net`` penalty, which is a
        combination of L1 and L2 penalties.

        The function that is minimized is:

        .. math::

            -loglike/n + alpha*((1-L1\_wt)*|params|_2^2/2 + L1\_wt*|params|_1)

        where :math:`|*|_1` and :math:`|*|_2` are the L1 and L2 norms.

        Post-estimation results are based on the same data used to
        select variables, hence may be subject to overfitting biases.

        The elastic_net method uses the following keyword arguments:

        maxiter : int
            Maximum number of iterations
        L1_wt  : float
            Must be in [0, 1].  The L1 penalty has weight L1_wt and the
            L2 penalty has weight 1 - L1_wt.
        cnvrg_tol : float
            Convergence threshold for maximum parameter change after
            one sweep through all coefficients.
        zero_tol : float
            Coefficients below this threshold are treated as zero.
        L1_wtr   r   )fit_elasticnetrN  z.method for fit_regularized must be elastic_net2   g|=)r  rQ  	cnvrg_tolzero_tolloglike_kwds
score_kwds	hess_kwdsrw   )r   alphar
  refitz$Elastic net fitting did not converge)r  
_fit_ridgestatsmodels.base.elastic_netrR  r(   updater   r   r>   r   rw   rG  rI   rJ   )r%   r   rY  r
  rZ  
opt_methodr\   rR  defaultsllkwsckwhekwresults                r   fit_regularizedzGLM.fit_regularized[  s|   z ::gq!!Q&&??5,
CCC??????]""MNNN!AE %' 'zz."--zz,++zz+r**WWW#' !% $ ,V&+-9&+, , #+	, , ,,v}--((11
 	BM@AAAr   c                     |$t          j         j        j        d                   } fd} fd}ddlm} ddlm}m}  |||||          }	|	j	        }
|	j
        sGt          j        t          j        |	j        dz                      }d	|z  }t          j        |            | |
          } ||          }|S )
Nr   c                 |                         |           j        z  t          j        | dz  z            dz  z
   S r   )rx   rS   r   rc   xrY  r%   s    r   funzGLM._fit_ridge.<locals>.fun  s9    \\!__ty026%!Q$,3G3G!3KKLLr   c                 L                         |           j        z  | z  z
   S r"   )r   rS   rg  s    r   gradzGLM._fit_ridge.<locals>.grad  s&    ZZ]]TY.:;;r   r   )minimize)RegularizedResultsRegularizedResultsWrapper)jacr   r4   z1GLM ridge optimization may have failed, |grad|=%f)r   r9  r[   rR   r   rl  r\  rm  rn  rh  successsqrtrc   ro  rI   rJ   )r%   rY  r
  r   ri  rk  rl  rm  rn  mrr   ngradr   resultss   ``            r   r[  zGLM._fit_ridge  s+   8DIOA$677L	M 	M 	M 	M 	M 	M	< 	< 	< 	< 	< 	< 	,+++++	
 	
 	
 	
 	
 	
 	
 	

 Xc<T&AAAz 	GBF2619--..EEMCM#$$T622++G44r   c                    ddl m} ddlm}m}  || j                                      |          }|j        |j        }	} || ||	||          \  }
}}| 	                    |
d          }|
|j
        _        ||j
        _        |                    dd          }|dk    r||j        z  |j
        _        nd|j
        _        |j        |j
        _        t!          |	          }|j
        xj        |z  c_        |j
        xj        |z  c_        |                    |          |j
        _        ||j
        _        ||j
        _        |S )	af  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
        r   )
DesignInfo)LinearConstraintsfit_constrained)r
  fit_kwds)r
  r  r  r  N)patsyrv  statsmodels.base._constraintsrw  rx  
exog_nameslinear_constraintcoefs	constantsr  _resultsr   cov_params_defaultr  rw   r!  rl   re   rb   
from_patsyconstraintsk_constrresults_constrained)r%   r  r
  ry  rv  rw  rx  lcRqr   cov
res_constrresr  r  s                   r   rx  zGLM.fit_constrained  sw   > 	%$$$$$	
 	
 	
 	
 	
 	
 	
 	
 Z((::;GGx1 #2/$1?K;C#E #E #EZ hhFAh66$*-'<<
K88{""14z7G1GCL..15CL.'-q66))#4#?#?#C#C  (+5(
r   )NNNNNr5   )rt   r"   )NT)NN)Nr   NNNN)Nr   NN)NNT)r   r   r   )NNNr   N)NNNNrt   rt   )Nr  r  r  Nr  NNTFr  )Nr  r  r  TTNr  NNr  )Nr  r  Nr  NN)rN  rB   NFrO  )(r-   r.   r/   formatbase_missing_param_doc__doc___formula_max_endogrN   rf   rO   rq   ry   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rX   r  r  r  rd  r[  rx  __classcell__r^   s   @r   r   r   T   sQ       Qb 	D344c f 8<?C> > > > > >@C C C.I .I .I`    = = = =   1 1 1 1,/ / / /,       D8 8 8 8t% % % %@A A A A =C37/3M M M M8  & @F26# # # #J  &1 1 1 1B <@-1F- F- F- F-P	 	 	/E /E /EbH H H$ $ $ $L @D%)UM UM UM UMn HL?A6 6 6 6p	/ 	/ 	/ FJCG9:w w w wr 7?9=6A@AC2 C2 C2 C2 C2 C2J =A=AW. W. W. W.r ;=16#)_ _ _ _B  >? ? ? ? ? ? ? ?r   	pred_kwdsc                   `    e Zd ZdZ	 d4 fd	Zed             Zed             Zed             Zed             Z	ed	             Z
ed
             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd5dZed             Zd6dZed             Zed             Zed             Zed             Zd7dZ	 	 	 	 d8dZ e e!j"        j                  	 	 	 d9d!            Z"d:d"Z#d:d#Z$	 	 d;d%Z%	 	 d<d(Z& e e'j(        j)        j                   fd)            Z) e e*d*d+iz            	 	 	 d=d,            Z+ e e,d*d+iz            d5d-            Z- e e.d*d+iz            	 	 d>d/            Z/d?d1Z0	 	 d@d3Z1 xZ2S )Ar#  av  
    Class to contain GLM results.

    GLMResults inherits from statsmodels.LikelihoodModelResults

    Attributes
    ----------
    df_model : float
        See GLM.df_model
    df_resid : float
        See GLM.df_resid
    fit_history : dict
        Contains information about the iterations. Its keys are `iterations`,
        `deviance` and `params`.
    model : class instance
        Pointer to GLM model instance that called fit.
    nobs : float
        The number of observations n.
    normalized_cov_params : ndarray
        See GLM docstring
    params : ndarray
        The coefficients of the fitted model.  Note that interpretation
        of the coefficients often depends on the distribution family and the
        data.
    pvalues : ndarray
        The two-tailed p-values for the parameters.
    scale : float
        The estimate of the scale / dispersion for the model fit.
        See GLM.fit and GLM.estimate_scale for more information.
    stand_errors : ndarray
        The standard errors of the fitted GLM.   #TODO still named bse

    See Also
    --------
    statsmodels.base.model.LikelihoodModelResults
    r  Nc                    t                                          ||||           |j        | _        |j        | _        |j        j        d         | _        |j        | _        |j	        | _
        |j        | _        t          | j        t          j                  r| j        j        | _        nd| _        |j        | _        |j        | _        i | _        | j                            g d           | j                            ddg           t3          | dg           | _        | j                            d           ddlm} |d	| _        n|| _        |d
k    p&|                                                     d          }	| j        j!        r|	sddl"m#}
 tI          j%        d|
           | j        j&        r|	sddl"m#}
 tI          j%        d|
           |d
k    rd
| _'        ddi| _(        d S |i } || f|d|d| d S )N)r!  rw   r   r   )r  _freq_weights_var_weights	_iweightsnullr>   _data_attr_model)get_robustcov_resultsFr  HC)SpecificationWarningz.cov_type not fully supported with freq_weightsz-cov_type not fully supported with var_weightsdescriptionzWStandard Errors assume that the covariance matrix of the errors is correctly specified.T)r  use_selfr  ))rM   rN   rA   rP   _endogrR   rS   r;   r  r<   r  r?   r  rE   r   r   modelr7   	_n_trialsre   rb   _cacherT   rU   _data_in_cacher   r  rW   statsmodels.base.covtyper  r  upper
startswithrm   statsmodels.tools.sm_exceptionsr  rI   rJ   ro   r  r  )r%   r  r   r!  rw   r  r  r  r  ctr  r^   s              r   rN   zGLMResults.__init__G  sp   &;	 	 	 	 	
 lkK%a(	"/!-dk8#455 	!Z0DNNDN
 	  =  =  = 	> 	> 	>""FD>222 '.@" E E$$T*** 	CBBBBB=DJJDJ +%M8>>+;+;+F+Ft+L+L:' 	0 	0LLLLLLMJ.0 0 0 :& 	0r 	0LLLLLLMI.0 0 0 {""'DM* -* +DMMM
 !!$ ;D(-; ;19; ; ; ; ;r   c                 0    | j         | j        | j        z
  z  S )zm
        Response residuals.  The response residuals are defined as
        `endog` - `fittedvalues`
        )r  r  r>   r$   s    r   resid_responsezGLMResults.resid_response  s     ~TW!455r   c                     t          j        | j                  | j        | j        z
  z  t          j        | j                  z  t          j        | j                            | j                            z  S )a  
        Pearson residuals.  The Pearson residuals are defined as
        (`endog` - `mu`)/sqrt(VAR(`mu`)) where VAR is the distribution
        specific variance function.  See statsmodels.families.family and
        statsmodels.families.varfuncs for more information.
        )r   rq  r  r  r>   r  rA   r   r$   s    r   resid_pearsonzGLMResults.resid_pearson  s_     ''4;tw+>?)**+,,TW55667 	8r   c                 r    | j         | j        j                            | j                  z  }|| j        z  }|S )z
        Working residuals.  The working residuals are defined as
        `resid_response`/link'(`mu`).  See statsmodels.family.links for the
        derivatives of the link functions.  They are defined analytically.
        )r  rA   rF   r   r>   r  r*   s     r   resid_workingzGLMResults.resid_working  s7     "T[%5%;%;DG%D%DDt~
r   c                     | j         S )z
        Anscombe residuals.  See statsmodels.families.family for distribution-
        specific Anscombe residuals. Currently, the unscaled residuals are
        provided. In a future version, the scaled residuals will be provided.
        )resid_anscombe_scaledr$   s    r   resid_anscombezGLMResults.resid_anscombe  s     ))r   c                 f    | j                             | j        | j        | j        | j                  S )z
        Scaled Anscombe residuals.  See statsmodels.families.family for
        distribution-specific Anscombe residuals.
        r<   rw   )rA   r  r  fittedvaluesr  rw   r$   s    r   r  z GLMResults.resid_anscombe_scaled  s7     {))$+t7H6:6G04
 * < < 	<r   c                 \    | j                             | j        | j        | j        d          S )z
        Unscaled Anscombe residuals.  See statsmodels.families.family for
        distribution-specific Anscombe residuals.
        rt   r  )rA   r  r  r  r  r$   s    r   resid_anscombe_unscaledz"GLMResults.resid_anscombe_unscaled  s5     {))$+t7H6:6G02 * 4 4 	4r   c                 `    | j                             | j        | j        | j        d          }|S )z}
        Deviance residuals.  See statsmodels.families.family for distribution-
        specific deviance residuals.
        rt   r  )rA   	resid_devr  r  r  )r%   r   s     r   resid_deviancezGLMResults.resid_deviance  s9     k##DK1B040A*, $ . . 
r   c                     | j         | j        z
  dz  | j                            | j                  z  }|| j        | j        z  z  }t          j        |          }|S )zx
        Pearson's Chi-Squared statistic is defined as the sum of the squares
        of the Pearson residuals.
        r4   )r  r>   rA   r   r  r  r   rc   )r%   chisqchisqsums      r   pearson_chi2zGLMResults.pearson_chi2  sS     tw&*T[-A-A$'-J-JJ$.006%==r   c                     | j         S )a  
        The estimated mean response.

        This is the value of the inverse of the link function at
        lin_pred, where lin_pred is the linear predicted value
        obtained by multiplying the design matrix by the coefficient
        vector.
        )r>   r$   s    r   r  zGLMResults.fittedvalues  s     wr   c                 @    | j                             | j                  S )z$
        See GLM docstring.
        )r  r   r   r$   s    r   r>   zGLMResults.mu  s    
 z!!$+...r   c                 B   | j         }| j        }t          j        t	          |          df          }|                                                                }|                    d           t          |dg           D ]}||= t          j	        | j
                            |                                                    }| j        j        }t          j        |          dk    r|dk    svt          j                    5  t          j        dt$                     t'          ||fd| j
        i|}|                    |          j        }	ddd           n# 1 swxY w Y   n=t-          j        ||| j        | j        z            }
|
                                j        }	|	S )	z1
        Fitted values of the null model
        r   rA   _null_drop_keysr   ignore)r
  N)r=   )r  r  r   rn   rl   rq   copyr8  r   
atleast_1drA   rF   r   r@   sizerI   catch_warningssimplefilterr   r   r  r  r>  rD  r  r  )r%   rP   r  r[   r\   keyr
  oemodr   rM  s              r   r  zGLMResults.null  s   
 
wE

A''%%'',,..

85"3R88 	 	Cs}T[%5%5ejjll%C%CDDZ(q  R1WW(** I I%h>>>%DDdkDVDDl;;HI I I I I I I I I I I I I I I ud'+~'FH H HI]]__1Fs   ?AEEEc                 d    | j                             | j        | j        | j        | j                  S )zk
        See statsmodels.families.family for the distribution-specific deviance
        functions.
        )rA   r   r  r>   r  r  r$   s    r   r   zGLMResults.deviance	  s1     {##DK$:K$($68 8 	8r   c                 d    | j                             | j        | j        | j        | j                  S )zcThe value of the deviance function for the model fit with a constant
        as the only regressor.)rA   r   r  r  r  r  r$   s    r   null_deviancezGLMResults.null_deviance  s1     {##DKD<M$($68 8 	8r   c                 r    | j                             | j        | j        | j        | j        | j                  S )zW
        Log-likelihood of the model fit with a constant as the only regressor
        r<   r;   rw   )rA   rx   r  r  r  r  rw   r$   s    r   llnullzGLMResults.llnull  s<    
 {""4;	/3/@040B)- # 5 5 	5r   c                    | j         }|t          | j         t          j                  rt          | j         j        t          j        j                  ra| j         j        j        dk    rLt          j        | j	        | j
        z
  d          | j        z                                  }|| j        j        z  }n| j        }|                    | j	        | j
        | j        | j        |          }|S )z
        Return the log-likelihood at the given scale, using the
        estimated scale if the provided scale is None.  In the Gaussian
        case with linear link, the concentrated log-likelihood is
        returned.
        Nrt   r4   r  )rA   rE   r   ri   rF   rj   Powerr   r   r  r>   r  rc   r  rd   rw   rx   r  r  )r%   rw   _modelfamilyr+   s       r   
llf_scaledzGLMResults.llf_scaled#  s     {=4;(9:: 	#t{/1EFF	#[%+r11 $+"7;;dnLQQSS))
""4;/3/@040B). # 0 0 
r   c                 *    |                                  S )au  
        Value of the loglikelihood function evalued at params.
        See statsmodels.families.family for distribution-specific
        loglikelihoods.  The result uses the concentrated
        log-likelihood if the family is Gaussian and the link is linear,
        otherwise it uses the non-concentrated log-likelihood evaluated
        at the estimated scale.
        )r  r$   s    r   r   zGLMResults.llf=  s        r   csc                 .   |                                 }|                    d          rd| j        | j        z  z
  }nX|                    d          s|dv r0dt	          j        | j        | j        z
  d| j        z  z            z
  }nt          d          |S )a  
        Pseudo R-squared

        Cox-Snell likelihood ratio pseudo R-squared is valid for both discrete
        and continuous data. McFadden's pseudo R-squared is only valid for
        discrete data.

        Cox & Snell's pseudo-R-squared:  1 - exp((llnull - llf)*(2/nobs))

        McFadden's pseudo-R-squared: 1 - (llf / llnull)

        Parameters
        ----------
        kind : P"cs", "mcf"}
            Type of pseudo R-square to return

        Returns
        -------
        float
            Pseudo R-squared
        mcfr   cox)r  lrr4   z)only McFadden and Cox-Snell are available)r   r  r   r  r   rr   rS   r(   )r%   kindprsqs      r   pseudo_rsquaredzGLMResults.pseudo_rsquaredI  s    , zz||??5!! 	Jtx$+--DD__U## 	Jt|';';rvt{TX5!di-HIIIDDHIIIr   c                 ,    |                      d          S )zX
        Akaike Information Criterion
        -2 * `llf` + 2 * (`df_model` + 1)
        aicinfo_criteriar$   s    r   r  zGLMResults.aich  s     !!%(((r   c                     t           j        dvrt          j        dt                     t          t           j                  r| j        S | j        S )a  
        Bayes Information Criterion

        `deviance` - `df_resid` * log(`nobs`)

        .. warning::

            The current definition is based on the deviance rather than the
            log-likelihood. This is not consistent with the AIC definition,
            and after 0.13 both will make use of the log-likelihood definition.

        Notes
        -----
        The log-likelihood version is defined
        -2 * `llf` + (`df_model` + 1)*log(n)
        )TFa  The bic value is computed using the deviance formula. After 0.13 this will change to the log-likelihood based formula. This change has no impact on the relative rank of models compared using BIC. You can directly access the log-likelihood version using the `bic_llf` attribute. You can suppress this message by calling statsmodels.genmod.generalized_linear_model.SET_USE_BIC_LLF with True to get the LLF-based version now or False to retainthe deviance version.)_use_bic_helperr&   rI   rJ   r   r)   bic_llfbic_deviancer$   s    r   biczGLMResults.bicp  sV    $ &m;;M(    +,, 	 <  r   c                 ~    | j         | j        j        | j        z
  dz
  t	          j        | j        j                  z  z
  S )z{
        Bayes Information Criterion

        Based on the deviance,
        `deviance` - `df_resid` * log(`nobs`)
        r   )r   r  rd   rb   r   rK   r$   s    r   r  zGLMResults.bic_deviance  s>     !DM1A5tz'(()) 	*r   c                 ,    |                      d          S )z
        Bayes Information Criterion

        Based on the log-likelihood,
        -2 * `llf` + log(n) * (`df_model` + 1)
        r  r  r$   s    r   r  zGLMResults.bic_llf  s     !!%(((r   r   c                    |                                 }| j        dz   |z   }|dk    rd| j        z  d|z  z   S |dk    r6| j        | j        z   dz   }d| j        z  |t	          j        |          z  z   }|S |dk    r~| j        j        }t          j	        t          j
        t          j        f}t          ||          sd}	|	dz  }	t          j        |	           |                     d	          }
d|
z  |z  d|z  z   S d
S )aH  Return an information criterion for the model.

        Parameters
        ----------
        crit : string
            One of 'aic', 'bic', or 'qaic'.
        scale : float
            The scale parameter estimated using the parent model,
            used only for qaic.
        dk_params : int or float
            Correction to the number of parameters used in the information
            criterion. By default, only mean parameters are included, the
            scale parameter is not included in the parameter count.
            Use ``dk_params=1`` to include scale in the parameter count.

        Returns
        -------
        Value of information criterion.

        Notes
        -----
        The quasi-Akaike Information criterion (qaic) is -2 *
        `llf`/`scale` + 2 * (`df_model` + 1).  It may not give
        meaningful results except for Poisson and related models.

        The QAIC (ic_type='qaic') must be evaluated with a provided
        scale parameter.  Two QAIC values are only comparable if they
        are calculated using the same scale parameter.  The scale
        parameter should be estimated using the largest model among
        all models being compared.

        References
        ----------
        Burnham KP, Anderson KR (2002). Model Selection and Multimodel
        Inference; Springer New York.
        r   r  r4   r  qaicz-QAIC is only valid for Binomial, Poisson and zNegative Binomial families.r   N)r   rb   r   re   r   rK   r  rA   r   r   r   r   rE   rI   rJ   r  )r%   critrw   	dk_paramsk_paramsrS   r  fflr   r   s              r   r  zGLMResults.info_criteria  s   J zz||=1$y05===1x<//U]]=4=014DTX+ 55CJV^^
!A"H$=#%Ba$$ #E44c"""///**C8E>AL00 ^r   TFc
           
          ddl mc m}
 ||dd}|J|
                    | |||	|          }d|d<   t	          j        | |||	|| j        j        j        |          }n!||d	}t	          j        | ||||	|||
          }|S )a  
    Compute prediction results for GLM compatible models.

    Options and return class depend on whether "which" is None or not.

    Parameters
    ----------
    exog : array_like, optional
        The values for which you want to predict.
    exposure : array_like, optional
        Exposure time values, only can be used with the log link
        function.
    offset : array_like, optional
        Offset values.
    transform : bool, optional
        If the model was fit via a formula, do you want to pass
        exog through the formula. Default is True. E.g., if you fit
        a model y ~ log(x1) + log(x2), and transform is True, then
        you can pass a data structure that contains x1 and x2 in
        their original form. Otherwise, you'd need to log the data
        first.
    which : 'mean', 'linear', 'var'(optional)
        Statitistic to predict. Default is 'mean'.
        If which is None, then the deprecated keyword "linear" applies.
        If which is not None, then a generic Prediction results class will
        be returned. Some options are only available if which is not None.
        See notes.

        - 'mean' returns the conditional expectation of endog E(y | x),
          i.e. inverse of the model's link function of linear predictor.
        - 'linear' returns the linear predictor of the mean function.
        - 'var_unscaled' variance of endog implied by the likelihood model.
          This does not include scale or var_weights.

    linear : bool
        The ``linear` keyword is deprecated and will be removed,
        use ``which`` keyword instead.
        If which is None, then the linear keyword is used, otherwise it will
        be ignored.
        If True and which is None, the linear predicted values are returned.
        If False or None, then the statistic specified by ``which`` will be
        returned.
    average : bool
        Keyword is only used if ``which`` is not None.
        If average is True, then the mean prediction is computed, that is,
        predictions are computed for individual exog and then the average
        over observation is used.
        If average is False, then the results are the predictions for all
        observations, i.e. same length as ``exog``.
    agg_weights : ndarray, optional
        Keyword is only used if ``which`` is not None.
        Aggregation weights, only used if average is True.
    row_labels : list of str or None
        If row_lables are provided, then they will replace the generated
        labels.

    Returns
    -------
    prediction_results : instance of a PredictionResults class.
        The prediction results instance contains prediction and prediction
        variance and can on demand calculate confidence intervals and summary
        tables for the prediction of the mean and of new observations.
        The Results class of the return depends on the value of ``which``.

    See Also
    --------
    GLM.predict
    GLMResults.predict

    Notes
    -----
    Changes in statsmodels 0.14: The ``which`` keyword has been added.
    If ``which`` is None, then the behavior is the same as in previous
    versions, and returns the mean and linear prediction results.
    If the ``which`` keyword is not None, then a generic prediction results
    class is returned and is not backwards compatible with the old prediction
    results class, e.g. column names of summary_frame differs.
    There are more choices for the returned predicted statistic using
    ``which``. More choices will be added in the next release.
    Two additional keyword, average and agg_weights options are now also
    available if ``which`` is not None.
    In a future version ``which`` will become not None and the backwards
    compatible prediction results class will be removed.

    r   Nr   )r:   r9   r   )r[   r   
row_labelsr  r   r   )r[   r   r  r   rF   r  )r:   r9   )r[   r   r   r  averageagg_weightsr  )	"statsmodels.regression._prediction
regression_predictionget_predictionpredget_prediction_glmr  rA   rF   )r%   r[   r:   r9   r   r   r   r  r  r  r   r  res_linpredr  s                 r   r  zGLMResults.get_prediction  s    t 	=<<<<<<<<!)VhOO	=!00D;D<F;D 1 F FK
 "(Ig)$TY5?2=/3z/@/E4=	? ? ?CC &.@@I %#%'#	 	 	C 
r   jointc                 `   | j         j        du rt          j        dt                     | j         j        du rt          j        dt                     | j         j        }| j        | j         _        || j         xj        |z  c_        t          j        | ||||||d |	  	        }	|| j         _        |	S )NTz2score test has not been verified with freq_weightsz1score test has not been verified with var_weights)r   r   
hypothesisr  r  r   rw   r   )	r  rm   rI   rJ   UserWarningro   re   pinferr   )
r%   r   r   r  r  r  r   r   mod_df_residr  s
             r   r   zGLMResults.score_testf  s    
 :'4//MN%' ' ':&$..MM%' ' '
 z*"m
$J=03E+5)1H.;&*)13 3 3 +

r   c                    | j                             | j        |          }t          j        |          dddf         | j         j        z  }|t          j                            |          j        z  	                    d          }|S )a  
        Compute the diagonal of the hat matrix

        Parameters
        ----------
        observed : bool
            If true, then observed hessian is used in the hat matrix
            computation. If false, then the expected hessian is used.
            In the case of a canonical link function both are the same.

        Returns
        -------
        hat_matrix_diag : ndarray
            The diagonal of the hat matrix computed from the observed
            or expected hessian.
        r   Nr   )
r  r   r   r   rq  r[   r`   r6  r   rc   )r%   r   r=   wexoghds        r   get_hat_matrix_diagzGLMResults.get_hat_matrix_diag  ss    " *++DK(+KK  D)DJO;binnU++--22155	r   c           	      V   ddl m} | j                            | j        |          }t          j        |          }|dddf         | j        j        z  }|| j        j        z  }| 	                    |          } || ||| j
        t          j        | j                  z  |          }|S )a  
        Get an instance of GLMInfluence with influence and outlier measures

        Parameters
        ----------
        observed : bool
            If true, then observed hessian is used in the hat matrix
            computation. If false, then the expected hessian is used.
            In the case of a canonical link function both are the same.

        Returns
        -------
        infl : GLMInfluence instance
            The instance has methods to calculate the main influence and
            outlier measures as attributes.

        See Also
        --------
        statsmodels.stats.outliers_influence.GLMInfluence
        r   )GLMInfluencer   N)rP   r[   r   hat_matrix_diag)$statsmodels.stats.outliers_influencer  r  r   r   r   rq  r[   rP   r  r  rw   )	r%   r   r  r=   weights_sqrtr  wendogr	  infls	            r   get_influencezGLMResults.get_influence  s    * 	FEEEEE*++DK(+KKww''QQQW%
7
 00 22H2EE|DU#1BGDJ4G4GG)8: : : r   rt   c                    t          | j        j        t          j        t          j        t          j        f          r*d}| j        dk    rd}t          j	        |t                     n| j        }|                     |||d          }i }	t          j        |dk              r)t          | j        j        t          j                  r||	d<    | j        j        j        ||fd|i|	}
|
S )a  
        Return a instance of the predictive distribution.

        Parameters
        ----------
        scale : scalar
            The scale parameter.
        exog : array_like
            The predictor variable matrix.
        offset : array_like or None
            Offset variable for predicted mean.
        exposure : array_like or None
            Log(exposure) will be added to the linear prediction.
        var_weights : array_like
            1d array of variance (analytic) weights. The default is None.
        n_trials : int
            Number of trials for the binomial distribution. The default is 1
            which corresponds to a Bernoulli random variable.

        Returns
        -------
        gen
            Instance of a scipy frozen distribution based on estimated
            parameters.
            Use the ``rvs`` method to generate random values.

        Notes
        -----
        Due to the behavior of ``scipy.stats.distributions objects``, the
        returned random number generator must be called with ``gen.rvs(n)``
        where ``n`` is the number of observations in the data set used
        to fit the model.  If any other value is used for ``n``, misleading
        results will be produced.
        rt   z2using scale=1, no exess dispersion in distributionr   r   r   r7   r<   )rE   r  rA   r   r   r   r   rw   rI   rJ   r   r   r   r   r   )r%   r[   r:   r9   r<   r7   rw   r   r>   rs   r   s              r   r   zGLMResults.get_distribution  s    N dj'(*;X=M$,$=*? @ @ 	 EzRJc;///JE\\$&\??F8q=!! 	(4:,h.?@@	(  (D2
!28 8#.8268 8r   overallr   c                 *   t          | j        dd          t          d          t          j        | j        j        dk              s"t          j        | j        j        dk              rt          j        d           ddl	m
}  || |||||f          S )a  Get marginal effects of the fitted model.

        Warning: offset, exposure and weights (var_weights and freq_weights)
        are not supported by margeff.

        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 semi-elasticity -- dy/d(lnx)
            - 'eydx' - estimate semi-elasticity -- 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. For interpretations of these methods
            see notes below.
        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
        -----
        Interpretations of methods:

        - 'dydx' - change in `endog` for a change in `exog`.
        - 'eyex' - proportional change in `endog` for a proportional change
          in `exog`.
        - 'dyex' - change in `endog` for a proportional change in `exog`.
        - 'eydx' - proportional change in `endog` for a change in `exog`.

        When using after Poisson, returns the expected number of events per
        period, assuming that the model is loglinear.

        Status : unsupported features offset, exposure and weights. Default
        handling of freq_weights for average effect "overall" might change.

        r9   Nz&Margins with offset are not available.r   z-weights are not taken into account by margeffr   )DiscreteMargins)r   r  r   r   r   r<   r;   rI   rJ   r   r  )r%   atr   atexogdummycountr  s          r   get_margeffzGLMResults.get_margeff  s    ^ 4:x..:%&NOOOF4:)Q.// 	Ktz.!344	KMIJJJIIIIIItb&&%%GHHHr   c                     | j                             d | j        j         D                        t          | j        |                                            d | _        d | _        d | _        d | _	        d | _
        d S )Nc                     g | ]}d |v|	S )z_data.r1   ).0is     r   
<listcomp>z*GLMResults.remove_data.<locals>.<listcomp>T	  s,      6  6  6a#+1#4#4 !"#4#4#4r   )rT   rU   r  rM   r^   remove_datar  r  r  r  r  )r%   r^   s    r   r  zGLMResults.remove_dataQ	  s     	  6  64:+@  6  6  6 	7 	7 	7dnd##//111 ! r   extra_params_doc c                 4    ddl m}  || |||||          }|S )Nr   )plot_added_variable)
resid_typeuse_glm_weights
fit_kwargsax)$statsmodels.graphics.regressionplotsr!  )r%   
focus_exogr"  r#  r$  r%  r!  figs           r   r!  zGLMResults.plot_added_variable_	  sG    
 	MLLLLL!!$
-72A-7B@ @ @
 
r   c                 *    ddl m}  || ||          S )Nr   )plot_partial_residuals)r%  )r&  r*  )r%   r'  r%  r*  s       r   r*  z!GLMResults.plot_partial_residualsm	  s/     	POOOOO%%dJ2>>>>r   Q?c                 .    ddl m}  || ||||          S )Nr   )plot_ceres_residuals)
cond_meansr%  )r&  r-  )r%   r'  fracr.  r%  r-  s         r   r-  zGLMResults.plot_ceres_residualst	  s@     	NMMMMM##D*d/9bB B B 	Br   皙?c                    ddd| j         j        j        gfd| j         j        j        j        gfd| j        gfdddd	| j        d
         z  gfg}	 |                     d          }n# t          $ r t          j	        }Y nw xY wddddd| j
        z  gfddd| j        z  gfdd| j        z  gfdd|z  gfg}t          | d          r|                    d| j        gf           |d}ddlm}  |            }	|	                    | |||||           |	                    | |||| j                   t          | d           r|	                    d!g           |	S )"ac  
        Summarize the Regression Results

        Parameters
        ----------
        yname : str, optional
            Default is `y`
        xname : list[str], optional
            Names for the exogenous variables, default is `var_#` for ## in
            the number of regressors. Must match the number of parameters in
            the model
        title : str, 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
        )zDep. Variable:N)zModel:NzModel Family:zLink Function:zMethod:)zDate:N)zTime:NzNo. Iterations:z%dr   r  )r  )zNo. Observations:N)zDf Residuals:N)z	Df Model:NzScale:z%#8.5g)zLog-Likelihood:Nz	Deviance:zPearson chi2:z%#6.3gzPseudo R-squ. (CS):z%#6.4gr  zCovariance Type:Nz+Generalized Linear Model Regression Resultsr   )Summary)gleftgrightynamexnametitle)r5  r6  rY  r  r  @Model has been estimated subject to linear equality constraints.)rA   r^   r-   rF   r   r&  r  r(   r   nanrw   r   r  rY   rW   r  statsmodels.iolib.summaryr2  add_table_2colsadd_table_paramsr  add_extra_txt)
r%   r5  r6  r7  rY  top_left	prsquared	top_rightr2  smrys
             r   summaryzGLMResults.summary}	  s   : -$$t{'<'E&FG%(8(B(K'LM.##&T-k::;=		,,$,77II 	 	 	III	 1,(DJ!6 78.!Ht}$<#=>%43D(D'EF+h.B-CD	 4$$ 	COO/$-ABBB=AE 	655555wyyT)#(U 	 	D 	D 	Dd%uE$(J 	 	0 	0 	0 4'' 	: !8  9 : : :s   A% %A>=A>%.4fc           	      R   d| _         ddlm} |                                }t	          j                    5  t	          j        dt                     |                    | |||||           ddd           n# 1 swxY w Y   t          | d          r|
                    d           |S )	a  Experimental summary for regression Results

        Parameters
        ----------
        yname : str
            Name of the dependent variable (optional)
        xname : list[str], optional
            Names for the exogenous variables, default is `var_#` for ## in
            the number of regressors. Must match the number of parameters in
            the model
        title : str, 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 : str
            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
        r  r   )summary2r  )rt  rY  float_formatr6  r5  r7  Nr  r8  )r   statsmodels.iolibrE  r2  rI   r  r  r   add_baserY   add_text)r%   r5  r6  r7  rY  rF  rE  rA  s           r   rE  zGLMResults.summary2	  s   < ......!!$&& 	A 	A!(M:::MM$e, %U%  A A A	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 4'' 	3MM 2 3 3 3 s   6A77A;>A;)r  NNr"   )r  )Nr   )	NNNTNNFNN)NNr  NNNT)T)NNNrt   rt   )r  r   NFF)NTNN)r+  NN)NNNr0  )NNNr0  rC  )3r-   r.   r/   r  rN   r   r  r  r  r  r  r  r  r   r  r  r>   r
   r  r   r  r  r  r   r  r  r0   r  r  r  r  r  r   r  r   r  r  r   r  r  LikelihoodModelResultsr  r   r!  r	   r*  r   r-  rB  rE  r  r  s   @r   r#  r#  !  s       # #L =A=; =; =; =; =; =;~ 6 6 [6 	8 	8 [	8 	 	 [	 * * [* < < [< 4 4 [4   [   \ 	 	 [	 / / [/   ^8 8 8 ^8 8 8 ^8 5 5 ^5   4 	! 	! \	!   > ) ) \) !! !! X!!F 	* 	* \	* ) ) \)71 71 71 71v ?C:>26"&   B Xf'((=A?C04   )(8   .! ! ! !F 48?A; ; ; ;z ?C$UI UI UI UIn Xd)5=>>    ?> X&*<b)AABB9==A#   CB X)-?,DDEE? ? ? FE? X'+=r*BBCCEI $B B B DCBH H H HT BE$) ) ) ) ) ) ) )r   r#  c                   T    e Zd ZddddddZ ej        ej        j        e          ZdS )r$  rows)r  r  r  r  r  N)	r-   r.   r/   _attrswrapunion_dictsr>  RegressionResultsWrapper_wrap_attrsr1   r   r   r$  r$  	  sJ            F #$"2#>#J#)+ +KKKr   r$  __main__)longleytables)returnszTest GLM)r7  )Kr  statsmodels.compat.pandasr   rI   numpyr   numpy.linalgr   statsmodels.base.modelr  r  statsmodels.base.wrapperwrapperrN  statsmodels.baser   r  &statsmodels.base._prediction_inferencer   %statsmodels.base._parameter_inference_parameter_inferencer  )statsmodels.graphics._regressionplots_docr   r   r	   statsmodels.regression._toolsr  _toolsrA  #statsmodels.regression.linear_modellinear_modelr>  statsmodels.tools.decoratorsr
   r   r   statsmodels.tools.docstringr   r  r   r   r   statsmodels.tools.validationr   r  r   __all__r   r    r  r,   SET_USE_BIC_LLFLikelihoodModelr   r  get_prediction_docremove_parametersrJ  r#  rP  r$  populate_wrapperr-   statsmodels.datasetsrS  loaddatarP   r[   r  GLMmodrB  GLMTGLMTpr1   r   r   <module>rt     s   & / . . . . .      $ $ $ $ $ $ % % % % % % % % % ' ' ' ' ' ' ' ' ' : : : : : : H H H H H H 6 6 6 6 6 6 6 6 6         
 2 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0         
 2 1 1 1 1 1         
 4 3 3 3 3 3       )
*- - -
< 
< 
< 
< 
< 
< 
< 
< "/##!1F F F F F$
 F F FR. Yt6>??   $ $[ 1 1 1O O O O O, O O Od	+ 	+ 	+ 	+ 	+3 	+ 	+ 	+  ' 4 4 4z,,,,,,7<>>DSTY''++--F>>(>++D NNN,,E! ! r   