
    M/Phu                         d Z ddlZddlmZ ddlmc mZ ddlmZ ddl	m
Z
 dZdZdZ G d	 d
          Z G d dej                  Z G d dej                  Z G d d          ZdS )a  
Implementation of proportional hazards regression models for duration
data that may be censored ("Cox models").

References
----------
T Therneau (1996).  Extending the Cox model.  Technical report.
http://www.mayo.edu/research/documents/biostat-58pdf/DOC-10027288

G Rodriguez (2005).  Non-parametric estimation in survival models.
http://data.princeton.edu/pop509/NonParametricSurvival.pdf

B Gillespie (2006).  Checking the assumptions in the Cox proportional
hazards model.
http://www.mwsug.org/proceedings/2006/stats/MWSUG-2006-SD08.pdf
    N)model)cache_readonly)Appendera  
    Returns predicted values from the proportional hazards
    regression model.

    Parameters
    ----------%(params_doc)s
    exog : array_like
        Data to use as `exog` in forming predictions.  If not
        provided, the `exog` values from the model used to fit the
        data are used.%(cov_params_doc)s
    endog : array_like
        Duration (time) values at which the predictions are made.
        Only used if pred_type is either 'cumhaz' or 'surv'.  If
        using model `exog`, defaults to model `endog` (time), but
        may be provided explicitly to make predictions at
        alternative times.
    strata : array_like
        A vector of stratum values used to form the predictions.
        Not used (may be 'None') if pred_type is 'lhr' or 'hr'.
        If `exog` is None, the model stratum values are used.  If
        `exog` is not None and pred_type is 'surv' or 'cumhaz',
        stratum values must be provided (unless there is only one
        stratum).
    offset : array_like
        Offset values used to create the predicted values.
    pred_type : str
        If 'lhr', returns log hazard ratios, if 'hr' returns
        hazard ratios, if 'surv' returns the survival function, if
        'cumhaz' returns the cumulative hazard function.
    pred_only : bool
        If True, returns only an array of predicted values.  Otherwise
        returns a bunch containing the predicted values and standard
        errors.

    Returns
    -------
    A bunch containing two fields: `predicted_values` and
    `standard_errors`.

    Notes
    -----
    Standard errors are only returned when predicting the log
    hazard ratio (pred_type is 'lhr').

    Types `surv` and `cumhaz` require estimation of the cumulative
    hazard function.
zK
    params : array_like
        The proportional hazards model parameters.z
    cov_params : array_like
        The covariance matrix of the estimated `params` vector,
        used to obtain prediction errors if pred_type='lhr',
        otherwise optional.c                   &    e Zd Z	 	 ddZd Zd ZdS )PHSurvivalTimeNc                 
   |-t          j        t          |          t           j                  }|!t          j        t          |                    }|                     |||           t          j        |          }d |D             t          |          D ] \  }}	|	                             |           !fd|D             |fdt                    D             }
t                    | _        fd|
D             fd|
D             t                    }|| _	        t                    D ]\\  }}
t          ||
         |
         dk                       fd	t          ||
                   D             }|         |         |<   ]t                    D ]\\  }}
t          ||
         |
         dk                       fd
t          ||
                   D             }|         |         |<   ]t                    D ]0\  }}
t          j        ||
                   }|         |         |<   1|@g | _        t          |          D ](}| j                            ||                             )nd| _        t          d D                       | _        | _        | _        |                     |          | _        |                     |          | _        |                               | _        |                     |          | _        g g g g f\  | _        | _        | _        | _        t          | j	                  D ]'}t          j        | j        |         dk              }| j        |         |         }t          j        |          }t          |          }d t          |          D             }d t          |          D             }t;          ||          D ]&\  }
}|||                                      |
           'd t          |          D             }t          | j        |                   D ]?\  }}t          j        ||d          dz
  }
|
dk    r||
                             |           @d t          |          D             }t          | j        |                   D ]5\  }}t          j        ||          }
||
                             |           6| j                            |           | j                            d |D                        | j                            d |D                        | j                            d |D                        )dS )a  
        Represent a collection of survival times with possible
        stratification and left truncation.

        Parameters
        ----------
        time : array_like
            The times at which either the event (failure) occurs or
            the observation is censored.
        status : array_like
            Indicates whether the event (failure) occurs at `time`
            (`status` is 1), or if `time` is a censoring time (`status`
            is 0).
        exog : array_like
            The exogeneous (covariate) data matrix, cases are rows and
            variables are columns.
        strata : array_like
            Grouping variable defining the strata.  If None, all
            observations are in a single stratum.
        entry : array_like
            Entry (left truncation) times.  The observation is not
            part of the risk set for times before the entry time.  If
            None, the entry time is treated as being zero, which
            gives no left truncation.  The entry time must be less
            than or equal to `time`.
        offset : array_like
            An optional array of offsets
        Ndtypec                     i | ]}|g S  r   .0xs     f/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/duration/hazard_regression.py
<dictcomp>z+PHSurvivalTime.__init__.<locals>.<dictcomp>   s    """q""""    c                 \    g | ](}t          j        |         t           j                   )S r	   npasarrayint32)r   ksths     r   
<listcomp>z+PHSurvivalTime.__init__.<locals>.<listcomp>   s.    HHHq
3q6:::HHHr   c                 V    g | ]%\  }}|                                          d k    #|&S )r   )sum)r   iixstatuss      r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s5    LLLDAbvbz~~7G7G!7K7Ka7K7K7Kr   c                      g | ]
}|         S r   r   )r   r   stratum_rowss     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s    444AQ444r   c                      g | ]
}|         S r   r   )r   r   stratum_namess     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s    666aq)666r      c                 &    g | ]\  }}|k    |S r   r   )r   r   tlast_failures      r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s1     % % %!|## ###r   c                 &    g | ]\  }}|k    |S r   r   )r   r   r'   first_failures      r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s1     & & &!}$$ $$$r   c                 ,    g | ]}t          |          S r   )len)r   r   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s    999b#b''999r   c                     i | ]\  }}||	S r   r   )r   r   r   s      r   r   z+PHSurvivalTime.__init__.<locals>.<dictcomp>   s    666!q!666r   c                     g | ]}g S r   r   r   r   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s    ...Qb...r   c                     g | ]}g S r   r   r/   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s    333!2333r   rightr   c                     g | ]}g S r   r   r/   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s    222"222r   c                 N    g | ]"}t          j        |t           j                   #S r   r   r   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s9     #4 #4 #4'( $&:arx#@#@#@ #4 #4 #4r   c                 N    g | ]"}t          j        |t           j                   #S r   r   r   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s9     $: $: $:() %'Jq$A$A$A $: $: $:r   c                 N    g | ]"}t          j        |t           j                   #S r   r   r   s     r   r   z+PHSurvivalTime.__init__.<locals>.<listcomp>   s9     #8 #8 #8'( $&:arx#@#@#@ #8 #8 #8r   )r   zerosr,   r   _checkunique	enumerateappendnstrat_orignstratmaxminargsortoffset_sranger   n_obsr"   r$   _splittime_sexog_sstatus_sentry_s	ufailt_ix
risk_enter	risk_exitufailtflatnonzerozipsearchsorted)selftimer    exogstrataentryoffsetstur   r   r   r<   stxiiiftftuftnuftuft_mapuft_ixtirisk_enter1r'   
risk_exit1r*   r(   r   r$   r"   s     `                     @@@@@r   __init__zPHSurvivalTime.__init__W   s   @ >Xc$iirx888F =HSYY''E 	D&&%000 i""c"""V$$ 	 	CAaFMM!HHHHCHHH MLLLIl33LLL|,,444444466662666 \""  -- 	6 	6FCtBxr
a899L% % % %yr33 % % %B ,S 1" 5L  -- 	6 	6FCRq 9::M& & & &yb22 & & &B ,S 1" 5L  -- 	6 	6FCDH%%B ,S 1" 5LDMV}} @ @$$VL,=%>????@ !DM 99L999::
(* kk$''kk$''F++{{5))" BN 	E %% &	9 &	9C .s!3q!899CS!#&B )B--Cs88D 76y~~666G..%++...FS / /2wr{#**2.... 43uT{{333K S!122 . .!_S!W55977O**1--- 32eDkk222J c!233 ) )!_S!,,2%%a((((Ks###N!! #4 #4,2#4 #4 #4 5 5 5O"" $: $:-8$: $: $: ; ; ;N!! #8 #8,6#8 #8 #8 9 9 9 9K&	9 &	9r   c                     g }|j         dk    r&| j        D ]}|                    ||                    n)| j        D ]!}|                    ||d d f                    "|S )Nr%   )ndimr"   r:   )rO   r   vr   s       r   rC   zPHSurvivalTime._split   sx    6Q;;'    2  ' # #2qqq5""""r   c                    t          |          t          |          t          |          t          |          f\  }}}}||||g}	t          |	          t          |	          k    rt          d          t          |          dk     rt          d          t          |          dk     rt          d          t	          j        ||k              rt          d          d S )Nz>endog, status, strata, and entry must all have the same lengthr   zendog must be non-negativezentry time must be non-negativez8entry times may not occur after event or censoring times)r,   r=   r>   
ValueErrorr   any)
rO   rP   r    rR   rS   n1n2n3n4nvs
             r   r7   zPHSurvivalTime._check  s    TCKKVJJBB"b"r77c"gg C D D Dt99q==9:::u::>>>??? 6%$, 	? > ? ? ?	? 	?r   )NNN)__name__
__module____qualname__ra   rC   r7   r   r   r   r   r   U   sQ        >B^9 ^9 ^9 ^9@  ? ? ? ? ?r   r   c                        e Zd ZdZ	 	 	 d fd	Ze	 	 	 d  fd	            Zd! fd	Z	 	 d"dZd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z eeeedz            	 	 d#d            Zd$dZ xZS )%PHRega  
    Cox Proportional Hazards Regression Model

    The Cox PH Model is for right censored data.

    Parameters
    ----------
    endog : array_like
        The observed times (event or censoring)
    exog : 2D array_like
        The covariates or exogeneous variables
    status : array_like
        The censoring status values; status=1 indicates that an
        event occurred (e.g. failure or death), status=0 indicates
        that the observation was right censored. If None, defaults
        to status=1 for all cases.
    entry : array_like
        The entry times, if left truncation occurs
    strata : array_like
        Stratum labels.  If None, all observations are taken to be
        in a single stratum.
    ties : str
        The method used to handle tied times, must be either 'breslow'
        or 'efron'.
    offset : array_like
        Array of offset values
    missing : str
        The method used to handle missing data

    Notes
    -----
    Proportional hazards regression models should not include an
    explicit or implicit intercept.  The effect of an intercept is
    not identified using the partial likelihood approach.

    `endog`, `event`, `strata`, `entry`, and the first dimension
    of `exog` all must have the same length
    Nbreslowdropc	           	         |!t          j        t          |                    } t                      j        ||f|||||d|	 | j        t          j        | j                  | _        | j        t          j        | j                  | _        | j        t          j        | j                  | _        | j	        t          j        | j	                  | _	        t          | j        | j        | j        | j        | j        | j	                  | _        t          | j                  | _        d | _        || _        t#          | j        j        d         t           j                            | j                  z
            | _        t#          t           j                            | j                            | _        |                                }|dvrt1          d          || _        d S )N)r    rS   rR   rT   missingr   )efronrr   z*`ties` must be either `efron` or `breslow`)r   onesr,   superra   r    r   rS   rR   rT   r   endogrQ   survnobsgroupsru   floatshapelinalgmatrix_rankdf_residdf_modellowerrf   ties)rO   ry   rQ   r    rS   rR   rT   r   ru   kwargs	__class__s             r   ra   zPHReg.__init__;  s   
 >WSZZ((F 	.V*/+17	. 	. '-	. 	. 	. ;"*T[11DK:!DJ//DJ;"*T[11DK;"*T[11DK"4:t{$(It{$(J= =	 
OO	 dioa0 i33DI>>? @ @bi33DI>>??zz||+++ ) * * * 			r   c
                    t          |t                    r||         }t          |t                    r||         }t          |t                    r||         }t          |t                    r||         }ddl}|                    d|          }|D ]3}|                                }|dv rddl}|                    d           4 t                      j        ||g|
R |||||||	dgd|}|S )aK  
        Create a proportional hazards regression model from a formula
        and dataframe.

        Parameters
        ----------
        formula : str or generic Formula object
            The formula specifying the model
        data : array_like
            The data for the model. See Notes.
        status : array_like
            The censoring status values; status=1 indicates that an
            event occurred (e.g. failure or death), status=0 indicates
            that the observation was right censored. If None, defaults
            to status=1 for all cases.
        entry : array_like
            The entry times, if left truncation occurs
        strata : array_like
            Stratum labels.  If None, all observations are taken to be
            in a single stratum.
        offset : array_like
            Array of offset values
        subset : array_like
            An array-like object of booleans, integers, or index
            values that indicate the subset of df to use in the
            model. Assumes df is a `pandas.DataFrame`
        ties : str
            The method used to handle tied times, must be either 'breslow'
            or 'efron'.
        missing : str
            The method used to handle missing data
        args : extra arguments
            These are passed to the model
        kwargs : extra keyword arguments
            These are passed to the model with one exception. The
            ``eval_env`` keyword is passed to patsy. It can be either a
            :class:`patsy:patsy.EvalEnvironment` object or an integer
            indicating the depth of the namespace to use. For example, the
            default ``eval_env=0`` uses the calling namespace. If you wish
            to use a "clean" environment set ``eval_env=-1``.

        Returns
        -------
        model : PHReg model instance
        r   Nz[+\-~])01z6PHReg formulas should not include any '0' or '1' terms	Intercept)r    rS   rR   rT   subsetr   ru   	drop_cols)	
isinstancestrresplitstripwarningswarnrx   from_formula)clsformuladatar    rS   rR   rT   r   r   ru   argsr   r   termstermr   modr   s                    r   r   zPHReg.from_formulag  s1   f fc"" 	"&\FeS!! 	 KEfc"" 	"&\Ffc"" 	"&\F			G,, 	X 	XD::<<Dz!!VWWW"egg"7D  @D !v!&t#}  	  
r   c                    |tt          |          t          | j                  k    r5dt          |          t          | j                  fz  }t          |          t          j        |          | _        nd| _        d|vrd|d<    t                      j        di |}| j        |                                }n| 	                    |j
                  }t          | |j
        |          }|S )a  
        Fit a proportional hazards regression model.

        Parameters
        ----------
        groups : array_like
            Labels indicating groups of observations that may be
            dependent.  If present, the standard errors account for
            this dependence. Does not affect fitted values.

        Returns
        -------
        PHRegResults
            Returns a results instance.
        Nz+len(groups) = %d and len(endog) = %d differdispFr   )r,   ry   rf   r   r   r|   rx   fit
cov_paramsrobust_covarianceparamsPHRegResults)rO   r|   r   msg	fit_rsltsr   resultsr   s          r   r   z	PHReg.fit  s    $ 6{{c$*oo--DFS__56 oo%*V,,DKKDK DLEGGK''$''	;"--//JJ//	0@AAJtY%5zBBr   elastic_net        Fc                     ddl m} |dk    rt          d          ddddd}|                    |            || f||||d	|S )
af  
        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.
        **kwargs
            Additional keyword arguments used to fit the model.

        Returns
        -------
        PHRegResults
            Returns a results instance.

        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 line searches
        zero_tol : float
            Coefficients below this threshold are treated as zero.
        r   )fit_elasticnetr   z.method for fit_regularized must be elastic_net2   r%   g|=)maxiterL1_wt	cnvrg_tolzero_tol)methodalphastart_paramsrefit)statsmodels.base.elastic_netr   rf   update)rO   r   r   r   r   r   r   defaultss           r   fit_regularizedzPHReg.fit_regularized  s    r 	@?????]""MNNN "au!&( (~d *6$)+7$)* * !)	* * 	*r   c                     | j         dk    r|                     |          S | j         dk    r|                     |          S dS )z\
        Returns the log partial likelihood function evaluated at
        `params`.
        rr   rv   N)r   breslow_loglikeefron_loglikerO   r   s     r   loglikezPHReg.loglike%  sN     9	!!''///Y'!!%%f--- "!r   c                     | j         dk    r|                     |          S | j         dk    r|                     |          S dS )zC
        Returns the score function evaluated at `params`.
        rr   rv   N)r   breslow_gradientefron_gradientr   s     r   scorezPHReg.score0  sN    
 9	!!((000Y'!!&&v... "!r   c                 l    | j         dk    r|                     |          S |                     |          S )zr
        Returns the Hessian matrix of the log partial likelihood
        function evaluated at `params`.
        rr   )r   breslow_hessianefron_hessianr   s     r   hessianzPHReg.hessian:  s9     9	!!''///%%f---r   c                    | j         }d}t          |j                  D ]:}|j        |         }|j        |         }t          |          }t          j        ||          }|j        ||j        |         z  }||	                                z  }t          j
        |          }	d}
t          |          ddd         D ]}|j        |         |         }|
|	|                                         z  }
||         }|||         t          j        |
          z
                                  z  }|j        |         |         }|
|	|                                         z  }
<|S )z
        Returns the value of the log partial likelihood function
        evaluated at `params`, using the Breslow method to handle tied
        times.
        r   N)rz   rA   r<   rH   rE   r,   r   dotr@   r=   exprI   r   logrJ   )rO   r   rz   likerV   r]   rE   r[   linpred	e_linpredxp0r   r   s                r   r   zPHReg.breslow_loglikeE  sX    y %% 	+ 	+C^C(F[%Fv;;DfVV,,G}(4=--w{{}}$GwIC 4[[2& + + _S)!,y}((*** AYrvc{{277999 ^C(+y}((***+ r   c                    | j         }d}t          |j                  D ]}|j        |         }t	          j        ||          }|j        ||j        |         z  }||                                z  }t	          j        |          }d}|j	        |         }	t          |	          }
t          |
          ddd         D ]	}|j        |         |         }|||                                         z  }||	|                                                  }|	|         }|||                                         z  }t          |          }t	          j        |t          j                  |z  }|t	          j        |||z  z
                                            z  }|j        |         |         }|||                                         z  }|S )z
        Returns the value of the log partial likelihood function
        evaluated at `params`, using the Efron method to handle tied
        times.
        r   Nr   r	   )rz   rA   r<   rE   r   r   r@   r=   r   rH   r,   rI   r   arangefloat64r   rJ   )rO   r   rz   r   rV   rE   r   r   r   r]   r[   r   r   xp0fmJs                   r   r   zPHReg.efron_loglikep  s    y %%  	+  	+C [%FfVV,,G}(4=--w{{}}$GwIC ^C(Fv;;D4[[2& + + _S)!,y}((*** +//11 AY)))GGIarz222Q6sQtV|,,00222 ^C(+y}((***#+& r   c                    | j         }d}t          |j                  D ]}|j        |         }|j        |         }t          |          }|j        |         }t          j        ||          }	|j	        |	|j	        |         z  }	|	|	
                                z  }	t          j        |	          }
d\  }}t          |          ddd         D ]'}|j        |         |         }t          |          dk    rT||ddf         }||
|                                         z  }||
|         dddf         |z                      d          z  }||         }|||ddf         ||z  z
                      d          z  }|j        |         |         }t          |          dk    rT||ddf         }||
|                                         z  }||
|         dddf         |z                      d          z  })|S )z|
        Returns the gradient of the log partial likelihood, using the
        Breslow method to handle tied times.
        r   Nr   r   r   r   )rz   rA   r<   r"   rH   r,   rE   r   r   r@   r=   r   rI   r   rJ   )rO   r   rz   gradrV   strat_ixr]   r[   rE   r   r   r   xp1r   r   rd   s                   r   r   zPHReg.breslow_gradient  s    y %% &	> &	>C (-H ^C(Fv;;D [%FfVV,,G}(4=--w{{}}$GwIHC 4[[2& > > _S)!,r77Q;;r!!!tA9R=,,...CIbM!!!D&1A5::1===C AY111c	166q999 ^C(+r77Q;;r!!!tA9R=,,...CIbM!!!D&1A5::1===C%>( r   c                    | j         }d}t          |j                  D ]}|j        |         }|j        |         }t          j        ||          }|j        ||j        |         z  }||                                z  }t          j	        |          }d\  }	}
|j
        |         }t          |          }t          |          ddd         D ]}|j        |         |         }t          |          dk    rT||ddf         }|	||                                         z  }	|
||         dddf         |z                      d          z  }
||         }t          |          dk    r||ddf         }||                                         }||         dddf         |z                      d          }||                    d          z  }t          |          }t          j        |t
          j                  |z  }|
t          j        ||          z
  }|	t          j        ||          z
  }||z  }|                    d          }||z  }|j        |         |         }t          |          dk    rT||ddf         }|	||                                         z  }	|
||         dddf         |z                      d          z  }
|S )z
        Returns the gradient of the log partial likelihood evaluated
        at `params`, using the Efron method to handle tied times.
        r   Nr   r   r   r	   )rz   rA   r<   r"   rE   r   r   r@   r=   r   rH   r,   rI   r   r   r   outerrJ   )rO   r   rz   r   rV   r   rE   r   r   r   r   r]   r[   r   r   rd   ixfr   xp1fr   r   numerdenomratiorsums                            r   r   zPHReg.efron_gradient  s    y %% 0	> 0	>C (-H [%FfVV,,G}(4=--w{{}}$GwIHC ^C(Fv;;D4[[2& > > _S)!,r77Q;;r!!!tA9R=,,...CIbM!!!D&1A5::1===CQis88a<<s111uA$S>--//D%cN111T62Q6;;A>>D AEE!HH$DCA	!2:666:A"(1d"3"33E"(1d"3"33E!EME 99Q<<DDLD ^C(+r77Q;;r!!!tA9R=,,...CIbM!!!D&1A5::1===C=>@ r   c           	      z   | j         }d}t          |j                  D ]}|j        |         }t	          |          }|j        |         }t          j        ||          }|j        ||j        |         z  }||	                                z  }t          j
        |          }	d\  }
}}t          |          ddd         D ]w}|j        |         |         }t	          |          dk    rv|
|	|                                         z  }
||ddf         }||	|         dddf         |z                      d          z  }|	|         }|t          j        d|||          z  }t	          ||                   }||||
z  t          j        ||          |
dz  z  z
  z  z  }|j        |         |         }t	          |          dk    rv|
|	|                                         z  }
||ddf         }||	|         dddf         |z                      d          z  }|	|         }|t          j        d|||          z  }y| S )z
        Returns the Hessian of the log partial likelihood evaluated at
        `params`, using the Breslow method to handle tied times.
        r   Nr   r   r   r   r   ij,ik,i->jk   )rz   rA   r<   rH   r,   rE   r   r   r@   r=   r   rI   r   einsumr   rJ   )rO   r   rz   hessrV   r]   r[   rE   r   r   r   r   xp2r   r   rd   elxr   s                     r   r   zPHReg.breslow_hessian  sM    y %% &	? &	?C^C(Fv;;D[%FfVV,,G}(4=--w{{}}$GwI&MCc 4[[2& ? ? _S)!,r77Q;;9R=,,...Cr!!!tAIbM!!!D&1A5::1===C#B-C29]Aq#>>>C q	NN39c(:(:S!V(CCDD ^C(+r77Q;;9R=,,...Cr!!!tAIbM!!!D&1A5::1===C#B-C29]Aq#>>>C-?. ur   c           	         | j         }d}t          |j                  D ](}|j        |         }t	          j        ||          }|j        ||j        |         z  }||                                z  }t	          j        |          }d\  }}	}
|j	        |         }t          |          }t          |          ddd         D ]}|j        |         |         }t          |          dk    rv|||                                         z  }||ddf         }|	||         dddf         |z                      d          z  }	||         }|
t	          j        d|||          z  }
||         }t          |          dk    rm||ddf         }||                                         }||         dddf         |z                      d          }||         }t	          j        d|||          }t          ||                   }t	          j        |t          j                  |z  }|||z  z
  }||
t	          j        d|z            z  z  }||t	          j        ||z            z  z  }|	dddf         t	          j        ||          z
  |dddf         z  }|t	          j        d	||          z  }|j        |         |         }t          |          dk    rv|||                                         z  }||ddf         }|	||         dddf         |z                      d          z  }	||         }|
t	          j        d|||          z  }
*| S )
z
        Returns the Hessian matrix of the partial log-likelihood
        evaluated at `params`, using the Efron method to handle tied
        times.
        r   Nr   r   r   r   r	   r%   z	ij,ik->jk)rz   rA   r<   rE   r   r   r@   r=   r   rH   r,   rI   r   r   r   r   r   rJ   )rO   r   rz   r   rV   rE   r   r   r   r   r   r]   r[   r   r   rd   r   r   r   r   xp2fr   r   c0mats                            r   r   zPHReg.efron_hessianH  sP    y %% 2	? 2	?C[%FfVV,,G}(4=--w{{}}$GwI&MCc ^C(Fv;;D4[[2& #? #? _S)!,r77Q;;9R=,,...Cr!!!tAIbM!!!D&1A5::1===C#B-C29]Aq#>>>CQis88a<<s111uA$S>--//D%cN111T62Q6;;A>>D#C.C9]Aq#>>D q	NNIarz222Q61T6\bfQVnn,,rva"f~~--47|bhq$&7&772aaag;F	+sC888 ^C(+r77Q;;9R=,,...Cr!!!tAIbM!!!D&1A5::1===C#B-C29]Aq#>>>CG#?J ur   c                 T   | j         t          d          |                     |          }|                     |          }i }t	          | j                   D ](\  }}||vrd||<   ||xx         ||ddf         z  cc<   )t          j        t          |                                                    }|dddddf         }|j	        |z  }|
                    d          }t
          j                            |          }t          j        |t          j        ||                    }	|	S )a  
        Returns a covariance matrix for the proportional hazards model
        regresion coefficient estimates that is robust to certain
        forms of model misspecification.

        Parameters
        ----------
        params : ndarray
            The parameter vector at which the covariance matrix is
            calculated.

        Returns
        -------
        The robust covariance matrix as a square ndarray.

        Notes
        -----
        This function uses the `groups` argument to determine groups
        within which observations may be dependent.  The covariance
        matrix is calculated using the Huber-White "sandwich" approach.
        NzD`groups` must be specified to calculate the robust covariance matrixr   r%   )r|   rf   r   score_residualsr9   r   r   listvaluesTr   r   invr   )
rO   r   r   	score_obsgradsr   gr   hess_invcmats
             r   r   zPHReg.robust_covariance  s   . ;cddd||F##((00	 T[)) 	( 	(CAa~~a!HHH	!QQQ$'HHHH
4//00D!!!QQQJeckggajj9==&&vhsH 5 566r   c           	         | j         }t          j        | j        j        t          j                  }t          j        | j        j        d         t          j                  }|                     |          }t          |j	                  D ]}|j
        |         }|j        |         }t          |          }	|j        |         }
d}t          j        ||          }|j        ||j        |         z  }||                                z  }t          j        |          }t%                      }t          |	          ddd         D ]9}|j        |         |         }|t%          |          z  }|||                                         z  }t+          |          }||ddf         ||         |ddf         z
  }t          j        |j        d                   }d|||         <   t          ||                   |z  }||         ||         |z  z
  }|
|         }||ddfxx         ||dddf         z  z  cc<   d||<   |j        |         |         }|t%          |          z  }|||                                         z  };t          j        |dk              }t          |          dk    rt          j        ||ddf<   |S )a  
        Returns the score residuals calculated at a given vector of
        parameters.

        Parameters
        ----------
        params : ndarray
            The parameter vector at which the score residuals are
            calculated.

        Returns
        -------
        The score residuals, returned as a ndarray having the same
        shape as `exog`.

        Notes
        -----
        Observations in a stratum with no observed events have undefined
        score residuals, and contain NaN in the returned matrix.
        r	   r   r   Nr   r%   )rz   r   r6   rQ   r~   r   r   weighted_covariate_averagesrA   r<   rH   rE   r,   r"   r   r@   r=   r   setrI   r   r   rJ   rL   nan)rO   r   rz   score_residmaskw_avgrV   r]   rE   r[   r   r   r   r   
at_risk_ixr   r   atr_ixleverageddchazmrprW   jjs                           r   r   zPHReg.score_residuals  s   , yhtybjAAA x	*"(;;;0088 %% .	+ .	+C^C(F[%Fv;;D(-HCfVV,,G}(4=--w{{}}$GwIJ 4[[2& + + _S)!,c"gg%
y}((***j))!&!!!),uSz!QQQ$/?? HV\!_-- &) F1I, i)F"3e";; f%BqqqD!!!XAAAtG%<<!!!R ^C(+c"gg%
y}((***9+< ^DAI&&r77Q;;!#KAAAr   c           
         | j         }g }d\  }}t          |j                  D ]}|j        |         }|j        |         }t          |          }	t          j        t          |          |j        d         ft          j	                  }
t          j
        ||          }|j        ||j        |         z  }||                                z  }t          j        |          }t          |	          ddd         D ]}|j        |         |         }|||                                         z  }|t          j
        ||         ||ddf                   z  }||z  |
|ddf<   |j        |         |         }|||                                         z  }|t          j
        ||         ||ddf                   z  }|                    |
           |S )a  
        Returns the hazard-weighted average of covariate values for
        subjects who are at-risk at a particular time.

        Parameters
        ----------
        params : ndarray
            Parameter vector

        Returns
        -------
        averages : list of ndarrays
            averages[stx][i,:] is a row vector containing the weighted
            average values (for all the covariates) of at-risk
            subjects a the i^th largest observed failure time in
            stratum `stx`, using the hazard multipliers as weights.

        Notes
        -----
        Used to calculate leverages and score residuals.
        r   r%   r	   Nr   )rz   rA   r<   rH   rE   r,   r   r6   r~   r   r   r@   r=   r   rI   r   rJ   r:   )rO   r   rz   averagesr   r   rV   r]   rE   r[   	average_sr   r   r   r   s                  r   r   z!PHReg.weighted_covariate_averages  s   . yS %% 	' 	'C^C(F[%Fv;;D#f++v|A!?(*
4 4 4I fVV,,G}(4=--w{{}}$GwI 4[[2& < < _S)!,y}((***rvimVBE];;;"%)	!QQQ$ ^C(+y}((***rvimVBE];;;OOI&&&&r   c                 2   | j         }g }t          |j                  D ]w}|j        |         }|j        |         }|j        |         }t          |          }t          j        ||          }	|j	        |	|j	        |         z  }	t          j
        |	          }
d}t          j        |t          j                  }t          |          ddd         D ]}|j        |         |         }||
|                                         z  }||         }t          |          |z  ||<   |j        |         |         }||
|                                         z  }t          j        |          |z
  }t          j
        |           }|                    |||g           y|S )a  
        Estimate the baseline cumulative hazard and survival
        functions.

        Parameters
        ----------
        params : ndarray
            The model parameters.

        Returns
        -------
        A list of triples (time, hazard, survival) containing the time
        values and corresponding cumulative hazard and survival
        function values for each stratum.

        Notes
        -----
        Uses the Nelson-Aalen estimator.
        Nr   r	   r   )rz   rA   r<   rK   rH   rE   r,   r   r   r@   r   r6   r   rI   r   rJ   cumsumr:   )rO   r   rz   rsltrV   rZ   r]   rE   r[   r   r   r   h0r   r   cumhazcurrent_strata_survs                    r   baseline_cumulative_hazardz PHReg.baseline_cumulative_hazardN  s   0 y %%  	<  	<C+c"C^C(F[%Fv;;DfVV,,G}(4=--wIC$bj111B 4[[2& + + _S)!,y}((*** AYB#1 ^C(+y}((***Yr]]R'F"$&&//KKf&9:;;;;r   c                    ddl m} | j        }|                     |          }i }t	          |j                  D ]}||         d         }||         d         }t          j        t          j         |t          j        f         }t          j        |d         ||d         f         } |||d          }	|	|| j        j	        |         <   |S )a\  
        Returns a function that calculates the baseline cumulative
        hazard function for each stratum.

        Parameters
        ----------
        params : ndarray
            The model parameters.

        Returns
        -------
        A dict mapping stratum names to the estimated baseline
        cumulative hazard function.
        r   )interp1dr%   r   zero)kind)
scipy.interpolater  rz   r  rA   r<   r   r_infr$   )
rO   r   r  rz   basecumhaz_frV   time_hr  funcs
             r   #baseline_cumulative_hazard_functionz)PHReg.baseline_cumulative_hazard_function  s      	/.....y..v66%% 	: 	:C#Yq\F#Yq\FUBF7FBF23FU6!9ffRj89F8FF888D59HTY,S122r   
params_doccov_params_doclhrc	                    |                                 }|dvrd|z  }	t          |	           G d d          }
 |
            }d}|	| j        }d}t          j        ||          }|||z  }n| j        |s
|| j        z  }|dk    rZ||_        |Ft          j        ||          }||z                      d          }t          j        |          |_	        |r|j        S |S t          j
        |          }|d	k    r||_        |r|j        S |S ||rd
}	t          |	          |	|s| j        }|S|r| j        j        dk    rt          d          | j        $| j        j        d         gt!          |          z  }n| j        }t          j        t          j        t!          |          t          j                  z  }t          j        |          }|                     |          }|D ]?}t          j        ||k              }||         } |||                   ||         z  ||<   @|dk    r||_        n |dk    rt          j
        |           |_        |r|j        S |S )N)r  hrrz   r  z"Type %s not allowed for predictionc                       e Zd ZdZdZdS )PHReg.predict.<locals>.bunchN)rm   rn   ro   predicted_valuesstandard_errorsr   r   r   bunchr"    s        #"OOOr   r%  TFr  r%   r   z/If `exog` is provided `endog` must be provided.z`strata` must be providedr   r	   r  rz   )r   rf   rQ   r   r   rT   r#  r   sqrtr$  r   ry   rz   r<   rR   r$   r,   r   rw   r   r8   r  rL   )rO   r   rQ   r   ry   rR   rT   	pred_type	pred_onlyr   r%  ret_valexog_providedr  r   var   r  stvbhazrV   r   r  s                          r   predictzPHReg.predict  s    OO%%	;;;6BCS//!	# 	# 	# 	# 	# 	# 	# 	# %''
 <9D!MfT6""6MCC[$]$4;C
 '*G$%fT:..Dj%%a((*,'"++' 0//NVC[[')G$ 0//N =]=CCS//!]=]JE > >!1A!5!5 !<==={")1!46UC"'#e**BJ????i77?? 	2 	2C#..B9DeBi2b61F2JJ  '-G$$&  ')vvgG$ 	,++r         ?c           	         | j         }|                     |          }g g }}||j        }n| j                             |          }t	          | j         j                  D ]8}	||	         }
t          j        |
|          }|j        ||j        |	         z  }t          j	        |          }||	         d         }t          j
        |||	         d                   }t          j	        |           }t          j        |j        d         df          }t          j        ||fd          }t          j        |d           }|                    |           |                    t          j
        t          j        |j        d                   |                     :t#          d |D                       }t	          | j         j                  D ]}||         j        d         |k     rt          j        ||         j        d         |f          }t          j        ||         j        d         |f          }||         |ddd||         j        d         f<   ||         |ddd||         j        d         f<   ||c||<   ||<   t          j        t          j        t'          | j                  |f          z  }t          j        t'          | j                  |ft          j                  |z  }t	          | j         j                  D ]2}	| j         j        |	         }||	         ||ddf<   ||	         ||ddf<   3t/          ||          }|S )a  
        Returns a scipy distribution object corresponding to the
        distribution of uncensored endog (duration) values for each
        case.

        Parameters
        ----------
        params : array_like
            The proportional hazards model parameters.
        scale : float
            Present for compatibility, not used.
        exog : array_like
            A design matrix, defaults to model.exog.

        Returns
        -------
        A list of objects of type scipy.stats.distributions.rv_discrete

        Notes
        -----
        The distributions are obtained from a simple discrete estimate
        of the survivor function that puts all mass on the observed
        failure times within a stratum.
        Nr   r%   axisc                 (    g | ]}|j         d          S )r%   )r~   r   s     r   r   z*PHReg.get_distribution.<locals>.<listcomp>K  s    ***!171:***r   r	   )rz   r  rE   rC   rA   r<   r   r   r@   r   r   r6   r~   concatenatediffr:   rw   r=   r   r,   ry   r   r"   rv_discrete_float)rO   r   scalerQ   rz   r-  pkxk
exog_splitrV   rE   r   r   ptsichazusurvzprobsmxcr   xk1pk1xkapkar   dists                             r   get_distributionzPHReg.get_distribution  s   4 y..v66 RB<JJ))$//J)** 	> 	>C_FfVV,,G}(4=--wI s)A,C HYS	!55E FE6NNE%+a.!,--ANE1:A666E WUA&&&EIIeIIbhrwu{1~66<<==== **r***++ty'(( 	( 	(A!u{1~##h1A455h1A455+-a5AAAqAQ''(+-a5AAAqAQ''("C1r!u frwDJ5666gs4:,BJ???#E)** 	! 	!C',BCCAAAJCCAAAJJ c**r   )NNNNrr   rs   )NNNNNrr   rs   N)r   r   NF)NNNNNr  F)r/  N)rm   rn   ro   __doc__ra   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   _predict_docstring_predict_params_doc_predict_cov_params_docstringr.  rF  __classcell__r   s   @r   rq   rq     s"       % %N 8<09* * * * * *X <@6:-3I I I I I [IV' ' ' ' ' 'R ;=16F* F* F* F*R	. 	. 	./ / /	. 	. 	.) ) )V. . .`3 3 3j= = =~2 2 2h@ @ @D- - -^T T Tl= = =~> > >@  > X )7$9 $9 9 : : AEEJV V V: :VpV V V V V V V Vr   rq   c                   &    e Zd ZdZd fd	Zed             Zed             Zd Z e	e
ddd	z            	 	 d fd	            Zd Zed             Zed             Zed             Zed             Zed             Zed             ZddZ xZS )r   a  
    Class to contain results of fitting a Cox proportional hazards
    survival model.

    PHregResults inherits from statsmodels.LikelihoodModelResults

    Parameters
    ----------
    See statsmodels.LikelihoodModelResults

    Attributes
    ----------
    model : class instance
        PHreg model instance that called fit.
    normalized_cov_params : ndarray
        The sampling covariance matrix of the estimates
    params : ndarray
        The coefficients of the fitted model.  Each coefficient is the
        log hazard ratio corresponding to a 1 unit difference in a
        single covariate while holding the other covariates fixed.
    bse : ndarray
        The standard errors of the fitted parameters.

    See Also
    --------
    statsmodels.LikelihoodModelResults
    r/  naivec                     || _         |j        | _        |j        | _        t                                          ||d|           d S )Nr/  )r7  normalized_cov_params)covariance_typer   r   rx   ra   )rO   r   r   r   r7  rS  r   s         r   ra   zPHRegResults.__init__~  sS    
  /b!+ 	 	- 	- 	- 	- 	-r   c                 r    t          j        t          j        |                                                     S zI
        Returns the standard errors of the parameter estimates.
        )r   r&  diagr   rO   s    r   r$  zPHRegResults.standard_errors  s(    
 wrwt0011222r   c                     | j         S rU  )r$  rW  s    r   bsezPHRegResults.bse  s    
 ##r   c                 @    | j                             | j                  S )a  
        Returns a scipy distribution object corresponding to the
        distribution of uncensored endog (duration) values for each
        case.

        Returns
        -------
        A list of objects of type scipy.stats.distributions.rv_discrete

        Notes
        -----
        The distributions are obtained from a simple discrete estimate
        of the survivor function that puts all mass on the observed
        failure times within a stratum.
        )r   rF  r   rW  s    r   rF  zPHRegResults.get_distribution  s    " z**4;777r    r  NTr  c           	      x    t                                          |||                                 ||||          S )N)rQ   	transformr   ry   rR   rT   r'  )rx   r.  r   )rO   ry   rQ   rR   rT   r]  r'  r   s          r   r.  zPHRegResults.predict  sE     wwD;D<@OO<M<M7<8>8>;D  F F 	Fr   c                     t          j        |d          }|d         }|                                |                                |                                t          |          fS )z7
        Descriptive statistics of the groups.
        T)return_countsr%   )r   r8   r>   r=   meanr,   )rO   r|   gsizess      r   _group_statszPHRegResults._group_stats  sN     6666zz||VZZ\\6;;==#f++EEr   c                 @    | j                             | j                  S )z{
        The average covariate values within the at-risk set at each
        event time point, weighted by hazard.
        )r   r   r   rW  s    r   r   z(PHRegResults.weighted_covariate_averages  s     z55dkBBBr   c                 @    | j                             | j                  S )z:
        A matrix containing the score residuals.
        )r   r   r   rW  s    r   r   zPHRegResults.score_residuals  s    
 z))$+666r   c                 @    | j                             | j                  S )z
        A list (corresponding to the strata) containing the baseline
        cumulative hazard function evaluated at the event points.
        )r   r  r   rW  s    r   r  z'PHRegResults.baseline_cumulative_hazard  s     z44T[AAAr   c                 @    | j                             | j                  S )z
        A list (corresponding to the strata) containing function
        objects that calculate the cumulative hazard function.
        )r   r  r   rW  s    r   r  z0PHRegResults.baseline_cumulative_hazard_function  s     z==dkJJJr   c                 |   | j         j        }| j        }t          j        t          j        | j         j        j        t          j                  z  }t          |j
                  D ]}|j        |         }|j        |         }|j        |         }|j        |         }t          j        ||          }	t          j        |	t#          |          k               }
||
ddf         ||         |	|
         ddf         z
  |||
         ddf<   t          j        | j         j        dk              }
t          j        ||
ddf<   |S )z
        A matrix containing the Schoenfeld residuals.

        Notes
        -----
        Schoenfeld residuals for censored observations are set to zero.
        r	   Nr   )r   rz   r   r   r   rw   rQ   r~   r   rA   r<   rK   rE   rD   r"   rN   rL   r,   r    )rO   rz   r   	sch_residrV   rZ   rE   rD   r   rW   r  s              r   schoenfeld_residualsz!PHRegResults.schoenfeld_residuals  s'    z0 F274:?#8
KKKK	 %% 	O 	OC+c"C[%F[%F(-Hf--B
 SXX..B)/AAAsBrFAAAI9N)NIhrlAAAo&&^DJ-2336	"aaa%r   c                    | j         j        }t          j        t          j        t          | j         j                  t          j                  z  }| j        }t          |j
                  D ]}||         }|j        |         }|j        |         }t          j        || j                  }|j        ||j        |         z  }t          j        |          }	|j        |         }
 ||          }| j         j        |
         |	|z  z
  ||
<   |S )z+
        The martingale residuals.
        r	   )r   rz   r   r   rw   r,   ry   r   r  rA   r<   rE   rD   r   r   r@   r   r"   r    )rO   rz   
mart_residcumhaz_f_listrV   r  rE   rD   r   r   rW   chazs               r   martingale_residualsz!PHRegResults.martingale_residuals  s     z VBGC
(8$9$9LLLL
@ %% 	F 	FC$S)H[%F[%FfVT[11G}(4=--wI"3'B8F##D!Z.r2Y5EEJrNNr   皙?c                 
   ddl m} |                                }d}i }d|d<   || j        j        }||d<   | j        j                                        |d<   t          | j        j        j	                  |d	<   t          t          t          | j        j                                      |d
<   | j        j        D|                     | j        j                  \  }	}
}}d|z  |d<   d|	z  |d<   d|
z  |d<   d|z  |d<   | j        j        D|                     | j        j                  \  }	}
}}d|z  |d<   d|	z  |d<   d|
z  |d<   d|z  |d<   |                    |d|           |                    | |          }|                    ddd          }|                    ddt)          j        |d                              d|dz  z  }t)          j        |j        dd|f                   |j        dd|f<   dd |dz  z
  z  }t)          j        |j        dd|f                   |j        dd|f<   |||_        |                    ||!           |                    || "           |                    d#           | j        j        j        | j        j        j        z
  }|dk    r4|d k    r|                    d$           n|                    d%|z             | j        j        Qt          | j        j        dk              }|d k    r|                    d&           n|                    d'|z             | j        j        |                    d(           t=          | d)          r|                    d*           |S )+ay  
        Summarize the proportional hazards regression results.

        Parameters
        ----------
        yname : str, optional
            Default is `y`
        xname : list[str], optional
            Names for the exogenous variables, default is `x#` for ## in p 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.summary2.Summary : class to hold summary results
        r   )summary2z%8.3fzPH RegzModel:NzDependent variable:zTies:zSample size:zNum. events:z%.0fzNum groups:zMin group size:zMax group size:z%.1fzAvg group size:zNum strata:zMin stratum size:zMax stratum size:zAvg stratum size:l)alignfloat_format)r   zlog HRz	log HR SE)zCoef.zStd.Err.)columnsr   HRz[%.3fz%.3f]r%   )rt  )titler   z.Confidence intervals are for the hazard ratiosz&1 stratum dropped for having no eventsz&%d strata dropped for having no eventsz'1 observation has a positive entry timez)%d observations have positive entry timesz4Standard errors account for dependence within groupsregularizedz5Standard errors do not account for the regularization)statsmodels.iolibrq  Summaryr   endog_namesr   
capitalizer   rz   rB   intr   r    r|   rb  rR   add_dictsummary_paramsrenameinsertr   r   locindexadd_df	add_titleadd_textr;   r<   rS   hasattr)rO   ynamexnamerw  r   rq  smryrt  infomnmxavgnumparamadstratn_entrys                    r   summaryzPHRegResults.summary%  s   : 	/.....!!!X=J*E&+"#
2244W"4:?#899^"3s4:+<'='=#>#>??^:(#001BCCBC"(3,D&,rkD"#&,rkD"#&,slD"#:(#001BCCBC"(3,D(.D$%(.D$%(.D$%d#LAAA''E'::x2=&? &? @ @QbfU8_55666uqy!&111a411	!!!Q$q519}%&111a411	!!!Q$EKE555UD111FGGG,tz/EEA::{{FGGGGFOPPP:'$**a/00G!||GHHHHIGSTTT:(MMPQQQ4'' 	SMMQRRRr   )r/  rP  )NNNNTr  )NNNro  )rm   rn   ro   rH  ra   r   r$  rY  rF  r   rJ  r.  rb  r   r   r  r  ri  rn  r  rM  rN  s   @r   r   r   a  s        8
- 
- 
- 
- 
- 
- 3 3 ^3 $ $ ^$8 8 8& X "#K#KKLL487<F F F F F MLFF F F C C ^C 7 7 ^7 B B ^B K K ^K $ $ ^$L   ^@\ \ \ \ \ \ \ \r   r   c                   2    e Zd ZdZd ZddZd Zd Zd ZdS )	r6  a  
    A class representing a collection of discrete distributions.

    Parameters
    ----------
    xk : 2d array_like
        The support points, should be non-decreasing within each
        row.
    pk : 2d array_like
        The probabilities, should sum to one within each row.

    Notes
    -----
    Each row of `xk`, and the corresponding row of `pk` describe a
    discrete distribution.

    `xk` and `pk` should both be two-dimensional ndarrays.  Each row
    of `pk` should sum to 1.

    This class is used as a substitute for scipy.distributions.
    rv_discrete, since that class does not allow non-integer support
    points, or vectorized operations.

    Only a limited number of methods are implemented here compared to
    the other scipy distribution classes.
    c                 b    || _         || _        t          j        | j        d          | _        d S )Nr%   r1  )r9  r8  r   r	  cpk)rO   r9  r8  s      r   ra   zrv_discrete_float.__init__  s,    9TW1---r   Nc                    | j         j        d         }t          j                            |          }| j        |dddf         k                         d          }t          j        |t          j                  }| j         ||f         S )aD  
        Returns a random sample from the discrete distribution.

        A vector is returned containing a single draw from each row of
        `xk`, using the probabilities of the corresponding row of `pk`

        Parameters
        ----------
        n : not used
            Present for signature compatibility
        r   )sizeNr%   r	   )	r9  r~   r   randomuniformr  r   r   r   )rO   nur   rW   s        r   rvszrv_discrete_float.rvs  sw     GM!I1%%h111d7#((++Yq)))w2wr   c                 F    | j         | j        z                      d          S )z
        Returns a vector containing the mean values of the discrete
        distributions.

        A vector is returned containing the mean value of each row of
        `xk`, using the probabilities in the corresponding row of
        `pk`.
        r%   )r9  r8  r   rW  s    r   r`  zrv_discrete_float.mean  s!     $'!&&q)))r   c                     |                                  }| j        |dddf         z
  }| j        | j        |z
  dz  z                      d          S )z
        Returns a vector containing the variances of the discrete
        distributions.

        A vector is returned containing the variance for each row of
        `xk`, using the probabilities in the corresponding row of
        `pk`.
        Nr   r%   )r`  r9  r8  r   )rO   r  xkcs      r   varzrv_discrete_float.var  sO     YY[[g111d7#47S=1,,11!444r   c                 N    t          j        |                                           S )a  
        Returns a vector containing the standard deviations of the
        discrete distributions.

        A vector is returned containing the standard deviation for
        each row of `xk`, using the probabilities in the corresponding
        row of `pk`.
        )r   r&  r  rW  s    r   stdzrv_discrete_float.std  s     wtxxzz"""r   rG  )	rm   rn   ro   rH  ra   r  r`  r  r  r   r   r   r6  r6    sn         6. . .       (
* 
* 
*5 5 5
# 
# 
# 
# 
#r   r6  )rH  numpyr   statsmodels.baser   statsmodels.base.modelr  statsmodels.tools.decoratorsr   statsmodels.compat.pandasr   rJ  rK  rL  r   LikelihoodModelrq   LikelihoodModelResultsr   r6  r   r   r   <module>r     s`         " " " " " " % % % % % % % % % 7 7 7 7 7 7 . . . . . .. `6 ! {? {? {? {? {? {? {? {?|K K K K KE! K K K\"` ` ` ` `4. ` ` `F	[# [# [# [# [# [# [# [# [# [#r   